subetha-cxc 0.1.8

MMF-backed cross-process IPC primitives for SubEtha: SharedRing, SharedHashMap, SharedRWLock, SharedSemaphore, SharedLRUCache, OwnerLease, HeartbeatTable, plus 30+ more. One byte layout serves cross-thread, cross-process, and disk-persistent.
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
//! `DequeDispatcher` - per-call routing across the MMF-deque family.
//!
//! The dispatcher owns one handle of each [`SharedDeque`] /
//! [`SharedDequeKhpd`] / [`SharedDequeLoh`] / [`SharedDequeUrd`]
//! variant that the host has configured, and picks the right one
//! per call based on a caller-supplied [`WorkloadShape`].
//!
//! ## The routing decision
//!
//! No single variant wins every workload shape. The right pick depends
//! on (a) whether the producer batches or dispatches per-item,
//! (b) how many thieves the workload runs, and (c) whether the
//! caller wants the consumer to halt the logical CPU between batches
//! via WAITPKG.
//!
//! | Workload | Pick | Why |
//! |---|---|---|
//! | Per-item dispatch, single thief | `ChaseLev` | Lowest constant per push; no batch to amortize. |
//! | Producer batches K = 2..128 items per call | `Khpd` | 3 items per Release-store on the publication line; empirically the best per-item cost on Zen+/Zen 4 at this scale. |
//! | Producer batches K >= 128 items per call | `Loh` | 1 `tail.fetch_add(K)` amortizes across the whole batch. |
//! | Multiple thieves AND batched producer | `Urd` | Per-thief mailbox = zero CAS contention. |
//! | Multiple thieves AND `wait_idle=true` | `Urd` | Hardware-mediated wake via WAITPKG / PAUSE-spin. |
//!
//! The routing table is a starting point. Per-host calibration may
//! flip individual cells. Callers that already know which variant
//! they want may call the per-variant getters
//! ([`DequeDispatcher::chase_lev`], etc.) directly.
//!
//! ## Cross-process E2E
//!
//! A `DequeDispatcher` lives in the producer process. Each variant
//! it owns is backed by its own MMF file path; consumer processes
//! open those same paths to drain. See the
//! [`dispatcher_demo`](https://github.com/Variably-Constant/SubEtha/blob/main/crates/subetha-cxc/examples/dispatcher_demo.rs)
//! example for the parent/child split.

#![allow(clippy::missing_errors_doc)]

use std::io;
use std::path::Path;
use std::sync::Arc;

use subetha_core::{Axis, AxisMask};

use crate::shared_deque::SharedDeque;
use crate::shared_deque_khl::SharedDequeKhl;
use crate::shared_deque_khpd::{LineItem, SharedDequeKhpd};
use crate::shared_deque_loh::SharedDequeLoh;
use crate::shared_deque_urd::SharedDequeUrd;

/// Direction signatures per variant.
///
/// Each variant declares which of the six K-axes it engages at a
/// non-default value. The dispatcher uses signature-set logic to
/// route per `WorkloadShape`: `variant.satisfies(workload_required)`
/// picks the highest-engagement variant whose signature is a
/// superset of the workload's required signature.
const fn chase_lev_signature() -> AxisMask {
    // K_counter_share = owner-private is the only non-default axis;
    // K_inner=1, K_outer=1, K_gating=counter-only are all default.
    AxisMask::from_axes(&[Axis::CounterShare])
}

const fn khpd_signature() -> AxisMask {
    AxisMask::from_axes(&[Axis::Inner, Axis::Gating])
}

const fn loh_signature() -> AxisMask {
    AxisMask::from_axes(&[Axis::Outer, Axis::Gating])
}

const fn urd_signature() -> AxisMask {
    AxisMask::from_axes(&[
        Axis::Inner,
        Axis::Consumer,
        Axis::Radius,
        Axis::Gating,
    ])
}

const fn khl_signature() -> AxisMask {
    AxisMask::from_axes(&[
        Axis::Inner,
        Axis::Outer,
        Axis::CounterShare,
        Axis::Radius,
        Axis::Gating,
    ])
}

