aws-s3-transfer 1.0.0

Transfer Manager for Amazon S3 - a high-performance data plane client for uploading and downloading objects and directories.
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
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Transfer descriptor and related types for scheduler-managed transfer state.
//!
//! See the [`scheduler`](super) module docs for the threading and cost
//! model that the descriptor's claim protocol enforces.

use crate::runtime::sync::sync::atomic::{AtomicU64, AtomicU8, Ordering};
use crate::runtime::sync::sync::Arc;

use tokio::sync::Notify;
use tokio_util::sync::CancellationToken;

use crate::transfer::{BoxTransfer, Transfer, TransferId};

/// Claim + wake-requested protocol primitives.
///
/// Scoped to a sub-module so the underlying atomics are sourced from the
/// `runtime/sync` compat layer (loom under `cfg(s3_tm_loom)`, std otherwise)
/// without forcing the rest of this file's atomics onto the same path.
///
/// # Protocol
///
/// The claim flag tracks whether a descriptor is queued in the ready set
/// or currently being polled. Asserted by [`ClaimState::try_claim`], cleared
/// by [`ClaimState::release_claim`]. The flag is held continuously from
/// ready-set insert through `pop` through `poll_work` until `generate_work`
/// finishes handling the outcome - this is what serializes `poll_work` to
/// one worker per descriptor at a time.
///
/// The wake-requested flag is set unconditionally by `Scheduler::wake`
/// whether or not the claim CAS succeeded, and consumed by `generate_work`
/// in a release-and-recheck pattern. The pairing closes the lost-wake race
/// where a wake arrives between `release_claim` and the descriptor leaving
/// the ready set.
///
/// All four operations are `SeqCst` so the release-and-recheck of the claim
/// flag composes correctly with the mark-and-try-claim of the wake flag.
/// These two pairs operate on independent atomics; without a global total
/// order, a schedule exists on weak memory models where neither side
/// observes the other and the wake is lost.
pub(super) mod claim {
    use crate::runtime::sync::sync::atomic::{AtomicBool, Ordering};

    pub(in crate::scheduler) struct ClaimState {
        claimed: AtomicBool,
        wake_requested: AtomicBool,
    }

    impl ClaimState {
        pub(in crate::scheduler) fn new() -> Self {
            Self {
                claimed: AtomicBool::new(false),
                wake_requested: AtomicBool::new(false),
            }
        }

        /// Atomically take the claim if it is currently free. Returns
        /// `true` on success.
        pub(in crate::scheduler) fn try_claim(&self) -> bool {
            // compare_exchange rather than swap: on failure we must NOT
            // overwrite the existing `true`. A bare swap loses ownership
            // information when two callers race.
            self.claimed
                .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
                .is_ok()
        }

        /// Release the claim. Pairs with `try_claim`.
        pub(in crate::scheduler) fn release_claim(&self) {
            self.claimed.store(false, Ordering::SeqCst);
        }

        /// Mark that a wake has been requested.
        pub(in crate::scheduler) fn mark_wake_requested(&self) {
            self.wake_requested.store(true, Ordering::SeqCst);
        }

        /// Atomically clear and return the wake-requested flag.
        pub(in crate::scheduler) fn take_wake_requested(&self) -> bool {
            self.wake_requested.swap(false, Ordering::SeqCst)
        }
    }
}

use claim::ClaimState;

/// Default priority assigned to new transfers
const DEFAULT_PRIORITY: u8 = 128;

/// Vruntime cost charged when a transfer dispatches one unit of real IO work
/// (a network/disk request).
pub(super) const IO_WORK_COST: u64 = 128;

