fibre 0.5.8

High-performance, safe, memory-efficient sync/async channels built for real-time, low-overhead communication in concurrent Rust applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
//! A hybrid semaphore that supports both synchronous and asynchronous waiters.
//!
//! This `CapacityGate` is the core primitive for creating bounded channels that
//! need to support both blocking and async operations. It uses a `parking_lot::Mutex`
//! to protect its internal state, ensuring that the management of permits and
//! the unified waiter queue (for sync `Thread`s and async `Waker`s) is
//! free of race conditions like "lost wakeups" and "permit stealing".
//!
//! The mutex is only contended when the gate is out of permits and a new waiter
//! must be enqueued, or when a permit is released and a waiter must be dequeued.
//! This is an efficient approach for a backpressure mechanism.

use std::collections::VecDeque;
use std::fmt;
use std::future::Future;
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll, Waker};
use std::thread::{self, Thread};

use parking_lot::Mutex;

pub(crate) const STATE_WAITING: u8 = 0;
pub(crate) const STATE_SUCCESS: u8 = 1;
pub(crate) const STATE_CANCELLED: u8 = 2;

/// An enum representing either a sync or async waiter.
#[derive(Debug)]
enum Waiter {
  Sync(Thread),
  Async {
    waker: Waker,
    state: *const AtomicU8,
  },
}

// Safety: the raw pointer points into a pinned AcquireFuture; its Drop impl
// removes the entry before the future is destroyed, so the pointer is valid
// for the lifetime of the Waiter in the queue.
unsafe impl Send for Waiter {}

impl Waiter {
  fn wake(self) {
    match self {
      Waiter::Sync(thread) => thread.unpark(),
      Waiter::Async { waker, .. } => waker.wake(),
    }
  }

  fn will_wake(&self, waker: &Waker) -> bool {
    match self {
      Waiter::Async { waker: self_waker, .. } => self_waker.will_wake(waker),
      Waiter::Sync(_) => false,
    }
  }
}

/// The internal state of the `CapacityGate`, protected by a `Mutex`.
#[derive(Debug)]
struct GateInternal {
  /// The number of currently available permits.
  permits: usize,
  /// Permits committed to woken *sync* waiters that have not yet re-acquired
  /// the lock to claim them. Kept out of `permits` so the capacity clamp in
  /// `release()` can never discard an in-flight handoff — for a capacity-0
  /// (rendezvous) gate that steal would re-park the woken sender forever.
  reserved: usize,
  /// A unified, fair (FIFO) queue of waiting threads and tasks.
  waiters: VecDeque<Waiter>,
  /// Set to true when the gate is closed; subsequent acquisitions return immediately.
  is_closed: bool,
}

/// A clonable handle to a hybrid sync/async semaphore.
pub struct CapacityGate {
  capacity: usize,
  internal: Arc<Mutex<GateInternal>>,
}

impl fmt::Debug for CapacityGate {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    let internal = self.internal.lock();
    f.debug_struct("CapacityGate")
      .field("capacity", &self.capacity)
      .field("permits", &internal.permits)
      .field("reserved", &internal.reserved)
      .field("waiters", &internal.waiters.len())
      .finish()
  }
}

impl CapacityGate {
  /// Creates a new `CapacityGate` with a given capacity.
  pub fn new(capacity: usize) -> Self {
    Self {
      capacity,
      internal: Arc::new(Mutex::new(GateInternal {
        permits: capacity,
        reserved: 0,
        waiters: VecDeque::new(),
        is_closed: false,
      })),
    }
  }

  /// Returns the total capacity of the gate.
  pub fn capacity(&self) -> usize {
    self.capacity
  }

  /// Acquires a permit, blocking the current thread if none are available.
  pub fn acquire_sync(&self) {
    // Optimistic fast path using the correct try_acquire logic.
    if self.try_acquire() {
      return;
    }

    // Slow path, must lock and wait.
    let mut internal = self.internal.lock();
    let mut was_parked = false;
    loop {
      if internal.is_closed {
        return;
      }
      // A permit reserved by release() for a woken waiter. Only claim it
      // after we have actually parked, so arriving threads cannot jump the
      // queue ahead of the waiter the permit was committed to.
      if was_parked && internal.reserved > 0 {
        internal.reserved -= 1;
        return;
      }
      // Check for a permit. This is safe from stealing because `try_acquire`
      // will fail for new arrivals if we are in the waiters queue.
      if internal.permits > 0 {
        internal.permits -= 1;
        return;
      }

      // Add our thread to the waiter queue, unlock, and park.
      internal.waiters.push_back(Waiter::Sync(thread::current()));
      drop(internal);
      thread::park();
      was_parked = true;
      internal = self.internal.lock();
    }
  }