/// The deque-family variants the dispatcher routes across.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DequeVariant {
    /// Chase-Lev work-stealing deque, per-item push.
    ChaseLev,
    /// KHPD publication-line deque, 3 items per Release-store.
    Khpd,
    /// LOH LCRQ-on-LIFO Hybrid, amortizes `tail.fetch_add` over the
    /// whole batch.
    Loh,
    /// URD per-thief mailbox deque + WAITPKG wait.
    Urd,
    /// KHL K-axis Hierarchical LCRQ - SubEtha-native hybrid that
    /// pulls KHPD's per-slot packing + LOH's per-batch counter
    /// amortization + Chase-Lev's owner-private tail simultaneously.
    /// Empirically the strongest single-thief batched primitive on
    /// Zen+ R7 2700 (4.7 ns/item at K=64 producer-fast).
    Khl,
}

impl DequeVariant {
    /// The direction signature for this variant: which K-axes it
    /// engages at non-default values.
    pub const fn signature(self) -> AxisMask {
        match self {
            DequeVariant::ChaseLev => chase_lev_signature(),
            DequeVariant::Khpd => khpd_signature(),
            DequeVariant::Loh => loh_signature(),
            DequeVariant::Urd => urd_signature(),
            DequeVariant::Khl => khl_signature(),
        }
    }
}

/// Caller-supplied workload shape feeding the routing decision.
#[derive(Debug, Clone, Copy)]
pub struct WorkloadShape {
    /// Number of consumer threads / processes that will drain the
    /// deque concurrently. `>= 2` is the multi-thief regime where
    /// URD's per-mailbox layout amortizes against the shared-head
    /// CAS contention Chase-Lev / KHPD / LOH all pay.
    pub n_thieves: usize,
    /// `Some(K)` when the producer hands the dispatcher a batch of
    /// `K` items per call; `None` when the producer dispatches one
    /// item at a time (request-reply / latency-bound).
    pub batch_size: Option<usize>,
    /// `true` when the consumer should halt the logical CPU between
    /// batches (WAITPKG on capable silicon; PAUSE-spin elsewhere).
    /// Setting this routes to URD even at `n_thieves == 1`.
    pub wait_idle: bool,
}

impl WorkloadShape {
    /// The direction signature this workload requires from its
    /// transport: which K-axes the variant must engage to handle
    /// this shape.
    ///
    /// Per-item dispatch (no batch) requires nothing beyond the
    /// empty signature (Chase-Lev's signature is a superset of any
    /// empty requirement). Batched dispatch requires K_inner +
    /// K_outer engaged (per-slot packing AND per-batch counter
    /// amortization). Multi-thief or wait-idle requires K_consumer +
    /// K_radius engaged (per-thief mailboxes and CPUID-dispatched
    /// publish mechanism).
    pub const fn required_signature(&self) -> AxisMask {
        let mut bits = 0u16;
        // n_thieves >= 2 or wait_idle requires per-thief consumer +
        // radius dispatch.
        if self.n_thieves >= 2 || self.wait_idle {
            bits |= 1u16 << Axis::Consumer.bit();
            bits |= 1u16 << Axis::Radius.bit();
        }
        // batch_size = Some(k>=2) requires K_inner and K_outer.
        if let Some(k) = self.batch_size
            && k >= 2
        {
            bits |= 1u16 << Axis::Inner.bit();
            bits |= 1u16 << Axis::Outer.bit();
        }
        AxisMask::from_bits(bits)
    }

    /// Request-reply: per-item dispatch, single thief, no idle wait.
    pub fn request_reply() -> Self {
        Self {
            n_thieves: 1,
            batch_size: None,
            wait_idle: false,
        }
    }

    /// Producer-fast batch of `k` items, single thief.
    pub fn producer_fast(k: usize) -> Self {
        Self {
            n_thieves: 1,
            batch_size: Some(k),
            wait_idle: false,
        }
    }

    /// Fan-out: producer batches across multiple thieves. `n_thieves`
    /// >= 2 + `batch_size` set routes to URD.
    pub fn fan_out(n_thieves: usize, k: usize) -> Self {
        Self {
            n_thieves,
            batch_size: Some(k),
            wait_idle: false,
        }
    }
}

