flynnel 0.2.2

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
//! `Parker`: per-worker park / unpark primitive with a yield-N-then-
//! park spin floor.
//!
//! Built on `std::thread::{park, current().unpark()}`. The std
//! primitive provides the permit-based race resolution: if `unpark`
//! is called before `park`, the permit is stored and the next
//! `park` returns immediately. That eliminates the lost-wakeup
//! window the rayon JEC protocol exists to solve, in exchange for
//! the (cheap) cost of always calling `unpark` even when no one is
//! parked.
//!
//!
//! ## Spin floor policy
//!
//! - Local tier: 8 rounds of `thread::yield_now()` before parking
//!   (per [`crate::sched::SchedTier::spin_rounds`]). Sub-microsecond
//!   work avoids the syscall.
//! - Hierarchical tier: 32 rounds. Multi-microsecond work amortizes
//!   the park / unpark pair.
//! - Federated tier: 0 rounds (direct park). Federated jobs are
//!   millisecond-scale; throughput beats latency.
//!
//! `Parker` accepts the spin-round count at construction so it
//! works across tiers without conditional plumbing.

use core::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::thread::{self, Thread};

/// Selects how the [`Parker`] waits after the spin floor is exhausted.
///
/// Picked at construction time via [`WaitStrategy::pick`]:
/// - WAITPKG-capable silicon (Intel Tremont/Tiger Lake+, AMD Zen 5+)
///   -> [`WaitStrategy::Waitpkg`]: UMONITOR + UMWAIT halt the logical
///   CPU sub-100ns until the watched cache line transitions or the
///   TSC deadline fires. No kernel syscall.
/// - All other silicon -> [`WaitStrategy::StdPark`]: the original
///   `std::thread::park()` path (kernel condvar; ~1us syscall on Linux
///   futex / Windows WaitForSingleObject).
///
/// The strategy is observable via [`Parker::wait_strategy`] for
/// diagnostics + per-host bench gating.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WaitStrategy {
    /// `std::thread::park()`. Permits-based; cross-platform; always
    /// works. Kernel transition on the wait path (~1us).
    StdPark,
    /// `UMONITOR` + `UMWAIT`. Halts the logical CPU; wake-on-store
    /// to the monitored cache line. Sub-100ns wake; no syscall.
    /// Available if and only if [`crate::cpu_info::has_waitpkg`]
    /// is true.
    Waitpkg,
}

impl WaitStrategy {
    /// Pick the best wait strategy for this host. Returns
    /// [`WaitStrategy::Waitpkg`] when WAITPKG is available, otherwise
    /// [`WaitStrategy::StdPark`].
    pub fn pick() -> Self {
        if crate::cpu_info::has_waitpkg() {
            Self::Waitpkg
        } else {
            Self::StdPark
        }
    }
}

/// Per-worker park primitive. One `Parker` per worker thread; the
/// thread that owns it parks via [`Self::park_until`], and any
/// other thread wakes it via [`Self::unpark`].
///
/// On WAITPKG-capable hosts the wait path bypasses the kernel
/// condvar entirely - the producer's `unpark` increments
/// `wake_counter` (one cache-line store), and the parked thread's
/// `UMWAIT` returns as soon as the cache-line transition is observed
/// by the hardware monitor. Sub-100ns wake instead of ~1us syscall.
///
/// On non-WAITPKG hosts the wake_counter increment is still issued
/// (it costs one atomic add) but the wait path falls through to
/// `std::thread::park()` as before.
#[derive(Debug)]
pub struct Parker {
    /// Cached `Thread` handle for cross-thread unpark.
    thread: Thread,
    /// Shutdown signal: set by the arena's drop / explicit
    /// shutdown path. When `true`, [`Self::park_until`] returns
    /// `false` to break the worker loop.
    shutdown: AtomicBool,
    /// How many `thread::yield_now()` rounds to spin before
    /// actually calling `thread::park()`. Picked per tier per
    /// [`crate::sched::SchedTier::spin_rounds`].
    spin_rounds: u32,
    /// Monotonic wake counter. Producers increment on `unpark`;
    /// the WAITPKG path snapshots before park + UMONITOR-watches
    /// the counter's cache line.
    wake_counter: AtomicU64,
    /// Wait strategy chosen at construction time.
    wait_strategy: WaitStrategy,
}