  /// Acquires a permit asynchronously, returning a future that resolves
  /// when a permit is available.
  pub fn acquire_async(&self) -> AcquireFuture<'_> {
    AcquireFuture {
      gate: self,
      state: AtomicU8::new(STATE_WAITING),
      is_registered: false,
      _phantom: PhantomPinned,
    }
  }

  /// Attempts to acquire a permit without blocking.
  ///
  /// The key to preventing deadlock: a permit can only be taken if no one
  /// is waiting. This gives waiters priority and prevents permit stealing.
  pub fn try_acquire(&self) -> bool {
    let mut internal = self.internal.lock();
    if internal.is_closed {
      return true;
    }
    if internal.waiters.is_empty() && internal.permits > 0 {
      internal.permits -= 1;
      true
    } else {
      false
    }
  }

  /// Closes the gate and unparks all waiting producers.
  ///
  /// Called when the consumer drops. Wakes every waiter in a single O(N) pass
  /// so they can observe the closed state and return an error, rather than
  /// relying on a fragile per-permit daisy-chain.
  pub fn close(&self) {
    let mut internal = self.internal.lock();
    if !internal.is_closed {
      internal.is_closed = true;
      while let Some(waiter) = internal.waiters.pop_front() {
        waiter.wake();
      }
    }
  }

  /// Releases a permit back to the gate.
  pub fn release(&self) {
    let mut internal = self.internal.lock();
    internal.permits += 1;

    while let Some(waiter) = internal.waiters.pop_front() {
      match waiter {
        Waiter::Sync(thread) => {
          // Move the permit out of the general pool and reserve it for the
          // woken thread. If it stayed in the pool, a concurrent release()
          // hitting the empty-queue clamp below could discard it before the
          // thread wakes up — a permanent livelock on capacity-0 gates.
          internal.permits -= 1;
          internal.reserved += 1;
          thread.unpark();
          return;
        }
        Waiter::Async { waker, state } => {
          let state_ref = unsafe { &*state };
          if state_ref
            .compare_exchange(
              STATE_WAITING,
              STATE_SUCCESS,
              Ordering::SeqCst,
              Ordering::SeqCst,
            )
            .is_ok()
          {
            // The woken future's STATE_SUCCESS fast path returns Ready
            // without touching the pool, so the permit is consumed here.
            // (This also stops the pool inflating past capacity on every
            // async wake.)
            internal.permits -= 1;
            waker.wake();
            return;
          }
          // STATE_CANCELLED: discard this waiter and try the next one.
        }
      }
    }

    internal.permits = internal.permits.min(self.capacity);
  }

  /// Attempts to acquire up to `n` permits without blocking.
  ///
  /// Returns the number of permits acquired (`0..=n`). Returns `0` if any
  /// waiters are queued (preserving the no-stealing rule of `try_acquire`)
  /// or if no permits are available. Returns `n` immediately if the gate is
  /// closed, matching `try_acquire`'s closed-returns-true convention so the
  /// caller can observe the closed state and bail out.
  pub fn try_acquire_many(&self, n: usize) -> usize {
    if n == 0 {
      return 0;
    }
    let mut internal = self.internal.lock();
    if internal.is_closed {
      return n;
    }
    if internal.waiters.is_empty() && internal.permits > 0 {
      let k = internal.permits.min(n);
      internal.permits -= k;
      k
    } else {
      0
    }
  }

  /// Acquires between 1 and `max` permits, blocking the current thread until
  /// at least one permit is available.
  ///
  /// Returns the number of permits acquired. If the gate is closed (or closes
  /// while waiting), returns `max` immediately so the caller can observe the
  /// closed state and bail out, matching `acquire_sync`'s behavior.
  pub fn acquire_many_sync(&self, max: usize) -> usize {
    if max == 0 {
      return 0;
    }
    // Optimistic fast path.
    let k = self.try_acquire_many(max);
    if k > 0 {
      return k;
    }

    // Slow path, must lock and wait.
    let mut internal = self.internal.lock();
    let mut was_parked = false;
    loop {
      if internal.is_closed {
        return max;
      }
      // A permit reserved for a woken waiter (see `acquire_sync`); claim it
      // plus any extra pool permits up to `max`.
      if was_parked && internal.reserved > 0 {
        internal.reserved -= 1;
        let extra = internal.permits.min(max - 1);
        internal.permits -= extra;
        return 1 + extra;
      }
      // Safe from stealing for the same reason as `acquire_sync`: new
      // arrivals fail `try_acquire_many` while we sit in the waiter queue.
      if internal.permits > 0 {
        let k = internal.permits.min(max);
        internal.permits -= k;
        return k;
      }

      internal.waiters.push_back(Waiter::Sync(thread::current()));
      drop(internal);
      thread::park();
      was_parked = true;
      internal = self.internal.lock();
    }
  }

  /// Acquires between 1 and `max` permits asynchronously.
  ///
  /// The returned future resolves with the number of permits acquired
  /// (or `max` if the gate is closed).
  pub fn acquire_many_async(&self, max: usize) -> AcquireManyFuture<'_> {
    AcquireManyFuture {
      gate: self,
      max,
      state: AtomicU8::new(STATE_WAITING),
      is_registered: false,
      _phantom: PhantomPinned,
    }
  }

  /// Releases `n` permits back to the gate in a single lock acquisition,
  /// waking up to `n` waiters in one coalesced pass after the lock is dropped.
  ///
  /// The per-waiter accounting matches `release()`: a permit committed to a
  /// woken *sync* waiter moves into `reserved` (clamp-proof) until the thread
  /// claims it; a permit committed to a woken *async* waiter is consumed
  /// immediately (its future's `STATE_SUCCESS` fast path never touches the
  /// pool).
  pub fn release_many(&self, n: usize) {
    if n == 0 {
      return;
    }
    let mut to_wake: Vec<Waiter> = Vec::new();
    {
      let mut internal = self.internal.lock();
      internal.permits += n;

      while to_wake.len() < n {
        match internal.waiters.pop_front() {
          None => break,
          Some(Waiter::Sync(thread)) => {
            internal.permits -= 1;
            internal.reserved += 1;
            to_wake.push(Waiter::Sync(thread));
          }
          Some(Waiter::Async { waker, state }) => {
            let state_ref = unsafe { &*state };
            if state_ref
              .compare_exchange(
                STATE_WAITING,
                STATE_SUCCESS,
                Ordering::SeqCst,
                Ordering::SeqCst,
              )
              .is_ok()
            {
              internal.permits -= 1;
              to_wake.push(Waiter::Async { waker, state });
            }
            // STATE_CANCELLED: discard this waiter and try the next one.
          }
        }
      }

      // Committed permits are no longer in the pool, so the clamp can only
      // ever trim genuinely surplus permits.
      internal.permits = internal.permits.min(self.capacity);
    }
    // Wake outside the lock so woken threads/tasks don't immediately
    // contend on the mutex we still hold.
    for waiter in to_wake {
      waiter.wake();
    }
  }
}