/// Vruntime cost charged when a composite *spawns a child* (as opposed to
/// dispatching real IO). A spawn is cheap coordination — it enqueues a
/// schedulable child but performs no IO itself — so it is charged less than an
/// IO unit. Charging spawn the full [`IO_WORK_COST`] makes a composite parent
/// accumulate vruntime as fast as its own children, so CFS stops selecting the
/// parent after a few spawns and it loses the poll-turns it needs to keep the
/// pipeline filled. A reduced spawn cost keeps the parent scheduling-competitive
/// with its children so it can refill continuously. This integrates spawn
/// cadence *into* CFS rather than governing it with ad-hoc spawn-batch-size
/// constants.
///
/// Charged to both individual and group vruntime (like all work). Composites
/// with the same spawn-to-IO ratio charge spawn identically, so their relative
/// `group_vruntime` ordering is unchanged. Composites with different ratios
/// accrue group vruntime at different rates per dispatched IO, producing a
/// bounded cross-group dispatch skew that penalizes the spawn-heavier group —
/// self-limiting, since a group cannot spawn its way to a larger dispatch
/// share, only a smaller one.
///
/// The `SPAWN_WORK_COST : IO_WORK_COST` ratio sets the steady-state in-flight
/// level: a smaller spawn cost lets a composite parent win more poll turns and
/// hold more children in flight.
pub(super) const SPAWN_WORK_COST: u64 = IO_WORK_COST / 2;

/// Precision-preserving scale factor for priority-weighted vruntime deltas.
///
/// `delta = (IO_WORK_COST * PRIORITY_SCALE) / priority`. Without scaling, integer
/// division would collapse to zero for priorities above `IO_WORK_COST` (e.g.,
/// priority 200 with `IO_WORK_COST = 128` would yield `delta = 0`, starving all
/// peers). Scaling by 256 keeps the formula resolution-correct across the
/// full `u8` priority range (1..=255). Mirrors the role of `NICE_0_LOAD` in
/// Linux CFS.
pub(super) const PRIORITY_SCALE: u64 = 256;

/// Compute the vruntime delta for a given priority at the full [`IO_WORK_COST`].
///
/// Higher priority yields a smaller delta, so the descriptor accumulates
/// vruntime more slowly and wins more dispatch share. Used for an individual
/// transfer's vruntime ([`TransferDescriptor::work_generated`]) and, via the
/// same charge, its group's vruntime when it generates work.
pub(super) fn vruntime_delta_for_priority(priority: u8) -> u64 {
    vruntime_delta_for_cost(IO_WORK_COST, priority)
}

/// Compute the vruntime delta for a given work cost and priority.
///
/// `delta = (cost * PRIORITY_SCALE) / priority`. Generalizes
/// [`vruntime_delta_for_priority`] so the spawn path can charge the reduced
/// [`SPAWN_WORK_COST`] while preserving the same priority scaling.
pub(super) fn vruntime_delta_for_cost(cost: u64, priority: u8) -> u64 {
    (cost * PRIORITY_SCALE) / (priority as u64).max(1)
}

/// The scheduler's handle to a transfer.
///
/// Clone is cheap (Arc wrapper). Contains all metadata for scheduling:
/// - Identity and priority
/// - Virtual runtime for CFS-style fair scheduling  
/// - Outstanding work tracking (queued + executing)
/// - The transfer itself for polling work
///
/// The transfer is dropped when the last clone drops.
// Weak references require std::sync::Arc; Loom's Arc has no Weak.
// Descriptor atomics remain Loom-instrumented; reference counts do not.
#[derive(Clone)]
pub(crate) struct TransferDescriptor(std::sync::Arc<Inner>);

/// A reference to a [`TransferDescriptor`] that does not keep it alive.
pub(crate) struct WeakTransferDescriptor(std::sync::Weak<Inner>);

impl WeakTransferDescriptor {
    /// Returns the descriptor, or `None` once every [`TransferDescriptor`]
    /// clone has dropped.
    pub(crate) fn upgrade(&self) -> Option<TransferDescriptor> {
        self.0.upgrade().map(TransferDescriptor)
    }
}

struct Inner {
    priority: AtomicU8,
    vruntime: AtomicU64,
    /// Shared with the GroupQueue this descriptor belongs to. When
    /// `work_generated` fires, both individual vruntime and group
    /// vruntime advance by the same priority-scaled delta.
    group_vruntime: Arc<AtomicU64>,
    queued_executing: QueuedExecuting,
    transfer: BoxTransfer,
    idle_notify: Notify,
    claim_state: ClaimState,
    /// Cancels this transfer's executing work. Fired by
    /// `Scheduler::cancel_descriptor`; the execution runtimes race each
    /// `execute` future against it.
    cancellation_token: CancellationToken,
}

