flynnel 0.6.0

K-aware NUMA-aware work-stealing scheduler with extended-Flynn-taxonomy dispatch (MIMD / SIMC / MIMC / MISD / SIMT / MIMT): a rayon-shaped pool reshaped around per-call execution-class plans with first-class CPU+GPU hybrid (join_hybrid, hybrid_pipeline) and per-call dispatch profiles.
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
//! Bump-allocated arena of fixed-size latch cells, backed by a
//! memory-mapped file.
//!
//! Each `LatchCell` is one 64-byte cache line: one `AtomicU8` state
//! byte plus a small result-bytes inline buffer. The arena's bump
//! pointer is itself in MMF so cross-process producers can allocate
//! latch cells without coordinating out-of-band.
//!
//! ## Role
//!
//! Carries the result-publication leg of the cross-process dispatch
//! shape paired with the MMF Chase-Lev deque
//! ([`super::chase_lev_mmf`]). The originator allocates a cell,
//! stamps its offset into a deque slot, then polls the cell's
//! `state` byte until the peer publishes the result inline; the
//! peer Release-stores `SET` after copying its reply bytes into the
//! cell, and the originator's `Acquire` load + read sees them.
//!
//! ## Layout
//!
//! ```text
//! +-----------------------------+
//! | LatchArenaHeader (64B)      |  magic, capacity, next_free
//! +-----------------------------+
//! | LatchCell[0]   (64B)        |  state + result_bytes[55]
//! | LatchCell[1]                |
//! | ...                         |
//! | LatchCell[capacity - 1]     |
//! +-----------------------------+
//! ```
//!
//! ## Protocol
//!
//! 1. **Originator** calls [`MmfLatchArena::alloc`] - bumps
//!    `next_free` with a `fetch_add` and returns the offset of a
//!    fresh `UNSET` cell.
//! 2. **Originator** writes the cell offset into the Chase-Lev
//!    deque slot's `latch_offset` field, then pushes the slot.
//! 3. **Thief** drains the slot, executes the handler, then calls
//!    [`MmfLatchArena::publish`] - copies the result bytes into the
//!    cell and Release-stores `state = SET`.
//! 4. **Originator** spins on [`MmfLatchArena::is_set`] (Acquire);
//!    once set, reads the result bytes with [`MmfLatchArena::read_result`].
//! 5. **Originator** optionally calls [`MmfLatchArena::reset`] to
//!    return the cell to `UNSET` for reuse (no free-list; cells age
//!    out as `next_free` wraps).
//!
//! Cells are 64 bytes wide so different requesters never share a
//! cache line; the per-cell `state` byte lives in its own line and
//! the originator's polling load never invalidates a sibling cell.

#![allow(clippy::missing_errors_doc)]

use std::fs::{File, OpenOptions};
use std::io;
use std::path::Path;
use std::sync::atomic::{AtomicU8, AtomicU64, Ordering};

use memmap2::{MmapMut, MmapOptions};

/// Magic byte sequence marking a valid latch arena file. Reads as
/// ASCII "FLLA" then a version byte. Distinct from the Chase-Lev
/// deque magic so a file confusion is rejected at open time.
pub const LATCH_ARENA_MAGIC: u64 = 0x464C_4C41_0000_0001;

/// Each latch cell is exactly one cache line so different requesters
/// poll on disjoint lines.
pub const LATCH_CELL_SIZE: usize = 64;

/// Result-bytes capacity per cell = [`LATCH_CELL_SIZE`] minus the
/// state byte and its alignment padding.
pub const RESULT_BYTES: usize = LATCH_CELL_SIZE - 8;

/// Cell state: not yet published.
pub const UNSET: u8 = 0;
/// Cell state: publisher has written `result_bytes` and Release-stored
/// this flag. Originator may Acquire-load and read the result.
pub const SET: u8 = 1;
/// Cell state: publisher reports an error condition. `result_bytes`
/// holds a UTF-8 diagnostic if `result_len > 0`.
pub const ERR: u8 = 2;