/// A future that resolves when a permit is acquired from the `CapacityGate`.
#[must_use = "futures do nothing unless you .await or poll them"]
pub struct AcquireFuture<'a> {
  gate: &'a CapacityGate,
  state: AtomicU8,
  is_registered: bool,
  _phantom: PhantomPinned,
}

impl<'a> Future for AcquireFuture<'a> {
  type Output = ();

  fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
    let this = unsafe { self.as_mut().get_unchecked_mut() };

    // Fast path: release() already claimed a permit on our behalf.
    if this.state.load(Ordering::Acquire) == STATE_SUCCESS {
      this.is_registered = false;
      return Poll::Ready(());
    }

    let mut internal = this.gate.internal.lock();
    let state_ptr = &this.state as *const AtomicU8;

    if internal.is_closed {
      this.is_registered = false;
      return Poll::Ready(());
    }

    // Re-check under lock in case release() raced with our fast-path check.
    if this.state.load(Ordering::Acquire) == STATE_SUCCESS {
      this.is_registered = false;
      return Poll::Ready(());
    }

    if internal.permits > 0 {
      internal.permits -= 1;
      // If we were registered, our waiter entry is still queued (we claimed
      // a pool permit instead of being woken). Unlink it so no stale state
      // pointer survives this future.
      if this.is_registered {
        internal.waiters.retain(|w| match w {
          Waiter::Async { state, .. } => *state != state_ptr,
          _ => true,
        });
        this.is_registered = false;
      }
      return Poll::Ready(());
    }

