-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathConcurrent.cpp
547 lines (482 loc) · 16.4 KB
/
Concurrent.cpp
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//===--- Concurrent.cpp - Concurrent data structure tests -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Runtime/Concurrent.h"
#include "gtest/gtest.h"
#include "ThreadingHelpers.h"
using namespace swift;
TEST(ConcurrentReadableArrayTest, SingleThreaded) {
ConcurrentReadableArray<size_t> array;
auto add = [&](size_t limit) {
for (size_t i = array.snapshot().count(); i < limit; i++)
array.push_back(i);
};
auto check = [&]{
size_t i = 0;
for (auto element : array.snapshot()) {
ASSERT_EQ(element, i);
i++;
}
};
check();
add(1);
check();
add(16);
check();
add(100);
check();
add(1000);
check();
add(1000000);
check();
}
#ifndef SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY
TEST(ConcurrentReadableArrayTest, MultiThreaded) {
const int insertCount = 100000;
struct Value {
int threadNumber;
int x;
};
auto runTest = [&](int writerCount, int readerCount) {
ConcurrentReadableArray<Value> array;
// The writers will append values with their thread number and increasing
// values of x.
auto writer = [&](int threadNumber) {
for (int i = 0; i < insertCount; i++)
array.push_back({ threadNumber, i });
};
auto reader = [&] {
// Track the maximum value we've seen for each writer thread.
std::vector<int> maxByThread;
maxByThread.resize(writerCount);
bool done = false;
while (!done) {
for (int i = 0; i < writerCount; i++)
maxByThread[i] = -1;
for (auto element : array.snapshot()) {
ASSERT_LT(element.threadNumber, writerCount);
// Each element we see must be larger than the maximum element we've
// previously seen for that writer thread, otherwise that means that
// we're seeing mutations out of order.
ASSERT_GT(element.x, maxByThread[element.threadNumber]);
maxByThread[element.threadNumber] = element.x;
}
// If the max for each thread is the max that'll be inserted, then we're
// done and should exit.
done = true;
for (int i = 0; i < writerCount; i++) {
if (maxByThread[i] < insertCount - 1)
done = false;
}
}
};
threadedExecute(writerCount + readerCount, [&](int i) {
if (i < writerCount)
writer(i);
else
reader();
});
ASSERT_EQ(array.snapshot().count(), (size_t)writerCount * insertCount);
};
runTest(1, 1);
runTest(1, 8);
runTest(16, 1);
runTest(16, 8);
}
TEST(ConcurrentReadableArrayTest, MultiThreaded2) {
const int insertCount = 100000;
struct Value {
int threadNumber;
int x;
};
auto runTest = [&](int writerCount, int readerCount) {
ConcurrentReadableArray<Value> array;
// The writers will append values with their thread number and increasing
// values of x.
auto writer = [&](int threadNumber) {
for (int i = 0; i < insertCount; i++)
array.push_back({ threadNumber, i });
};
auto reader = [&] {
// Track the maximum value we've seen for each writer thread.
std::vector<int> maxByThread;
maxByThread.resize(writerCount);
for (int i = 0; i < writerCount; i++)
maxByThread[i] = -1;
bool done = false;
while (!done) {
auto snapshot = array.snapshot();
// Don't do anything until some data is actually added.
if (snapshot.count() == 0)
continue;
// Grab the last element in the snapshot.
auto element = snapshot.begin()[snapshot.count() - 1];
ASSERT_LT(element.threadNumber, writerCount);
// Each element we see must be equal to or larger than the maximum element
// we've previously seen for that writer thread, otherwise that means that
// we're seeing mutations out of order.
ASSERT_GE(element.x, maxByThread[element.threadNumber]);
maxByThread[element.threadNumber] = element.x;
// We'll eventually see some thread add its maximum value. We'll call it
// done when we reach that point.
if (element.x == insertCount - 1)
done = true;
}
};
threadedExecute(writerCount + readerCount, [&](int i) {
if (i < writerCount)
writer(i);
else
reader();
});
ASSERT_EQ(array.snapshot().count(), (size_t)writerCount * insertCount);
};
runTest(1, 1);
runTest(1, 8);
runTest(16, 1);
runTest(16, 8);
}
struct SingleThreadedValue {
size_t key;
size_t x;
SingleThreadedValue(size_t key, size_t x) : key(key), x(x) {}
bool matchesKey(size_t key) { return this->key == key; }
friend llvm::hash_code hash_value(const SingleThreadedValue &value) {
return llvm::hash_value(value.key);
}
};
TEST(ConcurrentReadableHashMapTest, SingleThreaded) {
ConcurrentReadableHashMap<SingleThreadedValue> map;
auto permute = [](size_t value) { return value ^ 0x33333333U; };
auto add = [&](size_t limit) {
for (size_t i = 0; i < limit; i++)
map.getOrInsert(permute(i),
[&](SingleThreadedValue *value, bool created) {
if (created)
new (value) SingleThreadedValue(permute(i), i);
return true;
});
};
auto check = [&](size_t limit) {
auto snapshot = map.snapshot();
ASSERT_EQ(snapshot.find((size_t)~0), nullptr);
for (size_t i = 0; i < limit; i++) {
auto *value = snapshot.find(permute(i));
ASSERT_NE(value, nullptr);
ASSERT_EQ(permute(i), value->key);
ASSERT_EQ(i, value->x);
}
};
check(0);
add(1);
check(1);
add(16);
check(16);
add(100);
check(100);
add(1000);
check(1000);
add(1000000);
check(1000000);
map.clear();
check(0);
add(1);
check(1);
map.clear();
check(0);
add(16);
check(16);
map.clear();
check(0);
add(100);
check(100);
map.clear();
check(0);
add(1000);
check(1000);
map.clear();
check(0);
add(1000000);
check(1000000);
map.clear();
check(0);
ASSERT_FALSE(map.hasActiveReaders());
map.clear();
}
struct MultiThreadedKey {
int threadNumber;
int n;
friend llvm::hash_code hash_value(const MultiThreadedKey &value) {
return llvm::hash_combine(value.threadNumber, value.n);
}
};
struct MultiThreadedValue {
int threadNumber;
int n;
int x;
MultiThreadedValue(MultiThreadedKey key, int x)
: threadNumber(key.threadNumber), n(key.n), x(x) {}
bool matchesKey(const MultiThreadedKey &key) {
return threadNumber == key.threadNumber && n == key.n;
}
friend llvm::hash_code hash_value(const MultiThreadedValue &value) {
return llvm::hash_combine(value.threadNumber, value.n);
}
};
// Test simultaneous readers and writers.
TEST(ConcurrentReadableHashMapTest, MultiThreaded) {
const int insertCount = 10000;
auto runTest = [&](int writerCount, int readerCount) {
ConcurrentReadableHashMap<MultiThreadedValue> map;
// NOTE: The bizarre lambdas around the ASSERT_ statements works around the
// fact that these macros emit return statements, which conflict with our
// need to return true/false from these lambdas. Wrapping them in a lambda
// neutralizes the return.
auto writer = [&](int threadNumber) {
// Insert half, then insert all, to test adding an existing key.
for (int i = 0; i < insertCount / 2; i++)
map.getOrInsert(MultiThreadedKey{threadNumber, i}, [&](MultiThreadedValue
*value,
bool created) {
[&] { ASSERT_TRUE(created); }();
new (value) MultiThreadedValue(MultiThreadedKey{threadNumber, i}, i);
return true;
});
// Test discarding a new entry.
for (int i = 0; i < insertCount; i++)
map.getOrInsert(MultiThreadedKey{threadNumber, i},
[&](MultiThreadedValue *value, bool created) {
[&] { ASSERT_EQ(created, i >= insertCount / 2); }();
return false;
});
for (int i = 0; i < insertCount; i++)
map.getOrInsert(MultiThreadedKey{threadNumber, i}, [&](MultiThreadedValue
*value,
bool created) {
if (created) {
[&] { ASSERT_GE(i, insertCount / 2); }();
new (value) MultiThreadedValue(MultiThreadedKey{threadNumber, i}, i);
} else {
[&] { ASSERT_LT(i, insertCount / 2); }();
}
return true;
});
};
auto reader = [&] {
bool done = false;
while (!done) {
done = true;
for (int threadNumber = 0; threadNumber < writerCount; threadNumber++) {
// Read from the top down. We should see zero or more missing entries,
// and then the rest are present. Any hole is a bug.
int firstSeen = -1;
auto snapshot = map.snapshot();
for (int i = insertCount - 1; i >= 0; i--) {
MultiThreadedKey key = {threadNumber, i};
const MultiThreadedValue *value = snapshot.find(key);
if (value) {
if (firstSeen == -1)
firstSeen = value->x;
ASSERT_EQ(value->x, i);
} else {
ASSERT_EQ(firstSeen, -1);
done = false;
}
}
}
}
};
threadedExecute(writerCount + readerCount, [&](int i) {
if (i < writerCount)
writer(i);
else
reader();
});
ASSERT_FALSE(map.hasActiveReaders());
map.clear();
};
runTest(1, 1);
runTest(1, 8);
runTest(16, 1);
runTest(16, 8);
}
// Test readers and writers while also constantly clearing the map.
TEST(ConcurrentReadableHashMapTest, MultiThreaded2) {
const int insertCount = 10000;
auto runTest = [&](int writerCount, int readerCount) {
ConcurrentReadableHashMap<MultiThreadedValue> map;
std::atomic<int> writerDoneCount = {0};
auto writer = [&](int threadNumber) {
for (int i = 0; i < insertCount; i++)
map.getOrInsert(MultiThreadedKey{threadNumber, i}, [&](MultiThreadedValue
*value,
bool created) {
[&] { ASSERT_TRUE(created); }();
new (value) MultiThreadedValue(MultiThreadedKey{threadNumber, i}, i);
return true;
});
writerDoneCount.fetch_add(1, std::memory_order_relaxed);
};
auto reader = [&] {
while (writerDoneCount.load(std::memory_order_relaxed) < writerCount) {
for (int threadNumber = 0; threadNumber < writerCount; threadNumber++) {
// Read from the top down. We should see a single contiguous region of
// entries. Multiple regions indicates a bug.
int firstSeen = -1;
int lastSeen = -1;
auto snapshot = map.snapshot();
for (int i = insertCount - 1; i >= 0; i--) {
MultiThreadedKey key = {threadNumber, i};
const MultiThreadedValue *value = snapshot.find(key);
if (value) {
if (firstSeen == -1)
firstSeen = value->x;
if (lastSeen != -1)
ASSERT_EQ(lastSeen, i + 1);
lastSeen = value->x;
ASSERT_EQ(value->x, i);
}
}
}
}
};
auto clear = [&] {
while (writerDoneCount.load(std::memory_order_relaxed) < writerCount) {
map.clear();
}
};
threadedExecute(writerCount + readerCount + 1, [&](int i) {
if (i < writerCount)
writer(i);
else if (i < writerCount + readerCount)
reader();
else
clear();
});
ASSERT_FALSE(map.hasActiveReaders());
map.clear();
};
runTest(1, 1);
runTest(1, 8);
runTest(16, 1);
runTest(16, 8);
}
// Test readers and writers, with readers taking lots of snapshots.
TEST(ConcurrentReadableHashMapTest, MultiThreaded3) {
const int insertCount = 10000;
auto runTest = [&](int writerCount, int readerCount) {
ConcurrentReadableHashMap<MultiThreadedValue> map;
std::atomic<int> writerDoneCount = {0};
auto writer = [&](int threadNumber) {
for (int i = 0; i < insertCount; i++)
map.getOrInsert(MultiThreadedKey{threadNumber, i}, [&](MultiThreadedValue
*value,
bool created) {
[&] { ASSERT_TRUE(created); }();
new (value) MultiThreadedValue(MultiThreadedKey{threadNumber, i}, i);
return true;
});
writerDoneCount.fetch_add(1, std::memory_order_relaxed);
};
auto reader = [&] {
while (writerDoneCount.load(std::memory_order_relaxed) < writerCount) {
for (int threadNumber = 0; threadNumber < writerCount; threadNumber++) {
// Read from the top down. When we're not clearing the map, we should
// see zero or more missing entries, and then the rest are present. Any
// hole is a bug.
int firstSeen = -1;
int lastSeen = -1;
for (int i = insertCount - 1; i >= 0; i--) {
auto snapshot = map.snapshot();
MultiThreadedKey key = {threadNumber, i};
const MultiThreadedValue *value = snapshot.find(key);
if (value) {
if (firstSeen == -1)
firstSeen = value->x;
if (lastSeen != -1)
ASSERT_EQ(lastSeen, i + 1);
lastSeen = value->x;
ASSERT_EQ(value->x, i);
}
}
}
}
};
threadedExecute(writerCount + readerCount, [&](int i) {
if (i < writerCount)
writer(i);
else
reader();
});
ASSERT_FALSE(map.hasActiveReaders());
map.clear();
};
runTest(1, 1);
runTest(1, 8);
runTest(16, 1);
runTest(16, 8);
}
// Test readers and writers, with readers taking lots of snapshots, and
// simultaneous clearing.
TEST(ConcurrentReadableHashMapTest, MultiThreaded4) {
const int insertCount = 10000;
auto runTest = [&](int writerCount, int readerCount) {
ConcurrentReadableHashMap<MultiThreadedValue> map;
std::atomic<int> writerDoneCount = {0};
auto writer = [&](int threadNumber) {
for (int i = 0; i < insertCount; i++)
map.getOrInsert(MultiThreadedKey{threadNumber, i}, [&](MultiThreadedValue
*value,
bool created) {
[&] { ASSERT_TRUE(created); }();
new (value) MultiThreadedValue(MultiThreadedKey{threadNumber, i}, i);
return true;
});
writerDoneCount.fetch_add(1, std::memory_order_relaxed);
};
auto reader = [&] {
while (writerDoneCount.load(std::memory_order_relaxed) < writerCount) {
for (int threadNumber = 0; threadNumber < writerCount; threadNumber++) {
// With clearing, we can't expect any particular pattern. Just validate
// the values we do see, and make sure we don't crash.
for (int i = insertCount - 1; i >= 0; i--) {
auto snapshot = map.snapshot();
MultiThreadedKey key = {threadNumber, i};
const MultiThreadedValue *value = snapshot.find(key);
if (value) {
ASSERT_EQ(value->x, i);
}
}
}
}
};
auto clear = [&] {
while (writerDoneCount.load(std::memory_order_relaxed) < writerCount) {
map.clear();
}
};
threadedExecute(writerCount + readerCount + 1, [&](int i) {
if (i < writerCount)
writer(i);
else if (i < writerCount + readerCount)
reader();
else
clear();
});
ASSERT_FALSE(map.hasActiveReaders());
map.clear();
};
runTest(1, 1);
runTest(1, 8);
runTest(16, 1);
runTest(16, 8);
}
#endif // !SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY