commonware-runtime 2026.5.0

Execute asynchronous tasks with a configurable scheduler.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
//! Hybrid futex/eventfd wake coordination for the io_uring loop.
//!
//! This module implements the producer-to-loop wake protocol used by [`super::IoUringLoop`]:
//! - Producers call [`Waker::publish`] after enqueueing work.
//! - The loop calls [`Waker::park_idle`] when it is fully idle.
//! - The loop acquires an [`ArmGuard`] from [`Waker::arm`] before
//!   blocking in `submit_and_wait`.
//! - Producers wake only the currently armed wait target.
//! - A dedicated "wake signalled" bit coalesces repeated wake attempts.
//! - Out-of-band wake requests use [`Waker::wake`].
//! - Wake CQEs are acknowledged with [`Waker::acknowledge`].
//!
//! The packed atomic state combines:
//! - bit 0: waiting on futex
//! - bit 1: waiting on eventfd
//! - bit 2: wake already signalled
//! - bits 3..: submitted sequence
//!
//! This keeps the arm-and-recheck handshake lock-free, enables futex sleep when
//! the loop is truly idle, and avoids repeated wake writes while a wake is
//! already pending.

use super::UserData;
use io_uring::squeue::SubmissionQueue;
#[cfg(not(feature = "loom"))]
use io_uring::{opcode::PollAdd, types::Fd};
#[cfg(feature = "loom")]
use loom::sync::{
    atomic::{AtomicU32, AtomicU64, Ordering},
    Arc, Condvar, Mutex,
};
use std::time::{Duration, Instant};
#[cfg(not(feature = "loom"))]
use std::{
    mem::size_of,
    os::fd::{AsRawFd, FromRawFd, OwnedFd},
    sync::{
        atomic::{AtomicU32, Ordering},
        Arc,
    },
};
#[cfg(not(feature = "loom"))]
use tracing::warn;

/// Reserved `user_data` value for internal wake poll completions.
pub const WAKE_USER_DATA: UserData = UserData::MAX;

/// Number of low bits reserved for wake-state flags.
const STATE_BITS: u32 = 3;
/// Bit used when the loop is sleeping on a futex.
const WAITING_ON_FUTEX_BIT: u32 = 1;
/// Bit used when the loop is blocked in `submit_and_wait` and wakeable via eventfd.
const WAITING_ON_EVENTFD_BIT: u32 = 1 << 1;
/// Bit used once a wake has already been signalled for the current wait.
const WAKE_SIGNALLED_BIT: u32 = 1 << 2;
/// Mask covering all non-sequence wake-state flags.
const STATE_MASK: u32 = WAITING_ON_FUTEX_BIT | WAITING_ON_EVENTFD_BIT | WAKE_SIGNALLED_BIT;
/// Mask covering just the current wait target bits.
const WAITING_MASK: u32 = WAITING_ON_FUTEX_BIT | WAITING_ON_EVENTFD_BIT;
/// Packed-state increment for one submitted operation (low bits are reserved).
const SUBMISSION_INCREMENT: u32 = 1 << STATE_BITS;
/// Full sequence domain used by the packed submission counter (state >> 3).
pub const SUBMISSION_SEQ_MASK: u32 = u32::MAX >> STATE_BITS;
/// Maximum live published-minus-processed gap that keeps modular order directional.
pub const HALF_SUBMISSION_SEQUENCE_DOMAIN: u32 = SUBMISSION_SEQ_MASK.div_ceil(2);

/// RAII guard returned by [`Waker::arm`] for a `submit_and_wait` blocking section.
///
/// While this guard is live, the loop is armed to receive an eventfd-based
/// wake if producers publish new work or the final handle disconnects.
pub struct ArmGuard<'a> {
    waker: &'a Waker,
    still_idle: bool,
    wake_latched: bool,
}

impl ArmGuard<'_> {
    /// Return whether the post-arm snapshot still looked idle, meaning no
    /// wake was latched and the published sequence still matched the loop's
    /// `processed_seq`.
    pub const fn still_idle(&self) -> bool {
        self.still_idle
    }

    /// Return whether a wake was already latched before or during arming.
    pub const fn wake_latched(&self) -> bool {
        self.wake_latched
    }
}

impl Drop for ArmGuard<'_> {
    fn drop(&mut self) {
        self.waker.clear_wait();
    }
}

/// Shared wake state used by submitters and the io_uring loop.
///
/// `state` packs two values:
/// - bits 0..2: wait target and wake state
/// - bits 3..: submitted sequence (`submitted_seq`)
///
/// Submitters always increment `submitted_seq` after enqueueing onto the MPSC. The
/// loop tracks how many submissions it has drained from the MPSC (`processed_seq`,
/// stored in loop-local state). After arming a wait target, the loop blocks only
/// if the same post-arm snapshot still shows no latched wake and still carries
/// the exact `submitted_seq == processed_seq` snapshot the loop armed against.
///
/// The loop bounds the rounded channel/ring size strictly below half the packed
/// sequence domain. That makes the modular delta `submitted_seq - processed_seq`
/// directional: any non-zero delta smaller than half the domain means
/// `submitted_seq` is ahead, while larger deltas mean the visible submission
/// sequence is lagging behind requests the loop has already drained.
///
/// Blocking follows an arm-and-recheck protocol:
/// - The loop first checks for a published-ahead delta, then arms a wait target.
/// - The loop blocks only if the post-arm snapshot still looks idle after that
///   same atomic state transition.
/// - Submitters signal the currently armed wait target exactly once.
/// - Out-of-band notifications latch one wake even while unarmed, so the next
///   arm-and-recheck cycle skips blocking once.
///
/// This makes submissions racing with the sleep transition observable either by
/// sequence mismatch in the loop or by a futex/eventfd wakeup.
#[cfg(not(feature = "loom"))]
struct WakerInner {
    /// Non-blocking eventfd monitored by the loop's multishot wake poll.
    wake_fd: OwnedFd,
    /// Packed wait-target, wake-latch, and submitted-sequence state.
    state: AtomicU32,
}

/// Loom-only model of the waker state.
///
/// Loom cannot observe real futexes, eventfds, or io_uring CQEs, so this
/// variant keeps the same packed atomic state as the production waker and
/// replaces the kernel wake surfaces with userspace condvar models. The goal is
/// to model the producer/loop atomic protocol closely enough for loom to
/// explore memory orderings and wake races. It is not a model of kernel CQE
/// ordering, `io_uring_enter`, or wake-poll rearm behavior.
#[cfg(feature = "loom")]
struct WakerInner {
    /// Packed wait-target, wake-latch, and submitted-sequence state.
    state: AtomicU32,
    /// Mutex standing in for the kernel futex bucket lock.
    futex_bucket: Mutex<()>,
    /// Condvar standing in for the fully-idle futex wait queue.
    futex_waiters: Condvar,
    /// Durable eventfd readiness counter observed by the modeled eventfd wait.
    eventfd_counter: AtomicU64,
    /// Mutex pairing eventfd readiness checks with condvar parking.
    eventfd_readiness: Mutex<()>,
    /// Condvar standing in for `submit_and_wait` waking on a wake CQE.
    eventfd_waiters: Condvar,
}

/// Internal hybrid futex/eventfd wake source for the io_uring loop.
///
/// - Publish submissions from producers via [`Waker::publish`]
/// - Wake without publishing via [`Waker::wake`]
/// - Test whether published work is still pending via [`Waker::pending`]
/// - Park in the fully-idle path via [`Waker::park_idle`]
/// - Arm a `submit_and_wait` blocking section via [`Waker::arm`]
/// - Drain `eventfd` readiness on wake CQEs via [`Waker::acknowledge`]
/// - Re-arm the multishot poll request when needed via [`Waker::reinstall`]
///
/// This type intentionally separates:
/// - sequence publication (`state` high bits)
/// - wait gating (`state` low bits)
/// - kernel readiness consumption (`eventfd` read path)
///
/// Keeping these concerns separate makes the wake protocol explicit and avoids
/// coupling correctness to exact eventfd coalescing behavior.
#[derive(Clone)]
pub struct Waker {
    inner: Arc<WakerInner>,
}

impl Waker {
    /// Create a hybrid futex/eventfd wake source backed by a non-blocking
    /// `eventfd`.
    #[cfg(not(feature = "loom"))]
    pub fn new() -> Result<Self, std::io::Error> {
        // SAFETY: `eventfd` is called with valid flags and no aliasing pointers.
        let fd = unsafe { libc::eventfd(0, libc::EFD_CLOEXEC | libc::EFD_NONBLOCK) };
        if fd < 0 {
            return Err(std::io::Error::last_os_error());
        }
        // SAFETY: `eventfd` returned a new owned descriptor.
        let wake_fd = unsafe { OwnedFd::from_raw_fd(fd) };

        Ok(Self {
            inner: Arc::new(WakerInner {
                wake_fd,
                state: AtomicU32::new(0),
            }),
        })
    }