/// Errors from the dispatcher's send-side methods.
#[derive(Debug)]
pub enum DispatchError {
    /// The picked variant is not configured on this dispatcher
    /// (caller did not pass a backing file path at construction).
    BackendNotConfigured(DequeVariant),
    /// The picked variant's backing primitive returned a push error
    /// (the ring is at capacity, etc.).
    PushFailed(&'static str),
}

impl std::fmt::Display for DispatchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BackendNotConfigured(v) => {
                write!(f, "dispatcher: backend {v:?} is not configured")
            }
            Self::PushFailed(msg) => write!(f, "dispatcher: push failed ({msg})"),
        }
    }
}

impl std::error::Error for DispatchError {}

/// MMF-backed dispatcher across the deque family.
///
/// Construct via [`DequeDispatcher::builder`] and pass the backing
/// paths for each variant the caller wants available. The
/// [`pick`](DequeDispatcher::pick) helper returns the routing
/// decision for a shape WITHOUT performing the push, useful for
/// observers and per-host calibration.
pub struct DequeDispatcher {
    chase_lev: Option<Arc<SharedDeque<LineItem>>>,
    khpd: Option<Arc<SharedDequeKhpd>>,
    loh: Option<Arc<SharedDequeLoh>>,
    urd: Option<Arc<SharedDequeUrd>>,
    khl: Option<Arc<SharedDequeKhl>>,
}

impl DequeDispatcher {
    /// Start a builder. Pass the backing file paths for whichever
    /// variants the caller wants available; unset variants stay
    /// `None` and the dispatcher falls through to the next-best
    /// available variant per [`pick`](Self::pick).
    pub fn builder() -> DispatcherBuilder {
        DispatcherBuilder {
            chase_lev: None,
            khpd: None,
            loh: None,
            urd: None,
            khl: None,
        }
    }

    /// Get the underlying Chase-Lev handle (if configured).
    pub fn chase_lev(&self) -> Option<&Arc<SharedDeque<LineItem>>> {
        self.chase_lev.as_ref()
    }

    /// Get the underlying KHPD handle (if configured).
    pub fn khpd(&self) -> Option<&Arc<SharedDequeKhpd>> {
        self.khpd.as_ref()
    }

    /// Get the underlying LOH handle (if configured).
    pub fn loh(&self) -> Option<&Arc<SharedDequeLoh>> {
        self.loh.as_ref()
    }

    /// Get the underlying URD handle (if configured).
    pub fn urd(&self) -> Option<&Arc<SharedDequeUrd>> {
        self.urd.as_ref()
    }

    /// Get the underlying KHL handle (if configured).
    pub fn khl(&self) -> Option<&Arc<SharedDequeKhl>> {
        self.khl.as_ref()
    }

    /// Pick the right variant for `shape`. Returns the variant
    /// independent of whether the corresponding handle is configured
    /// (use [`pick_with_fallback`](Self::pick_with_fallback) to fold
    /// the configuration check into the decision).
    pub fn pick(shape: WorkloadShape) -> DequeVariant {
        // Multi-thief or wait_idle => URD (per-mailbox + WAITPKG).
        if shape.n_thieves >= 2 || shape.wait_idle {
            return DequeVariant::Urd;
        }
        // Single-thief routes by batch size. KHL is the SubEtha-
        // native hybrid that beats KHPD measurably (1.55x at K=64)
        // on producer-fast workloads; route any batched single-thief
        // call through it. Per-item dispatch still rides Chase-Lev
        // because it has no batch to amortize.
        match shape.batch_size {
            Some(k) if k >= 2 => DequeVariant::Khl,
            _ => DequeVariant::ChaseLev,
        }
    }