    let new_waker = cx.waker();

    // Update waker in-place if already registered (waker may have changed).
    let mut found = false;
    for waiter in internal.waiters.iter_mut() {
      if let Waiter::Async { state, waker: ref mut existing_waker } = waiter {
        if *state == state_ptr {
          *existing_waker = new_waker.clone();
          found = true;
          break;
        }
      }
    }

    if !found {
      this.is_registered = true;
      this.state.store(STATE_WAITING, Ordering::SeqCst);
      internal.waiters.push_back(Waiter::Async {
        waker: new_waker.clone(),
        state: state_ptr,
      });
    }

    Poll::Pending
  }
}

impl<'a> Drop for AcquireFuture<'a> {
  fn drop(&mut self) {
    if self.is_registered
      && self
        .state
        .compare_exchange(STATE_WAITING, STATE_CANCELLED, Ordering::SeqCst, Ordering::SeqCst)
        .is_ok()
    {
      let mut internal = self.gate.internal.lock();
      let state_ptr = &self.state as *const AtomicU8;
      internal.waiters.retain(|w| match w {
        Waiter::Async { state, .. } => *state != state_ptr,
        _ => true,
      });
    }
  }
}

/// A future that resolves with the number of permits (1..=max) acquired from
/// the `CapacityGate`, or `max` if the gate is closed.
#[must_use = "futures do nothing unless you .await or poll them"]
pub struct AcquireManyFuture<'a> {
  gate: &'a CapacityGate,
  max: usize,
  state: AtomicU8,
  is_registered: bool,
  _phantom: PhantomPinned,
}

impl<'a> Future for AcquireManyFuture<'a> {
  type Output = usize;

  fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
    let this = unsafe { self.as_mut().get_unchecked_mut() };

    if this.max == 0 {
      this.is_registered = false;
      return Poll::Ready(0);
    }

    // Fast path: release()/release_many() consumed one permit from the pool
    // on our behalf when it flagged us STATE_SUCCESS. Grab up to `max - 1`
    // extra permits under the lock.
    if this.state.load(Ordering::Acquire) == STATE_SUCCESS {
      let mut internal = this.gate.internal.lock();
      let extra = internal.permits.min(this.max - 1);
      internal.permits -= extra;
      this.is_registered = false;
      return Poll::Ready(1 + extra);
    }

    let mut internal = this.gate.internal.lock();
    let state_ptr = &this.state as *const AtomicU8;

    if internal.is_closed {
      this.is_registered = false;
      return Poll::Ready(this.max);
    }

    // Re-check under lock in case a release raced with our fast-path check.
    if this.state.load(Ordering::Acquire) == STATE_SUCCESS {
      let extra = internal.permits.min(this.max - 1);
      internal.permits -= extra;
      this.is_registered = false;
      return Poll::Ready(1 + extra);
    }

    // Mirrors the permit-taking branch of `AcquireFuture::poll`.
    if internal.permits > 0 {
      let k = internal.permits.min(this.max);
      internal.permits -= k;
      // Unlink a still-queued waiter entry so no stale state pointer
      // survives this future (see `AcquireFuture::poll`).
      if this.is_registered {
        internal.waiters.retain(|w| match w {
          Waiter::Async { state, .. } => *state != state_ptr,
          _ => true,
        });
        this.is_registered = false;
      }
      return Poll::Ready(k);
    }

    let new_waker = cx.waker();

    // Update waker in-place if already registered (waker may have changed).
    let mut found = false;
    for waiter in internal.waiters.iter_mut() {
      if let Waiter::Async { state, waker: ref mut existing_waker } = waiter {
        if *state == state_ptr {
          *existing_waker = new_waker.clone();
          found = true;
          break;
        }
      }
    }