impl std::fmt::Debug for TransferDescriptor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TransferDescriptor")
            .field("tid", &self.0.transfer.ctx().id)
            .field("priority", &self.priority())
            .field("vruntime", &self.vruntime())
            .finish_non_exhaustive()
    }
}

impl TransferDescriptor {
    #[cfg(test)]
    pub(crate) fn new(transfer: BoxTransfer) -> Self {
        Self::new_with_vruntime(transfer, 0, Arc::new(AtomicU64::new(0)))
    }

    pub(crate) fn new_with_vruntime(
        transfer: BoxTransfer,
        initial_vruntime: u64,
        group_vruntime: Arc<AtomicU64>,
    ) -> Self {
        Self(std::sync::Arc::new(Inner {
            priority: AtomicU8::new(DEFAULT_PRIORITY),
            vruntime: AtomicU64::new(initial_vruntime),
            group_vruntime,
            queued_executing: QueuedExecuting::new(),
            transfer,
            idle_notify: Notify::new(),
            claim_state: ClaimState::new(),
            cancellation_token: CancellationToken::new(),
        }))
    }

    /// Atomically take the claim if free. Returns `true` on success.
    pub(super) fn try_claim(&self) -> bool {
        self.0.claim_state.try_claim()
    }

    /// Release the claim.
    pub(super) fn release_claim(&self) {
        self.0.claim_state.release_claim()
    }

    /// Records a scheduler wake request.
    ///
    /// The claim-state flag preserves scheduler liveness. Pending accounting
    /// records the wake immediately when the transfer has already published its
    /// cause.
    pub(super) fn record_wake_request(&self) {
        self.0.claim_state.mark_wake_requested();
        self.transfer().ctx().record_wake();
    }

    /// Discards a wake request retained before the scheduler starts a new poll.
    pub(super) fn clear_wake_request(&self) {
        self.0.claim_state.take_wake_requested();
    }

    /// Reconciles a wake retained while `poll_work` was executing.
    ///
    /// A registered future may wake before the state machine publishes its
    /// pending cause. In that ordering, the initial observation is a no-op and
    /// this release-and-recheck path records the wake after `poll_work`
    /// returns `Pending`.
    pub(super) fn reconcile_pending_wake(&self) -> bool {
        if !self.0.claim_state.take_wake_requested() {
            return false;
        }
        self.transfer().ctx().record_wake();
        true
    }

    /// Returns a reference to this descriptor that does not keep it alive.
    pub(crate) fn downgrade(&self) -> WeakTransferDescriptor {
        WeakTransferDescriptor(std::sync::Arc::downgrade(&self.0))
    }

    pub(crate) fn transfer(&self) -> &dyn Transfer {
        self.0.transfer.as_ref()
    }

    /// Token that cancels this transfer's executing work.
    pub(crate) fn cancellation_token(&self) -> &CancellationToken {
        &self.0.cancellation_token
    }

    pub(crate) fn id(&self) -> TransferId {
        self.0.transfer.ctx().id
    }

    pub(crate) fn priority(&self) -> u8 {
        self.0.priority.load(Ordering::Acquire)
    }

    pub(crate) fn set_priority(&self, priority: u8) {
        self.0.priority.store(priority, Ordering::Release);
    }

    pub(super) fn group_vruntime_arc(&self) -> Arc<AtomicU64> {
        Arc::clone(&self.0.group_vruntime)
    }

    pub(crate) fn vruntime(&self) -> u64 {
        self.0.vruntime.load(Ordering::Acquire)
    }

    #[cfg(test)]
    pub(crate) fn set_vruntime(&self, vruntime: u64) {
        self.0.vruntime.store(vruntime, Ordering::Release);
    }