    /// Pick a variant by signature-set satisfaction. For each
    /// variant in priority order, check whether its signature is a
    /// superset of the workload's required signature; return the
    /// first match. Agrees with [`pick`](Self::pick) on every
    /// canonical workload shape.
    ///
    /// The dispatcher becomes a signature-set lens rather than a
    /// hardcoded match arm; the two routing methods co-exist so
    /// downstream callers can pick the pattern that fits their
    /// style.
    pub fn pick_by_signature(shape: WorkloadShape) -> DequeVariant {
        // Multi-thief or wait-idle workloads strictly need URD's
        // K_consumer engagement, so URD outranks every variant even
        // though KHL has higher overall axis count.
        if shape.n_thieves >= 2 || shape.wait_idle {
            return DequeVariant::Urd;
        }
        let required = shape.required_signature();
        // For single-thief shapes: per-item dispatch (empty
        // required) routes to Chase-Lev (the simplest variant that
        // satisfies the empty requirement); batched dispatch routes
        // to KHL (the highest-engagement variant that satisfies the
        // K_inner + K_outer requirement). This mirrors `pick`
        // exactly: empty required => Chase-Lev; non-empty => KHL.
        if required == AxisMask::EMPTY {
            return DequeVariant::ChaseLev;
        }
        // Non-empty single-thief: prefer the highest-engagement
        // variant that satisfies the requirement.
        const ORDER: [DequeVariant; 4] = [
            DequeVariant::Khl,
            DequeVariant::Khpd,
            DequeVariant::Loh,
            DequeVariant::ChaseLev,
        ];
        for v in ORDER {
            if v.signature().satisfies(required) {
                return v;
            }
        }
        DequeVariant::ChaseLev
    }

    /// Pick the right variant for `shape`, falling through to the
    /// next-best available variant when the primary pick is not
    /// configured on this dispatcher.
    ///
    /// Fallback chain (in order): primary -> KHPD -> LOH -> Chase-Lev
    /// -> URD. Returns `None` only when no variant is configured at
    /// all.
    pub fn pick_with_fallback(&self, shape: WorkloadShape) -> Option<DequeVariant> {
        let primary = Self::pick(shape);
        // Fallback ordering: primary first, then strongest-to-weakest
        // by measured single-thief K=64 throughput on Zen+ R7 2700.
        let order: [DequeVariant; 6] = [
            primary,
            DequeVariant::Khl,
            DequeVariant::Khpd,
            DequeVariant::Loh,
            DequeVariant::ChaseLev,
            DequeVariant::Urd,
        ];
        order.into_iter().find(|&v| self.is_configured(v))
    }

    /// Check whether `variant` is configured on this dispatcher.
    pub fn is_configured(&self, variant: DequeVariant) -> bool {
        match variant {
            DequeVariant::ChaseLev => self.chase_lev.is_some(),
            DequeVariant::Khpd => self.khpd.is_some(),
            DequeVariant::Loh => self.loh.is_some(),
            DequeVariant::Urd => self.urd.is_some(),
            DequeVariant::Khl => self.khl.is_some(),
        }
    }