/// Header sits at file offset 0; cache-line aligned so `next_free`
/// has its own coherence-traffic line away from the cells.
#[repr(C, align(64))]
pub struct LatchArenaHeader {
    /// Magic constant set to [`LATCH_ARENA_MAGIC`] on `create`.
    pub magic: u64,
    /// Number of cells in the arena.
    pub capacity: u64,
    /// Bump-allocator counter; cells [0..next_free.load()) have been
    /// handed out at some point. Modulo `capacity` for wrap-around
    /// reuse.
    pub next_free: AtomicU64,
    /// Reserved for future per-arena policy bits.
    pub _reserved: [u8; 40],
}

/// One latch cell. Exactly 64 bytes; the layout is hand-packed to
/// keep the polled `state` byte at offset 0 (lowest cache-line byte)
/// so the load address is the cell's natural alignment.
#[repr(C, align(64))]
pub struct LatchCell {
    /// One of [`UNSET`], [`SET`], [`ERR`]. Originator polls with
    /// Acquire; publisher transitions with Release.
    pub state: AtomicU8,
    /// Length of the published result in `result_bytes`. Valid only
    /// when `state != UNSET`.
    pub result_len: u8,
    /// Padding so `result_bytes` starts on an 8-byte boundary.
    pub _pad: [u8; 6],
    /// Inline result payload. Read-after-Acquire is sound because
    /// the publisher Release-stored `state` after writing here.
    pub result_bytes: [u8; RESULT_BYTES],
}

/// Total file size for an arena with `capacity` cells, including
/// the header.
pub const fn arena_file_size(capacity: usize) -> usize {
    std::mem::size_of::<LatchArenaHeader>() + capacity * LATCH_CELL_SIZE
}

/// Errors from arena operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LatchError {
    /// `alloc()` requested but arena's bump counter has wrapped past
    /// the cell with the given offset and the cell has not been
    /// reset. Caller should pick a different cell or wait.
    NotResetable(u32),
    /// `read_result` / `publish` called with an offset that is not
    /// 64-byte aligned within the arena.
    BadOffset(u32),
    /// `publish` payload larger than `RESULT_BYTES`.
    PayloadTooLarge,
}

/// Cross-thread / cross-process bump-allocated latch arena.
pub struct MmfLatchArena {
    _file: File,
    mmap: MmapMut,
    capacity: usize,
}

// SAFETY: The underlying mmap handle is Send + Sync per memmap2, and
// every cell access is gated by the per-cell Acquire/Release pair on
// `state`. The `UnsafeCell`-equivalent raw-pointer writes through the
// mmap are sound because exactly one publisher writes to a given cell
// between an `alloc()` that returns its offset and the subsequent
// `reset()` or wrap-around reuse.
unsafe impl Send for MmfLatchArena {}
// SAFETY: Same justification as the `Send` impl directly above.
unsafe impl Sync for MmfLatchArena {}