    /// Create the loom model of the hybrid wake source.
    ///
    /// This keeps the same packed atomic state as production, but replaces the
    /// eventfd and futex kernel objects with loom-visible counters and
    /// condition variables.
    #[cfg(feature = "loom")]
    pub fn new() -> Result<Self, std::io::Error> {
        Ok(Self {
            inner: Arc::new(WakerInner {
                state: AtomicU32::new(0),
                futex_bucket: Mutex::new(()),
                futex_waiters: Condvar::new(),
                eventfd_counter: AtomicU64::new(0),
                eventfd_readiness: Mutex::new(()),
                eventfd_waiters: Condvar::new(),
            }),
        })
    }

    /// Latch one pending wake and, if a target is currently armed, wake it.
    ///
    /// The first caller to set `WAKE_SIGNALLED_BIT` in an epoch performs the
    /// wake. Subsequent callers do nothing until the loop disarms and clears
    /// the bit.
    ///
    /// All claimed wakes flow through this path, whether they come from
    /// `publish()` on an armed epoch or from an out-of-band caller such as the
    /// final sender disconnecting.
    pub fn wake(&self) {
        // `HandleInner::drop` uses this path without bumping the submission
        // sequence. Publish that disconnect here so that after the loop resumes
        // and `clear_wait()` acquires, the next channel check cannot observe
        // the wake without also observing the disconnect that caused it.
        let prev = self
            .inner
            .state
            .fetch_or(WAKE_SIGNALLED_BIT, Ordering::Release);

        if (prev & WAKE_SIGNALLED_BIT) != 0 {
            return;
        }

        let waiting = prev & WAITING_MASK;
        assert_ne!(
            waiting, WAITING_MASK,
            "iouring wake state cannot wait on futex and eventfd simultaneously"
        );

        match waiting {
            0 => {}
            WAITING_ON_FUTEX_BIT => self.futex_wake(),
            WAITING_ON_EVENTFD_BIT => self.eventfd_wake(),
            _ => unreachable!("unexpected iouring wake target"),
        }
    }

    /// Publish one submitted operation and optionally wake the currently armed
    /// wait target.
    ///
    /// Callers must invoke this only after successfully enqueueing work into
    /// the MPSC channel.
    ///
    /// The common unarmed path performs only one `fetch_add`. When a wait is
    /// armed and no wake has yet been claimed for that epoch, this caller
    /// claims `WAKE_SIGNALLED_BIT` with a follow-up atomic update and then
    /// signals the armed wait target.
    #[inline]
    pub fn publish(&self) {
        // Use `Release` so that when `pending()` later observes a published-ahead
        // sequence delta with its `Acquire` load, a following
        // `self.receiver.try_recv()` in `fill_submission_queue()` must observe
        // the corresponding request.
        let prev = self
            .inner
            .state
            .fetch_add(SUBMISSION_INCREMENT, Ordering::Release);

        let waiting = prev & WAITING_MASK;

        // Fast path: the loop is not waiting, or another publisher already
        // claimed the wake for the current armed epoch.
        if waiting == 0 || (prev & WAKE_SIGNALLED_BIT) != 0 {
            return;
        }

        self.wake();
    }

    /// Return whether any published submissions are still pending relative to
    /// `processed_seq`, i.e. whether the published sequence is currently ahead
    /// of that drained sequence.
    #[inline]
    pub fn pending(&self, processed_seq: u32) -> bool {
        // Pair this `Acquire` with `publish()`'s `Release`. The rounded ring
        // size is kept strictly below half the packed sequence domain, so a
        // non-zero modular delta smaller than that half-range unambiguously
        // means `published_seq` is ahead of `processed_seq`.
        let published_seq =
            (self.inner.state.load(Ordering::Acquire) >> STATE_BITS) & SUBMISSION_SEQ_MASK;

        let delta = published_seq.wrapping_sub(processed_seq) & SUBMISSION_SEQ_MASK;
        delta != 0 && delta < HALF_SUBMISSION_SEQUENCE_DOMAIN
    }

    /// Park on the idle path until the packed wake state changes.
    ///
    /// This method hides the arm-and-recheck futex sequence used when the ring
    /// is fully idle. It always clears the current wait state before returning.
    ///
    /// Returns `Some(duration)` only if `futex_wait` actually blocked in the
    /// kernel and later resumed. Returns `None` if the armed snapshot already
    /// showed published work or a latched wake, or if a concurrent state
    /// change rejected the snapshot before the thread could sleep.
    pub fn park_idle(&self, processed_seq: u32) -> Option<Duration> {
        // Arming only updates the packed wake state machine. It does not
        // publish queue memory or consume any out-of-band wake publication, so
        // `Relaxed` is sufficient on this RMW.
        let prev = self
            .inner
            .state
            .fetch_or(WAITING_ON_FUTEX_BIT, Ordering::Relaxed);

        assert_eq!(
            prev & WAITING_MASK,
            0,
            "iouring wait target should be disarmed before re-arming"
        );

        let snapshot = prev | WAITING_ON_FUTEX_BIT;

        // Only block if the post-arm snapshot still looks idle. When that is
        // true, futex-wait on the same packed state word that was just armed.
        if (snapshot & WAKE_SIGNALLED_BIT) == 0
            && ((snapshot >> STATE_BITS) & SUBMISSION_SEQ_MASK) == processed_seq
        {
            let before = Instant::now();
            let slept = self.futex_wait(snapshot);
            self.clear_wait();
            slept.then(|| before.elapsed())
        } else {
            self.clear_wait();
            None
        }
    }

    /// Arm the blocking wake path used around `submit_and_wait`.
    ///
    /// The returned guard automatically clears the current wait state on drop.
    /// Call [`ArmGuard::still_idle`] to decide whether the loop may block on
    /// the normal "still idle" path, or [`ArmGuard::wake_latched`] to detect
    /// an already-latched wake without conflating it with published-ahead
    /// sequence progress.
    pub fn arm(&self, processed_seq: u32) -> ArmGuard<'_> {
        // Arming only updates the packed wake state machine. It does not
        // publish queue memory or consume any out-of-band wake publication, so
        // `Relaxed` is sufficient on this RMW.
        let prev = self
            .inner
            .state
            .fetch_or(WAITING_ON_EVENTFD_BIT, Ordering::Relaxed);

        assert_eq!(
            prev & WAITING_MASK,
            0,
            "iouring wait target should be disarmed before re-arming"
        );

        let snapshot = prev | WAITING_ON_EVENTFD_BIT;
        let wake_latched = (snapshot & WAKE_SIGNALLED_BIT) != 0;
        let still_idle =
            !wake_latched && ((snapshot >> STATE_BITS) & SUBMISSION_SEQ_MASK) == processed_seq;