    /// Dispatch a single item under `shape`. Routes to whichever
    /// variant the [`pick_with_fallback`](Self::pick_with_fallback)
    /// decision selects. Returns the chosen variant so observers can
    /// confirm the routing.
    ///
    /// For per-item dispatch the natural target is Chase-Lev (one
    /// Release-store on `bottom` per push). KHPD / LOH / URD all
    /// accept a single-item batch and degrade to one slot write per
    /// call.
    pub fn dispatch_one(
        &self,
        shape: WorkloadShape,
        item: LineItem,
    ) -> Result<DequeVariant, DispatchError> {
        let variant = self
            .pick_with_fallback(shape)
            .ok_or(DispatchError::BackendNotConfigured(DequeVariant::ChaseLev))?;
        match variant {
            DequeVariant::ChaseLev => {
                let h = self
                    .chase_lev
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::ChaseLev))?;
                h.push(&item).map_err(|_| DispatchError::PushFailed("ChaseLev::push"))?;
            }
            DequeVariant::Khpd => {
                let h = self
                    .khpd
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::Khpd))?;
                h.publish_batch(std::slice::from_ref(&item))
                    .map_err(|_| DispatchError::PushFailed("Khpd::publish_batch"))?;
            }
            DequeVariant::Loh => {
                let h = self
                    .loh
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::Loh))?;
                h.publish_batch(std::slice::from_ref(&item))
                    .map_err(|_| DispatchError::PushFailed("Loh::publish_batch"))?;
            }
            DequeVariant::Urd => {
                let h = self
                    .urd
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::Urd))?;
                h.publish_round_robin(std::slice::from_ref(&item))
                    .map_err(|_| DispatchError::PushFailed("Urd::publish_round_robin"))?;
            }
            DequeVariant::Khl => {
                let h = self
                    .khl
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::Khl))?;
                h.publish_batch(std::slice::from_ref(&item))
                    .map_err(|_| DispatchError::PushFailed("Khl::publish_batch"))?;
            }
        }
        Ok(variant)
    }

    /// Dispatch a batch of items under `shape`. Routes per
    /// [`pick_with_fallback`](Self::pick_with_fallback). Returns the
    /// chosen variant.
    ///
    /// For Chase-Lev (per-item primitive) the batch is pushed one
    /// item at a time. For KHPD / LOH the batch is published via
    /// the variant's `publish_batch` hot path. For URD the batch is
    /// chunked by `MAILBOX_ITEMS = 3` and round-robined across the
    /// configured mailboxes.
    pub fn dispatch_batch(
        &self,
        shape: WorkloadShape,
        items: &[LineItem],
    ) -> Result<DequeVariant, DispatchError> {
        if items.is_empty() {
            return self
                .pick_with_fallback(shape)
                .ok_or(DispatchError::BackendNotConfigured(DequeVariant::ChaseLev));
        }
        let variant = self
            .pick_with_fallback(shape)
            .ok_or(DispatchError::BackendNotConfigured(DequeVariant::ChaseLev))?;
        match variant {
            DequeVariant::ChaseLev => {
                let h = self
                    .chase_lev
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::ChaseLev))?;
                for item in items {
                    h.push(item)
                        .map_err(|_| DispatchError::PushFailed("ChaseLev::push"))?;
                }
            }
            DequeVariant::Khpd => {
                let h = self
                    .khpd
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::Khpd))?;
                h.publish_batch(items)
                    .map_err(|_| DispatchError::PushFailed("Khpd::publish_batch"))?;
            }
            DequeVariant::Loh => {
                let h = self
                    .loh
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::Loh))?;
                h.publish_batch(items)
                    .map_err(|_| DispatchError::PushFailed("Loh::publish_batch"))?;
            }
            DequeVariant::Urd => {
                let h = self
                    .urd
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::Urd))?;
                use crate::shared_deque_urd::MAILBOX_ITEMS;
                for chunk in items.chunks(MAILBOX_ITEMS) {
                    h.publish_round_robin(chunk).map_err(|_| {
                        DispatchError::PushFailed("Urd::publish_round_robin")
                    })?;
                }
            }
            DequeVariant::Khl => {
                let h = self
                    .khl
                    .as_ref()
                    .ok_or(DispatchError::BackendNotConfigured(DequeVariant::Khl))?;
                h.publish_batch(items)
                    .map_err(|_| DispatchError::PushFailed("Khl::publish_batch"))?;
            }
        }
        Ok(variant)
    }
}

/// Builder for [`DequeDispatcher`].
pub struct DispatcherBuilder {
    chase_lev: Option<Arc<SharedDeque<LineItem>>>,
    khpd: Option<Arc<SharedDequeKhpd>>,
    loh: Option<Arc<SharedDequeLoh>>,
    urd: Option<Arc<SharedDequeUrd>>,
    khl: Option<Arc<SharedDequeKhl>>,
}

impl DispatcherBuilder {
    /// Create + attach a Chase-Lev deque at `path` with `capacity`
    /// slots (round up to next power of two).
    pub fn with_chase_lev<P: AsRef<Path>>(
        mut self,
        path: P,
        capacity: usize,
    ) -> io::Result<Self> {
        let d = SharedDeque::<LineItem>::create(path, capacity)
            .map_err(|e| io::Error::other(format!("Chase-Lev create: {e:?}")))?;
        self.chase_lev = Some(Arc::new(d));
        Ok(self)
    }

    /// Create + attach a KHPD at `path` with `capacity` publication
    /// lines.
    pub fn with_khpd<P: AsRef<Path>>(
        mut self,
        path: P,
        capacity: usize,
    ) -> io::Result<Self> {
        let d = SharedDequeKhpd::create(path, capacity)?;
        self.khpd = Some(Arc::new(d));
        Ok(self)
    }