impl Parker {
    /// Construct a Parker owned by the calling thread. Captures
    /// the current `Thread` handle for later cross-thread unpark.
    /// Wait strategy is auto-picked via [`WaitStrategy::pick`].
    pub fn new(spin_rounds: u32) -> Self {
        Self::with_strategy(spin_rounds, WaitStrategy::pick())
    }

    /// Construct a Parker with an explicit wait strategy. Used by
    /// benches + tests that need to A/B against the auto-picked
    /// strategy. Callers MUST NOT pass [`WaitStrategy::Waitpkg`] on
    /// a host where [`crate::cpu_info::has_waitpkg`] returns false
    /// (the inline `UMONITOR`/`UMWAIT` opcodes would raise `#UD`).
    pub fn with_strategy(spin_rounds: u32, wait_strategy: WaitStrategy) -> Self {
        Self {
            thread: thread::current(),
            shutdown: AtomicBool::new(false),
            spin_rounds,
            wake_counter: AtomicU64::new(0),
            wait_strategy,
        }
    }

    /// Observable wait strategy. Used by benches + diagnostics to
    /// confirm which path the Parker is on.
    pub fn wait_strategy(&self) -> WaitStrategy {
        self.wait_strategy
    }

    /// Block the calling thread until `is_ready` returns `true`,
    /// shutdown is signalled, or the thread is unparked.
    ///
    /// Returns `true` when `is_ready()` was observed or the thread
    /// was unparked; returns `false` on shutdown.
    ///
    /// Polling sequence:
    /// 1. Loop `spin_rounds` times calling `thread::yield_now()`
    ///    between polls. Cheapest path: a worker about to receive
    ///    work via unpark stays out of the parker.
    /// 2. After the spin floor, `thread::park()` ONCE. If we wake
    ///    via unpark (regardless of predicate state) we return
    ///    `true` and let the caller re-attempt the work search.
    ///    This is important when the caller has out-of-band signals
    ///    (e.g., wake-on-push from a peer) that don't update the
    ///    predicate's observed state - the peer's deque might have
    ///    work but the predicate doesn't see it. Returning on
    ///    unpark hands control back to the caller, which then walks
    ///    the peer stealers in its main loop.
    pub fn park_until<F: FnMut() -> bool>(&self, mut is_ready: F) -> bool {
        // Snapshot wake_counter BEFORE the spin floor so the WAITPKG
        // path can detect any unpark that fires after this snapshot
        // (whether during the spin floor or during the UMWAIT itself).
        let initial_wake = self.wake_counter.load(Ordering::Acquire);

        for _ in 0..self.spin_rounds {
            if self.shutdown.load(Ordering::Acquire) {
                return false;
            }
            if is_ready() {
                return true;
            }
            thread::yield_now();
        }
        if self.shutdown.load(Ordering::Acquire) {
            return false;
        }
        if is_ready() {
            return true;
        }

        // Dispatch on wait strategy. Either path returns to the
        // caller on wake (real or spurious); the caller's loop
        // re-attempts the work search and re-enters park_until
        // when still empty.
        match self.wait_strategy {
            WaitStrategy::StdPark => {
                thread::park();
            }
            WaitStrategy::Waitpkg => {
                self.wait_via_waitpkg(initial_wake);
            }
        }

        // Final shutdown check before returning so a shutdown
        // unpark surfaces cleanly.
        if self.shutdown.load(Ordering::Acquire) {
            return false;
        }
        true
    }

    /// Wake the parked thread if any. Unconditional: if no thread
    /// is parked, the permit is stored for the next park. This
    /// trades a no-op syscall on the empty case for not having to
    /// track an explicit "is this worker parked" flag.
    ///
    /// Increments [`Self::wake_counter`] FIRST so the WAITPKG
    /// observer's monitor fires; then calls `thread::unpark()` so
    /// the [`WaitStrategy::StdPark`] path also wakes. Both are
    /// needed because the Parker is constructed knowing its
    /// strategy but the caller does not need to: this method works
    /// for both strategies uniformly.
    pub fn unpark(&self) {
        // Release-store on wake_counter happens-before the parked
        // observer's Acquire-load post-UMWAIT, so the observer sees
        // any state the producer published prior to unpark.
        self.wake_counter.fetch_add(1, Ordering::Release);
        self.thread.unpark();
    }

    /// Signal shutdown. The parked thread observes this on its
    /// next park return and exits its loop.
    ///
    /// Routes through [`Self::unpark`] so the wake_counter increments
    /// AND the std::thread permit fires - the WAITPKG observer
    /// returns from UMWAIT on the cache-line transition and then
    /// observes `shutdown == true` on its post-park check.
    pub fn shutdown(&self) {
        self.shutdown.store(true, Ordering::Release);
        self.unpark();
    }