        ArmGuard {
            waker: self,
            still_idle,
            wake_latched,
        }
    }

    /// Drain readiness from the internal `eventfd` after a wake CQE.
    ///
    /// This acknowledges kernel-visible `eventfd` readiness. Wait gating is
    /// tracked separately in the packed `state` atomic and is managed by
    /// [`Waker::park_idle`] and [`Waker::arm`].
    ///
    /// Retries on `EINTR`. Treats `EAGAIN` as "nothing to drain". Without
    /// `EFD_SEMAPHORE`, one successful read drains the full counter to zero.
    #[cfg(not(feature = "loom"))]
    pub fn acknowledge(&self) {
        let mut value: u64 = 0;
        loop {
            // SAFETY: `wake_fd` is a valid eventfd descriptor and `value` points
            // to writable 8-byte storage for the duration of the call.
            let ret = unsafe {
                libc::read(
                    self.inner.wake_fd.as_raw_fd(),
                    &mut value as *mut u64 as *mut libc::c_void,
                    size_of::<u64>(),
                )
            };
            if ret == size_of::<u64>() as isize {
                // eventfd (without EFD_SEMAPHORE) returns the full counter and
                // resets it to zero in one read.
                return;
            }
            assert_eq!(
                ret, -1,
                "eventfd read returned unexpected byte count: {ret}"
            );
            match std::io::Error::last_os_error().raw_os_error() {
                // Retry if interrupted by a signal before completion.
                Some(libc::EINTR) => continue,
                // Non-blocking read would block because the counter is zero,
                // there is nothing left to drain right now.
                Some(libc::EAGAIN) => return,
                _ => {
                    tracing::warn!("eventfd read failed");
                    return;
                }
            }
        }
    }

    /// Model an eventfd read that drains all pending readiness.
    ///
    /// Production eventfd reads without `EFD_SEMAPHORE` return the current
    /// counter and reset it to zero atomically. The loom model uses one atomic
    /// swap to preserve that contract for wake-coalescing tests.
    #[cfg(feature = "loom")]
    pub fn acknowledge(&self) {
        self.inner.eventfd_counter.swap(0, Ordering::AcqRel);
    }

    /// Install the internal `eventfd` multishot poll request into the SQ.
    ///
    /// This uses multishot poll and is called on startup and whenever a wake
    /// CQE indicates the previous multishot request is no longer active.
    ///
    /// Returns `false` if the local SQ is already full and the rearm must be
    /// retried in a later staging pass.
    #[cfg(not(feature = "loom"))]
    pub fn reinstall(&self, submission_queue: &mut SubmissionQueue<'_>) -> bool {
        if submission_queue.is_full() {
            return false;
        }

        let wake_poll = PollAdd::new(Fd(self.inner.wake_fd.as_raw_fd()), libc::POLLIN as u32)
            .multi(true)
            .build()
            .user_data(WAKE_USER_DATA);

        // SAFETY: The poll SQE owns no user pointers and references a valid FD.
        unsafe {
            submission_queue
                .push(&wake_poll)
                .expect("checked wake poll SQE capacity");
        }

        true
    }

    /// Model wake-poll reinstall as a successful no-op.
    ///
    /// The loom tests in this module do not model the `io_uring` submission
    /// queue or wake-poll rearm state. Keeping this method present lets the
    /// crate compile with `loom` while keeping that boundary explicit.
    #[cfg(feature = "loom")]
    pub const fn reinstall(&self, _submission_queue: &mut SubmissionQueue<'_>) -> bool {
        true
    }

    /// Clear the current wait epoch after we resume running.
    ///
    /// Keeping wait bits clear while actively running avoids redundant futex
    /// wakes and eventfd writes during bursts. This is done both after
    /// `park_idle()` / `submit_and_wait` return and after a post-arm recheck
    /// decides not to block.
    #[inline]
    fn clear_wait(&self) {
        // Pair with `wake()`'s `Release`. This is the first common point after
        // resuming from a wake and before the next channel check, so acquiring
        // here ensures the loop cannot observe the wake without also observing
        // the sender-side state change that caused it.
        self.inner.state.fetch_and(!STATE_MASK, Ordering::Acquire);
    }

    /// Wake the loop while it is blocked in `submit_and_wait`.
    ///
    /// This writes to the internal `eventfd` monitored by the ring's multishot
    /// poll request. The resulting wake CQE causes the loop to leave its
    /// eventfd-backed blocking section and resume in userspace.
    #[cfg(not(feature = "loom"))]
    fn eventfd_wake(&self) {
        let value: u64 = 1;
        loop {
            // SAFETY: `wake_fd` is a valid eventfd descriptor and `value` points
            // to an initialized 8-byte integer for the duration of the call.
            let ret = unsafe {
                libc::write(
                    self.inner.wake_fd.as_raw_fd(),
                    &value as *const u64 as *const libc::c_void,
                    size_of::<u64>(),
                )
            };
            if ret == size_of::<u64>() as isize {
                return;
            }
            assert_eq!(
                ret, -1,
                "eventfd write returned unexpected byte count: {ret}"
            );
            match std::io::Error::last_os_error().raw_os_error() {
                // Retry if interrupted by a signal before completion.
                Some(libc::EINTR) => continue,
                // Non-blocking write would block because the eventfd
                // counter is saturated. A wake is already queued, so no
                // retry is needed.
                Some(libc::EAGAIN) => return,
                _ => {
                    warn!("eventfd write failed");
                    return;
                }
            }
        }
    }

    /// Model an eventfd write plus wake-CQE delivery.
    ///
    /// Incrementing `eventfd_counter` preserves the durable readiness bit of a
    /// real eventfd, while notifying `eventfd_waiters` stands in for
    /// `submit_and_wait` returning after the wake CQE becomes available.
    #[cfg(feature = "loom")]
    fn eventfd_wake(&self) {
        self.inner.eventfd_counter.fetch_add(1, Ordering::Release);
        let _guard = self.inner.eventfd_readiness.lock().unwrap();
        self.inner.eventfd_waiters.notify_one();
    }

    /// Wake one thread sleeping on the fully-idle futex path.
    ///
    /// This is used only when the loop has no active ring waiters and is
    /// blocked in [`Waker::futex_wait`] on the packed wake-state word.
    #[cfg(not(feature = "loom"))]
    fn futex_wake(&self) {
        loop {
            // SAFETY: `state` is a valid aligned futex word for the duration of
            // the syscall.
            let ret = unsafe {
                libc::syscall(
                    libc::SYS_futex,
                    self.inner.state.as_ptr(),
                    libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
                    1u32,
                )
            };
            if ret >= 0 {
                return;
            }
            let err = std::io::Error::last_os_error();
            match err.raw_os_error() {
                Some(libc::EINTR) => continue,
                _ => {
                    // The operation-specific `FUTEX_WAKE` error here is `EINVAL` for
                    // a PI waiter mismatch, and the generic futex syscall errors are
                    // invalid or inaccessible user memory, invalid arguments, or an
                    // unsupported op. For this private, aligned in-process futex,
                    // all of those indicate a broken invariant or environment.
                    // Unlike `futex_wait()`, there is no safe "just continue in
                    // userspace" fallback here: because `WAKE_SIGNALLED_BIT` is
                    // already latched for this epoch, logging and continuing would
                    // risk a permanent lost wake.
                    //
                    // [https://www.man7.org/linux/man-pages/man2/FUTEX_WAKE.2const.html#ERRORS]
                    panic!("futex wake failed: {err}");
                }
            }
        }
    }

    /// Model `FUTEX_WAKE` for the fully-idle path.
    ///
    /// Taking `futex_bucket` before notifying mirrors the serialization the
    /// kernel futex bucket provides between compare-and-park and wake.
    #[cfg(feature = "loom")]
    fn futex_wake(&self) {
        let _guard = self.inner.futex_bucket.lock().unwrap();
        self.inner.futex_waiters.notify_one();
    }

    /// Sleep on the packed wake-state word for the fully-idle path.
    ///
    /// The caller must pass the exact post-arm snapshot from the same atomic
    /// transition that set `WAITING_ON_FUTEX_BIT`. `FUTEX_WAIT` only blocks
    /// while the word still equals that value, which closes the race between
    /// arming idle sleep and a concurrent publish or out-of-band wake.
    ///
    /// Retries on `EINTR`. Treats `EAGAIN` as "state already changed before
    /// the kernel slept".
    ///
    /// Returns `true` only if the kernel actually blocked the thread and later
    /// resumed it. Returns `false` for stale-snapshot races, userspace
    /// equality mismatches, and unexpected futex wait failures.
    #[cfg(not(feature = "loom"))]
    fn futex_wait(&self, snapshot: u32) -> bool {
        loop {
            // This is only a same-word equality check before entering the
            // syscall. It relies only on modification order of this atomic, so
            // `Relaxed` is sufficient.
            if self.inner.state.load(Ordering::Relaxed) != snapshot {
                return false;
            }

            // SAFETY: `state` is a valid aligned futex word for the duration of
            // the syscall.
            let ret = unsafe {
                libc::syscall(
                    libc::SYS_futex,
                    self.inner.state.as_ptr(),
                    libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG,
                    snapshot,
                    std::ptr::null::<libc::timespec>(),
                )
            };
            if ret == 0 {
                return true;
            }
            let err = std::io::Error::last_os_error();
            match err.raw_os_error() {
                Some(libc::EINTR) => continue,
                Some(libc::EAGAIN) => return false,
                _ => {
                    // With a null timeout, documented timeout-specific errors do not
                    // apply here. An unexpected futex wait error means the kernel
                    // refused to block, so the safe fallback is to return to
                    // userspace and re-check the packed state rather than panic.
                    warn!("futex wait failed: {err}");
                    return false;
                }
            }
        }
    }

    /// Model `FUTEX_WAIT` for the fully-idle path.
    ///
    /// The condition variable wait keeps the compare and park under
    /// `futex_bucket`, so loom can explore the same lost-wake boundary that the
    /// kernel's atomic futex wait protects in production.
    #[cfg(feature = "loom")]
    fn futex_wait(&self, snapshot: u32) -> bool {
        let mut guard = self.inner.futex_bucket.lock().unwrap();
        let mut slept = false;
        while self.inner.state.load(Ordering::Acquire) == snapshot {
            slept = true;
            guard = self.inner.futex_waiters.wait(guard).unwrap();
        }
        slept
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use io_uring::IoUring;
    #[cfg(not(feature = "loom"))]
    use std::{
        mem::size_of,
        os::fd::{AsRawFd, FromRawFd},
    };

    pub fn wait_until_futex_armed(waker: &Waker) {
        while waker.inner.state.load(Ordering::Relaxed) & WAITING_ON_FUTEX_BIT == 0 {
            std::hint::spin_loop();
        }
    }

    pub fn wait_until_eventfd_armed(waker: &Waker) {
        while waker.inner.state.load(Ordering::Relaxed) & WAITING_ON_EVENTFD_BIT == 0 {
            std::hint::spin_loop();
        }
    }

    pub fn state_bits(waker: &Waker) -> u32 {
        waker.inner.state.load(Ordering::Relaxed) & STATE_MASK
    }

    pub fn submitted_seq(waker: &Waker) -> u32 {
        (waker.inner.state.load(Ordering::Relaxed) >> STATE_BITS) & SUBMISSION_SEQ_MASK
    }

    pub fn eventfd_count(waker: &Waker) -> u64 {
        #[cfg(not(feature = "loom"))]
        {
            let mut value = 0u64;
            // SAFETY: `wake_fd` is a valid eventfd descriptor and `value` points
            // to writable 8-byte storage for the duration of the call.
            let ret = unsafe {
                libc::read(
                    waker.inner.wake_fd.as_raw_fd(),
                    &mut value as *mut u64 as *mut libc::c_void,
                    size_of::<u64>(),
                )
            };
            if ret == -1 && std::io::Error::last_os_error().raw_os_error() == Some(libc::EAGAIN) {
                return 0;
            }
            assert_eq!(ret, size_of::<u64>() as isize);
            value
        }

        #[cfg(feature = "loom")]
        {
            waker.inner.eventfd_counter.load(Ordering::Relaxed)
        }
    }

    #[test]
    fn test_publish_arm_guard_and_submitted() {
        // Verify the packed wake state tracks submission sequence separately
        // from the blocking wake state across the normal publish and
        // acknowledge flow.
        let waker = Waker::new().expect("eventfd creation should succeed");
        assert_eq!(submitted_seq(&waker), 0);

        // Publish without an armed wait target only advances sequence.
        waker.publish();
        assert_eq!(submitted_seq(&waker), 1);

        // Arm and publish should trigger an eventfd wake; acknowledge drains it.
        let arm = waker.arm(1);
        assert!(arm.still_idle());
        assert!(!arm.wake_latched());
        waker.publish();
        assert_eq!(submitted_seq(&waker), 2);

        // Acknowledge and guard drop are wake-gating operations and must not change
        // the submitted sequence domain.
        waker.acknowledge();
        assert_eq!(submitted_seq(&waker), 2);
        drop(arm);
        assert_eq!(submitted_seq(&waker), 2);
        assert_eq!(state_bits(&waker), 0);

        // Re-arming should observe the same submitted snapshot while idle.
        let arm = waker.arm(2);
        assert!(arm.still_idle());
        assert!(!arm.wake_latched());
        drop(arm);
    }

    #[test]
    fn test_pending_uses_directional_half_range_compare() {
        // Verify `pending()` only reports work when the published sequence is
        // directionally ahead within the half-range window.
        let waker = Waker::new().expect("eventfd creation should succeed");

        // A one-step published-ahead delta is pending for `processed_seq = 0`,
        // but not once the loop has caught up.
        waker.inner.state.store(1 << STATE_BITS, Ordering::Relaxed);
        assert!(waker.pending(0));
        assert!(!waker.pending(1));

        // A visible published sequence that lags behind `processed_seq` must
        // not be treated as pending work.
        waker.inner.state.store(0, Ordering::Relaxed);
        assert!(!waker.pending(1));

        // Exactly half the domain is ambiguous and therefore not directional.
        waker.inner.state.store(
            HALF_SUBMISSION_SEQUENCE_DOMAIN << STATE_BITS,
            Ordering::Relaxed,
        );
        assert!(!waker.pending(0));

        // Wrapping by one still counts as a published-ahead delta.
        waker.inner.state.store(0, Ordering::Relaxed);
        assert!(waker.pending(SUBMISSION_SEQ_MASK));
    }

    #[test]
    fn test_park_idle_handles_concurrent_publish_and_wake_races() {
        #[derive(Clone, Copy, Debug)]
        enum Notifier {
            Wake,
            Publish,
        }

        // Stress the real concurrent idle-path races rather than only the
        // single-threaded stale-snapshot path. The notifier thread waits until
        // `WAITING_ON_FUTEX_BIT` is visible and then races a `wake()` or
        // `publish()` against the parked thread's equality check, futex
        // syscall, and eventual `clear_wait()`.
        for notifier in [Notifier::Wake, Notifier::Publish] {
            for _ in 0..64 {
                let waker = Waker::new().expect("eventfd creation should succeed");
                let before = submitted_seq(&waker);
                let notifier_waker = waker.clone();

                let handle = std::thread::spawn(move || {
                    while state_bits(&notifier_waker) & WAITING_ON_FUTEX_BIT == 0 {
                        std::hint::spin_loop();
                    }
                    match notifier {
                        Notifier::Wake => notifier_waker.wake(),
                        Notifier::Publish => notifier_waker.publish(),
                    }
                });

                let _ = waker.park_idle(before);
                handle.join().expect("idle notifier thread panicked");

                let expected = match notifier {
                    Notifier::Wake => before,
                    Notifier::Publish => before.wrapping_add(1) & SUBMISSION_SEQ_MASK,
                };

                assert_eq!(submitted_seq(&waker), expected, "{notifier:?}");
                assert_eq!(state_bits(&waker), 0, "{notifier:?}");
            }
        }
    }

    #[test]
    fn test_wake_without_idle_wait_keeps_sequence_stable() {
        // Verify out-of-band notifications without an idle wait do not perturb
        // submission sequence.
        let waker = Waker::new().expect("eventfd creation should succeed");
        let before = submitted_seq(&waker);
        waker.wake();
        assert_eq!(submitted_seq(&waker), before);
    }

    #[test]
    fn test_wake_before_park_idle_skips_sleep() {
        // Verify an out-of-band wake latched before idle arming makes the next
        // idle park return immediately instead of sleeping.
        let waker = Waker::new().expect("eventfd creation should succeed");
        let before = submitted_seq(&waker);

        waker.wake();
        let duration = waker.park_idle(before);

        assert!(duration.is_none(), "should not have slept");
        assert_eq!(submitted_seq(&waker), before);
        assert_eq!(state_bits(&waker), 0);
    }

    #[test]
    fn test_publish_before_park_idle_skips_sleep() {
        // Verify a sequence published before idle arming makes the next idle
        // park return immediately without manufacturing a wake.
        let waker = Waker::new().expect("eventfd creation should succeed");

        waker.publish();
        assert!(waker.park_idle(0).is_none());

        assert_eq!(submitted_seq(&waker), 1);
        assert_eq!(state_bits(&waker), 0);
        assert_eq!(eventfd_count(&waker), 0);
    }

    #[test]
    fn test_publish_after_futex_arm_rejects_stale_snapshot() {
        // Verify the futex idle path tolerates a publish that lands after
        // WAITING_ON_FUTEX_BIT is armed but before the armed thread commits to
        // a stable futex wait on that snapshot.
        //
        // This models the race that `park_idle()` closes:
        // 1. idle path arms WAITING_ON_FUTEX_BIT and computes a snapshot
        // 2. producer publishes, changing the packed state word
        // 3. a futex wait on the stale snapshot must return immediately
        let waker = Waker::new().expect("eventfd creation should succeed");
        let before = submitted_seq(&waker);

        // Manually split `park_idle()` into "arm" and "wait" so the publish
        // can be injected exactly between those two steps.
        let prev = waker
            .inner
            .state
            .fetch_or(WAITING_ON_FUTEX_BIT, Ordering::Relaxed);
        assert_eq!(prev & WAITING_MASK, 0);
        let snapshot = prev | WAITING_ON_FUTEX_BIT;

        // This publish changes the packed word after arming, so a futex wait
        // on the stale snapshot must now return immediately.
        waker.publish();
        assert_eq!(
            submitted_seq(&waker),
            before.wrapping_add(1) & SUBMISSION_SEQ_MASK
        );

        // If the stale snapshot were incorrectly accepted, this call could
        // block indefinitely. Returning here proves the userspace equality
        // check / futex EAGAIN path rejected the outdated snapshot without
        // ever committing to a real futex sleep.
        assert!(!waker.futex_wait(snapshot));
        waker.clear_wait();

        // The publish should remain visible and the wait bits should be fully
        // cleared on exit, matching `park_idle()`'s contract.
        assert_eq!(
            submitted_seq(&waker),
            before.wrapping_add(1) & SUBMISSION_SEQ_MASK
        );
        assert_eq!(state_bits(&waker), 0);
    }

    #[test]
    fn test_publish_deduplicates_eventfd_wakes() {
        // Verify contended publishes while the same eventfd wait is armed only
        // queue one wake write, while still advancing the sequence for every
        // publisher that raced in this epoch.
        let waker = Waker::new().expect("eventfd creation should succeed");
        let barrier = Arc::new(std::sync::Barrier::new(5));
        let mut handles = Vec::new();

        let arm = waker.arm(0);
        assert!(arm.still_idle());
        assert!(!arm.wake_latched());
        for _ in 0..4 {
            let publisher = waker.clone();
            let barrier = barrier.clone();
            handles.push(std::thread::spawn(move || {
                barrier.wait();
                publisher.publish();
            }));
        }
        barrier.wait();
        for handle in handles {
            handle.join().expect("publish thread panicked");
        }

        assert_eq!(submitted_seq(&waker), 4);
        assert_eq!(eventfd_count(&waker), 1);
        drop(arm);
    }

    #[test]
    fn test_arm_after_sticky_wake_skips_blocking() {
        // Verify a wake latched before arming makes the next blocking section
        // skip the normal idle-based blocking decision, and surface that the
        // reason was an out-of-band wake rather than published-ahead work.
        let waker = Waker::new().expect("eventfd creation should succeed");

        waker.wake();
        let arm = waker.arm(0);
        assert!(!arm.still_idle());
        assert!(arm.wake_latched());
        drop(arm);

        assert_eq!(submitted_seq(&waker), 0);
        assert_eq!(state_bits(&waker), 0);
    }

    #[test]
    fn test_unarmed_wakes_rearm_across_epochs() {
        // Verify unarmed wake latches are consumed when the loop next arms,
        // and that later unarmed wakes can be observed in later epochs.
        let waker = Waker::new().expect("eventfd creation should succeed");

        waker.wake();
        let arm = waker.arm(0);
        assert!(!arm.still_idle());
        assert!(arm.wake_latched());
        drop(arm);

        assert_eq!(submitted_seq(&waker), 0);
        assert_eq!(state_bits(&waker), 0);
        assert_eq!(eventfd_count(&waker), 0);

        waker.wake();
        let arm = waker.arm(0);
        assert!(!arm.still_idle());
        assert!(arm.wake_latched());
        drop(arm);

        assert_eq!(submitted_seq(&waker), 0);
        assert_eq!(state_bits(&waker), 0);
        assert_eq!(eventfd_count(&waker), 0);

        waker.publish();
        waker.wake();
        assert_eq!(submitted_seq(&waker), 1);
        assert_eq!(eventfd_count(&waker), 0);
        let arm = waker.arm(1);
        assert!(!arm.still_idle());
        assert!(arm.wake_latched());
        drop(arm);

        assert_eq!(state_bits(&waker), 0);
        assert_eq!(eventfd_count(&waker), 0);
    }

    #[test]
    fn test_arm_after_publish_skips_blocking() {
        // Verify arming with a stale processed sequence notices the newly
        // published submission and skips blocking.
        let waker = Waker::new().expect("eventfd creation should succeed");

        waker.publish();
        let arm = waker.arm(0);
        assert!(!arm.still_idle());
        assert!(!arm.wake_latched());
        drop(arm);

        assert_eq!(submitted_seq(&waker), 1);
        assert_eq!(state_bits(&waker), 0);
    }

    #[test]
    fn test_wake_deduplicates_eventfd_wakes() {
        // Verify contended out-of-band notifications while the same eventfd
        // wait is armed only queue one wake write and do not perturb sequence.
        let waker = Waker::new().expect("eventfd creation should succeed");
        let barrier = Arc::new(std::sync::Barrier::new(5));
        let mut handles = Vec::new();

        let arm = waker.arm(0);
        assert!(arm.still_idle());
        assert!(!arm.wake_latched());
        for _ in 0..4 {
            let notifier = waker.clone();
            let barrier = barrier.clone();
            handles.push(std::thread::spawn(move || {
                barrier.wait();
                notifier.wake();
            }));
        }
        barrier.wait();
        for handle in handles {
            handle.join().expect("wake thread panicked");
        }

        assert_eq!(submitted_seq(&waker), 0);
        assert_eq!(eventfd_count(&waker), 1);
        drop(arm);
    }

    #[test]
    fn test_eventfd_wake_and_acknowledge_empty_paths_keep_sequence_stable() {
        // Verify eventfd wake and drain do not perturb the
        // logical submission sequence, even when the counter is already empty.
        let waker = Waker::new().expect("eventfd creation should succeed");
        let before = submitted_seq(&waker);

        // Drive one normal wake cycle, then immediately drain again to hit the
        // non-blocking empty-read path.
        waker.eventfd_wake();
        waker.acknowledge();
        // Second acknowledge should take the non-blocking empty path.
        waker.acknowledge();

        assert_eq!(submitted_seq(&waker), before);
    }

    #[test]
    fn test_reinstall_pushes_wake_poll() {
        // Verify `reinstall()` queues one multishot wake poll SQE when space
        // is available and reports failure without mutating the SQ when it is
        // full.
        let waker = Waker::new().expect("eventfd creation should succeed");
        let mut ring = IoUring::new(8).expect("io_uring creation should succeed");

        // With SQ space available, `reinstall()` should enqueue exactly one
        // wake poll request.
        let mut sq = ring.submission();
        let before = sq.len();
        assert!(waker.reinstall(&mut sq));
        assert_eq!(sq.len(), before + 1);

        // Once the SQ is full, `reinstall()` must leave it unchanged and ask
        // the caller to retry later.
        while !sq.is_full() {
            let nop = io_uring::opcode::Nop::new().build().user_data(0);
            // SAFETY: Nop SQE owns no user pointers or external resources.
            unsafe {
                sq.push(&nop).expect("unable to fill submission queue");
            }
        }

        let before = sq.len();
        assert!(!waker.reinstall(&mut sq));
        assert_eq!(sq.len(), before);
    }

    #[cfg(not(feature = "loom"))]
    #[test]
    fn test_eventfd_wake_and_acknowledge_error_branches() {
        // Verify the explicit EAGAIN and generic error branches leave the
        // logical submission sequence unchanged.
        let mut waker = Waker::new().expect("eventfd creation should succeed");
        let before = submitted_seq(&waker);

        // Saturate the eventfd counter near its maximum so `eventfd_wake` takes the
        // non-blocking EAGAIN path and `acknowledge` drains the queued wake.
        let fd = waker.inner.wake_fd.as_raw_fd();
        let value = u64::MAX - 1;
        // SAFETY: `fd` is a valid eventfd and `value` points to initialized memory.
        let wrote = unsafe {
            libc::write(
                fd,
                &value as *const u64 as *const libc::c_void,
                size_of::<u64>(),
            )
        };
        assert_eq!(wrote, size_of::<u64>() as isize);
        waker.eventfd_wake();
        waker.acknowledge();

        // Then close the descriptor so both helpers exercise their generic
        // error-logging paths.
        // SAFETY: closing a valid fd is safe.
        let closed = unsafe { libc::close(fd) };
        assert_eq!(closed, 0);
        waker.eventfd_wake();
        waker.acknowledge();

        // Replace with a known-good fd so drop doesn't accidentally close a reused
        // descriptor number from the manually closed one.
        // SAFETY: `dup` returns a new owned fd on success.
        let replacement = unsafe { libc::dup(libc::STDIN_FILENO) };
        assert!(replacement >= 0);
        let old = {
            let inner = std::sync::Arc::get_mut(&mut waker.inner).expect("unique waker in test");
            // SAFETY: `replacement` came from `dup` above and is uniquely owned here.
            std::mem::replace(&mut inner.wake_fd, unsafe {
                std::os::fd::OwnedFd::from_raw_fd(replacement)
            })
        };
        std::mem::forget(old);

        // Direct eventfd read/write error paths should not perturb sequence tracking.
        assert_eq!(submitted_seq(&waker), before);
    }
}