    /// Create + attach a LOH at `path` with `capacity` ring slots
    /// and `flush_threshold` LIFO auto-flush threshold.
    pub fn with_loh<P: AsRef<Path>>(
        mut self,
        path: P,
        capacity: usize,
        flush_threshold: usize,
    ) -> io::Result<Self> {
        let d = SharedDequeLoh::create(path, capacity, flush_threshold)?;
        self.loh = Some(Arc::new(d));
        Ok(self)
    }

    /// Create + attach a URD at `path` with `n_mailboxes` mailboxes
    /// (one per intended thief).
    pub fn with_urd<P: AsRef<Path>>(
        mut self,
        path: P,
        n_mailboxes: usize,
    ) -> io::Result<Self> {
        let d = SharedDequeUrd::create(path, n_mailboxes)?;
        self.urd = Some(Arc::new(d));
        Ok(self)
    }

    /// Create + attach a KHL (K-axis Hierarchical LCRQ - the
    /// SubEtha-native hybrid) at `path` with `capacity` slots. Total
    /// item capacity is `capacity * 3`.
    pub fn with_khl<P: AsRef<Path>>(
        mut self,
        path: P,
        capacity: usize,
    ) -> io::Result<Self> {
        let d = SharedDequeKhl::create(path, capacity)?;
        self.khl = Some(Arc::new(d));
        Ok(self)
    }