    fn add_vruntime(&self, delta: u64) {
        self.0.vruntime.fetch_add(delta, Ordering::AcqRel);
        self.0.group_vruntime.fetch_add(delta, Ordering::SeqCst);
    }

    /// Record work generation and update vruntime based on priority.
    /// Higher priority = slower vruntime accumulation = more work share.
    pub(crate) fn work_generated(&self) {
        let delta = vruntime_delta_for_priority(self.priority());
        self.add_vruntime(delta);
    }

    /// Record a *spawn* (a composite enqueued a child) and update vruntime at
    /// the reduced [`SPAWN_WORK_COST`] rather than the full [`IO_WORK_COST`].
    /// Charges both individual and group vruntime (via [`Self::add_vruntime`]),
    /// so the discount is uniform across composites and preserves cross-group
    /// fairness. Keeps a composite parent scheduling-competitive with its own
    /// children so it can refill the pipeline continuously instead of in bursts.
    pub(crate) fn work_generated_spawn(&self) {
        let delta = vruntime_delta_for_cost(SPAWN_WORK_COST, self.priority());
        self.add_vruntime(delta);
    }

    /// Called when work is enqueued (to be executed)
    pub(crate) fn work_queued(&self) {
        self.0.queued_executing.increment_queued();
    }

    /// Called when worker picks up work (atomic queued-- / executing++)
    pub(crate) fn work_started(&self) {
        self.0.queued_executing.start_executing();
    }

    /// Called when worker completes work. Returns true if transfer is now idle.
    pub(crate) fn work_finished(&self) -> bool {
        self.0.queued_executing.finish_executing()
    }

    /// Called when queued work is purged (cancelled before execution)
    pub(crate) fn work_purged(&self, count: usize) {
        self.0.queued_executing.decrement_queued(count as u32);
    }

    pub(crate) fn is_idle(&self) -> bool {
        self.0.queued_executing.is_idle()
    }

    /// Wake anyone waiting on this transfer to become idle
    pub(crate) fn notify_idle(&self) {
        self.0.idle_notify.notify_waiters();
    }

    /// Check if this transfer has reached a terminal state and no longer needs polled
    pub(crate) fn is_terminal(&self) -> bool {
        !self.0.transfer.ctx().is_active()
    }

    pub(crate) async fn wait_for_idle(&self) {
        loop {
            // Register interest before checking
            let notified = self.0.idle_notify.notified();
            if self.is_idle() {
                return;
            }
            notified.await;
        }
    }
}

/// Packed atomic counter for queued + executing counts.
///
/// A single `AtomicU64` instead of two `AtomicU32`s so that `start_executing`
/// (queued-1, executing+1) is a single CAS - no window where the counts are
/// inconsistent and `is_idle()` could return a false positive.
///
/// Layout: `[queued: u32][executing: u32]`
#[derive(Debug, Default)]
struct QueuedExecuting(AtomicU64);

impl QueuedExecuting {
    const QUEUED_ONE: u64 = 1 << 32;
    const EXECUTING_ONE: u64 = 1;
    /// Subtract 1 from queued (upper), add 1 to executing (lower)
    const QUEUED_TO_EXECUTING: u64 = Self::EXECUTING_ONE.wrapping_sub(Self::QUEUED_ONE);

    fn new() -> Self {
        Self(AtomicU64::new(0))
    }

    fn increment_queued(&self) {
        let prev = self.0.fetch_add(Self::QUEUED_ONE, Ordering::AcqRel);
        debug_assert!((prev >> 32) < u32::MAX as u64, "queued overflow");
    }

    fn decrement_queued(&self, count: u32) {
        let delta = (count as u64) << 32;
        let prev = self.0.fetch_sub(delta, Ordering::AcqRel);
        debug_assert!((prev >> 32) >= count as u64, "queued underflow");
    }

    fn start_executing(&self) {
        let prev = self
            .0
            .fetch_add(Self::QUEUED_TO_EXECUTING, Ordering::AcqRel);
        debug_assert!((prev >> 32) > 0, "queued underflow in start_executing");
    }