impl MmfLatchArena {
    /// Create a fresh arena file at `path` with `capacity` cells
    /// (rounded up to the next power of two; minimum 2). Truncates
    /// any existing file.
    pub fn create<P: AsRef<Path>>(path: P, capacity: usize) -> io::Result<Self> {
        let capacity = capacity.max(2).next_power_of_two();
        let size = arena_file_size(capacity);

        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(true)
            .open(path.as_ref())?;
        file.set_len(size as u64)?;

        // SAFETY: `map_mut` is unsafe because the kernel cannot
        // prevent another process from truncating or mutating the
        // backing file. This call site upholds the soundness contract
        // by writing only through the per-cell Acquire/Release
        // protocol on `state` (which every accessor follows); the
        // file size is fixed by `file.set_len` immediately above and
        // never shrunk for the lifetime of any mapping.
        let mut mmap = unsafe { MmapOptions::new().len(size).map_mut(&file)? };

        let header_ptr = mmap.as_mut_ptr() as *mut LatchArenaHeader;
        // SAFETY: `mmap.as_mut_ptr()` is page-aligned (well above the
        // 64-byte alignment LatchArenaHeader requires) and the map
        // covers `arena_file_size(capacity)` bytes by construction.
        unsafe {
            (*header_ptr).magic = LATCH_ARENA_MAGIC;
            (*header_ptr).capacity = capacity as u64;
            (*header_ptr).next_free = AtomicU64::new(0);
            std::ptr::write_bytes((*header_ptr)._reserved.as_mut_ptr(), 0, 40);
        }

        let cells_start = std::mem::size_of::<LatchArenaHeader>();
        for i in 0..capacity {
            let off = cells_start + i * LATCH_CELL_SIZE;
            // SAFETY: `off + LATCH_CELL_SIZE <= arena_file_size(capacity)`
            // by construction; cast to `*mut LatchCell` is sound
            // because `LatchCell` is `repr(C, align(64))` and `off`
            // is a multiple of 64.
            let cell_ptr = unsafe { mmap.as_mut_ptr().add(off) as *mut LatchCell };
            // SAFETY: `cell_ptr` is the in-bounds aligned pointer
            // computed immediately above; the cell payload bytes are
            // valid for any bit pattern.
            unsafe {
                (*cell_ptr).state = AtomicU8::new(UNSET);
                (*cell_ptr).result_len = 0;
                std::ptr::write_bytes((*cell_ptr)._pad.as_mut_ptr(), 0, 6);
                std::ptr::write_bytes(
                    (*cell_ptr).result_bytes.as_mut_ptr(),
                    0,
                    RESULT_BYTES,
                );
            }
        }

        mmap.flush()?;

        Ok(Self {
            _file: file,
            mmap,
            capacity,
        })
    }