#[cfg(all(test, feature = "loom"))]
mod loom_tests {
    use super::{
        tests::{eventfd_count, state_bits, submitted_seq},
        *,
    };
    use commonware_utils::test_rng;
    use loom::{
        sync::{
            atomic::{AtomicU32, Ordering},
            Arc,
        },
        thread,
    };
    use rand::Rng;

    // This module uses loom to model the waker's producer/loop protocol over
    // the packed atomic state word. The model keeps the production sequence and
    // wait-bit state machine, but replaces kernel wake surfaces with
    // loom-visible userspace models: the futex path uses a mutex and condvar to
    // preserve the atomic compare-and-park property of `FUTEX_WAIT`, and the
    // eventfd path uses a durable readiness counter plus a condvar to model
    // both persistent wake readiness and a blocked `submit_and_wait` returning
    // after a wake CQE.
    //
    // The tests keep schedules small while exercising the important races and
    // invariants: publish versus arm-and-recheck, futex idle parking, eventfd
    // wake coalescing, sticky out-of-band wakes, sequence wraparound, and the
    // Release/Acquire edges that make producer state visible after `pending()`
    // or `clear_wait()`. The model intentionally stops at this userspace
    // protocol boundary. It does not validate kernel CQE ordering,
    // `io_uring_enter`, wake-poll rearming, or syscall error handling.