    fn finish_executing(&self) -> bool {
        let prev = self.0.fetch_sub(Self::EXECUTING_ONE, Ordering::AcqRel);
        debug_assert!((prev as u32) > 0, "executing underflow");
        prev == Self::EXECUTING_ONE
    }

    fn is_idle(&self) -> bool {
        self.0.load(Ordering::Acquire) == 0
    }

    #[cfg(test)]
    fn get(&self) -> (u32, u32) {
        let val = self.0.load(Ordering::Acquire);
        ((val >> 32) as u32, val as u32)
    }

    #[cfg(test)]
    fn outstanding(&self) -> u64 {
        let (q, e) = self.get();
        q as u64 + e as u64
    }
}

/// RAII guard for a descriptor's claim. On drop, releases the claim
/// unless [`hold`](Self::hold) was called to consume the guard without
/// releasing. Used by the scheduler's `generate_work` to ensure the
/// claim is released on every exit path, including panic.
pub(super) struct ClaimGuard<'a> {
    desc: &'a TransferDescriptor,
    released: bool,
}

impl<'a> ClaimGuard<'a> {
    /// Wrap a descriptor whose claim is currently held. The caller is
    /// responsible for having taken the claim (via `try_claim` or `pop`).
    pub(super) fn new(desc: &'a TransferDescriptor) -> Self {
        Self {
            desc,
            released: false,
        }
    }

    /// Explicit release now; subsequent drop is a no-op.
    pub(super) fn release(mut self) {
        self.desc.release_claim();
        self.released = true;
    }

    /// Consume the guard without releasing - the claim stays asserted.
    /// Used by the `PollWork::Ready` path which keeps the claim held
    /// across re-insert.
    pub(super) fn hold(mut self) {
        self.released = true;
    }
}