    /// Open an existing arena file at `path`. Validates magic +
    /// capacity headers.
    pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> {
        let file = OpenOptions::new().read(true).write(true).open(path.as_ref())?;
        let size = file.metadata()?.len() as usize;
        if size < std::mem::size_of::<LatchArenaHeader>() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "latch arena file too small to contain header",
            ));
        }

        // SAFETY: Same justification as the `create` path above -
        // protocol-only access. Other processes may concurrently
        // mutate the cells through the per-cell Acquire/Release
        // protocol on `state`; we honor the same protocol here.
        let mmap = unsafe { MmapOptions::new().len(size).map_mut(&file)? };

        let header_ptr = mmap.as_ptr() as *const LatchArenaHeader;
        // SAFETY: map size verified to cover header; mmap alignment
        // exceeds header alignment.
        let (magic, capacity) = unsafe {
            ((*header_ptr).magic, (*header_ptr).capacity as usize)
        };
        if magic != LATCH_ARENA_MAGIC {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("latch arena magic mismatch: got {magic:#x}, want {LATCH_ARENA_MAGIC:#x}"),
            ));
        }
        if !capacity.is_power_of_two() || capacity < 2 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("latch arena capacity {capacity} is not a power of two >= 2"),
            ));
        }
        if size < arena_file_size(capacity) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("latch arena file size {size} below expected {}", arena_file_size(capacity)),
            ));
        }

        Ok(Self {
            _file: file,
            mmap,
            capacity,
        })
    }

    /// Number of cells in this arena (always a power of two).
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    fn header(&self) -> &LatchArenaHeader {
        // SAFETY: map is sized to cover the header; alignment
        // satisfied by mmap's page alignment.
        unsafe { &*(self.mmap.as_ptr() as *const LatchArenaHeader) }
    }

    fn cell(&self, idx: usize) -> &LatchCell {
        let off = std::mem::size_of::<LatchArenaHeader>() + idx * LATCH_CELL_SIZE;
        // SAFETY: idx < capacity by caller contract; `off` is within
        // the mapped region and 64-byte aligned.
        unsafe { &*(self.mmap.as_ptr().add(off) as *const LatchCell) }
    }

    /// Allocate a fresh cell. Bumps `next_free` and returns the
    /// cell's byte offset within the file (suitable for stashing in
    /// a Chase-Lev slot's `latch_offset` field). The returned cell
    /// has `state = UNSET` (Release-stored) and `result_len = 0`.
    ///
    /// Note: `result_bytes` is not zeroed by `alloc`; it may still
    /// hold data from the previous publisher when a wrap-around
    /// reuses the cell. Safety of subsequent reads relies on
    /// `result_len` bounding the copy in [`Self::read_result`] and
    /// on [`Self::publish`] zeroing before writing new bytes.
    ///
    /// Cells wrap modulo capacity; reuse of an in-flight cell is
    /// caller-detectable via [`Self::is_set`] returning `true` for a
    /// cell that the caller did not just publish (impossible if the
    /// caller publishes promptly and `capacity` exceeds steady-state
    /// in-flight latch count).
    pub fn alloc(&self) -> u32 {
        let n = self.header().next_free.fetch_add(1, Ordering::Relaxed);
        let idx = (n & (self.capacity as u64 - 1)) as usize;
        let cell = self.cell(idx);
        // Reset the cell so the new owner observes a clean UNSET
        // even if the wrap-around case is hit.
        cell.state.store(UNSET, Ordering::Release);
        // SAFETY: Mutating `result_len` requires &mut access on the
        // cell, but we only hold &. The protocol guarantees no other
        // thread reads `result_len` until `state != UNSET`, and the
        // Release-store of UNSET above happens-before this write
        // (single thread, single function). Using the raw pointer
        // path is sound for the same reason `_pad` is initialized
        // through `write_bytes`: the cell layout is process-local
        // POD until the state Release is observed.
        unsafe {
            let cell_ptr = cell as *const _ as *mut LatchCell;
            (*cell_ptr).result_len = 0;
        }
        (std::mem::size_of::<LatchArenaHeader>() + idx * LATCH_CELL_SIZE) as u32
    }

    /// Translate a byte offset back to a cell index. Returns
    /// [`LatchError::BadOffset`] if the offset is not 64-byte
    /// aligned or falls outside the arena.
    fn offset_to_idx(&self, offset: u32) -> Result<usize, LatchError> {
        let header_size = std::mem::size_of::<LatchArenaHeader>();
        if (offset as usize) < header_size {
            return Err(LatchError::BadOffset(offset));
        }
        let rel = offset as usize - header_size;
        if !rel.is_multiple_of(LATCH_CELL_SIZE) {
            return Err(LatchError::BadOffset(offset));
        }
        let idx = rel / LATCH_CELL_SIZE;
        if idx >= self.capacity {
            return Err(LatchError::BadOffset(offset));
        }
        Ok(idx)
    }

    /// Test whether the cell at `offset` has been published. Acquire
    /// ordered so a subsequent read of `result_bytes` sees the
    /// publisher's writes.
    pub fn is_set(&self, offset: u32) -> Result<bool, LatchError> {
        let idx = self.offset_to_idx(offset)?;
        let s = self.cell(idx).state.load(Ordering::Acquire);
        Ok(s != UNSET)
    }

    /// The address of the cell's polled `state` byte, the first byte of
    /// the cell's own cache line, for a waiter that arms the host's
    /// monitor on that line. The publisher's `Release` store to `state`
    /// lands on this line whichever process makes it. The pointer is
    /// into this arena's mapping and is valid for as long as the arena.
    pub(crate) fn state_line(&self, offset: u32) -> Result<*const u8, LatchError> {
        let idx = self.offset_to_idx(offset)?;
        Ok(self.cell(idx).state.as_ptr().cast_const())
    }

    /// Publisher path. Copies `payload` into the cell's
    /// `result_bytes` and Release-stores `state = SET`. Returns
    /// [`LatchError::PayloadTooLarge`] if the payload exceeds
    /// [`RESULT_BYTES`].
    pub fn publish(&self, offset: u32, payload: &[u8]) -> Result<(), LatchError> {
        if payload.len() > RESULT_BYTES {
            return Err(LatchError::PayloadTooLarge);
        }
        let idx = self.offset_to_idx(offset)?;
        let cell = self.cell(idx);
        // SAFETY: writes go through the protocol: we copy bytes then
        // Release-store state. Until that Release, no other thread
        // reads `result_bytes` (originator polls `state` with
        // Acquire). Casting to mut is sound because (a) we are the
        // only writer to this cell by protocol convention - exactly
        // one publisher per cell allocation - and (b) the cell
        // layout is POD outside the atomic.
        unsafe {
            let cell_ptr = cell as *const _ as *mut LatchCell;
            std::ptr::write_bytes(
                (*cell_ptr).result_bytes.as_mut_ptr(),
                0,
                RESULT_BYTES,
            );
            std::ptr::copy_nonoverlapping(
                payload.as_ptr(),
                (*cell_ptr).result_bytes.as_mut_ptr(),
                payload.len(),
            );
            (*cell_ptr).result_len = payload.len() as u8;
        }
        cell.state.store(SET, Ordering::Release);
        Ok(())
    }

    /// Publish an error condition into the cell. `state` transitions
    /// to [`ERR`]; the caller's `read_result` returns the diagnostic
    /// bytes.
    pub fn publish_err(&self, offset: u32, diagnostic: &[u8]) -> Result<(), LatchError> {
        if diagnostic.len() > RESULT_BYTES {
            return Err(LatchError::PayloadTooLarge);
        }
        let idx = self.offset_to_idx(offset)?;
        let cell = self.cell(idx);
        // SAFETY: same justification as `publish` - we hold the
        // single-publisher slot, Release-store gates the bytes.
        unsafe {
            let cell_ptr = cell as *const _ as *mut LatchCell;
            std::ptr::write_bytes(
                (*cell_ptr).result_bytes.as_mut_ptr(),
                0,
                RESULT_BYTES,
            );
            std::ptr::copy_nonoverlapping(
                diagnostic.as_ptr(),
                (*cell_ptr).result_bytes.as_mut_ptr(),
                diagnostic.len(),
            );
            (*cell_ptr).result_len = diagnostic.len() as u8;
        }
        cell.state.store(ERR, Ordering::Release);
        Ok(())
    }

    /// Read the published result bytes into `dst`. Returns the cell
    /// state (`SET` or `ERR`); call [`Self::is_set`] first to gate
    /// the read.
    pub fn read_result(&self, offset: u32, dst: &mut Vec<u8>) -> Result<u8, LatchError> {
        let idx = self.offset_to_idx(offset)?;
        let cell = self.cell(idx);
        let state = cell.state.load(Ordering::Acquire);
        let len = cell.result_len as usize;
        dst.clear();
        dst.resize(len, 0);
        // SAFETY: `result_bytes` is a fixed-size inline array of
        // length RESULT_BYTES; `len <= RESULT_BYTES` because
        // result_len is a u8 and we bounds-checked in publish. The
        // Acquire above synchronizes-with the publisher's Release
        // so the bytes we read are the bytes the publisher wrote.
        unsafe {
            std::ptr::copy_nonoverlapping(
                cell.result_bytes.as_ptr(),
                dst.as_mut_ptr(),
                len,
            );
        }
        Ok(state)
    }

    /// Reset a cell to `UNSET`. Used by the originator after
    /// consuming the result, freeing the cell for reuse before the
    /// bump counter wraps. No fence is needed beyond the Release
    /// because subsequent reuse will Release-store again on publish.
    pub fn reset(&self, offset: u32) -> Result<(), LatchError> {
        let idx = self.offset_to_idx(offset)?;
        self.cell(idx).state.store(UNSET, Ordering::Release);
        Ok(())
    }

    /// Force any dirty pages to disk. Only meaningful for durable
    /// arenas; tmpfs / disposable arenas no-op.
    pub fn flush(&self) -> io::Result<()> {
        self.mmap.flush()
    }
}

/// The spin phase of a wait on one latch cell: `polls` calls of `poll`
/// with a `PAUSE` after each, answering the first that completes, or
/// `None` when the phase ends with the cell still unset, so the caller
/// goes on to whatever follows its spin.
///
/// With `monitor`, on a host whose monitor holds, the phase is instead a
/// timed floor of those polls and then bounded monitor waits on the
/// cell's own line for as long as the rest of the polls would have taken
/// on this host. Both arms leave the phase at the same moment and differ
/// only in what the thread did inside it: a poll issues into the
/// pipeline for the whole span, while a monitor wait issues nothing and
/// ends on the publisher's store as promptly as the next poll would have
/// seen it. It is the shape of
/// [`crate::sched::latch::LockLatch::wait_spin_then_monitor`], for a store
/// that comes from another process rather than another thread.
///
/// `monitor` is decided once, by the caller: the backends pass
/// [`crate::sched::levers::backend_spin_monitor`], and a test passes
/// either arm. `line_error` turns the one error only the monitor rung
/// can meet, a cell with no line, into the caller's own error type.
pub(crate) fn spin_phase<T, E>(
    arena: &MmfLatchArena,
    offset: u32,
    polls: u32,
    monitor: bool,
    line_error: impl FnOnce(LatchError) -> E,
    mut poll: impl FnMut() -> Result<Option<T>, E>,
) -> Result<Option<T>, E> {
    let polls = polls as usize;
    let use_monitor = monitor && crate::sched::sleep::monitor_wait_available();
    if !use_monitor {
        for _ in 0..polls {
            if let Some(done) = poll()? {
                return Ok(Some(done));
            }
            std::hint::spin_loop();
        }
        return Ok(None);
    }

    // The floor covers a store already on its way, and its timing sizes
    // the rung: a poll costs a different number of cycles on every part,
    // so the only way to spend the wall time the other arm spends is to
    // measure this host's poll here, on polls that were going to run.
    let floor = polls.min(crate::sched::latch::MONITOR_SPIN_FLOOR);
    let floor_start = crate::sched::sleep::cycles_now();
    for _ in 0..floor {
        if let Some(done) = poll()? {
            return Ok(Some(done));
        }
        std::hint::spin_loop();
    }
    if floor == polls {
        return Ok(None);
    }
    let per_poll = crate::sched::sleep::cycles_now().saturating_sub(floor_start) / floor as u64;
    // A floor the scheduler interrupted measured the interruption, and a
    // rung sized from it would hold this thread far past the polls it
    // stands in for, so that phase takes the rest of its polls instead.
    if per_poll == 0 || per_poll > crate::sched::latch::MAX_MEASURED_POLL_CYCLES {
        for _ in floor..polls {
            if let Some(done) = poll()? {
                return Ok(Some(done));
            }
            std::hint::spin_loop();
        }
        return Ok(None);
    }

    let span = ((polls - floor) as u64).saturating_mul(per_poll);
    let line = arena.state_line(offset).map_err(line_error)?;
    let state = line.cast::<AtomicU8>();
    // What the cell's poll reads, straight from the line the monitor is
    // armed on: `state_line` has already located the cell, so this read
    // has nothing left that can fail.
    let set_now = || {
        // SAFETY: `state` is this cell's `AtomicU8` state byte inside the
        // arena's mapping, which lives as long as `arena`.
        unsafe { &*state }.load(Ordering::Acquire) != UNSET
    };
    crate::sched::sleep::note_backend_monitor_wait();
    let start = crate::sched::sleep::cycles_now();
    loop {
        if let Some(done) = poll()? {
            return Ok(Some(done));
        }
        let left = span.saturating_sub(crate::sched::sleep::cycles_now().saturating_sub(start));
        if left == 0 {
            return Ok(None);
        }
        crate::sched::sleep::note_backend_monitor_arm();
        // SAFETY: `line` points into this arena's own mapping, which
        // lives as long as `arena`, and the wait is issued only where
        // CPUID reported the instruction pair.
        let waited = unsafe { crate::sched::sleep::monitor_wait_once(line, left, set_now) };
        if !waited {
            // The monitor stopped holding. The rest of the span polls, so
            // this arm still leaves the phase when the other would.
            while crate::sched::sleep::cycles_now().saturating_sub(start) < span {
                if let Some(done) = poll()? {
                    return Ok(Some(done));
                }
                std::hint::spin_loop();
            }
            return Ok(None);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::thread;

    /// Removes a test's arena file, saying so when it cannot.
    fn remove(path: &std::path::Path) {
        if let Err(e) = std::fs::remove_file(path) {
            eprintln!("could not remove {}: {e}", path.display());
        }
    }

    /// The result of the cell at `off` once it is set, read the way a
    /// backend's poll reads it. The tests publish only successes, so any
    /// other state is a failure of the test itself.
    fn poll_cell(arena: &MmfLatchArena, off: u32) -> Result<Option<Vec<u8>>, LatchError> {
        if !arena.is_set(off)? {
            return Ok(None);
        }
        let mut out = Vec::new();
        let state = arena.read_result(off, &mut out)?;
        assert_eq!(state, SET, "the tests publish only successes");
        Ok(Some(out))
    }

    #[test]
    fn the_spin_phase_answers_a_publish_on_both_arms() {
        for monitor in [false, true] {
            let path = temp_path(if monitor { "spin_monitor" } else { "spin_plain" });
            let arena = Arc::new(MmfLatchArena::create(&path, 4).expect("create"));
            let off = arena.alloc();
            let publisher = {
                let arena = Arc::clone(&arena);
                thread::spawn(move || {
                    thread::sleep(std::time::Duration::from_millis(5));
                    arena.publish(off, b"done").expect("publish");
                })
            };
            let got = spin_phase(&arena, off, u32::MAX, monitor, |e| e, || poll_cell(&arena, off))
                .expect("the cell reads");
            publisher.join().expect("publisher joined");
            assert_eq!(got.as_deref(), Some(&b"done"[..]), "monitor = {monitor}");
            remove(&path);
        }
    }

    #[test]
    fn the_spin_phase_ends_unset_when_nothing_publishes() {
        for monitor in [false, true] {
            let path = temp_path(if monitor { "spin_monitor_none" } else { "spin_plain_none" });
            let arena = MmfLatchArena::create(&path, 4).expect("create");
            let off = arena.alloc();
            let got = spin_phase(&arena, off, 4096, monitor, |e| e, || poll_cell(&arena, off))
                .expect("the cell reads");
            assert!(got.is_none(), "monitor = {monitor}: an unpublished cell ends the phase unset");
            remove(&path);
        }
    }

    #[test]
    fn the_spin_phase_reports_a_bad_offset_on_both_arms() {
        let path = temp_path("spin_bad_offset");
        let arena = MmfLatchArena::create(&path, 4).expect("create");
        for monitor in [false, true] {
            let r = spin_phase(&arena, 8, 4096, monitor, |e| e, || poll_cell(&arena, 8));
            assert!(matches!(r, Err(LatchError::BadOffset(8))), "monitor = {monitor}");
        }
        remove(&path);
    }

    fn temp_path(name: &str) -> std::path::PathBuf {
        let mut p = std::env::temp_dir();
        let pid = std::process::id();
        let nonce = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0);
        p.push(format!("flynnel_latch_arena_{pid}_{nonce}_{name}.bin"));
        p
    }

    #[test]
    fn create_then_open_validates_header() {
        let path = temp_path("create_open");
        let _arena = MmfLatchArena::create(&path, 16).expect("create");
        let reopen = MmfLatchArena::open(&path).expect("open");
        assert_eq!(reopen.capacity(), 16);
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn open_rejects_bad_magic() {
        let path = temp_path("bad_magic");
        std::fs::write(&path, vec![0u8; 8192]).expect("seed");
        let r = MmfLatchArena::open(&path);
        assert!(r.is_err(), "open must reject bad-magic file");
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn alloc_publish_read_round_trip() {
        let path = temp_path("alloc_publish_read");
        let arena = MmfLatchArena::create(&path, 4).expect("create");
        let off = arena.alloc();
        assert!(!arena.is_set(off).expect("is_set"));
        let payload = b"hello-latch";
        arena.publish(off, payload).expect("publish");
        assert!(arena.is_set(off).expect("is_set after publish"));
        let mut out = Vec::new();
        let s = arena.read_result(off, &mut out).expect("read");
        assert_eq!(s, SET);
        assert_eq!(&out[..], payload);
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn publish_err_round_trips() {
        let path = temp_path("publish_err");
        let arena = MmfLatchArena::create(&path, 4).expect("create");
        let off = arena.alloc();
        arena.publish_err(off, b"oops").expect("publish_err");
        let mut out = Vec::new();
        let s = arena.read_result(off, &mut out).expect("read");
        assert_eq!(s, ERR);
        assert_eq!(&out[..], b"oops");
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn reset_returns_cell_to_unset() {
        let path = temp_path("reset");
        let arena = MmfLatchArena::create(&path, 4).expect("create");
        let off = arena.alloc();
        arena.publish(off, b"x").expect("publish");
        arena.reset(off).expect("reset");
        assert!(!arena.is_set(off).expect("is_set after reset"));
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn bad_offset_rejected() {
        let path = temp_path("bad_offset");
        let arena = MmfLatchArena::create(&path, 4).expect("create");
        // Offset inside the header is invalid.
        let r = arena.is_set(8);
        assert!(matches!(r, Err(LatchError::BadOffset(_))));
        // Misaligned offset is invalid.
        let r = arena.is_set((std::mem::size_of::<LatchArenaHeader>() + 1) as u32);
        assert!(matches!(r, Err(LatchError::BadOffset(_))));
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn payload_too_large_rejected() {
        let path = temp_path("oversize");
        let arena = MmfLatchArena::create(&path, 2).expect("create");
        let off = arena.alloc();
        let big = vec![0u8; RESULT_BYTES + 1];
        let err = arena.publish(off, &big).expect_err("expected oversize");
        assert_eq!(err, LatchError::PayloadTooLarge);
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn alloc_wraps_modulo_capacity() {
        let path = temp_path("wrap");
        let arena = MmfLatchArena::create(&path, 4).expect("create");
        let header_size = std::mem::size_of::<LatchArenaHeader>() as u32;
        let mut seen = Vec::new();
        for _ in 0..8 {
            seen.push(arena.alloc());
        }
        // First 4 allocs map to cells 0..3 ; next 4 wrap.
        assert_eq!(seen[0], header_size);
        assert_eq!(seen[4], header_size);
        assert_eq!(seen[1], header_size + LATCH_CELL_SIZE as u32);
        assert_eq!(seen[5], header_size + LATCH_CELL_SIZE as u32);
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn two_threads_publish_and_observe() {
        let path = temp_path("two_threads");
        // Capacity must exceed `n` so the bump allocator does not
        // wrap and reuse cells before the publisher finishes -
        // wrap-around reuse would clobber in-flight values. Use
        // `n.next_power_of_two() * 2` for headroom.
        let n = 200usize;
        let capacity = (n * 2).next_power_of_two();
        let arena = Arc::new(MmfLatchArena::create(&path, capacity).expect("create"));

        // Originator side allocs cells, hands offsets to the publisher.
        let mut offsets = Vec::with_capacity(n);
        for _ in 0..n {
            offsets.push(arena.alloc());
        }

        let pub_arena = Arc::clone(&arena);
        let pub_offsets = offsets.clone();
        let pub_handle = thread::spawn(move || {
            for (i, off) in pub_offsets.iter().enumerate() {
                let bytes = (i as u32).to_le_bytes();
                pub_arena.publish(*off, &bytes).expect("publish");
            }
        });

        let mut buf = Vec::new();
        for (i, off) in offsets.iter().enumerate() {
            // Spin until publisher has set the cell.
            loop {
                if arena.is_set(*off).expect("is_set") {
                    break;
                }
                std::thread::yield_now();
            }
            let s = arena.read_result(*off, &mut buf).expect("read");
            assert_eq!(s, SET);
            let mut arr = [0u8; 4];
            arr.copy_from_slice(&buf[..4]);
            assert_eq!(u32::from_le_bytes(arr) as usize, i, "value mismatch at i={i}");
        }
        pub_handle.join().expect("publisher joined");
        std::fs::remove_file(&path).ok();
    }
}