    /// WAITPKG wait path: snapshot wake_counter (caller's), arm
    /// UMONITOR on its cache line, double-check the counter to
    /// catch a wake that landed between caller's snapshot and our
    /// UMONITOR setup, then UMWAIT until the line transitions OR
    /// a TSC deadline fires (~10ms).
    ///
    /// Returning here does NOT mean wake_counter actually changed -
    /// UMWAIT can return on signals, interrupts, or its hint
    /// expiry. The caller (`park_until`) re-checks `is_ready` and
    /// `shutdown` after wake_via_waitpkg returns and decides
    /// whether to re-park.
    #[cfg(target_arch = "x86_64")]
    fn wait_via_waitpkg(&self, initial_wake: u64) {
        // 10 ms deadline cap so a missed wake (e.g. shutdown raced
        // with a UMONITOR that armed AFTER the shutdown unpark)
        // does not block forever. The deadline TSC is computed
        // assuming a ~2.5 GHz TSC frequency; off-by-2x error is
        // immaterial because the caller re-enters park_until on
        // spurious return anyway.
        const WAIT_DEADLINE_NS: u64 = 10_000_000;
        const TSC_HZ_ESTIMATE: u64 = 2_500_000_000;
        let cycles =
            WAIT_DEADLINE_NS.saturating_mul(TSC_HZ_ESTIMATE / 1_000_000_000);
        // SAFETY: `_rdtsc` is a no-side-effect read of the TSC
        // counter; available on every x86_64 CPU produced this
        // century.
        let now = unsafe { core::arch::x86_64::_rdtsc() };
        let deadline = now.wrapping_add(cycles);
        let lo = deadline as u32;
        let hi = (deadline >> 32) as u32;

        let addr = (&raw const self.wake_counter).cast::<u8>();

        // UMONITOR rax: arm hardware monitor on the cache line
        // containing the wake_counter. Any store to that line
        // (including unrelated writes that share the line) wakes
        // UMWAIT. Cache-line padding inside Parker keeps adjacent
        // fields off the same line so unrelated writes do not
        // produce spurious wakes.
        //
        // SAFETY: caller (Parker::new -> WaitStrategy::pick) only
        // installs the Waitpkg strategy when has_waitpkg() returned
        // true, so UMONITOR is not a `#UD`. `addr` is a stable
        // pointer to a live AtomicU64 field of `self`.
        unsafe {
            core::arch::asm!(
                "umonitor rax",
                in("rax") addr,
                options(nostack, preserves_flags),
            );
        }

        // Race window: an unpark that fired between the caller's
        // initial_wake snapshot and the UMONITOR arming would not
        // wake UMWAIT (the monitor was not yet armed). Re-check
        // wake_counter; if it advanced, skip UMWAIT entirely.
        if self.wake_counter.load(Ordering::Acquire) != initial_wake {
            return;
        }

        // UMWAIT ecx, edx:eax with ecx = wake hint:
        //   1 = C0.1 (light wait, fastest wake)
        //   0 = C0.2 (deeper wait, lower power, slower wake)
        // We pick C0.1 for the scheduler's latency-sensitive
        // workload. EDX:EAX carries the absolute TSC deadline.
        //
        // SAFETY: same WAITPKG-available reasoning as the UMONITOR
        // above. UMWAIT modifies CF on return (timeout-vs-wake);
        // we drop preserves_flags accordingly.
        unsafe {
            core::arch::asm!(
                "umwait {hint:e}",
                hint = in(reg) 1u32,
                in("eax") lo,
                in("edx") hi,
                options(nostack),
            );
        }
    }

    /// Non-x86_64 stub. The WAITPKG strategy cannot be installed on
    /// non-x86_64 targets (the CPUID probe in `crate::cpu_info`
    /// returns false), so this branch is unreachable in practice.
    /// Fall through to `thread::park()` as a defensive default.
    #[cfg(not(target_arch = "x86_64"))]
    fn wait_via_waitpkg(&self, _initial_wake: u64) {
        thread::park();
    }

    /// Test whether shutdown has been signalled. Workers can poll
    /// this between job executions to exit promptly.
    pub fn is_shutdown(&self) -> bool {
        self.shutdown.load(Ordering::Acquire)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::AtomicU32;
    use std::thread;
    use std::time::{Duration, Instant};

    #[test]
    fn park_until_returns_immediately_when_ready() {
        let p = Parker::new(8);
        let t0 = Instant::now();
        let ok = p.park_until(|| true);
        let elapsed = t0.elapsed();
        assert!(ok);
        assert!(elapsed < Duration::from_millis(10),
            "park_until with ready=true must be fast; took {elapsed:?}");
    }

    #[test]
    fn park_until_returns_false_on_shutdown() {
        let p = Arc::new(Parker::new(8));
        let p_signal = Arc::clone(&p);
        let signal = thread::spawn(move || {
            thread::sleep(Duration::from_millis(50));
            p_signal.shutdown();
        });
        let ok = p.park_until(|| false);
        signal.join().unwrap();
        assert!(!ok, "park_until must return false after shutdown");
    }

    #[test]
    fn park_until_wakes_on_unpark_from_other_thread() {
        // Owner thread parks. Helper thread unparks after 50 ms.
        // The owner observes `is_ready` becoming true and returns.
        let ready = Arc::new(AtomicU32::new(0));
        let ready_clone = Arc::clone(&ready);

        let (tx, rx) = std::sync::mpsc::channel::<Arc<Parker>>();

        let owner = thread::spawn(move || {
            let p = Arc::new(Parker::new(8));
            tx.send(Arc::clone(&p)).unwrap();
            let t0 = Instant::now();
            let ok = p.park_until(|| ready.load(Ordering::Acquire) == 1);
            (ok, t0.elapsed())
        });

        let p_owner = rx.recv().expect("owner must send its parker");
        thread::sleep(Duration::from_millis(50));
        ready_clone.store(1, Ordering::Release);
        p_owner.unpark();

        let (ok, elapsed) = owner.join().unwrap();
        assert!(ok, "park_until must return true after ready becomes true");
        // Should wake within ~100 ms.
        assert!(elapsed < Duration::from_millis(500),
            "park_until took too long: {elapsed:?}");
    }

    #[test]
    fn park_until_spin_floor_eight_rounds_no_park() {
        // With spin_rounds=8 and is_ready becoming true on round 3,
        // park_until should return without ever calling park().
        // We can't observe park directly, but we can verify the
        // sequence completes quickly.
        let p = Parker::new(8);
        let mut polls = 0u32;
        let ok = p.park_until(|| {
            polls += 1;
            polls >= 3
        });
        assert!(ok);
        assert_eq!(polls, 3);
    }

    #[test]
    fn park_until_zero_spin_floor_goes_straight_to_park() {
        // With spin_rounds=0, park_until skips the yield loop. We
        // verify by setting ready=true synchronously - the call
        // returns at the loop's first iteration.
        let p = Parker::new(0);
        let ok = p.park_until(|| true);
        assert!(ok);
    }

    #[test]
    fn unpark_before_park_is_observable_via_permit() {
        // std::thread::park's permit semantics: unpark before park
        // stores a permit; next park returns immediately. We test
        // this through park_until: helper unparks BEFORE owner
        // calls park_until. The owner's first park sees the
        // permit and returns; the subsequent re-check observes
        // ready=true.
        let p = Arc::new(Parker::new(0)); // 0 spin so we go to park fast
        let ready = Arc::new(AtomicU32::new(0));
        let p_clone = Arc::clone(&p);
        let ready_clone = Arc::clone(&ready);

        // Pre-store an unpark permit on the owner thread BEFORE
        // it calls park_until. We do this by having the owner be
        // the main thread, and a helper that unparks then sets
        // ready.
        // (Easier: just stage the unpark via a delayed thread
        //  before the owner's park_until call.)

        let signal = thread::spawn(move || {
            // Caller's thread::current() is captured inside p_clone
            // when the main thread instantiates Parker. The unpark
            // targets the main thread (the parker's owner).
            ready_clone.store(1, Ordering::Release);
            p_clone.unpark();
        });
        signal.join().unwrap();

        let ok = p.park_until(|| ready.load(Ordering::Acquire) == 1);
        assert!(ok);
    }

    #[test]
    fn is_shutdown_reflects_shutdown_call() {
        let p = Parker::new(8);
        assert!(!p.is_shutdown());
        p.shutdown();
        assert!(p.is_shutdown());
    }

    #[test]
    fn wait_strategy_pick_matches_cpuid() {
        // pick() returns Waitpkg if and only if cpu_info::has_waitpkg
        // reports true. Test asserts the two queries agree.
        let want = if crate::cpu_info::has_waitpkg() {
            WaitStrategy::Waitpkg
        } else {
            WaitStrategy::StdPark
        };
        assert_eq!(WaitStrategy::pick(), want);
    }

    #[test]
    fn with_strategy_stdpark_round_trips_like_default() {
        // Explicitly construct a StdPark Parker; same semantics as
        // the original implementation.
        let p = Parker::with_strategy(8, WaitStrategy::StdPark);
        assert_eq!(p.wait_strategy(), WaitStrategy::StdPark);
        let ok = p.park_until(|| true);
        assert!(ok, "StdPark park_until must return true on ready=true");
    }

    #[test]
    fn unpark_increments_wake_counter() {
        // Verify the WAITPKG observer mechanism is wired: every
        // unpark MUST bump wake_counter so the WAITPKG path's
        // double-check after UMONITOR catches the wake.
        let p = Parker::new(8);
        let before = p.wake_counter.load(Ordering::Acquire);
        p.unpark();
        let after = p.wake_counter.load(Ordering::Acquire);
        assert_eq!(after, before + 1, "unpark must increment wake_counter");
    }

    #[test]
    fn shutdown_increments_wake_counter() {
        // shutdown routes through unpark so the WAITPKG observer
        // also wakes on shutdown (not just the std::thread permit).
        let p = Parker::new(8);
        let before = p.wake_counter.load(Ordering::Acquire);
        p.shutdown();
        let after = p.wake_counter.load(Ordering::Acquire);
        assert_eq!(after, before + 1, "shutdown must increment wake_counter via unpark");
        assert!(p.is_shutdown());
    }

    #[test]
    fn waitpkg_strategy_wakes_on_unpark_when_available() {
        // Skip on hosts without WAITPKG (the UMONITOR/UMWAIT opcodes
        // would #UD). Per-architecture cpuid check; on Zen+ R7 2700
        // this returns false and the test is a no-op.
        if !crate::cpu_info::has_waitpkg() {
            eprintln!(
                "skip waitpkg_strategy_wakes_on_unpark_when_available: \
                 host has no WAITPKG (cpuid leaf 7 ECX bit 5 = 0)"
            );
            return;
        }
        // WAITPKG-capable host: park with Waitpkg strategy + unpark
        // from helper thread. Owner thread must observe the wake
        // within the 10ms deadline.
        let ready = Arc::new(AtomicU32::new(0));
        let ready_clone = Arc::clone(&ready);
        let (tx, rx) = std::sync::mpsc::channel::<Arc<Parker>>();
        let owner = thread::spawn(move || {
            let p = Arc::new(Parker::with_strategy(8, WaitStrategy::Waitpkg));
            tx.send(Arc::clone(&p)).unwrap();
            let t0 = Instant::now();
            let ok = p.park_until(|| ready.load(Ordering::Acquire) == 1);
            (ok, t0.elapsed())
        });
        let p_owner = rx.recv().expect("owner must send its parker");
        thread::sleep(Duration::from_millis(20));
        ready_clone.store(1, Ordering::Release);
        p_owner.unpark();
        let (ok, elapsed) = owner.join().unwrap();
        assert!(ok, "Waitpkg park_until must return true on unpark");
        // Cap should be well under 100ms; the 10ms UMWAIT deadline
        // bounds the worst case to ~10ms even if UMWAIT misses the
        // wake.
        assert!(elapsed < Duration::from_millis(100),
            "Waitpkg park_until took {elapsed:?}, expected < 100ms");
    }

    #[test]
    fn shutdown_unparks_so_blocked_thread_exits() {
        // Parker MUST be constructed inside the thread that will
        // park on it, because `Parker::new` captures
        // `thread::current()` for the unpark target. A Parker
        // built in main and parked-on by a spawned thread would
        // unpark main, not the spawned thread, and deadlock.
        let (tx, rx) = std::sync::mpsc::channel::<Arc<Parker>>();
        let owner = thread::spawn(move || {
            let p = Arc::new(Parker::new(8));
            tx.send(Arc::clone(&p)).unwrap();
            p.park_until(|| false)
        });
        let p_owner = rx.recv().expect("owner must send its parker");
        thread::sleep(Duration::from_millis(50));
        p_owner.shutdown();
        let ok = owner.join().unwrap();
        assert!(!ok, "shutdown must surface as park_until -> false");
    }
}