impl Drop for ClaimGuard<'_> {
    fn drop(&mut self) {
        if !self.released {
            self.desc.release_claim();
        }
    }
}

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

    mod queued_executing {
        use super::*;

        #[test]
        fn test_new_is_idle() {
            let qe = QueuedExecuting::new();
            assert!(qe.is_idle());
            assert_eq!(qe.get(), (0, 0));
            assert_eq!(qe.outstanding(), 0);
        }

        #[test]
        fn test_increment_queued() {
            let qe = QueuedExecuting::new();
            qe.increment_queued();
            assert_eq!(qe.get(), (1, 0));
            assert!(!qe.is_idle());

            qe.increment_queued();
            assert_eq!(qe.get(), (2, 0));
        }

        #[test]
        fn test_start_executing() {
            let qe = QueuedExecuting::new();
            qe.increment_queued();
            qe.increment_queued();
            qe.start_executing();
            assert_eq!(qe.get(), (1, 1));
        }

        #[test]
        fn test_finish_executing_returns_idle_status() {
            let qe = QueuedExecuting::new();
            qe.increment_queued();
            qe.start_executing();
            assert!(qe.finish_executing());
            assert!(qe.is_idle());
        }

        #[test]
        fn test_finish_executing_not_idle_when_queued_remains() {
            let qe = QueuedExecuting::new();
            qe.increment_queued();
            qe.increment_queued();
            qe.start_executing();
            assert!(!qe.finish_executing());
            assert_eq!(qe.get(), (1, 0));
        }

        // FIXME: crossbeam-epoch is incompatible with miri (https://github.com/crossbeam-rs/crossbeam/issues/1181)
        #[cfg_attr(miri, ignore)]
        #[test]
        fn test_full_lifecycle() {
            let qe = QueuedExecuting::new();
            qe.increment_queued();
            qe.increment_queued();
            qe.increment_queued();
            qe.start_executing();
            qe.start_executing();
            assert!(!qe.finish_executing());
            qe.decrement_queued(1);
            assert!(qe.finish_executing());
            assert!(qe.is_idle());
        }

        // FIXME: crossbeam-epoch is incompatible with miri (https://github.com/crossbeam-rs/crossbeam/issues/1181)
        #[cfg_attr(miri, ignore)]
        #[test]
        #[should_panic(expected = "queued underflow")]
        #[cfg(debug_assertions)]
        fn test_decrement_queued_underflow_panics() {
            QueuedExecuting::new().decrement_queued(1);
        }

        // FIXME: crossbeam-epoch is incompatible with miri (https://github.com/crossbeam-rs/crossbeam/issues/1181)
        #[cfg_attr(miri, ignore)]
        #[test]
        #[should_panic(expected = "executing underflow")]
        #[cfg(debug_assertions)]
        fn test_finish_executing_underflow_panics() {
            QueuedExecuting::new().finish_executing();
        }

        // FIXME: crossbeam-epoch is incompatible with miri (https://github.com/crossbeam-rs/crossbeam/issues/1181)
        #[cfg_attr(miri, ignore)]
        #[test]
        #[should_panic(expected = "queued underflow in start_executing")]
        #[cfg(debug_assertions)]
        fn test_start_executing_without_queued_panics() {
            QueuedExecuting::new().start_executing();
        }
    }

    mod priority_vruntime {
        use super::*;
        use crate::scheduler::transfer::mock::FixedWorkCount;
        use crate::scheduler::MockTransfer;
        use std::sync::Arc;

        fn test_descriptor(id: u64) -> TransferDescriptor {
            let transfer_id = TransferId { id, parent: None };
            let sm = Arc::new(FixedWorkCount::new(100));
            let transfer = Box::new(MockTransfer::new(transfer_id, sm));
            TransferDescriptor::new(transfer)
        }

        // FIXME: crossbeam-epoch is incompatible with miri (https://github.com/crossbeam-rs/crossbeam/issues/1181)
        #[cfg_attr(miri, ignore)]
        #[test]
        fn test_priority_affects_vruntime_accumulation() {
            let high = test_descriptor(1);
            let low = test_descriptor(2);

            high.set_priority(255); // highest
            low.set_priority(64); // low

            // Simulate 10 work items each
            for _ in 0..10 {
                high.work_generated();
                low.work_generated();
            }

            // Higher priority should accumulate LESS vruntime
            // (lower vruntime = scheduled first = more work share)
            let high_vrt = high.vruntime();
            let low_vrt = low.vruntime();

            assert!(
                high_vrt < low_vrt,
                "high priority should have lower vruntime: high={}, low={}",
                high_vrt,
                low_vrt
            );
        }

        // FIXME: crossbeam-epoch is incompatible with miri (https://github.com/crossbeam-rs/crossbeam/issues/1181)
        #[cfg_attr(miri, ignore)]
        #[test]
        fn test_max_priority_still_accumulates_vruntime() {
            let desc = test_descriptor(1);
            desc.set_priority(255); // max priority

            desc.work_generated();

            // Even max priority should accumulate SOME vruntime
            // Otherwise it would starve all other transfers
            assert!(
                desc.vruntime() > 0,
                "max priority should still accumulate vruntime, got {}",
                desc.vruntime()
            );
        }

        // FIXME: crossbeam-epoch is incompatible with miri (https://github.com/crossbeam-rs/crossbeam/issues/1181)
        #[cfg_attr(miri, ignore)]
        #[test]
        fn test_priority_ratio_reflected_in_vruntime() {
            let high = test_descriptor(1);
            let low = test_descriptor(2);

            high.set_priority(200);
            low.set_priority(50); // 4x lower priority

            // Same number of work items
            for _ in 0..100 {
                high.work_generated();
                low.work_generated();
            }

            let high_vrt = high.vruntime();
            let low_vrt = low.vruntime();

            // Low priority should accumulate roughly 4x more vruntime
            // Allow some tolerance for integer math
            let ratio = low_vrt as f64 / high_vrt.max(1) as f64;
            assert!(
                ratio > 2.0,
                "4x priority difference should yield >2x vruntime ratio: high={}, low={}, ratio={}",
                high_vrt,
                low_vrt,
                ratio
            );
        }

        // FIXME: crossbeam-epoch is incompatible with miri (https://github.com/crossbeam-rs/crossbeam/issues/1181)
        #[cfg_attr(miri, ignore)]
        #[test]
        fn test_work_generated_spawn_charges_both_individual_and_group_vruntime() {
            let desc = test_descriptor(1);
            let group = desc.group_vruntime_arc();
            assert_eq!(desc.vruntime(), 0);
            assert_eq!(group.load(Ordering::SeqCst), 0);

            desc.work_generated_spawn();

            // A spawn charges the reduced spawn cost to BOTH the individual and
            // the group vruntime. The group charge is what makes a composite's
            // spawn cadence visible to cross-group CFS fairness; dropping it
            // (charging individual only) would silently change cross-group
            // dispatch share for composites with differing spawn:IO ratios.
            let expected = vruntime_delta_for_cost(SPAWN_WORK_COST, desc.priority());
            assert_eq!(
                desc.vruntime(),
                expected,
                "spawn must charge individual vruntime by the spawn delta"
            );
            assert_eq!(
                group.load(Ordering::SeqCst),
                expected,
                "spawn must charge group vruntime by the same spawn delta"
            );
        }

        // FIXME: crossbeam-epoch is incompatible with miri (https://github.com/crossbeam-rs/crossbeam/issues/1181)
        #[cfg_attr(miri, ignore)]
        #[test]
        fn test_spawn_charge_is_less_than_io_charge() {
            // The spawn discount is what keeps a composite parent scheduling-
            // competitive with its own children. If SPAWN_WORK_COST ever equaled
            // IO_WORK_COST the parent would accumulate vruntime as fast as the
            // work it dispatches and lose the poll turns it needs to refill.
            let spawn = test_descriptor(1);
            let io = test_descriptor(2);

            spawn.work_generated_spawn();
            io.work_generated();

            assert!(
                spawn.vruntime() < io.vruntime(),
                "spawn charge ({}) must be less than IO charge ({})",
                spawn.vruntime(),
                io.vruntime()
            );
        }
    }
}

#[cfg(all(test, s3_tm_loom))]
mod loom_tests {
    use super::claim::ClaimState;
    use loom::sync::atomic::{AtomicBool, Ordering};
    use loom::sync::Arc;
    use loom::thread;

    /// After any interleaving of the release-and-recheck pattern with a
    /// concurrent wake arrival, at least one of the two threads must own
    /// the re-insert. If neither does, a wake is outstanding but the
    /// descriptor is absent from the ready set and the transfer is stuck.
    #[test]
    fn release_recheck_no_lost_wake() {
        loom::model(|| {
            // Start state: claim held by A (simulating post-poll_work Pending).
            let state = Arc::new(ClaimState::new());
            assert!(state.try_claim());

            let inserted = Arc::new(AtomicBool::new(false));

            let s2 = state.clone();
            let i2 = inserted.clone();
            let a = thread::spawn(move || {
                s2.release_claim();
                if s2.take_wake_requested() {
                    // generate_work sees wake; tries to reclaim + insert.
                    if s2.try_claim() {
                        i2.store(true, Ordering::SeqCst);
                    }
                }
            });

            let s3 = state.clone();
            let i3 = inserted.clone();
            let b = thread::spawn(move || {
                s3.mark_wake_requested();
                if s3.try_claim() {
                    i3.store(true, Ordering::SeqCst);
                }
            });

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

            assert!(
                inserted.load(Ordering::SeqCst),
                "lost wake: neither A nor B claimed the descriptor to re-insert it"
            );
        });
    }

    /// Single-poll exclusivity: two threads concurrently calling
    /// `ClaimState::try_claim` must never both succeed. The ready-set uses
    /// this to guarantee at most one thread inside `poll_work(desc)` at a
    /// time.
    #[test]
    fn try_claim_is_exclusive() {
        loom::model(|| {
            let state = Arc::new(ClaimState::new());

            let s2 = state.clone();
            let a = thread::spawn(move || s2.try_claim());
            let s3 = state.clone();
            let b = thread::spawn(move || s3.try_claim());

            let got_a = a.join().unwrap();
            let got_b = b.join().unwrap();

            assert!(got_a ^ got_b, "try_claim was not exclusive");
        });
    }
}