    // Minimal model of the inbound request queue that feeds the ring.
    //
    // The queue model deliberately uses only relaxed accesses. These tests rely
    // on the waker's Release/Acquire edges to make an enqueued request visible
    // to the loop after it observes progress or resumes from a wake.
    struct QueuedRequest {
        value: AtomicU32,
    }

    impl QueuedRequest {
        fn empty() -> Self {
            Self {
                value: AtomicU32::new(0),
            }
        }

        fn enqueue(&self, value: u32) {
            self.value.store(value, Ordering::Relaxed);
        }

        fn read(&self) -> u32 {
            self.value.load(Ordering::Relaxed)
        }
    }

    // Wait until the modeled eventfd has durable readiness.
    //
    // In production, `submit_and_wait` returns after the wake poll produces a
    // CQE. In the loom model, `eventfd_wake()` increments `eventfd_counter` and
    // notifies this condvar, so this helper represents only that blocking
    // boundary.
    fn wait_for_eventfd_readiness(waker: &Waker) {
        let mut guard = waker.inner.eventfd_readiness.lock().unwrap();
        while waker.inner.eventfd_counter.load(Ordering::Acquire) == 0 {
            guard = waker.inner.eventfd_waiters.wait(guard).unwrap();
        }
    }

    // Wait until a producer/notifier has latched a wake bit.
    //
    // This is deliberately a relaxed spin: the tests using it pair with the
    // producer's Release through the later `clear_wait()` Acquire.
    fn wait_for_wake_signal(waker: &Waker) {
        while state_bits(waker) & WAKE_SIGNALLED_BIT == 0 {
            thread::yield_now();
        }
    }

