-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathmulti_threaded_vector.cpp
More file actions
468 lines (409 loc) · 15.5 KB
/
Copy pathmulti_threaded_vector.cpp
File metadata and controls
468 lines (409 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "pch.h"
#include "multi_threaded_common.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace concurrent_collections;
// Vector correctness tests exist elsewhere. These tests are strictly geared toward testing multi threaded functionality.
namespace
{
// We use a customized container that mimics std::vector and which
// validates that C++ concurrency rules are observed.
// C++ rules for library types are that concurrent use of const methods is allowed,
// but no method call may be concurrent with a non-const method. (Const methods may
// be "shared", but non-const methods are "exclusive".)
//
// NOTE! As the C++/WinRT implementation changes, you may need to add additional members
// to our fake vector and vector iterator classes.
//
// The regular single_threaded_vector and multi_threaded_vector functions requires std::vector,
// so we bypass that method and go directly to input_vector, which takes an arbitrary container
// that acts vector-like.
enum class VectorKind
{
IVector,
IObservableVector,
IObservableVectorAsInspectable,
};
// Change the next line to "#if 0" to use a single-threaded vector and confirm that every test fails.
// The scenarios use "CHECK" instead of "REQUIRE" so that they continue running even on failure.
// That way, you can just step through the entire test and confirm that something bad happens
// at each scenario.
#if 1
template<typename T, typename Container>
using custom_threaded_vector = winrt::impl::multi_threaded_vector<T, Container>;
template<typename Container>
using custom_inspectable_observable_vector = winrt::impl::multi_threaded_inspectable_observable_vector<Container>;
template<typename T, typename Container>
using custom_convertible_observable_vector = winrt::impl::multi_threaded_convertible_observable_vector<T, Container>;
#else
template<typename T, typename Container>
using custom_threaded_vector = winrt::impl::input_vector<T, Container>;
template<typename Container>
using custom_inspectable_observable_vector = winrt::impl::inspectable_observable_vector<Container>;
template<typename T, typename Container>
using custom_convertible_observable_vector = winrt::impl::convertible_observable_vector<T, Container>;
#endif
template<VectorKind kind, typename Container>
auto make_threaded_vector(Container&& values)
{
using T = typename Container::value_type;
if constexpr (kind == VectorKind::IVector)
{
return static_cast<IVector<T>>(winrt::make<custom_threaded_vector<T, Container>>(std::move(values)));
}
else
{
IObservableVector<T> vector;
if constexpr (std::is_same_v<T, Windows::Foundation::IInspectable>)
{
vector = make<custom_inspectable_observable_vector<Container>>(std::move(values));
}
else
{
vector = make<custom_convertible_observable_vector<T, Container>>(std::move(values));
}
if constexpr (kind == VectorKind::IObservableVector)
{
return vector;
}
else
{
return vector.template as<IObservableVector<IInspectable>>();
}
}
}
#pragma region vector wrapper
// Add more wrapper methods as necessary.
template<typename T, typename Allocator = std::allocator<T>>
struct concurrency_checked_vector : private std::vector<T, Allocator>, concurrency_guard
{
using inner = typename concurrency_checked_vector::vector;
using value_type = typename inner::value_type;
using allocator_type = typename inner::allocator_type;
using size_type = typename inner::size_type;
using difference_type = typename inner::difference_type;
using reference = typename inner::reference;
using const_reference = typename inner::const_reference;
using iterator = concurrency_checked_random_access_iterator<concurrency_checked_vector, typename inner::iterator>;
using const_iterator = concurrency_checked_random_access_iterator<concurrency_checked_vector, typename inner::const_iterator, typename inner::iterator>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static_assert(!std::is_same_v<T, bool>, "Has never been tested with bool.");
concurrency_checked_vector() = default;
concurrency_checked_vector(concurrency_checked_vector&& other) = default;
iterator begin()
{
auto guard = concurrency_guard::lock_nonconst();
return { this, inner::begin() };
}
const_iterator begin() const
{
auto guard = concurrency_guard::lock_const();
return { this, inner::begin() };
}
iterator end()
{
auto guard = concurrency_guard::lock_nonconst();
return { this, inner::end() };
}
const_iterator end() const
{
auto guard = concurrency_guard::lock_const();
return { this, inner::end() };
}
bool empty() const
{
auto guard = concurrency_guard::lock_const();
return inner::empty();
}
reference back()
{
auto guard = concurrency_guard::lock_nonconst();
return inner::back();
}
const_reference back() const
{
auto guard = concurrency_guard::lock_const();
return inner::back();
}
void pop_back()
{
auto guard = concurrency_guard::lock_nonconst();
inner::pop_back();
}
void push_back(T const& value)
{
auto guard = concurrency_guard::lock_nonconst();
concurrency_guard::call_hook(collection_action::push_back);
inner::push_back(value);
}
void push_back(T&& value)
{
auto guard = concurrency_guard::lock_nonconst();
concurrency_guard::call_hook(collection_action::push_back);
inner::push_back(std::move(value));
}
size_type size() const
{
auto guard = concurrency_guard::lock_const();
return inner::size();
}
iterator erase(const_iterator pos)
{
auto guard = concurrency_guard::lock_nonconst();
concurrency_guard::call_hook(collection_action::erase);
return { this, inner::erase(pos) };
}
iterator insert(const_iterator pos, T const& value)
{
auto guard = concurrency_guard::lock_nonconst();
concurrency_guard::call_hook(collection_action::insert);
return { this, inner::insert(pos, value) };
}
iterator insert(const_iterator pos, T&& value)
{
auto guard = concurrency_guard::lock_nonconst();
concurrency_guard::call_hook(collection_action::insert);
return { this, inner::insert(pos, std::move(value)) };
}
reference operator[](size_type pos)
{
return at(pos);
}
reference at(size_type pos)
{
auto guard = concurrency_guard::lock_nonconst();
concurrency_guard::call_hook(collection_action::at);
return inner::at(pos);
}
void clear()
{
auto guard = concurrency_guard::lock_nonconst();
return inner::clear();
}
template<typename InputIt>
void assign(InputIt first, InputIt last)
{
auto guard = concurrency_guard::lock_nonconst();
return inner::assign(first, last);
}
void reserve(size_type capacity) {
auto guard = concurrency_guard::lock_nonconst();
return inner::reserve(capacity);
}
void swap(concurrency_checked_vector& other)
{
auto guard = concurrency_guard::lock_nonconst();
inner::swap(other);
}
template<typename Iterator>
decltype(auto) dereference_iterator(Iterator const& it) const
{
auto guard = concurrency_guard::lock_const();
concurrency_guard::call_hook(collection_action::at);
return *it;
}
operator array_view<T>()
{
auto guard = concurrency_guard::lock_nonconst();
return { inner::data(), static_cast<uint32_t>(inner::size()) };
}
operator array_view<T const>() const
{
auto guard = concurrency_guard::lock_const();
return { inner::data(), static_cast<uint32_t>(inner::size()) };
}
};
#pragma endregion
template<typename T, VectorKind kind>
void test_vector_concurrency()
{
auto raw = concurrency_checked_vector<T>();
auto hook = raw.hook;
// Convert the raw_vector into the desired Windows Runtime vector interface.
auto v = make_threaded_vector<kind>(std::move(raw));
auto race = [&](collection_action action, auto&& background, auto&& foreground)
{
// Vector initial contents are { 1, 2, 3 }.
v.ReplaceAll({ conditional_box<T>(1), conditional_box<T>(2), conditional_box<T>(3) });
hook->race(action, background, foreground);
};
// Verify that Append does not run concurrently with GetAt().
race(collection_action::push_back, [&]
{
v.Append(conditional_box<T>(42));
}, [&]
{
CHECK(conditional_unbox<T>(v.GetAt(3)) == 42);
});
// Verify that Append does not run concurrently with Size().
race(collection_action::push_back, [&]
{
v.Append(conditional_box<T>(42));
}, [&]
{
CHECK(v.Size() == 4);
});
// Verify that Append does not run concurrently with IndexOf().
race(collection_action::push_back, [&]
{
v.Append(conditional_box<T>(42));
}, [&]
{
uint32_t index;
bool found = v.IndexOf(conditional_box<T>(3), index);
if constexpr (std::is_same_v<T, int>)
{
CHECK(found);
CHECK(index == 2);
}
else
{
// Boxed integers do not compare equal even if the values are the same.
CHECK(!found);
}
});
// Verify that Append does not run concurrently with another Append().
race(collection_action::push_back, [&]
{
v.Append(conditional_box<T>(42));
}, [&]
{
v.Append(conditional_box<T>(43));
});
// Verify that Append does not run concurrently with ReplaceAll().
race(collection_action::push_back, [&]
{
v.Append(conditional_box<T>(42));
}, [&]
{
v.ReplaceAll({ conditional_box<T>(1), conditional_box<T>(2) });
});
// Verify that Append does not run concurrently with GetMany().
race(collection_action::push_back, [&]
{
v.Append(conditional_box<T>(42));
}, [&]
{
T values[10];
CHECK(v.GetMany(0, values) == 4);
CHECK(conditional_unbox<T>(values[0]) == 1);
CHECK(conditional_unbox<T>(values[1]) == 2);
CHECK(conditional_unbox<T>(values[2]) == 3);
CHECK(conditional_unbox<T>(values[3]) == 42);
});
// Verify that InsertAt does not run concurrently with GetAt().
race(collection_action::insert, [&]
{
v.InsertAt(1, conditional_box<T>(42));
}, [&]
{
CHECK(conditional_unbox<T>(v.GetAt(1)) == 42);
});
// Verify that InsertAt does not run concurrently with Size().
race(collection_action::insert, [&]
{
v.InsertAt(1, conditional_box<T>(42));
}, [&]
{
CHECK(v.Size() == 4);
});
// Verify that InsertAt does not run concurrently with GetMany().
race(collection_action::insert, [&]
{
v.InsertAt(1, conditional_box<T>(42));
}, [&]
{
T values[10];
CHECK(v.GetMany(0, values) == 4);
CHECK(conditional_unbox<T>(values[0]) == 1);
CHECK(conditional_unbox<T>(values[1]) == 42);
CHECK(conditional_unbox<T>(values[2]) == 2);
CHECK(conditional_unbox<T>(values[3]) == 3);
});
// Verify that RemoveAt does not run concurrently with GetAt().
race(collection_action::erase, [&]
{
v.RemoveAt(1);
}, [&]
{
CHECK(conditional_unbox<T>(v.GetAt(1)) == 3);
});
// Verify that RemoveAt does not run concurrently with Size().
race(collection_action::erase, [&]
{
v.RemoveAt(1);
}, [&]
{
CHECK(v.Size() == 2);
});
// Verify that SetAt does not run concurrently with GetAt().
race(collection_action::at, [&]
{
v.SetAt(1, conditional_box<T>(42));
}, [&]
{
CHECK(conditional_unbox<T>(v.GetAt(1)) == 42);
});
// Iterator invalidation tests are a little different because we perform
// the mutation from the foreground thread after the read operation
// has begun on the background thread.
{
// Verify that iterator invalidation doesn't race against
// iterator use.
decltype(v.First()) it;
T t;
race(collection_action::at, [&]
{
it = v.First();
t = it.Current();
}, [&]
{
v.InsertAt(0, conditional_box<T>(42));
});
CHECK(conditional_unbox<T>(t) == 1);
}
{
// Verify that concurrent iteration works via GetMany(), which is atomic.
// (Current + MoveNext is non-atomic and can result in two threads
// both grabbing the same Current and then moving two steps forward.)
decltype(v.First()) it;
T t1[1];
T t2[1];
race(collection_action::at, [&]
{
it = v.First();
CHECK(it.GetMany(t1) == 1);
}, [&]
{
CHECK(it.GetMany(t2) == 1);
});
CHECK(conditional_unbox<T>(t1[0]) != conditional_unbox<T>(t2[0]));
}
}
void deadlock_test()
{
auto v = make_threaded_vector<VectorKind::IVector>(concurrency_checked_vector<IInspectable>());
v.Append(make<deadlock_object<IVector<IInspectable>>>(v));
auto task = [](auto v)-> IAsyncAction
{
co_await resume_background();
v.RemoveAtEnd();
}(v);
auto status = task.wait_for(std::chrono::milliseconds(DEADLOCK_TIMEOUT));
REQUIRE(status == AsyncStatus::Completed);
}
}
TEST_CASE("multi_threaded_vector")
{
test_vector_concurrency<int, VectorKind::IVector>();
test_vector_concurrency<IInspectable, VectorKind::IVector>();
deadlock_test();
}
TEST_CASE("multi_threaded_observable_vector")
{
test_vector_concurrency<int, VectorKind::IObservableVector>();
test_vector_concurrency<IInspectable, VectorKind::IObservableVector>();
test_vector_concurrency<IInspectable, VectorKind::IObservableVectorAsInspectable>();
}