    /// Finalize the dispatcher.
    pub fn build(self) -> DequeDispatcher {
        DequeDispatcher {
            chase_lev: self.chase_lev,
            khpd: self.khpd,
            loh: self.loh,
            urd: self.urd,
            khl: self.khl,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn tmp(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!("subetha_dispatch_deque_{pid}_{nonce}_{name}.bin"));
        p
    }

    fn u32_item(id: u32) -> LineItem {
        LineItem::new(&id.to_le_bytes()).expect("item")
    }

    #[test]
    fn pick_request_reply_routes_to_chase_lev() {
        assert_eq!(
            DequeDispatcher::pick(WorkloadShape::request_reply()),
            DequeVariant::ChaseLev
        );
    }

    #[test]
    fn signature_pick_agrees_with_hardcoded_pick_on_all_shapes() {
        // The signature-set logic should reproduce the existing
        // hardcoded routing for every canonical workload shape.
        let shapes = [
            WorkloadShape::request_reply(),
            WorkloadShape::producer_fast(4),
            WorkloadShape::producer_fast(16),
            WorkloadShape::producer_fast(64),
            WorkloadShape::producer_fast(256),
            WorkloadShape::fan_out(2, 16),
            WorkloadShape::fan_out(4, 64),
            WorkloadShape {
                n_thieves: 1,
                batch_size: Some(8),
                wait_idle: true,
            },
        ];
        for shape in shapes {
            let hardcoded = DequeDispatcher::pick(shape);
            let signature_based = DequeDispatcher::pick_by_signature(shape);
            assert_eq!(
                hardcoded, signature_based,
                "shape {shape:?}: hardcoded picked {hardcoded:?}, signature picked {signature_based:?}",
            );
        }
    }

    #[test]
    fn variant_signatures_are_distinct() {
        // Each variant occupies a distinct corner of the design cube.
        let sigs = [
            DequeVariant::ChaseLev.signature(),
            DequeVariant::Khpd.signature(),
            DequeVariant::Loh.signature(),
            DequeVariant::Urd.signature(),
            DequeVariant::Khl.signature(),
        ];
        for i in 0..sigs.len() {
            for j in (i + 1)..sigs.len() {
                assert_ne!(
                    sigs[i], sigs[j],
                    "variants {i} and {j} share the same signature",
                );
            }
        }
    }

    #[test]
    fn request_reply_has_empty_required_signature() {
        let req = WorkloadShape::request_reply().required_signature();
        assert_eq!(req.count(), 0);
    }

    #[test]
    fn producer_fast_requires_inner_and_outer() {
        let req = WorkloadShape::producer_fast(64).required_signature();
        assert!(req.contains(Axis::Inner));
        assert!(req.contains(Axis::Outer));
    }

    #[test]
    fn fan_out_requires_consumer_and_radius() {
        let req = WorkloadShape::fan_out(4, 64).required_signature();
        assert!(req.contains(Axis::Consumer));
        assert!(req.contains(Axis::Radius));
    }

    #[test]
    fn pick_any_batch_routes_to_khl() {
        // KHL is the SubEtha-native hybrid that beats KHPD and LOH
        // empirically at single-thief batched workloads. The routing
        // picks it for any batch size K >= 2.
        assert_eq!(
            DequeDispatcher::pick(WorkloadShape::producer_fast(4)),
            DequeVariant::Khl
        );
        assert_eq!(
            DequeDispatcher::pick(WorkloadShape::producer_fast(64)),
            DequeVariant::Khl
        );
        assert_eq!(
            DequeDispatcher::pick(WorkloadShape::producer_fast(256)),
            DequeVariant::Khl
        );
    }

    #[test]
    fn pick_multi_thief_routes_to_urd() {
        assert_eq!(
            DequeDispatcher::pick(WorkloadShape::fan_out(2, 16)),
            DequeVariant::Urd
        );
        assert_eq!(
            DequeDispatcher::pick(WorkloadShape::fan_out(4, 64)),
            DequeVariant::Urd
        );
    }

    #[test]
    fn pick_wait_idle_routes_to_urd_even_single_thief() {
        let shape = WorkloadShape {
            n_thieves: 1,
            batch_size: Some(8),
            wait_idle: true,
        };
        assert_eq!(DequeDispatcher::pick(shape), DequeVariant::Urd);
    }

    #[test]
    fn pick_with_fallback_skips_unconfigured() {
        // Only Chase-Lev configured; a batch shape that picks KHL
        // falls through KHL -> KHPD -> LOH -> Chase-Lev.
        let path = tmp("fallback_cl");
        let dispatcher = DequeDispatcher::builder()
            .with_chase_lev(&path, 64)
            .expect("create cl")
            .build();
        let shape = WorkloadShape::producer_fast(8);
        assert_eq!(DequeDispatcher::pick(shape), DequeVariant::Khl);
        assert_eq!(
            dispatcher.pick_with_fallback(shape),
            Some(DequeVariant::ChaseLev)
        );
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn dispatch_one_routes_to_chase_lev_when_per_item() {
        let cl_path = tmp("dispatch_one_cl");
        let dispatcher = DequeDispatcher::builder()
            .with_chase_lev(&cl_path, 64)
            .expect("create cl")
            .build();
        let chosen = dispatcher
            .dispatch_one(WorkloadShape::request_reply(), u32_item(42))
            .expect("dispatch_one");
        assert_eq!(chosen, DequeVariant::ChaseLev);
        // Drain via the Chase-Lev handle.
        let cl = dispatcher.chase_lev().expect("cl");
        let got = cl.steal().expect("steal");
        assert_eq!(got, u32_item(42));
        std::fs::remove_file(&cl_path).ok();
    }

    #[test]
    fn dispatch_batch_routes_to_khl_when_configured() {
        let khl_path = tmp("dispatch_batch_khl");
        let dispatcher = DequeDispatcher::builder()
            .with_khl(&khl_path, 256)
            .expect("create khl")
            .build();
        let items: Vec<LineItem> = (0..64u32).map(u32_item).collect();
        let chosen = dispatcher
            .dispatch_batch(WorkloadShape::producer_fast(64), &items)
            .expect("dispatch_batch");
        assert_eq!(chosen, DequeVariant::Khl);
        // 64 items = 22 slots (ceil(64/3)).
        let khl = dispatcher.khl().expect("khl");
        let (_, tail, _) = khl.snapshot_size();
        assert_eq!(tail, 22);
        std::fs::remove_file(&khl_path).ok();
    }

    #[test]
    fn dispatch_batch_falls_through_to_khpd_when_khl_unconfigured() {
        // No KHL configured; KHPD next in fallback chain.
        let khpd_path = tmp("fallback_khpd");
        let dispatcher = DequeDispatcher::builder()
            .with_khpd(&khpd_path, 64)
            .expect("create khpd")
            .build();
        let items: Vec<LineItem> = (0..6u32).map(u32_item).collect();
        let chosen = dispatcher
            .dispatch_batch(WorkloadShape::producer_fast(6), &items)
            .expect("dispatch_batch");
        assert_eq!(chosen, DequeVariant::Khpd);
        let khpd = dispatcher.khpd().expect("khpd");
        let (_, tail, _, _) = khpd.snapshot_size();
        assert_eq!(tail, 2);
        std::fs::remove_file(&khpd_path).ok();
    }

    #[test]
    fn dispatch_batch_falls_through_to_loh_when_khl_khpd_unconfigured() {
        // No KHL, no KHPD configured; LOH next in fallback chain.
        let loh_path = tmp("fallback_loh");
        let dispatcher = DequeDispatcher::builder()
            .with_loh(&loh_path, 512, usize::MAX)
            .expect("create loh")
            .build();
        let items: Vec<LineItem> = (0..200u32).map(u32_item).collect();
        let chosen = dispatcher
            .dispatch_batch(WorkloadShape::producer_fast(200), &items)
            .expect("dispatch_batch");
        assert_eq!(chosen, DequeVariant::Loh);
        let loh = dispatcher.loh().expect("loh");
        let (_, tail, _, _) = loh.snapshot_size();
        assert_eq!(tail, 200);
        std::fs::remove_file(&loh_path).ok();
    }

    #[test]
    fn dispatch_batch_routes_to_urd_for_multi_thief() {
        let urd_path = tmp("dispatch_batch_urd");
        let dispatcher = DequeDispatcher::builder()
            .with_urd(&urd_path, 2)
            .expect("create urd")
            .build();
        let items: Vec<LineItem> = (0..6u32).map(u32_item).collect();
        let chosen = dispatcher
            .dispatch_batch(WorkloadShape::fan_out(2, 6), &items)
            .expect("dispatch_batch");
        assert_eq!(chosen, DequeVariant::Urd);
        std::fs::remove_file(&urd_path).ok();
    }

    #[test]
    fn full_dispatcher_round_trips_mixed_shapes() {
        // Full dispatcher with Chase-Lev + KHL configured.
        // Per-item shape routes to Chase-Lev; batch shape routes to
        // KHL. Drain both sides and verify bit-exact recovery.
        let cl_path = tmp("full_cl");
        let khl_path = tmp("full_khl");
        let dispatcher = DequeDispatcher::builder()
            .with_chase_lev(&cl_path, 128)
            .expect("create cl")
            .with_khl(&khl_path, 64)
            .expect("create khl")
            .build();

        // 5 per-item dispatches -> Chase-Lev.
        for i in 0..5u32 {
            let v = dispatcher
                .dispatch_one(WorkloadShape::request_reply(), u32_item(i))
                .expect("dispatch_one");
            assert_eq!(v, DequeVariant::ChaseLev);
        }
        // 12-item batch -> KHL.
        let batch: Vec<LineItem> = (100..112u32).map(u32_item).collect();
        let v = dispatcher
            .dispatch_batch(WorkloadShape::producer_fast(12), &batch)
            .expect("dispatch_batch");
        assert_eq!(v, DequeVariant::Khl);

        // Drain Chase-Lev.
        let cl = dispatcher.chase_lev().expect("cl");
        let mut seen = Vec::new();
        while let Some(x) = cl.steal() {
            seen.push(x);
        }
        assert_eq!(seen.len(), 5);
        for (i, item) in seen.iter().enumerate() {
            assert_eq!(*item, u32_item(i as u32));
        }

        // Drain KHL.
        let khl = dispatcher.khl().expect("khl");
        let mut drained = Vec::new();
        loop {
            match khl.steal_slot() {
                crate::shared_deque_khl::Steal::Success(r) => {
                    for i in 0..r.n_items {
                        drained.push(r.items[i]);
                    }
                }
                crate::shared_deque_khl::Steal::Empty => break,
                crate::shared_deque_khl::Steal::Retry => continue,
            }
        }
        assert_eq!(drained.len(), 12);
        for (i, item) in drained.iter().enumerate() {
            assert_eq!(*item, u32_item(100 + i as u32));
        }

        std::fs::remove_file(&cl_path).ok();
        std::fs::remove_file(&khl_path).ok();
    }
}