    // Finish any wake epoch left over after a loop-simulator test has already
    // observed the sequence progress it cares about.
    //
    // A producer can claim `WAKE_SIGNALLED_BIT` and queue modeled eventfd
    // readiness while the simulated loop is also able to make progress by
    // observing `pending()`. At that point the leftover wake is cleanup noise,
    // not the property under test. Tests that care about exact wake coalescing
    // should assert the modeled eventfd counter before calling this helper.
    fn finish_leftover_wake(waker: &Waker) {
        assert_eq!(state_bits(waker) & WAITING_MASK, 0);
        if (state_bits(waker) & WAKE_SIGNALLED_BIT) != 0 {
            let guard = waker.arm(submitted_seq(waker));
            assert!(guard.wake_latched());
            drop(guard);
        }
        // A raced publisher can queue eventfd readiness after the loop has
        // already observed its sequence bump. Finish that modeled wake CQE so
        // cleanup assertions do not confuse it with the unarmed-wake cases.
        waker.acknowledge();
        assert_eq!(state_bits(waker), 0);
        assert_eq!(eventfd_count(waker), 0);
    }

    // Simulate the loop's eventfd-backed wait path until it has observed
    // `target` published submissions. This is not modeling the request queue
    // itself, only the waker-side control flow: check `pending()`, arm the
    // eventfd target, block only if the post-arm snapshot is still idle, then
    // drop the guard and acknowledge modeled eventfd readiness.
    //
    // The final `acknowledge()` is model cleanup for any wake CQE readiness
    // produced during the brief arm window. It may be a no-op when the loop did
    // not actually block, so tests that validate exact wake counts assert the
    // counter directly instead of relying on this helper.
    fn simulate_eventfd_loop_until(waker: &Waker, mut processed: u32, target: u32) -> u32 {
        while processed != target {
            if waker.pending(processed) {
                processed = processed.wrapping_add(1) & SUBMISSION_SEQ_MASK;
                continue;
            }

            let guard = waker.arm(processed);
            if guard.still_idle() {
                wait_for_eventfd_readiness(waker);
                assert!(
                    eventfd_count(waker) > 0,
                    "blocking eventfd wait must observe queued readiness before cleanup",
                );
            }
            drop(guard);
            waker.acknowledge();
        }
        processed
    }

    // Simulate the loop's fully-idle futex path until it has observed `target`
    // published submissions. Like the eventfd loop simulator, this models only
    // waker-side control flow: check `pending()`, otherwise call `park_idle()`
    // to arm the futex wait target and perform the stale-snapshot recheck.
    fn simulate_futex_loop_until(waker: &Waker, mut processed: u32, target: u32) -> u32 {
        while processed != target {
            if waker.pending(processed) {
                processed = processed.wrapping_add(1) & SUBMISSION_SEQ_MASK;
                continue;
            }
            let _ = waker.park_idle(processed);
        }
        processed
    }

    /// Producer operation used by the deterministic generated loom tests.
    ///
    /// Each generated program is built before entering `loom::model`, then run
    /// by a producer thread. `Publish` models a producer making one request
    /// visible to the loop, while `Wake` models an out-of-band notification that
    /// must not affect submission sequence accounting.
    #[derive(Clone, Copy, Debug)]
    enum ProducerOp {
        /// Publish one request to the waker.
        Publish,
        /// Notify the loop without publishing a request.
        Wake,
    }

    impl ProducerOp {
        // Generate a deterministic publish/wake program for loom exploration.
        fn generate_program(rng: &mut impl Rng, len: usize) -> Vec<Self> {
            (0..len)
                .map(|_| {
                    if rng.gen_bool(0.5) {
                        Self::Publish
                    } else {
                        Self::Wake
                    }
                })
                .collect()
        }

        // Execute one generated producer operation.
        fn execute(self, waker: &Waker, publishes: &AtomicU32) {
            match self {
                ProducerOp::Publish => {
                    waker.publish();
                    publishes.fetch_add(1, Ordering::Relaxed);
                }
                ProducerOp::Wake => waker.wake(),
            }
        }
    }

