-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_notifiers.cpp
More file actions
1619 lines (1261 loc) · 60.1 KB
/
Copy pathtest_notifiers.cpp
File metadata and controls
1619 lines (1261 loc) · 60.1 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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest.h>
#include <taskflow/taskflow.hpp>
#include <atomic>
#include <thread>
#include <vector>
#include <random>
#include <chrono>
#include <algorithm>
// ============================================================================
// Shared helpers
// ============================================================================
static inline void tiny_jitter(std::mt19937& rng) {
std::uniform_int_distribution<int> pick(0, 9);
int x = pick(rng);
if (x < 4) {
std::this_thread::yield();
} else if (x < 8) {
volatile int sink = 0;
int spins = 20 + (pick(rng) * 30);
for (int i = 0; i < spins; ++i) sink += i;
(void)sink;
} else {
std::this_thread::sleep_for(std::chrono::nanoseconds(200));
}
}
enum class NotificationType { ONE, N, ALL };
// ============================================================================
// no_missing_notify_all
// Each round: all N threads prepare_wait(), pause in the window between
// prepare and commit until all threads are ready, then commit_wait().
// Main calls notify_all() in that window. All commit_wait() calls must
// return without blocking (no lost wakeup).
// End check: completed == R*N, num_waiters() == 0.
// ============================================================================
template <typename T>
void no_missing_notify_all(size_t N) {
T notifier(N);
REQUIRE(notifier.size() == N);
size_t R = 20 * (N + 1);
if (N >= 31) R = 1 * (N + 1);
std::atomic<size_t> prepared(0);
std::atomic<size_t> completed(0);
std::atomic<size_t> round(0);
std::atomic<bool> stop(false);
std::vector<std::thread> threads;
threads.reserve(N);
for (size_t i = 0; i < N; ++i) {
threads.emplace_back([&, i]() {
size_t local_round = 0;
while (!stop.load(std::memory_order_relaxed)) {
while (round.load(std::memory_order_acquire) == local_round &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) break;
notifier.prepare_wait(i);
prepared.fetch_add(1, std::memory_order_relaxed);
while (prepared.load(std::memory_order_acquire) < (local_round + 1) * N &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
notifier.commit_wait(i);
completed.fetch_add(1, std::memory_order_relaxed);
local_round++;
}
});
}
for (size_t r = 0; r < R; ++r) {
round.fetch_add(1, std::memory_order_release);
while (prepared.load(std::memory_order_acquire) != (r + 1) * N) {
std::this_thread::yield();
}
notifier.notify_all();
}
stop.store(true, std::memory_order_release);
notifier.notify_all();
for (auto& t : threads) t.join();
REQUIRE(completed.load() == R * N);
REQUIRE(notifier.num_waiters() == 0);
}
// ============================================================================
// no_missing_notify_one
// Each round: all N threads prepare_wait() then commit_wait().
// Main calls notify_one() and verifies at least 1 thread unblocks,
// then notify_all() drains the rest. num_waiters() == 0 at end.
// ============================================================================
template <typename T>
void no_missing_notify_one(size_t N) {
T notifier(N);
REQUIRE(notifier.size() == N);
size_t R = 20 * (N + 1);
if (N >= 31) R = 1 * (N + 1);
std::atomic<size_t> round(0);
std::atomic<size_t> prepared(0);
std::atomic<size_t> committed(0);
std::atomic<bool> stop(false);
std::vector<std::thread> threads;
threads.reserve(N);
for (size_t i = 0; i < N; ++i) {
threads.emplace_back([&, i]() {
size_t local_round = 0;
while (!stop.load(std::memory_order_relaxed)) {
while (round.load(std::memory_order_acquire) <= local_round &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) break;
notifier.prepare_wait(i);
prepared.fetch_add(1, std::memory_order_release);
notifier.commit_wait(i);
committed.fetch_add(1, std::memory_order_release);
local_round++;
}
});
}
for (size_t r = 0; r < R; ++r) {
prepared.store(0, std::memory_order_relaxed);
committed.store(0, std::memory_order_relaxed);
round.store(r + 1, std::memory_order_release);
while (prepared.load(std::memory_order_acquire) != N) {
std::this_thread::yield();
}
notifier.notify_one();
// At least one thread must unblock from commit_wait.
while (committed.load() == 0) std::this_thread::yield();
// Drain any remaining waiters.
notifier.notify_all();
while (committed.load(std::memory_order_acquire) != N) {
std::this_thread::yield();
}
}
stop.store(true, std::memory_order_release);
notifier.notify_all();
for (auto& t : threads) t.join();
REQUIRE(notifier.num_waiters() == 0);
}
// ============================================================================
// no_missing_notify_n
// Each round: all N threads prepare then commit_wait. Main calls notify_n(k)
// where k is random in [0, N], verifies at least k unblock, then notify_all
// drains the rest. num_waiters() == 0 at end.
// ============================================================================
template <typename T>
void no_missing_notify_n(size_t N, uint32_t seed = 12345) {
T notifier(N);
REQUIRE(notifier.size() == N);
size_t R = 20 * (N + 1);
if (N >= 31) R = 1 * (N + 1);
std::atomic<size_t> round(0);
std::atomic<size_t> prepared(0);
std::atomic<size_t> committed(0);
std::atomic<bool> stop(false);
std::vector<std::thread> threads;
threads.reserve(N);
for (size_t i = 0; i < N; ++i) {
threads.emplace_back([&, i]() {
size_t local_round = 0;
while (!stop.load(std::memory_order_relaxed)) {
while (round.load(std::memory_order_acquire) <= local_round &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) break;
notifier.prepare_wait(i);
prepared.fetch_add(1, std::memory_order_release);
notifier.commit_wait(i);
committed.fetch_add(1, std::memory_order_release);
local_round++;
}
});
}
std::mt19937 rng(seed);
std::uniform_int_distribution<size_t> dist(0, N);
for (size_t r = 0; r < R; ++r) {
prepared.store(0, std::memory_order_relaxed);
committed.store(0, std::memory_order_relaxed);
round.store(r + 1, std::memory_order_release);
while (prepared.load(std::memory_order_acquire) != N) {
std::this_thread::yield();
}
size_t k = dist(rng);
notifier.notify_n(k);
while (committed.load(std::memory_order_acquire) < k) {
std::this_thread::yield();
}
notifier.notify_all();
while (committed.load(std::memory_order_acquire) != N) {
std::this_thread::yield();
}
}
stop.store(true, std::memory_order_release);
notifier.notify_all();
for (auto& t : threads) t.join();
REQUIRE(notifier.num_waiters() == 0);
}
// ============================================================================
// no_missing_notify_x (mixed: notify_one / notify_n / notify_all)
// Same structure as no_missing_notify_n but randomly picks notify variant
// each round. Verifies all variants can coexist without losing wakeups.
// ============================================================================
template <typename T>
void no_missing_notify_x(size_t N, uint32_t seed = 12345) {
T notifier(N);
REQUIRE(notifier.size() == N);
size_t R = 20 * (N + 1);
if (N >= 31) R = 1 * (N + 1);
std::atomic<size_t> round(0);
std::atomic<size_t> prepared(0);
std::atomic<size_t> committed(0);
std::atomic<bool> stop(false);
std::vector<std::thread> threads;
threads.reserve(N);
for (size_t i = 0; i < N; ++i) {
threads.emplace_back([&, i]() {
size_t local_round = 0;
while (!stop.load(std::memory_order_relaxed)) {
while (round.load(std::memory_order_acquire) <= local_round &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) break;
notifier.prepare_wait(i);
prepared.fetch_add(1, std::memory_order_release);
notifier.commit_wait(i);
committed.fetch_add(1, std::memory_order_release);
local_round++;
}
});
}
std::mt19937 rng(seed);
std::uniform_int_distribution<size_t> dist(0, N);
for (size_t r = 0; r < R; ++r) {
prepared.store(0, std::memory_order_relaxed);
committed.store(0, std::memory_order_relaxed);
round.store(r + 1, std::memory_order_release);
while (prepared.load(std::memory_order_acquire) != N) {
std::this_thread::yield();
}
size_t k = dist(rng);
if (k == 1) {
notifier.notify_one();
} else if (k == N) {
notifier.notify_all();
} else {
notifier.notify_n(k);
}
while (committed.load(std::memory_order_acquire) < k) {
std::this_thread::yield();
}
notifier.notify_all();
while (committed.load(std::memory_order_acquire) != N) {
std::this_thread::yield();
}
}
stop.store(true, std::memory_order_release);
notifier.notify_all();
for (auto& t : threads) t.join();
REQUIRE(notifier.num_waiters() == 0);
}
// ============================================================================
// no_missing_cancel_wait
// Each round: every thread calls prepare_wait(). Main randomly assigns
// per-thread has_work flag. Threads with has_work cancel, rest commit.
// Verifies cancel_wait cleans up state (no ghost waiters) and that
// commit_wait + notifications complete the round. num_waiters() == 0
// after every round and at end.
// ============================================================================
template <typename T>
void no_missing_cancel_wait(size_t N, uint32_t seed = 12345) {
T notifier(N);
REQUIRE(notifier.size() == N);
size_t R = 20 * (N + 1);
if (N >= 31) R = 1 * (N + 1);
std::atomic<size_t> round(0);
std::atomic<size_t> prepared(0);
std::atomic<size_t> canceled(0);
std::atomic<size_t> committed(0);
std::atomic<bool> stop(false);
std::atomic<bool> go(false);
std::vector<std::atomic<bool>> has_work(N);
for (size_t i = 0; i < N; ++i) has_work[i].store(false, std::memory_order_relaxed);
std::vector<std::thread> threads;
threads.reserve(N);
for (size_t i = 0; i < N; ++i) {
threads.emplace_back([&, i]() {
size_t local_round = 0;
while (!stop.load(std::memory_order_relaxed)) {
while (round.load(std::memory_order_acquire) <= local_round &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) break;
notifier.prepare_wait(i);
prepared.fetch_add(1, std::memory_order_release);
while (!go.load(std::memory_order_acquire) &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) break;
if (has_work[i].load(std::memory_order_acquire)) {
notifier.cancel_wait(i);
canceled.fetch_add(1, std::memory_order_release);
} else {
notifier.commit_wait(i);
committed.fetch_add(1, std::memory_order_release);
}
local_round++;
}
});
}
std::mt19937 rng(seed);
std::uniform_int_distribution<int> dice(0, 1);
for (size_t r = 0; r < R; ++r) {
prepared.store(0, std::memory_order_relaxed);
canceled.store(0, std::memory_order_relaxed);
committed.store(0, std::memory_order_relaxed);
go.store(false, std::memory_order_release);
for (size_t i = 0; i < N; ++i) has_work[i].store(false, std::memory_order_relaxed);
round.store(r + 1, std::memory_order_release);
while (prepared.load(std::memory_order_acquire) != N) std::this_thread::yield();
size_t expected_cancels = 0;
for (size_t i = 0; i < N; ++i) {
if (dice(rng)) {
has_work[i].store(true, std::memory_order_release);
++expected_cancels;
}
}
size_t expected_commits = N - expected_cancels;
go.store(true, std::memory_order_release);
while (canceled.load(std::memory_order_acquire) != expected_cancels) std::this_thread::yield();
while (notifier.num_waiters() != expected_commits) std::this_thread::yield();
if (expected_commits > 0) {
std::uniform_int_distribution<size_t> pickX(0, expected_commits);
size_t X = pickX(rng);
size_t first = expected_commits - X;
notifier.notify_n(first);
for (size_t j = 0; j < X; ++j) notifier.notify_one();
}
// Ensure the round always completes.
notifier.notify_all();
while ((canceled.load(std::memory_order_acquire) +
committed.load(std::memory_order_acquire)) != N) {
std::this_thread::yield();
}
REQUIRE(notifier.num_waiters() == 0);
}
stop.store(true, std::memory_order_release);
go.store(true, std::memory_order_release);
notifier.notify_all();
for (auto& t : threads) t.join();
REQUIRE(notifier.num_waiters() == 0);
}
// ============================================================================
// no_missing_notifications (compile-time switch: ONE / N / ALL)
// M concurrent notifier threads continuously hammer one notify variant.
// N worker threads follow prepare -> cancel_or_commit protocol.
// Round ends when every worker has either canceled or returned from commit.
// num_waiters() == 0 after every round.
// ============================================================================
template <typename T, NotificationType NT>
void no_missing_notifications(size_t N, size_t M = 4, uint32_t seed = 12345) {
T notifier(N);
REQUIRE(notifier.size() == N);
size_t R = 20 * (N + 1);
if (N >= 31) R = 1 * (N + 1);
std::atomic<size_t> round(0);
std::atomic<size_t> prepared(0);
std::atomic<size_t> canceled(0);
std::atomic<size_t> committed(0);
std::atomic<bool> stop(false);
std::atomic<bool> go(false);
std::vector<std::atomic<bool>> has_work(N);
for (size_t i = 0; i < N; ++i) has_work[i].store(false, std::memory_order_relaxed);
std::vector<std::thread> workers;
workers.reserve(N);
for (size_t i = 0; i < N; ++i) {
workers.emplace_back([&, i]() {
size_t local_round = 0;
while (!stop.load(std::memory_order_relaxed)) {
while (round.load(std::memory_order_acquire) <= local_round &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) break;
notifier.prepare_wait(i);
prepared.fetch_add(1, std::memory_order_release);
while (!go.load(std::memory_order_acquire) &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) break;
if (has_work[i].load(std::memory_order_acquire)) {
notifier.cancel_wait(i);
canceled.fetch_add(1, std::memory_order_release);
} else {
notifier.commit_wait(i);
committed.fetch_add(1, std::memory_order_release);
}
local_round++;
}
});
}
std::vector<std::thread> notifiers;
notifiers.reserve(M);
if constexpr (NT == NotificationType::ONE) {
for (size_t t = 0; t < M; ++t) {
notifiers.emplace_back([&]() {
while (!stop.load(std::memory_order_relaxed)) {
notifier.notify_one();
std::this_thread::yield();
}
});
}
} else if constexpr (NT == NotificationType::N) {
for (size_t t = 0; t < M; ++t) {
notifiers.emplace_back([&, t]() {
std::mt19937 trng(seed + static_cast<uint32_t>(t + 1));
std::uniform_int_distribution<size_t> dist(0, N);
while (!stop.load(std::memory_order_relaxed)) {
notifier.notify_n(dist(trng));
std::this_thread::yield();
}
});
}
} else if constexpr (NT == NotificationType::ALL) {
for (size_t t = 0; t < M; ++t) {
notifiers.emplace_back([&, t]() {
std::mt19937 trng(seed + static_cast<uint32_t>(777 + t));
std::uniform_int_distribution<int> burst(1, 8);
while (!stop.load(std::memory_order_relaxed)) {
int b = burst(trng);
for (int i = 0; i < b; ++i) notifier.notify_all();
std::this_thread::yield();
}
});
}
}
std::mt19937 rng(seed);
std::uniform_int_distribution<int> dice(0, 1);
for (size_t r = 0; r < R; ++r) {
prepared.store(0, std::memory_order_relaxed);
canceled.store(0, std::memory_order_relaxed);
committed.store(0, std::memory_order_relaxed);
go.store(false, std::memory_order_release);
for (size_t i = 0; i < N; ++i) has_work[i].store(false, std::memory_order_relaxed);
round.store(r + 1, std::memory_order_release);
while (prepared.load(std::memory_order_acquire) != N) std::this_thread::yield();
for (size_t i = 0; i < N; ++i) {
if (dice(rng)) has_work[i].store(true, std::memory_order_release);
}
go.store(true, std::memory_order_release);
while ((canceled.load(std::memory_order_acquire) +
committed.load(std::memory_order_acquire)) != N) {
std::this_thread::yield();
}
REQUIRE(notifier.num_waiters() == 0);
}
stop.store(true, std::memory_order_release);
go.store(true, std::memory_order_release);
notifier.notify_all();
for (auto& w : workers) w.join();
for (auto& n : notifiers) n.join();
REQUIRE(notifier.num_waiters() == 0);
}
// ============================================================================
// fuzz_stress_notifier
// Condition-variable style predicate (signal_epoch). Workers follow the
// full two-phase wait protocol with randomized delays and partial
// participation. M notifier threads continuously advance the epoch and
// mix all three notify variants. Validates:
// - Every commit_wait returns (no stuck thread).
// - num_waiters() == 0 after shutdown.
// - prepares > 0 (slow path exercised).
// - fast_path > 0 (predicate check before commit works).
// ============================================================================
template <typename T>
void fuzz_stress_notifier(size_t N, size_t M_notifiers, size_t rounds, uint32_t seed) {
T notifier(N);
REQUIRE(notifier.size() == N);
std::atomic<bool> stop{false};
std::atomic<uint64_t> signal_epoch{0};
std::atomic<uint64_t> prepares{0};
std::atomic<uint64_t> cancels{0};
std::atomic<uint64_t> commits_entered{0};
std::atomic<uint64_t> commits_returned{0};
std::atomic<uint64_t> fast_path{0};
std::vector<std::thread> workers;
workers.reserve(N);
for (size_t i = 0; i < N; ++i) {
workers.emplace_back([&, i] {
std::mt19937 rng(seed ^ (0x9e3779b9u + (uint32_t)i * 101u));
uint64_t local = signal_epoch.load(std::memory_order_relaxed);
std::uniform_int_distribution<int> coin(0, 99);
for (size_t it = 0; it < rounds && !stop.load(std::memory_order_relaxed); ++it) {
// Partial participation: sometimes do "work" without waiting.
if (coin(rng) < 15) { tiny_jitter(rng); continue; }
uint64_t cur = signal_epoch.load(std::memory_order_acquire);
if (cur != local) {
local = cur;
fast_path.fetch_add(1, std::memory_order_relaxed);
tiny_jitter(rng);
continue;
}
// Two-phase wait protocol.
tiny_jitter(rng);
notifier.prepare_wait(i);
prepares.fetch_add(1, std::memory_order_relaxed);
tiny_jitter(rng);
cur = signal_epoch.load(std::memory_order_acquire);
if (cur != local) {
notifier.cancel_wait(i);
cancels.fetch_add(1, std::memory_order_relaxed);
local = cur;
tiny_jitter(rng);
continue;
}
commits_entered.fetch_add(1, std::memory_order_relaxed);
notifier.commit_wait(i);
commits_returned.fetch_add(1, std::memory_order_relaxed);
local = signal_epoch.load(std::memory_order_acquire);
tiny_jitter(rng);
}
});
}
std::vector<std::thread> notifiers;
notifiers.reserve(M_notifiers);
for (size_t t = 0; t < M_notifiers; ++t) {
notifiers.emplace_back([&, t] {
std::mt19937 rng(seed + (uint32_t)(777u + t * 17u));
std::uniform_int_distribution<int> which(0, 99);
while (!stop.load(std::memory_order_relaxed)) {
tiny_jitter(rng);
int w = which(rng);
// Occasionally send "empty" notifies (no predicate change) — must be harmless.
if (w < 15) {
int kind = which(rng) % 3;
if (kind == 0) notifier.notify_one();
else if (kind == 1) notifier.notify_n((size_t)(which(rng) % (int)(N + 1)));
else notifier.notify_all();
continue;
}
// Normal: advance predicate then notify (condition-variable style).
signal_epoch.fetch_add(1, std::memory_order_release);
if (w < 60) notifier.notify_one();
else if (w < 85) notifier.notify_n((size_t)(which(rng) % (int)(N + 1)));
else notifier.notify_all();
}
});
}
for (auto& w : workers) w.join();
stop.store(true, std::memory_order_release);
notifier.notify_all();
for (auto& n : notifiers) n.join();
REQUIRE(notifier.num_waiters() == 0);
REQUIRE(commits_returned.load() == commits_entered.load());
REQUIRE(prepares.load() > 0);
REQUIRE(fast_path.load() > 0);
}
// ============================================================================
// notify_n_releases_committed
// Forces ALL N threads to become committed waiters (num_waiters() == N),
// then verifies notify_n(k) alone releases at least min(k, N) of them.
// notify_all() drains the rest so the round completes.
// This is a targeted regression for notify_n boundary semantics and for
// spurious early-exit from commit_wait due to an epoch field bug.
// ============================================================================
template <typename T>
void notify_n_releases_committed(size_t N, size_t k, size_t rounds, uint32_t seed) {
(void)seed;
T notifier(N);
REQUIRE(notifier.size() == N);
std::atomic<size_t> round(0);
std::atomic<size_t> prepared(0);
std::atomic<size_t> committed_done(0);
std::atomic<bool> stop(false);
std::vector<std::thread> workers;
workers.reserve(N);
for (size_t i = 0; i < N; ++i) {
workers.emplace_back([&, i] {
size_t local_round = 0;
while (!stop.load(std::memory_order_relaxed)) {
while (round.load(std::memory_order_acquire) == local_round &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) break;
// No cancel path: we want all threads to become committed waiters.
notifier.prepare_wait(i);
prepared.fetch_add(1, std::memory_order_release);
notifier.commit_wait(i);
committed_done.fetch_add(1, std::memory_order_release);
local_round++;
}
});
}
for (size_t r = 1; r <= rounds; ++r) {
prepared.store(0, std::memory_order_relaxed);
committed_done.store(0, std::memory_order_relaxed);
round.store(r, std::memory_order_release);
while (prepared.load(std::memory_order_acquire) != N) std::this_thread::yield();
// Wait until ALL threads are truly committed waiters.
while (notifier.num_waiters() != N) std::this_thread::yield();
size_t target = (k < N) ? k : N;
notifier.notify_n(k);
auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5);
while (committed_done.load(std::memory_order_acquire) < target &&
std::chrono::steady_clock::now() < deadline) {
std::this_thread::yield();
}
REQUIRE(committed_done.load(std::memory_order_acquire) >= target);
// Drain remaining waiters.
notifier.notify_all();
auto deadline2 = std::chrono::steady_clock::now() + std::chrono::seconds(10);
while (committed_done.load(std::memory_order_acquire) != N &&
std::chrono::steady_clock::now() < deadline2) {
notifier.notify_all();
std::this_thread::yield();
}
REQUIRE(committed_done.load(std::memory_order_acquire) == N);
REQUIRE(notifier.num_waiters() == 0);
}
stop.store(true, std::memory_order_release);
notifier.notify_all();
for (auto& t : workers) t.join();
REQUIRE(notifier.num_waiters() == 0);
}
// ============================================================================
// stress_test_notifier
// Validates:
// 1. notify_n(k) wakes at least k threads if k <= N.
// 2. notify_all() clears all remaining waiters.
// 3. num_waiters() correctly reflects the epoch-waiter state.
// 4. The epoch mechanism handles wakeups without spurious immediate re-sleeps.
// ============================================================================
template <typename T>
void stress_test_notifier(size_t N, size_t k, size_t rounds) {
T notifier(N);
REQUIRE(notifier.size() == N);
std::atomic<size_t> round(0);
std::atomic<size_t> prepared_count(0);
std::atomic<size_t> wake_count(0);
std::atomic<bool> stop(false);
std::vector<std::thread> workers;
workers.reserve(N);
for (size_t i = 0; i < N; ++i) {
workers.emplace_back([&, i] {
size_t local_round = 0;
while (!stop.load(std::memory_order_relaxed)) {
// Include !stop in the condition so the inner spin exits cleanly
// when the main sets stop=true (not only when round advances).
while (round.load(std::memory_order_acquire) == local_round &&
!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
}
if (stop.load(std::memory_order_relaxed)) return;
notifier.prepare_wait(i);
prepared_count.fetch_add(1, std::memory_order_release);
// Two-phase stop check: prepare_wait's seq_cst fence guarantees
// that stop=true is visible here if main stored it before notify_all().
// Without this, notify_all() could fire before prepare_wait and the
// worker would block in commit_wait with no future wakeup.
if (stop.load(std::memory_order_relaxed)) {
notifier.cancel_wait(i);
return;
}
notifier.commit_wait(i);
wake_count.fetch_add(1, std::memory_order_release);
local_round++;
}
});
}
for (size_t r = 1; r <= rounds; ++r) {
prepared_count.store(0);
wake_count.store(0);
round.store(r, std::memory_order_release);
while (prepared_count.load(std::memory_order_acquire) != N) {
std::this_thread::yield();
}
while (notifier.num_waiters() != N) {
std::this_thread::yield();
}
size_t target = std::min(k, N);
notifier.notify_n(k);
auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2);
while (wake_count.load(std::memory_order_acquire) < target) {
if (std::chrono::steady_clock::now() > deadline) break;
std::this_thread::yield();
}
REQUIRE(wake_count.load(std::memory_order_acquire) >= target);
notifier.notify_all();
auto drain_deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2);
while (wake_count.load(std::memory_order_acquire) < N) {
if (std::chrono::steady_clock::now() > drain_deadline) {
notifier.notify_all();
}
std::this_thread::yield();
}
REQUIRE(wake_count.load(std::memory_order_acquire) == N);
REQUIRE(notifier.num_waiters() == 0);
}
stop.store(true);
round.fetch_add(1);
notifier.notify_all();
for (auto& t : workers) {
if (t.joinable()) t.join();
}
REQUIRE(notifier.num_waiters() == 0);
}
// ============================================================================
// notify_before_commit
// notify_all() fires BEFORE threads call commit_wait(). Threads must NOT
// block (no lost wakeup). Exercises the window between prepare_wait() and
// commit_wait() where the notification arrives.
// ============================================================================
template <typename T>
void notify_before_commit(size_t N) {
T notifier(N);
std::atomic<size_t> prep_count{0};
std::atomic<size_t> committed{0};
std::atomic<bool> notified{false};
std::vector<std::thread> threads;
threads.reserve(N);
for (size_t i = 0; i < N; ++i) {
threads.emplace_back([&, i]() {
notifier.prepare_wait(i);
prep_count.fetch_add(1, std::memory_order_release);
// Spin until the main thread has already called notify_all().
while (!notified.load(std::memory_order_acquire)) std::this_thread::yield();
notifier.commit_wait(i);
committed.fetch_add(1, std::memory_order_release);
});
}
while (prep_count.load(std::memory_order_acquire) != N) std::this_thread::yield();
// Notify before any thread calls commit_wait.
notifier.notify_all();
notified.store(true, std::memory_order_release);
for (auto& t : threads) t.join();
REQUIRE(committed.load() == N);
REQUIRE(notifier.num_waiters() == 0);
}
// ============================================================================
// notify_n_zero_is_noop
// After all N threads are fully committed (num_waiters() == N),
// notify_n(0) must not wake any of them. Immediately after the call
// num_waiters() must still be N. Then notify_all() drains cleanly.
// Catches implementations that treat 0 as "notify all" or misread the
// loop bound.
// ============================================================================
template <typename T>
void notify_n_zero_is_noop(size_t N) {
T notifier(N);
REQUIRE(notifier.size() == N);
std::atomic<size_t> prepared{0};
std::atomic<size_t> committed{0};