    if !found {
      this.is_registered = true;
      this.state.store(STATE_WAITING, Ordering::SeqCst);
      internal.waiters.push_back(Waiter::Async {
        waker: new_waker.clone(),
        state: state_ptr,
      });
    }

    Poll::Pending
  }
}

impl<'a> Drop for AcquireManyFuture<'a> {
  fn drop(&mut self) {
    if self.is_registered
      && self
        .state
        .compare_exchange(STATE_WAITING, STATE_CANCELLED, Ordering::SeqCst, Ordering::SeqCst)
        .is_ok()
    {
      let mut internal = self.gate.internal.lock();
      let state_ptr = &self.state as *const AtomicU8;
      internal.waiters.retain(|w| match w {
        Waiter::Async { state, .. } => *state != state_ptr,
        _ => true,
      });
    }
  }
}

impl Clone for CapacityGate {
  fn clone(&self) -> Self {
    Self {
      capacity: self.capacity,
      internal: self.internal.clone(),
    }
  }
}

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

  #[test]
  fn gate_new_and_capacity() {
    let gate = CapacityGate::new(5);
    assert_eq!(gate.capacity(), 5);
  }

  #[test]
  fn acquire_sync_release() {
    let gate = CapacityGate::new(1);
    gate.acquire_sync();
    // No easy way to check permits without another thread,
    // but this ensures it doesn't hang on first acquire.
    gate.release();
  }

  #[test]
  fn acquire_sync_blocks_and_unblocks() {
    let gate = Arc::new(CapacityGate::new(1));
    gate.acquire_sync(); // Acquire the only permit

    let gate_clone = gate.clone();
    let handle = thread::spawn(move || {
      // This should block
      gate_clone.acquire_sync();
    });

    // Give the thread time to block
    thread::sleep(Duration::from_millis(100));
    assert!(!handle.is_finished(), "Thread should have blocked");

    // Release the permit, which should unpark the thread
    gate.release();
    handle.join().expect("Thread panicked");
  }

  #[cfg(not(miri))]
  #[tokio::test]
  async fn acquire_async_waits_and_completes() {
    use tokio::time::timeout;

    let gate = Arc::new(CapacityGate::new(1));
    gate.acquire_sync(); // Use up the only permit

    let acquire_fut = gate.acquire_async();

    let gate_for_spawn = gate.clone();
    tokio::spawn(async move {
      tokio::time::sleep(Duration::from_millis(100)).await;
      gate_for_spawn.release();
    });

    timeout(Duration::from_millis(500), acquire_fut)
      .await
      .expect("Future did not complete after release");
  }

  #[cfg(not(miri))]
  #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
  async fn mixed_waiters_contention() {
    let gate = Arc::new(CapacityGate::new(2));
    let mut thread_handles = Vec::new();
    let mut task_handles = Vec::new();
    let completion_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));

    // Spawn 3 sync waiters
    for _ in 0..3 {
      let gate = gate.clone();
      let count = completion_count.clone();
      thread_handles.push(thread::spawn(move || {
        gate.acquire_sync();
        thread::sleep(Duration::from_millis(50));
        gate.release();
        count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
      }));
    }

    // Spawn 3 async waiters
    for _ in 0..3 {
      let gate = gate.clone();
      let count = completion_count.clone();
      task_handles.push(tokio::spawn(async move {
        gate.acquire_async().await;
        tokio::time::sleep(Duration::from_millis(50)).await;
        gate.release();
        count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
      }));
    }

    // Wait for all async tasks to complete.
    for handle in task_handles {
      handle.await.unwrap();
    }

    // Wait for all sync threads to complete.
    for handle in thread_handles {
      handle.join().unwrap();
    }

    assert_eq!(
      completion_count.load(std::sync::atomic::Ordering::Relaxed),
      6
    );
  }

  #[test]
  fn test_acquire_async_drop_leak() {
    let gate = CapacityGate::new(1);

    // 1. Consume the only available permit so subsequent async acquires must wait
    assert!(gate.try_acquire());

    // 2. Create a self-contained dummy waker for the context
    fn dummy_waker() -> Waker {
      use std::task::{RawWaker, RawWakerVTable};
      unsafe fn clone(_: *const ()) -> RawWaker {
        RawWaker::new(std::ptr::null(), &VTABLE)
      }
      unsafe fn wake(_: *const ()) {}
      unsafe fn wake_by_ref(_: *const ()) {}
      unsafe fn drop_raw(_: *const ()) {}
      static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop_raw);
      unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &VTABLE)) }
    }

    let waker = dummy_waker();
    let mut cx = Context::from_waker(&waker);

    // 3. Create the AcquireFuture and poll it once.
    // Since no permits are available, it will return Poll::Pending and register our waker.
    let mut fut = Box::pin(gate.acquire_async());
    assert!(fut.as_mut().poll(&mut cx).is_pending());

    // Verify that the waker was indeed registered in the internal queue
    {
      let internal = gate.internal.lock();
      assert_eq!(internal.waiters.len(), 1);
    }

    // 4. Drop (cancel) the future before it ever resolves to success
    drop(fut);

    // 5. Inspect the queue.
    // Under the unpatched code, this assertion will fail because the stale waker is still there.
    let leaked_count = {
      let internal = gate.internal.lock();
      internal.waiters.len()
    };

    assert_eq!(
      leaked_count, 0,
      "Waker leak detected: dropping AcquireFuture left a stale waker in the waiters queue!"
    );
  }

  #[test]
  fn try_acquire_many_basic() {
    let gate = CapacityGate::new(5);
    assert_eq!(gate.try_acquire_many(0), 0);
    assert_eq!(gate.try_acquire_many(3), 3);
    // Only 2 permits left; asking for 5 yields 2.
    assert_eq!(gate.try_acquire_many(5), 2);
    // Exhausted.
    assert_eq!(gate.try_acquire_many(1), 0);
  }

  #[test]
  fn release_many_restores_and_clamps() {
    let gate = CapacityGate::new(5);
    assert_eq!(gate.try_acquire_many(5), 5);
    gate.release_many(5);
    assert_eq!(gate.try_acquire_many(5), 5);
    // Over-release with no waiters clamps to capacity.
    gate.release_many(10);
    assert_eq!(gate.try_acquire_many(10), 5);
  }

  #[test]
  fn many_apis_on_closed_gate() {
    let gate = CapacityGate::new(2);
    assert_eq!(gate.try_acquire_many(2), 2);
    gate.close();
    // Closed gate grants immediately so callers can observe closure.
    assert_eq!(gate.try_acquire_many(7), 7);
    assert_eq!(gate.acquire_many_sync(4), 4);
  }

  #[test]
  fn acquire_many_sync_blocks_and_gets_batch() {
    let gate = Arc::new(CapacityGate::new(4));
    assert_eq!(gate.try_acquire_many(4), 4);

    let gate_clone = gate.clone();
    let handle = thread::spawn(move || gate_clone.acquire_many_sync(3));

    thread::sleep(Duration::from_millis(100));
    assert!(!handle.is_finished(), "Thread should have blocked");

    gate.release_many(3);
    let acquired = handle.join().expect("Thread panicked");
    assert!(
      (1..=3).contains(&acquired),
      "expected 1..=3 permits, got {acquired}"
    );
  }

  #[test]
  fn release_many_wakes_multiple_sync_waiters() {
    let gate = Arc::new(CapacityGate::new(3));
    assert_eq!(gate.try_acquire_many(3), 3);

    let mut handles = Vec::new();
    for _ in 0..3 {
      let gate = gate.clone();
      handles.push(thread::spawn(move || gate.acquire_many_sync(1)));
    }
    // Give all three time to enqueue as waiters.
    thread::sleep(Duration::from_millis(100));

    gate.release_many(3);
    let mut total = 0;
    for h in handles {
      total += h.join().expect("waiter panicked");
    }
    assert_eq!(total, 3);
  }

  #[cfg(not(miri))]
  #[tokio::test]
  async fn acquire_many_async_waits_and_completes() {
    use tokio::time::timeout;

    let gate = Arc::new(CapacityGate::new(4));
    assert_eq!(gate.try_acquire_many(4), 4);

    let acquire_fut = gate.acquire_many_async(3);

    let gate_for_spawn = gate.clone();
    tokio::spawn(async move {
      tokio::time::sleep(Duration::from_millis(100)).await;
      gate_for_spawn.release_many(3);
    });

    let acquired = timeout(Duration::from_millis(500), acquire_fut)
      .await
      .expect("Future did not complete after release_many");
    assert!(
      (1..=3).contains(&acquired),
      "expected 1..=3 permits, got {acquired}"
    );
  }

  /// Regression test for the capacity-0 handoff steal: a permit committed to
  /// a woken sync waiter must survive a concurrent release() whose
  /// empty-queue clamp would otherwise discard it (the woken thread would
  /// find no permit and re-park forever).
  #[test]
  fn release_does_not_steal_inflight_handoff_permit_cap0() {
    let gate = Arc::new(CapacityGate::new(0));

    let gate_clone = gate.clone();
    let waiter = thread::spawn(move || gate_clone.acquire_sync());

    // Let the waiter park at the gate.
    thread::sleep(Duration::from_millis(100));
    assert!(!waiter.is_finished(), "waiter should be parked");

    // First release commits a handoff to the parked waiter; the second one
    // finds an empty queue and clamps the pool — it must not be able to
    // touch the committed (reserved) permit.
    gate.release();
    gate.release();

    thread::sleep(Duration::from_millis(200));
    assert!(
      waiter.is_finished(),
      "REGRESSION: clamp stole the in-flight handoff permit; waiter re-parked forever"
    );
    waiter.join().unwrap();
  }

  /// Same scenario through acquire_many_sync (the batch path).
  #[test]
  fn release_does_not_steal_inflight_handoff_permit_cap0_many() {
    let gate = Arc::new(CapacityGate::new(0));

    let gate_clone = gate.clone();
    let waiter = thread::spawn(move || gate_clone.acquire_many_sync(4));

    thread::sleep(Duration::from_millis(100));
    assert!(!waiter.is_finished(), "waiter should be parked");

    gate.release();
    gate.release();

    thread::sleep(Duration::from_millis(200));
    assert!(
      waiter.is_finished(),
      "REGRESSION: clamp stole the in-flight handoff permit from acquire_many_sync"
    );
    let acquired = waiter.join().unwrap();
    assert!(acquired >= 1);
  }

  /// Async wakes must consume the released permit instead of leaving it in
  /// the pool (which used to inflate the pool past capacity on every async
  /// handoff).
  #[cfg(not(miri))]
  #[tokio::test]
  async fn async_wake_consumes_permit_no_pool_inflation() {
    let gate = Arc::new(CapacityGate::new(1));
    gate.acquire_sync(); // permits: 0

    let gate_for_task = gate.clone();
    let task = tokio::spawn(async move { gate_for_task.acquire_async().await });

    // Let the future register as a waiter.
    tokio::time::sleep(Duration::from_millis(100)).await;

    gate.release(); // committed to the async waiter, consumed at release time
    task.await.unwrap();

    // The async waiter's permit must not have leaked back into the pool:
    // exactly one permit is outstanding (held by the task that never
    // released), so none are available.
    {
      let internal = gate.internal.lock();
      assert_eq!(
        internal.permits, 0,
        "async wake must consume the permit, not inflate the pool"
      );
      assert_eq!(internal.reserved, 0);
    }
    assert!(!gate.try_acquire());
  }

  #[test]
  fn test_acquire_many_async_drop_leak() {
    let gate = CapacityGate::new(1);
    assert!(gate.try_acquire());

    fn dummy_waker() -> Waker {
      use std::task::{RawWaker, RawWakerVTable};
      unsafe fn clone(_: *const ()) -> RawWaker {
        RawWaker::new(std::ptr::null(), &VTABLE)
      }
      unsafe fn wake(_: *const ()) {}
      unsafe fn wake_by_ref(_: *const ()) {}
      unsafe fn drop_raw(_: *const ()) {}
      static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop_raw);
      unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &VTABLE)) }
    }

    let waker = dummy_waker();
    let mut cx = Context::from_waker(&waker);

    let mut fut = Box::pin(gate.acquire_many_async(3));
    assert!(fut.as_mut().poll(&mut cx).is_pending());
    {
      let internal = gate.internal.lock();
      assert_eq!(internal.waiters.len(), 1);
    }

    drop(fut);

    let leaked_count = {
      let internal = gate.internal.lock();
      internal.waiters.len()
    };
    assert_eq!(
      leaked_count, 0,
      "Waker leak: dropping AcquireManyFuture left a stale waker in the waiters queue!"
    );
  }
}