    #[test]
    fn publish_pending_pairing() {
        // `publish` must make the producer's earlier enqueue-side write visible
        // to a loop that observes the published sequence through `pending()`.
        // The loop deliberately spins on `pending()` before joining the producer
        // so the only intended synchronization is publish Release to pending
        // Acquire.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let queued = Arc::new(QueuedRequest::empty());

            let producer = thread::spawn({
                let waker = waker.clone();
                let queued = queued.clone();
                move || {
                    queued.enqueue(42);
                    waker.publish();
                }
            });

            while !waker.pending(0) {
                thread::yield_now();
            }

            assert_eq!(queued.read(), 42);
            producer.join().unwrap();
            assert_eq!(submitted_seq(&waker), 1);
        });
    }

    #[test]
    fn wake_clear_wait_pairing() {
        // `wake` is used by out-of-band callers such as final-handle drop. It
        // must publish the caller's earlier state change to the loop even though
        // it does not advance the submitted sequence.
        //
        // The loop waits for the wake bit before joining the notifier, arms
        // against the current sequence, and drops the guard so `clear_wait()`'s
        // Acquire can pair with `wake()`'s Release.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let queued = Arc::new(QueuedRequest::empty());

            let notifier = thread::spawn({
                let waker = waker.clone();
                let queued = queued.clone();
                move || {
                    queued.enqueue(42);
                    waker.wake();
                }
            });

            wait_for_wake_signal(&waker);

            assert_eq!(eventfd_count(&waker), 0);
            let guard = waker.arm(0);
            assert!(guard.wake_latched());
            drop(guard);

            assert_eq!(queued.read(), 42);
            assert_eq!(eventfd_count(&waker), 0);
            notifier.join().unwrap();
        });
    }

    #[test]
    fn concurrent_unarmed_wakes_coalesce() {
        // Concurrent out-of-band wakes that arrive before the loop arms should
        // coalesce to one sticky wake bit without queuing eventfd readiness.
        loom::model(|| {
            let waker = Waker::new().unwrap();

            let a = thread::spawn({
                let waker = waker.clone();
                move || waker.wake()
            });
            let b = thread::spawn({
                let waker = waker.clone();
                move || waker.wake()
            });

            a.join().unwrap();
            b.join().unwrap();

            assert_eq!(eventfd_count(&waker), 0);
            let guard = waker.arm(0);
            assert!(!guard.still_idle());
            assert!(guard.wake_latched());
            drop(guard);

            assert_eq!(submitted_seq(&waker), 0);
            assert_eq!(state_bits(&waker), 0);
            assert_eq!(eventfd_count(&waker), 0);
        });
    }

    #[test]
    fn arm_and_recheck_eventfd_race() {
        // A publish racing with the eventfd-backed arm path must be visible
        // either in the post-arm sequence snapshot or through a modeled eventfd
        // wake. After the blocking section exits, guard drop clears wait state
        // and `acknowledge` drains the eventfd counter.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let producer = thread::spawn({
                let waker = waker.clone();
                move || waker.publish()
            });

            let guard = waker.arm(0);
            if guard.still_idle() {
                wait_for_eventfd_readiness(&waker);
            }

            drop(guard);
            waker.acknowledge();
            producer.join().unwrap();

            assert_eq!(submitted_seq(&waker), 1);
            assert_eq!(state_bits(&waker), 0);
            assert_eq!(eventfd_count(&waker), 0);
        });
    }

    #[test]
    fn publish_clear_wait_pairing_when_armed() {
        // When a producer publishes into an armed eventfd epoch, the loop can
        // resume without first observing `pending()`. `clear_wait()` must still
        // acquire the producer's enqueue-side writes before the loop checks the
        // queue after waking.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let queued = Arc::new(QueuedRequest::empty());
            let guard = waker.arm(0);
            assert!(guard.still_idle());

            let producer = thread::spawn({
                let waker = waker.clone();
                let queued = queued.clone();
                move || {
                    queued.enqueue(42);
                    waker.publish();
                }
            });

            wait_for_wake_signal(&waker);

            drop(guard);
            assert_eq!(queued.read(), 42);
            producer.join().unwrap();

            assert_eq!(submitted_seq(&waker), 1);
            assert_eq!(state_bits(&waker), 0);
            assert_eq!(eventfd_count(&waker), 1);
            waker.acknowledge();
            assert_eq!(eventfd_count(&waker), 0);
        });
    }

    #[test]
    fn wake_clear_wait_pairing_when_armed() {
        // When an out-of-band wake lands in an armed eventfd epoch, the loop
        // resumes without any sequence progress. `clear_wait()` must still
        // acquire the notifier's earlier state change before the loop checks for
        // disconnect or shutdown state after waking.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let queued = Arc::new(QueuedRequest::empty());
            let guard = waker.arm(0);
            assert!(guard.still_idle());

            let notifier = thread::spawn({
                let waker = waker.clone();
                let queued = queued.clone();
                move || {
                    queued.enqueue(42);
                    waker.wake();
                }
            });

            wait_for_wake_signal(&waker);

            drop(guard);
            assert_eq!(queued.read(), 42);
            notifier.join().unwrap();

            assert_eq!(submitted_seq(&waker), 0);
            assert_eq!(state_bits(&waker), 0);
            assert_eq!(eventfd_count(&waker), 1);
            waker.acknowledge();
            assert_eq!(eventfd_count(&waker), 0);
        });
    }

    #[test]
    fn arm_and_recheck_futex_race() {
        // The fully-idle path arms a futex wait target on the same state word
        // that producers update. A racing publish must either change the
        // post-arm snapshot before sleep or wake the modeled futex waiter.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let producer = thread::spawn({
                let waker = waker.clone();
                move || waker.publish()
            });

            let _ = waker.park_idle(0);
            producer.join().unwrap();

            assert_eq!(submitted_seq(&waker), 1);
            finish_leftover_wake(&waker);
        });
    }

    #[test]
    fn publishers_dedup_eventfd_wake() {
        // Two publishers in one armed eventfd epoch must both advance the
        // submitted sequence, but only the first wake claimant should increment
        // the modeled eventfd counter.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let guard = waker.arm(0);
            assert!(guard.still_idle());

            let a = thread::spawn({
                let waker = waker.clone();
                move || waker.publish()
            });
            let b = thread::spawn({
                let waker = waker.clone();
                move || waker.publish()
            });

            a.join().unwrap();
            b.join().unwrap();

            assert_eq!(submitted_seq(&waker), 2);
            assert_eq!(eventfd_count(&waker), 1);

            drop(guard);
            waker.acknowledge();
            assert_eq!(state_bits(&waker), 0);
            assert_eq!(eventfd_count(&waker), 0);
        });
    }

    #[test]
    fn mixed_publish_and_wake_dedup() {
        // A publish and an out-of-band wake racing in the same armed eventfd
        // epoch should coalesce to one eventfd signal while still preserving the
        // publish's sequence increment.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let guard = waker.arm(0);
            assert!(guard.still_idle());

            let publisher = thread::spawn({
                let waker = waker.clone();
                move || waker.publish()
            });
            let notifier = thread::spawn({
                let waker = waker.clone();
                move || waker.wake()
            });

            publisher.join().unwrap();
            notifier.join().unwrap();

            assert_eq!(submitted_seq(&waker), 1);
            assert_eq!(eventfd_count(&waker), 1);

            drop(guard);
            waker.acknowledge();
            assert_eq!(state_bits(&waker), 0);
            assert_eq!(eventfd_count(&waker), 0);
        });
    }

    #[test]
    fn mixed_publish_and_wake_futex_arm() {
        // A publish and an out-of-band wake racing in the same futex-armed epoch
        // should coalesce through the shared wake latch while preserving the
        // publish's sequence increment. Unlike the eventfd path, there is no
        // durable counter to inspect, so this splits `park_idle()` at the arm
        // point and verifies the stale futex snapshot is rejected after the
        // state changes.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let prev = waker
                .inner
                .state
                .fetch_or(WAITING_ON_FUTEX_BIT, Ordering::Relaxed);
            assert_eq!(prev & WAITING_MASK, 0);
            let snapshot = prev | WAITING_ON_FUTEX_BIT;

            let publisher = thread::spawn({
                let waker = waker.clone();
                move || waker.publish()
            });
            let notifier = thread::spawn({
                let waker = waker.clone();
                move || waker.wake()
            });

            publisher.join().unwrap();
            notifier.join().unwrap();

            assert_eq!(submitted_seq(&waker), 1);
            assert_eq!(eventfd_count(&waker), 0);
            assert_eq!(
                state_bits(&waker),
                WAITING_ON_FUTEX_BIT | WAKE_SIGNALLED_BIT
            );
            assert!(!waker.futex_wait(snapshot));
            waker.clear_wait();
            assert_eq!(state_bits(&waker), 0);
        });
    }

    #[test]
    fn drop_wake() {
        // An out-of-band wake racing with the eventfd arm path must wake the
        // loop without advancing the submitted sequence. If it arrives before
        // arming, `wake_latched` skips the wait, otherwise the modeled eventfd
        // signal releases the loop.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let notifier = thread::spawn({
                let waker = waker.clone();
                move || waker.wake()
            });

            let guard = waker.arm(0);
            if guard.still_idle() {
                wait_for_eventfd_readiness(&waker);
            }

            drop(guard);
            waker.acknowledge();
            notifier.join().unwrap();

            assert_eq!(submitted_seq(&waker), 0);
            assert_eq!(state_bits(&waker), 0);
            assert_eq!(eventfd_count(&waker), 0);
        });
    }

    #[test]
    fn sequence_wraparound() {
        // Preload the sequence to the last representable value, then publish
        // twice so the visible sequence wraps through zero to one. The
        // half-range modular `pending()` check must remain directional across
        // that boundary.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            waker
                .inner
                .state
                .store(SUBMISSION_SEQ_MASK << STATE_BITS, Ordering::Relaxed);

            let producer = thread::spawn({
                let waker = waker.clone();
                move || {
                    waker.publish();
                    waker.publish();
                }
            });

            assert_eq!(
                simulate_eventfd_loop_until(&waker, SUBMISSION_SEQ_MASK, 1),
                1
            );
            producer.join().unwrap();
            assert_eq!(submitted_seq(&waker), 1);
            finish_leftover_wake(&waker);
        });
    }

    #[test]
    fn two_producers_mixed_ops() {
        // Producer-only mixed publish/wake programs should preserve submitted
        // sequence conservation and must not queue eventfd readiness while the
        // loop is unarmed. A sticky wake bit may remain for the next arm cycle.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let publishes = Arc::new(AtomicU32::new(0));

            let a = thread::spawn({
                let waker = waker.clone();
                let publishes = publishes.clone();
                move || {
                    waker.publish();
                    publishes.fetch_add(1, Ordering::Relaxed);
                    waker.wake();
                    waker.publish();
                    publishes.fetch_add(1, Ordering::Relaxed);
                }
            });

            let b = thread::spawn({
                let waker = waker.clone();
                let publishes = publishes.clone();
                move || {
                    waker.wake();
                    waker.publish();
                    publishes.fetch_add(1, Ordering::Relaxed);
                }
            });

            a.join().unwrap();
            b.join().unwrap();

            assert_eq!(submitted_seq(&waker), publishes.load(Ordering::Relaxed));
            assert_eq!(state_bits(&waker) & WAITING_MASK, 0);
            assert_eq!(eventfd_count(&waker), 0);
        });
    }

    #[test]
    fn producer_with_draining_loop() {
        // A minimal loop simulator must drain both publishes from one producer
        // using the eventfd arm-and-recheck path whenever no sequence progress
        // is currently visible.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let producer = thread::spawn({
                let waker = waker.clone();
                move || {
                    waker.publish();
                    waker.publish();
                }
            });

            let processed = simulate_eventfd_loop_until(&waker, 0, 2);
            producer.join().unwrap();

            assert_eq!(processed, 2);
            assert_eq!(submitted_seq(&waker), 2);
            finish_leftover_wake(&waker);
        });
    }

    #[test]
    fn park_idle_with_concurrent_wake() {
        // The fully-idle futex path must also handle pure out-of-band wakes.
        // The loop either sees the wake bit before sleeping or is resumed by the
        // modeled futex wake. No submission sequence bump is involved.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let notifier = thread::spawn({
                let waker = waker.clone();
                move || waker.wake()
            });

            let _ = waker.park_idle(0);
            notifier.join().unwrap();

            assert_eq!(submitted_seq(&waker), 0);
            assert_eq!(state_bits(&waker), 0);
            assert_eq!(eventfd_count(&waker), 0);
        });
    }

    #[test]
    fn two_cycle_drain_with_interleaved_wake() {
        // A drain loop must survive an explicit wake between two publishes. The
        // wake may be consumed as a sticky bit or as eventfd readiness, but both
        // publishes must still be processed exactly once.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let producer = thread::spawn({
                let waker = waker.clone();
                move || {
                    waker.publish();
                    waker.wake();
                    waker.publish();
                }
            });

            let processed = simulate_eventfd_loop_until(&waker, 0, 2);
            producer.join().unwrap();

            assert_eq!(processed, 2);
            assert_eq!(submitted_seq(&waker), 2);
            finish_leftover_wake(&waker);
        });
    }

    #[test]
    fn multiple_park_idle_cycles() {
        // Repeated fully-idle futex park cycles must continue to observe
        // publishes. This uses `park_idle()` instead of the eventfd arm path
        // whenever no sequence progress is currently visible.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let producer = thread::spawn({
                let waker = waker.clone();
                move || {
                    waker.publish();
                    waker.publish();
                }
            });

            let processed = simulate_futex_loop_until(&waker, 0, 2);
            producer.join().unwrap();

            assert_eq!(processed, 2);
            assert_eq!(submitted_seq(&waker), 2);
            finish_leftover_wake(&waker);
        });
    }

    #[test]
    fn three_thread_stress() {
        // Two producers publishing concurrently with one loop simulator should
        // still preserve conservation and progress. This adds one more producer
        // thread to the eventfd drain shape.
        loom::model(|| {
            let waker = Waker::new().unwrap();
            let a = thread::spawn({
                let waker = waker.clone();
                move || waker.publish()
            });
            let b = thread::spawn({
                let waker = waker.clone();
                move || waker.publish()
            });

            let processed = simulate_eventfd_loop_until(&waker, 0, 2);
            a.join().unwrap();
            b.join().unwrap();

            assert_eq!(processed, 2);
            assert_eq!(submitted_seq(&waker), 2);
            finish_leftover_wake(&waker);
        });
    }

    #[test]
    fn generated_producer_only_programs() {
        // Generate deterministic producer-only programs before entering loom,
        // then model each case with two concurrent producers. Each producer
        // runs a short sequence of `publish()` and out-of-band `wake()` calls
        // without any loop thread consuming them.
        //
        // The invariant is producer-side conservation: every generated `Publish`
        // must be reflected in the submitted sequence exactly once, while
        // generated `Wake`s must not affect the sequence. Since the waker is
        // never armed in this test, producers must also leave no wait target
        // armed and must not queue modeled eventfd readiness. A sticky wake bit
        // may remain because there is intentionally no loop to consume it.
        const CASES: usize = 24;
        const OPS_PER_PROGRAM: usize = 5;

        let mut rng = test_rng();
        let programs = (0..CASES)
            .map(|_| {
                [
                    ProducerOp::generate_program(&mut rng, OPS_PER_PROGRAM),
                    ProducerOp::generate_program(&mut rng, OPS_PER_PROGRAM),
                ]
            })
            .collect::<Vec<_>>();

        for (iter, programs) in programs.into_iter().enumerate() {
            loom::model(move || {
                let waker = Waker::new().unwrap();
                let publishes = Arc::new(AtomicU32::new(0));

                let handles = programs
                    .iter()
                    .map(|program| {
                        let program = program.clone();
                        let waker = waker.clone();
                        let publishes = publishes.clone();
                        thread::spawn(move || {
                            for &op in program.iter() {
                                op.execute(&waker, &publishes);
                            }
                        })
                    })
                    .collect::<Vec<_>>();

                for handle in handles {
                    handle.join().unwrap();
                }

                let expected = publishes.load(Ordering::Relaxed);
                let got = submitted_seq(&waker);
                assert_eq!(
                    got, expected,
                    "publish conservation failed: iter={iter} programs={programs:?}",
                );
                assert_eq!(
                    state_bits(&waker) & WAITING_MASK,
                    0,
                    "wait target remained armed: iter={iter} programs={programs:?}",
                );
                assert_eq!(
                    eventfd_count(&waker),
                    0,
                    "eventfd readiness queued while unarmed: iter={iter} programs={programs:?}",
                );
            });
        }
    }

    fn generated_loop_programs(
        cases: usize,
        ops_per_program: usize,
        simulate_loop_until: fn(&Waker, u32, u32) -> u32,
    ) {
        let mut rng = test_rng();
        let programs = (0..cases)
            .map(|_| ProducerOp::generate_program(&mut rng, ops_per_program))
            .collect::<Vec<_>>();

        for (iter, program) in programs.into_iter().enumerate() {
            let publish_count = program
                .iter()
                .filter(|op| matches!(op, ProducerOp::Publish))
                .count() as u32;

            loom::model(move || {
                let waker = Waker::new().unwrap();
                let publishes = Arc::new(AtomicU32::new(0));

                let producer = thread::spawn({
                    let program = program.clone();
                    let waker = waker.clone();
                    let publishes = publishes.clone();
                    move || {
                        for &op in program.iter() {
                            op.execute(&waker, &publishes);
                        }
                    }
                });

                let processed = simulate_loop_until(&waker, 0, publish_count);
                producer.join().unwrap();

                assert_eq!(
                    processed, publish_count,
                    "loop progress failed: iter={iter} program={program:?}",
                );
                assert_eq!(
                    submitted_seq(&waker),
                    publish_count,
                    "publish conservation failed: iter={iter} program={program:?}",
                );
                assert_eq!(
                    publishes.load(Ordering::Relaxed),
                    publish_count,
                    "producer accounting failed: iter={iter} program={program:?}",
                );
                finish_leftover_wake(&waker);
            });
        }
    }

    #[test]
    fn generated_eventfd_loop_programs() {
        // Generate deterministic single-producer programs before entering loom,
        // then model each case with one producer and the eventfd loop simulator.
        // The producer may interleave out-of-band `wake()` calls before,
        // between, or after its generated `publish()` calls.
        //
        // The loop simulator must eventually observe exactly the generated
        // publish count, regardless of whether progress arrives through
        // `pending()` or through the arm, eventfd readiness, and `clear_wait()`
        // path. Pure wakes are allowed to resume the loop, but they must not
        // create sequence progress or disturb producer accounting.
        const CASES: usize = 96;
        const OPS_PER_PROGRAM: usize = 3;

        generated_loop_programs(CASES, OPS_PER_PROGRAM, simulate_eventfd_loop_until);
    }

    #[test]
    fn generated_futex_loop_programs() {
        // Generate deterministic single-producer programs before entering loom,
        // then model each case with one producer and the futex idle loop
        // simulator. The producer may interleave out-of-band `wake()` calls
        // before, between, or after its generated `publish()` calls.
        //
        // This is the futex-path counterpart to `generated_eventfd_loop_programs`.
        // The loop simulator must drain exactly the generated publish count
        // through `pending()` or `park_idle()`, while pure wakes may resume the
        // futex wait without creating sequence progress.
        const CASES: usize = 16;
        const OPS_PER_PROGRAM: usize = 3;

        generated_loop_programs(CASES, OPS_PER_PROGRAM, simulate_futex_loop_until);
    }
}