arty_executor 0.5.0

Single-threaded async task executor for the Arty runtime.
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use std::cell::Cell;
use std::collections::VecDeque;
use std::marker::{PhantomData, PhantomPinned};
use std::mem::ManuallyDrop;
use std::ops::Deref;
use std::pin::Pin;
use std::ptr::NonNull;
use std::sync::atomic::{self, AtomicBool, AtomicPtr, AtomicUsize};
use std::sync::{Arc, Mutex, Weak};
use std::task::{RawWaker, RawWakerVTable, Waker};

use pin_project::{pin_project, pinned_drop};
use plurality::{Box as PoolBox, Pool};

use crate::TaskRef;

const MAX_WAKER_COUNT: usize = usize::MAX / 2;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Retirement {
    Active,
    WithoutState,
    WithState,
}

pub(crate) type AwakenedQueue = Arc<Mutex<VecDeque<TaskRef>>>;

#[derive(Debug)]
pub(crate) struct WakeShared {
    pub(crate) awakened: AwakenedQueue,
    pub(crate) probe_embedded_wake_signals: Arc<AtomicBool>,
    parent_waker: Waker,
    waker_states: Mutex<Pool<WakerState>>,
}

impl WakeShared {
    pub(crate) fn new(awakened: AwakenedQueue, probe_embedded_wake_signals: Arc<AtomicBool>, parent_waker: Waker) -> Self {
        Self {
            awakened,
            probe_embedded_wake_signals,
            parent_waker,
            waker_states: Mutex::new(Pool::new()),
        }
    }
}

/// Task-owned access to independently pooled wake metadata.
///
/// The wake signal supplies the waker that can be used to set the signal and enables the owner
/// to inspect the awakened state. It also integrates with the executor's shared state to signal
/// the awakened state directly to the executor, eliminating the requirement to access the
/// task to detect that it has awakened.
///
/// # Ownership
///
/// The type uses interior mutability because its state is accessed concurrently from multiple
/// threads - by the executor (and/or task) that owns it on one hand, and by any number of
/// awaited async futures on any number of threads on the other hand (via wakers).
///
/// The borrowed polling waker is scoped to the poll. Cloning it retains only the pooled
/// [`WakerState`], not task storage. Completion and cancellation retire that state before the
/// task is released, so retained wakers remain valid across shutdown but become inert no-ops.
///
/// # Thread safety
///
/// The type itself is single-threaded, although the `std::task::Waker` instances obtained
/// from it are thread-safe as required by the waker API contract.
#[derive(Debug)]
#[pin_project(PinnedDrop)]
pub(crate) struct WakeSignal {
    task_ref: TaskRef,
    shared: Arc<WakeShared>,
    state: AtomicPtr<WakerState>,
    retirement: Cell<Retirement>,
    awakened: AtomicBool,
    queued: AtomicBool,

    /// The type is single threaded... as far as other modules are concerned.
    ///
    /// We are secretly thread-safe internally but that is just a side-effect of
    /// the same type also masquerading as zero or more thread-safe `Waker` instances.
    ///
    /// When we act under the personality of a `Waker` within this module, Rust does not
    /// see the cross-thread access so will not complain about this marker.
    _single_threaded: PhantomData<*const ()>,

    /// This type cannot be unpinned once it has been pinned (latest when calling `waker_ref()`).
    _requires_pin: PhantomPinned,
}

impl WakeSignal {
    pub(crate) fn new(shared: Arc<WakeShared>, task_ref: TaskRef) -> Self {
        Self {
            task_ref,
            shared,
            state: AtomicPtr::new(std::ptr::null_mut()),
            retirement: Cell::new(Retirement::Active),
            awakened: AtomicBool::new(false),
            queued: AtomicBool::new(false),
            _single_threaded: PhantomData,
            _requires_pin: PhantomPinned,
        }
    }

    #[cfg(test)]
    pub(crate) fn new_with_parts(
        awakened_queue: AwakenedQueue,
        probe_embedded_wake_signals: Arc<AtomicBool>,
        parent_waker: Waker,
        task_ref: TaskRef,
    ) -> Self {
        let shared = Arc::new(WakeShared::new(awakened_queue, probe_embedded_wake_signals, parent_waker));
        Self::new(shared, task_ref)
    }

    /// Creates a fake wake signal that is not connected to an executor. This can be useful
    /// for testing, where you need a wake signal but do not care about what it actually does.
    #[cfg(test)]
    pub(crate) fn fake() -> Self {
        // SAFETY: We can use it as a placeholder value but not actually dereference it.
        // This is fine because `WakeSignal` only uses the `TaskRef`, not the task behind it.
        let fake_task_ref = unsafe { TaskRef::fake() };

        Self::new_with_parts(
            Arc::new(Mutex::new(VecDeque::new())),
            Arc::new(AtomicBool::new(false)),
            Waker::noop().clone(),
            fake_task_ref,
        )
    }

    fn state(&self) -> &WakerState {
        let mut state = self.state.load(atomic::Ordering::Acquire);
        if state.is_null() {
            state = self.install_state(self.allocate_state());
        }
        // SAFETY: The atomic pointer owns one intrusive reference until `WakeSignal::drop`.
        unsafe { state.as_ref() }.expect("initialized wake state pointer is never null")
    }

    fn allocate_state(&self) -> WakerStateRef {
        let value = WakerState::new(Arc::downgrade(&self.shared), self.task_ref);
        let candidate = self
            .shared
            .waker_states
            .lock()
            .expect("waker state pool is never held while invoking user code")
            .alloc_box(value);
        WakerStateRef::from_pool_box(candidate)
    }

    fn install_state(&self, candidate: WakerStateRef) -> *mut WakerState {
        let candidate_ptr = candidate.as_ptr().cast_mut();
        // Release publishes the fully initialized pooled state to Acquire readers. On failure,
        // Acquire observes the state installed by the winning contender before its pointer is used.
        match self.state.compare_exchange(
            std::ptr::null_mut(),
            candidate_ptr,
            atomic::Ordering::AcqRel,
            atomic::Ordering::Acquire,
        ) {
            Ok(_) => candidate.into_raw().as_ptr(),
            Err(existing) => {
                drop(candidate);
                existing
            }
        }
    }

    fn state_if_initialized(&self) -> Option<&WakerState> {
        // SAFETY: A non-null atomic pointer owns one intrusive reference until `WakeSignal::drop`.
        unsafe { self.state.load(atomic::Ordering::Acquire).as_ref() }
    }

    fn state_ptr(&self) -> *mut WakerState {
        _ = self.state();
        self.state.load(atomic::Ordering::Acquire)
    }

    pub(crate) fn retire(&self) {
        let state = self.state.load(atomic::Ordering::Acquire);
        self.retirement.set(if state.is_null() {
            Retirement::WithoutState
        } else {
            Retirement::WithState
        });
        // SAFETY: A non-null atomic pointer owns one intrusive reference until
        // `WakeSignal::drop`.
        if let Some(state) = unsafe { state.as_ref() } {
            state.retire();
        }
        // Lock before testing `queued`: an enqueue may hold the mutex after its active check but
        // before publishing the flag. Acquiring the same mutex waits for that insertion.
        let mut queue = self
            .shared
            .awakened
            .lock()
            .expect("wake queue is never held while invoking user code");
        if self.queued.swap(false, atomic::Ordering::AcqRel) {
            queue.retain(|queued_task_ref| *queued_task_ref != self.task_ref);
        }
    }

    pub(crate) fn clear_queued_notification(&self) {
        self.queued.store(false, atomic::Ordering::Release);
        if let Some(state) = self.state_if_initialized() {
            state.queued.store(false, atomic::Ordering::Release);
        }
    }

    /// Returns whether the signal has received a wake-up notification.
    ///
    /// If it has, resets the signal to a not-awakened state.
    pub(crate) fn consume_awakened(&self) -> bool {
        // Most of the time, the flag will be false so we at first probe it with Relaxed ordering.
        // If it is false, we can return early. If it is true, we need to ensure that we see all
        // memory operations that happened before the flag was set (i.e. the state changes that
        // led to the task being awakened). An Acquire fence sequenced after the relaxed RMW that
        // observes the Release store establishes the required synchronization.
        let inline = self.awakened.swap(false, atomic::Ordering::Relaxed);
        let independent = self.state_if_initialized().is_some_and(WakerState::consume_awakened);
        if inline || independent {
            atomic::fence(atomic::Ordering::Acquire);
            true
        } else {
            false
        }
    }

    pub(crate) fn is_awakened(&self) -> bool {
        self.awakened.load(atomic::Ordering::Acquire) || self.state_if_initialized().is_some_and(WakerState::is_awakened)
    }

    /// Returns whether task-owned signal storage is safe to drop.
    ///
    /// Before retirement this requires no owned wakers. Retirement releases the task's owner,
    /// after which independent wakers retain only pooled state and this returns `true`.
    #[cfg_attr(test, mutants::skip)] // White-box diagnostic used only by focused ownership tests.
    #[cfg(test)]
    pub(crate) fn can_drop_task_storage(&self) -> bool {
        // We use Acquire ordering to ensure we see all writes to the waker before we declare it
        // inert. Generally, we expect wakers to already be inert by the time their inertness is
        // queried, because this query will happen when a task has completed and its future has
        // been dropped, which should (unless there is a resource leak) drop any wakers held by
        // pending awaits triggered deeper in the future.
        self.state_if_initialized()
            .is_none_or(|state| self.retirement.get() != Retirement::Active || state.external_waker_count() == 0)
    }

    /// Borrows a waker associated with this signal without incrementing its reference count.
    ///
    /// # Safety
    ///
    /// The signal must remain pinned for the returned borrow. Any clones own independent pooled
    /// state and do not extend this borrow.
    pub(crate) unsafe fn waker_ref(self: Pin<&Self>) -> impl Deref<Target = Waker> + '_ {
        let signal_ptr: *const Self = self.get_ref();

        // Like a borrowed Arc waker, this borrow keeps the signal alive without an owned count.
        // Only Waker::clone creates an owned reference. Hide ManuallyDrop behind Deref so callers
        // cannot extract or consume the uncounted Waker; its lifetime stays tied to this borrow.
        // SAFETY: We are required to correctly implement the waker API contract, which we do.
        // This includes being thread-safe, etc. The `WakeSignal` is thread-safe and all the
        // methods use interior mutability, so we are only using shared references, thereby
        // ensuring we do not violate Rust aliasing rules (as long as the owner of the `WakeSignal`
        // does not create any mutable references - though given that all methods are `&self` that
        // might still be relatively harmless and anyway likely detected by Miri as a bug.
        ManuallyDrop::new(unsafe { Waker::from_raw(RawWaker::new(signal_ptr.cast(), &BORROWED_WAKER_VTABLE)) })
    }

    /// Creates an owned waker for tests of the reference-counting protocol.
    ///
    /// # Safety
    ///
    /// The signal must remain pinned until this call returns.
    #[cfg(test)]
    pub(crate) unsafe fn waker(self: Pin<&Self>) -> Waker {
        // SAFETY: The caller keeps the signal alive until all owned wakers are released.
        unsafe { self.waker_ref() }.clone()
    }

    #[cfg(test)]
    fn external_waker_count(&self) -> usize {
        self.state_if_initialized().map_or(0, WakerState::external_waker_count)
    }

    #[cfg(test)]
    pub(crate) fn has_queued_notification(&self) -> bool {
        self.queued.load(atomic::Ordering::Acquire)
    }

    fn wake_inline(&self) {
        if enqueue_wake(
            &self.shared.awakened,
            &self.queued,
            self.task_ref,
            &self.shared.parent_waker,
            || true,
        ) {
            return;
        }
        self.awakened.store(true, atomic::Ordering::Release);
        self.shared.probe_embedded_wake_signals.store(true, atomic::Ordering::Release);
        self.shared.parent_waker.wake_by_ref();
    }
}

#[pinned_drop]
impl PinnedDrop for WakeSignal {
    fn drop(self: Pin<&mut Self>) {
        if self.retirement.get() == Retirement::Active {
            self.retire();
        }
        if self.retirement.get() == Retirement::WithState {
            let state = self.state.swap(std::ptr::null_mut(), atomic::Ordering::AcqRel);
            let state = NonNull::new(state).expect("wake state was present before the owner swap");
            // SAFETY: The atomic pointer owns exactly one intrusive reference.
            drop(unsafe { WakerStateRef::from_non_null(state) });
        }
    }
}

pub(crate) struct WakerState {
    task_ref: TaskRef,
    shared: Weak<WakeShared>,
    ref_count: AtomicUsize,
    waker_count: AtomicUsize,
    awakened: AtomicBool,
    queued: AtomicBool,
    active: AtomicBool,
}

// SAFETY: `TaskRef` is only returned to the executor owner and never dereferenced here. All
// cross-thread mutation is atomic or mutex-protected.
unsafe impl Send for WakerState {}
// SAFETY: Same invariant as `Send`.
unsafe impl Sync for WakerState {}

impl WakerState {
    pub(crate) fn new(shared: Weak<WakeShared>, task_ref: TaskRef) -> Self {
        Self {
            task_ref,
            shared,
            ref_count: AtomicUsize::new(1),
            waker_count: AtomicUsize::new(0),
            awakened: AtomicBool::new(false),
            queued: AtomicBool::new(false),
            active: AtomicBool::new(true),
        }
    }

    fn wake(&self) {
        if !self.active.load(atomic::Ordering::Acquire) {
            return;
        }

        if let Some(shared) = self.shared.upgrade()
            && enqueue_wake(&shared.awakened, &self.queued, self.task_ref, &shared.parent_waker, || {
                self.active.load(atomic::Ordering::Acquire)
            })
        {
            return;
        }

        if !self.active.load(atomic::Ordering::Acquire) {
            return;
        }
        self.awakened.store(true, atomic::Ordering::Release);
        if let Some(shared) = self.shared.upgrade() {
            shared.probe_embedded_wake_signals.store(true, atomic::Ordering::Release);
            shared.parent_waker.wake_by_ref();
        }
    }

    fn retire(&self) {
        // Release publishes retirement before queue removal. Acquire wake checks that observe
        // inactivity must not enqueue this task; the queue mutex closes an enqueue already past
        // its active check.
        self.active.store(false, atomic::Ordering::Release);
        if let Some(shared) = self.shared.upgrade() {
            let mut queue = shared.awakened.lock().expect("wake queue is never held while invoking user code");
            if self.queued.swap(false, atomic::Ordering::AcqRel) {
                queue.retain(|queued_task_ref| *queued_task_ref != self.task_ref);
            }
        }
    }

    #[cfg(test)]
    #[cfg_attr(coverage_nightly, coverage(off))] // Test-only synchronization hook.
    fn retire_with(&self, after_deactivate: impl FnOnce(bool)) {
        // Release publishes retirement before queue removal. Acquire wake checks that observe
        // inactivity must not enqueue this task; the queue mutex closes an enqueue already past
        // its active check.
        self.active.store(false, atomic::Ordering::Release);
        after_deactivate(self.active.load(atomic::Ordering::Acquire));
        if let Some(shared) = self.shared.upgrade() {
            let mut queue = shared.awakened.lock().expect("wake queue is never held while invoking user code");
            if self.queued.swap(false, atomic::Ordering::AcqRel) {
                queue.retain(|queued_task_ref| *queued_task_ref != self.task_ref);
            }
        }
    }

    fn consume_awakened(&self) -> bool {
        self.awakened.swap(false, atomic::Ordering::Relaxed)
    }

    fn is_awakened(&self) -> bool {
        self.awakened.load(atomic::Ordering::Acquire)
    }

    #[cfg(test)]
    fn external_waker_count(&self) -> usize {
        self.waker_count.load(atomic::Ordering::Acquire)
    }
}

impl std::fmt::Debug for WakerState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(std::any::type_name::<Self>())
            .field("active", &self.active.load(atomic::Ordering::Relaxed))
            .field("ref_count", &self.ref_count.load(atomic::Ordering::Relaxed))
            .field("waker_count", &self.waker_count.load(atomic::Ordering::Relaxed))
            .field("queued", &self.queued.load(atomic::Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

fn enqueue_wake(
    queue: &AwakenedQueue,
    queued: &AtomicBool,
    task_ref: TaskRef,
    parent_waker: &Waker,
    is_active: impl FnOnce() -> bool,
) -> bool {
    let Ok(mut queue) = queue.try_lock() else {
        return false;
    };
    if !is_active() {
        return true;
    }
    if queue.len() >= queue.capacity() {
        return false;
    }
    queued.store(true, atomic::Ordering::Release);
    queue.push_back(task_ref);
    drop(queue);
    parent_waker.wake_by_ref();
    true
}

#[cfg(test)]
fn remove_queued_notification(queue: &AwakenedQueue, queued: &AtomicBool, task_ref: TaskRef) {
    // Lock before testing `queued`: an enqueue may hold the mutex after its active check but before
    // publishing the flag. Acquiring the same mutex waits for that insertion so retirement can
    // remove the task identity even when the flag was initially false.
    let mut queue = queue.lock().expect("wake queue is never held while invoking user code");
    if !queued.swap(false, atomic::Ordering::AcqRel) {
        return;
    }
    queue.retain(|queued_task_ref| *queued_task_ref != task_ref);
}

struct WakerStateRef(NonNull<WakerState>);

// SAFETY: The pointee is `Send + Sync`, uses an atomic reference count, and remains allocated
// until the final reference returns its slot to the pool.
unsafe impl Send for WakerStateRef {}
// SAFETY: Same invariant as `Send`.
unsafe impl Sync for WakerStateRef {}

impl WakerStateRef {
    fn from_pool_box(state: PoolBox<WakerState>) -> Self {
        Self(PoolBox::into_raw(state))
    }

    unsafe fn from_raw(ptr: *const ()) -> Self {
        Self(NonNull::new(ptr.cast_mut().cast()).expect("raw waker state pointer is never null"))
    }

    unsafe fn from_non_null(ptr: NonNull<WakerState>) -> Self {
        Self(ptr)
    }

    fn as_ptr(&self) -> *const WakerState {
        self.0.as_ptr()
    }

    fn into_raw(self) -> NonNull<WakerState> {
        let ptr = self.0;
        std::mem::forget(self);
        ptr
    }
}

impl Deref for WakerStateRef {
    type Target = WakerState;

    fn deref(&self) -> &Self::Target {
        // SAFETY: This reference contributes to the intrusive count, so the pooled value remains
        // alive for the returned borrow.
        unsafe { self.0.as_ref() }
    }
}

impl Drop for WakerStateRef {
    fn drop(&mut self) {
        // Each owner publishes its prior accesses with Release. The final owner acquires those
        // publications before reclaiming the pooled slot.
        if self.ref_count.fetch_sub(1, atomic::Ordering::Release) != 1 {
            return;
        }
        atomic::fence(atomic::Ordering::Acquire);
        // SAFETY: The count reached zero, so this is the unique final reconstruction of the exact
        // pointer produced by `PoolBox::into_raw`.
        drop(unsafe { PoolBox::<WakerState>::from_raw(self.0) });
    }
}

impl std::fmt::Debug for WakerStateRef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.deref().fmt(f)
    }
}

static BORROWED_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(borrowed_clone, borrowed_consume, borrowed_wake_by_ref, borrowed_drop);
static OWNED_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(waker_clone_waker, waker_wake, waker_wake_by_ref, waker_drop_waker);

fn borrowed_clone(ptr: *const ()) -> RawWaker {
    let signal = resurrect_signal_ref(ptr);
    let state_ptr = signal.state_ptr();
    // SAFETY: `state_ptr` owns one intrusive reference until `WakeSignal::drop`.
    let state = unsafe { state_ptr.as_ref() }.expect("initialized wake state pointer is never null");
    increment_waker_count(&state.waker_count, std::process::abort);
    increment_reference_count(&state.ref_count, std::process::abort);
    RawWaker::new(state_ptr.cast_const().cast(), &OWNED_WAKER_VTABLE)
}

fn borrowed_wake_by_ref(ptr: *const ()) {
    resurrect_signal_ref(ptr).wake_inline();
}

#[cfg_attr(coverage_nightly, coverage(off))] // Unreachable through safe public Waker APIs.
#[cfg_attr(test, mutants::skip)] // Safe Waker APIs never consume the borrowed polling waker.
fn borrowed_consume(_: *const ()) {
    unreachable!("a borrowed polling waker cannot be consumed");
}

#[cfg_attr(coverage_nightly, coverage(off))] // Unreachable through safe public Waker APIs.
#[cfg_attr(test, mutants::skip)] // Safe Waker APIs never drop the borrowed polling waker.
fn borrowed_drop(_: *const ()) {
    unreachable!("a borrowed polling waker cannot be dropped");
}

fn waker_clone_waker(ptr: *const ()) -> RawWaker {
    let state = resurrect_state_ref(ptr);
    increment_waker_count(&state.waker_count, std::process::abort);
    increment_reference_count(&state.ref_count, std::process::abort);

    RawWaker::new(ptr, &OWNED_WAKER_VTABLE)
}

fn increment_reference_count(ref_count: &AtomicUsize, on_overflow: fn() -> !) {
    // Cloning only reserves another owner; it does not publish or consume state, so Relaxed is
    // sufficient. Final Release/Acquire reclamation synchronizes prior owners before reuse.
    let previous = ref_count.fetch_add(1, atomic::Ordering::Relaxed);
    if previous >= MAX_WAKER_COUNT {
        on_overflow();
    }
}

fn increment_waker_count(waker_count: &AtomicUsize, on_overflow: fn() -> !) {
    // Reference count increment is independent of state transitions, so Relaxed is enough.
    let previous = waker_count.fetch_add(1, atomic::Ordering::Relaxed);

    // Like Arc, leave half the range for concurrent increments before aborting.
    // Unwinding instead would let repeated clone attempts keep growing the count.
    if previous >= MAX_WAKER_COUNT {
        on_overflow();
    }
}

#[cfg_attr(test, mutants::skip)] // If tasks do not wake up, tests tend to infinite loop.
fn waker_wake(ptr: *const ()) {
    // SAFETY: Consuming wake transfers this raw waker's counted ownership into the guard. Its
    // destructor releases that ownership even if the configured owner waker panics.
    let state = RawWakerOwner(unsafe { WakerStateRef::from_raw(ptr) });
    state.wake();
}

#[cfg_attr(test, mutants::skip)] // If tasks do not wake up, tests tend to infinite loop.
fn waker_wake_by_ref(ptr: *const ()) {
    resurrect_state_ref(ptr).wake();
}

#[cfg_attr(test, mutants::skip)] // It's well tested, but causes ocasional test timeouts
fn waker_drop_waker(ptr: *const ()) {
    // SAFETY: Dropping transfers this raw waker's counted ownership into the guard.
    drop(RawWakerOwner(unsafe { WakerStateRef::from_raw(ptr) }));
}

struct RawWakerOwner(WakerStateRef);

impl Deref for RawWakerOwner {
    type Target = WakerState;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Drop for RawWakerOwner {
    fn drop(&mut self) {
        self.waker_count.fetch_sub(1, atomic::Ordering::Release);
    }
}

/// Resurrects the pooled state reference that hides behind the waker's state pointer.
///
/// We return it with `'static` because there is no Rust lifetime that corresponds to
/// the waker reference's real lifetime. Just do not use it after the waker vtable methods.
#[cfg_attr(coverage_nightly, coverage(off))] // A null pointer would violate this module's RawWaker invariant.
fn resurrect_state_ref(ptr: *const ()) -> &'static WakerState {
    // SAFETY: Every raw waker pointer comes from a live pooled state retained by either the task
    // owner or the raw waker's own counted reference.
    let state = unsafe { ptr.cast::<WakerState>().as_ref() };

    let Some(state) = state else {
        unreachable!("waker has a null pointer for its inner state - impossible")
    };

    state
}

fn resurrect_signal_ref(ptr: *const ()) -> &'static WakeSignal {
    // SAFETY: Borrowed polling wakers exist only for the poll scope while their pinned signal
    // remains alive.
    let signal = unsafe { ptr.cast::<WakeSignal>().as_ref() };

    let Some(signal) = signal else {
        unreachable!("borrowed waker has a null pointer for its wake signal - impossible")
    };

    signal
}

#[cfg(test)]
mod tests {
    use std::pin::pin;
    use std::sync::{Barrier, mpsc};
    use std::thread;

    use super::*;
    use crate::testing::TestWaker;

    #[test]
    fn never_used() {
        // SAFETY: We can use it as a placeholder value but not actually dereference it.
        // This is fine because `WakeSignal` only uses the `TaskRef`, not the task behind it.
        let fake_task_ref = unsafe { TaskRef::fake() };

        let awakened_queue = Arc::new(Mutex::new(VecDeque::with_capacity(10)));
        let probe_embedded_wake_signals = Arc::new(AtomicBool::new(false));

        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::clone(&probe_embedded_wake_signals),
            Waker::noop().clone(),
            fake_task_ref
        ));

        assert!(signal.can_drop_task_storage());
    }

    #[test]
    fn borrowed_waker_reference_count() {
        let signal = pin!(WakeSignal::fake());
        let signal = signal.as_ref();

        {
            // SAFETY: The signal stays pinned until the borrowed waker and its clone are dropped.
            let waker = unsafe { signal.waker_ref() };
            assert_eq!(signal.external_waker_count(), 0);

            waker.wake_by_ref();
            assert!(signal.consume_awakened());
            assert_eq!(signal.external_waker_count(), 0);

            let clone = waker.clone();
            assert_eq!(signal.external_waker_count(), 1);
            drop(clone);
            assert_eq!(signal.external_waker_count(), 0);
        }

        assert_eq!(signal.external_waker_count(), 0);
        assert!(signal.can_drop_task_storage());
    }

    #[test]
    fn borrowed_wake_uses_inline_queue_without_allocating_state() {
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let awakened_queue = Arc::new(Mutex::new(VecDeque::with_capacity(1)));
        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::new(AtomicBool::new(false)),
            Waker::noop().clone(),
            fake_task_ref
        ));
        let signal = signal.as_ref();

        // SAFETY: The signal remains pinned for the borrowed wake.
        unsafe { signal.waker_ref() }.wake_by_ref();

        assert!(signal.state_if_initialized().is_none());
        assert_eq!(awakened_queue.lock().unwrap().len(), 1);
    }

    #[test]
    fn duplicate_state_installation_releases_the_unused_candidate() {
        let signal = pin!(WakeSignal::fake());
        let signal = signal.as_ref();
        let installed = std::ptr::from_ref(signal.state()).cast_mut();

        let candidate = signal.allocate_state();
        assert!(format!("{candidate:?}").contains(std::any::type_name::<WakerState>()));
        assert_eq!(signal.install_state(candidate), installed);
    }

    #[test]
    fn retirement_removes_notifications_and_makes_owned_wakers_inert() {
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let awakened_queue = Arc::new(Mutex::new(VecDeque::with_capacity(1)));
        let probe_embedded_wake_signals = Arc::new(AtomicBool::new(false));
        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::clone(&probe_embedded_wake_signals),
            Waker::noop().clone(),
            fake_task_ref
        ));
        let signal = signal.as_ref();
        // SAFETY: The signal remains pinned through retirement.
        let waker = unsafe { signal.waker() };

        waker.wake_by_ref();
        assert_eq!(awakened_queue.lock().unwrap().len(), 1);

        signal.retire();
        assert!(awakened_queue.lock().unwrap().is_empty());
        waker.wake_by_ref();
        assert!(awakened_queue.lock().unwrap().is_empty());
        assert!(!probe_embedded_wake_signals.load(atomic::Ordering::Relaxed));
    }

    #[test]
    fn retirement_without_state_removes_its_notification() {
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let awakened_queue = Arc::new(Mutex::new(VecDeque::from([fake_task_ref])));
        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::new(AtomicBool::new(false)),
            Waker::noop().clone(),
            fake_task_ref,
        ));
        signal.queued.store(true, atomic::Ordering::Release);

        signal.retire();

        assert!(awakened_queue.lock().unwrap().is_empty());
        assert!(!signal.queued.load(atomic::Ordering::Acquire));
    }

    #[test]
    fn waker_state_retirement_tolerates_dropped_shared_state() {
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let state = WakerState::new(Weak::new(), fake_task_ref);

        state.retire();

        assert!(!state.active.load(atomic::Ordering::Acquire));
    }

    #[test]
    fn retirement_waits_for_an_in_progress_enqueue() {
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let shared = Arc::new(WakeShared::new(
            Arc::new(Mutex::new(VecDeque::with_capacity(1))),
            Arc::new(AtomicBool::new(false)),
            Waker::noop().clone(),
        ));
        let state = WakerState::new(Arc::downgrade(&shared), fake_task_ref);
        let active_checked = Barrier::new(2);
        let resume_enqueue = Barrier::new(2);

        thread::scope(|scope| {
            let wake_state = &state;
            let retire_state = &state;
            let wake_task_ref = fake_task_ref;
            let active_checked = &active_checked;
            let resume_enqueue = &resume_enqueue;
            let (deactivated_tx, deactivated_rx) = mpsc::channel();
            let (retire_done_tx, retire_done_rx) = mpsc::channel();
            let wake = scope.spawn({
                let shared = Arc::clone(&shared);
                move || {
                    enqueue_wake(&shared.awakened, &wake_state.queued, wake_task_ref, Waker::noop(), || {
                        let active = wake_state.active.load(atomic::Ordering::Acquire);
                        active_checked.wait();
                        resume_enqueue.wait();
                        active
                    })
                }
            });

            active_checked.wait();
            let retire = scope.spawn(move || {
                retire_state.retire_with(|active| {
                    deactivated_tx.send(active).unwrap();
                });
                retire_done_tx.send(()).unwrap();
            });
            assert!(!deactivated_rx.recv().unwrap());
            assert_eq!(retire_done_rx.try_recv(), Err(mpsc::TryRecvError::Empty));
            resume_enqueue.wait();

            assert!(wake.join().unwrap());
            retire.join().unwrap();
            retire_done_rx.recv().unwrap();
        });

        assert!(shared.awakened.lock().unwrap().is_empty());
        assert!(!state.queued.load(atomic::Ordering::Acquire));
    }

    #[test]
    fn dropping_an_active_signal_retires_retained_wakers() {
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let awakened_queue = Arc::new(Mutex::new(VecDeque::with_capacity(1)));
        let shared = Arc::new(WakeShared {
            awakened: Arc::clone(&awakened_queue),
            probe_embedded_wake_signals: Arc::new(AtomicBool::new(false)),
            parent_waker: Waker::noop().clone(),
            waker_states: Mutex::new(Pool::builder().chunk_size(1).max_chunks(1).build()),
        });
        let retained = {
            let signal = pin!(WakeSignal::new(Arc::clone(&shared), fake_task_ref));
            // SAFETY: The signal remains pinned until the clone owns independent state.
            unsafe { signal.as_ref().waker() }
        };

        retained.wake_by_ref();
        assert!(awakened_queue.lock().unwrap().is_empty());
        drop(retained);

        let state = WakerState::new(Arc::downgrade(&shared), fake_task_ref);
        drop(shared.waker_states.lock().unwrap().try_alloc_box(state).unwrap());
    }

    #[test]
    fn final_state_reference_returns_its_pool_slot() {
        let pool = Pool::builder().chunk_size(1).max_chunks(1).build();
        let shared = Arc::new(WakeShared::new(
            Arc::new(Mutex::new(VecDeque::new())),
            Arc::new(AtomicBool::new(false)),
            Waker::noop().clone(),
        ));
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let state = || WakerState::new(Arc::downgrade(&shared), fake_task_ref);

        drop(WakerStateRef::from_pool_box(pool.alloc_box(state())));

        drop(pool.try_alloc_box(state()).unwrap());
    }

    #[test]
    fn wake_state_debug_reports_lifecycle_counts() {
        let signal = pin!(WakeSignal::fake());
        let debug = format!("{:?}", signal.state());

        assert!(debug.contains("active"));
        assert!(debug.contains("waker_count"));
        assert!(debug.contains("queued"));
    }

    #[test]
    fn inactive_notification_is_not_queued() {
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let queue = Arc::new(Mutex::new(VecDeque::with_capacity(1)));
        let queued = AtomicBool::new(false);

        assert!(enqueue_wake(&queue, &queued, fake_task_ref, Waker::noop(), || false));
        assert!(queue.lock().unwrap().is_empty());
        assert!(!queued.load(atomic::Ordering::Relaxed));
    }

    #[test]
    fn untracked_notification_is_not_removed() {
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let queue = Arc::new(Mutex::new(VecDeque::from([fake_task_ref])));
        let queued = AtomicBool::new(false);

        remove_queued_notification(&queue, &queued, fake_task_ref);

        assert_eq!(queue.lock().unwrap().len(), 1);
    }

    #[test]
    fn tracked_notification_is_removed() {
        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let queue = Arc::new(Mutex::new(VecDeque::from([fake_task_ref])));
        let queued = AtomicBool::new(true);

        remove_queued_notification(&queue, &queued, fake_task_ref);

        assert!(queue.lock().unwrap().is_empty());
        assert!(!queued.load(atomic::Ordering::Relaxed));
    }

    #[test]
    fn waker_count_reaches_limit() {
        let count = AtomicUsize::new(MAX_WAKER_COUNT - 1);

        increment_waker_count(&count, || panic!("unexpected waker count overflow"));

        assert_eq!(count.load(atomic::Ordering::Relaxed), MAX_WAKER_COUNT);
    }

    #[test]
    fn waker_count_overflow_is_rejected() {
        for initial in [MAX_WAKER_COUNT, MAX_WAKER_COUNT + 1] {
            let count = AtomicUsize::new(initial);

            testing_aids::assert_panic!(increment_waker_count(&count, || panic!("waker count overflow")));

            assert_eq!(count.load(atomic::Ordering::Relaxed), initial + 1);
        }
    }

    #[test]
    fn reference_count_overflow_is_rejected() {
        let count = AtomicUsize::new(MAX_WAKER_COUNT);

        testing_aids::assert_panic!(increment_reference_count(&count, || panic!("reference count overflow")));

        assert_eq!(count.load(atomic::Ordering::Relaxed), MAX_WAKER_COUNT + 1);
    }

    #[test]
    fn null_borrowed_signal_pointer_is_rejected() {
        testing_aids::assert_panic!(resurrect_signal_ref(std::ptr::null()));
    }

    #[test]
    fn awaken_via_embedded_signal() {
        // SAFETY: We can use it as a placeholder value but not actually dereference it.
        // This is fine because `WakeSignal` only uses the `TaskRef`, not the task behind it.
        let fake_task_ref = unsafe { TaskRef::fake() };

        let awakened_queue = Arc::new(Mutex::new(VecDeque::with_capacity(10)));
        let probe_embedded_wake_signals = Arc::new(AtomicBool::new(false));
        let parent_waker = Arc::new(TestWaker::new());

        // We hold the lock - the signal cannot use the set.
        let awakened_set_lock_guard = awakened_queue.lock().unwrap();

        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::clone(&probe_embedded_wake_signals),
            Arc::clone(&parent_waker).into(),
            fake_task_ref
        ));

        // WakeSignal is only meant to be consumed via shared references.
        let signal = signal.as_ref();

        // SAFETY: The signal remains alive through the end of the test.
        // We expect that the test leaves us in this state and rely on assertions to verify it.
        let waker = unsafe { signal.waker() };

        let initially_awakened = signal.consume_awakened();
        let parent_initially_awakened = parent_waker.awakened.load(atomic::Ordering::Relaxed);

        waker.wake_by_ref();
        let embedded_probe_awakened = probe_embedded_wake_signals.load(atomic::Ordering::Relaxed);
        let awakened = signal.consume_awakened();
        let parent_awakened = parent_waker.awakened.load(atomic::Ordering::Relaxed);

        // Verify that it is now consumed.
        let awakened_after_consumption = signal.consume_awakened();
        drop(awakened_set_lock_guard);

        assert!(!initially_awakened);
        assert!(!parent_initially_awakened);
        assert!(embedded_probe_awakened);
        assert!(awakened);
        assert!(parent_awakened);
        assert!(!awakened_after_consumption);
    }

    #[test]
    fn awaken_via_awakened_set() {
        // SAFETY: We can use it as a placeholder value but not actually dereference it.
        // This is fine because `WakeSignal` only uses the `TaskRef`, not the task behind it.
        let fake_task_ref = unsafe { TaskRef::fake() };

        let awakened_queue = Arc::new(Mutex::new(VecDeque::with_capacity(10)));
        let probe_embedded_wake_signals = Arc::new(AtomicBool::new(false));
        let parent_waker = Arc::new(TestWaker::new());

        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::clone(&probe_embedded_wake_signals),
            Arc::clone(&parent_waker).into(),
            fake_task_ref
        ));

        // WakeSignal is only meant to be consumed via shared references.
        let signal = signal.as_ref();

        // SAFETY: The signal remains alive through the end of the test.
        // We expect that the test leaves us in this state and rely on assertions to verify it.
        let waker = unsafe { signal.waker() };

        assert!(!signal.consume_awakened());
        assert!(!parent_waker.awakened.load(atomic::Ordering::Relaxed));

        waker.wake_by_ref();
        // It should not have set the embedded signal here because we use the awakened set.
        assert!(!probe_embedded_wake_signals.load(atomic::Ordering::Relaxed));
        assert!(!signal.consume_awakened());
        assert!(!awakened_queue.lock().unwrap().is_empty());

        // But it should always signal the parent waker.
        assert!(parent_waker.awakened.load(atomic::Ordering::Relaxed));
    }

    #[test]
    fn awaken_via_full_awakened_set() {
        // SAFETY: We can use it as a placeholder value but not actually dereference it.
        // This is fine because `WakeSignal` only uses the `TaskRef`, not the task behind it.
        let fake_task_ref = unsafe { TaskRef::fake() };

        // Capacity is 0 so the queue is not allowed to allocate (== is never used).
        let awakened_queue = Arc::new(Mutex::new(VecDeque::new()));
        let probe_embedded_wake_signals = Arc::new(AtomicBool::new(false));
        let parent_waker = Arc::new(TestWaker::new());

        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::clone(&probe_embedded_wake_signals),
            Arc::clone(&parent_waker).into(),
            fake_task_ref
        ));

        // WakeSignal is only meant to be consumed via shared references.
        let signal = signal.as_ref();

        // SAFETY: The signal remains alive through the end of the test.
        // We expect that the test leaves us in this state and rely on assertions to verify it.
        let waker = unsafe { signal.waker() };

        assert!(!signal.consume_awakened());
        assert!(!parent_waker.awakened.load(atomic::Ordering::Relaxed));

        waker.wake_by_ref();
        // Even though it could lock the set, it could not use it because it was at capacity.
        assert!(probe_embedded_wake_signals.load(atomic::Ordering::Relaxed));
        assert!(signal.consume_awakened());
        assert!(parent_waker.awakened.load(atomic::Ordering::Relaxed));
    }

    #[test]
    fn task_storage_drop_readiness_tracks_waker_ownership() {
        // SAFETY: We can use it as a placeholder value but not actually dereference it.
        // This is fine because `WakeSignal` only uses the `TaskRef`, not the task behind it.
        let fake_task_ref = unsafe { TaskRef::fake() };

        let awakened_queue = Arc::new(Mutex::new(VecDeque::with_capacity(10)));
        let probe_embedded_wake_signals = Arc::new(AtomicBool::new(false));

        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::clone(&probe_embedded_wake_signals),
            Waker::noop().clone(),
            fake_task_ref
        ));

        // WakeSignal is only meant to be consumed via shared references.
        let signal = signal.as_ref();

        // SAFETY: The signal remains alive through the end of the test.
        // We expect that the test leaves us in this state and rely on assertions to verify it.
        let waker = unsafe { signal.waker() };

        // A waker now exists, so it cannot be inert.
        assert!(!signal.can_drop_task_storage());

        let waker_clone = waker.clone();

        // A second one, just for extra measure.
        assert!(!signal.can_drop_task_storage());
        assert_eq!(signal.external_waker_count(), 2);

        // Drop the wakers and we are inert again.
        drop(waker);
        drop(waker_clone);

        assert_eq!(signal.external_waker_count(), 0);
        assert!(signal.can_drop_task_storage());

        // And back to having wakers!

        // SAFETY: The signal remains alive through the end of the test.
        // We expect that the test leaves us in this state and rely on assertions to verify it.
        let waker = unsafe { signal.waker() };

        // A waker now exists, so it cannot be inert.
        assert!(!signal.can_drop_task_storage());

        let waker_clone = waker.clone();

        // A second one, just for extra measure.
        assert!(!signal.can_drop_task_storage());
        assert_eq!(signal.external_waker_count(), 2);

        // Drop the wakers and we are inert again.
        drop(waker);
        drop(waker_clone);

        assert_eq!(signal.external_waker_count(), 0);
        assert!(signal.can_drop_task_storage());
    }

    #[test]
    fn consuming_waker_wakes_signal() {
        // SAFETY: We can use it as a placeholder value but not actually dereference it.
        let fake_task_ref = unsafe { TaskRef::fake() };

        let awakened_queue = Arc::new(Mutex::new(VecDeque::with_capacity(1)));
        let probe_embedded_wake_signals = Arc::new(AtomicBool::new(false));

        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::clone(&probe_embedded_wake_signals),
            Waker::noop().clone(),
            fake_task_ref
        ));
        let signal = signal.as_ref();

        // SAFETY: The consuming wake releases the only waker before the signal is dropped.
        let waker = unsafe { signal.waker() };
        waker.wake();

        assert_eq!(signal.external_waker_count(), 0);
        assert!(!awakened_queue.lock().unwrap().is_empty());
        assert!(signal.can_drop_task_storage());
    }

    #[test]
    fn consuming_waker_releases_ownership_when_owner_panics() {
        struct PanicWake;

        impl std::task::Wake for PanicWake {
            fn wake(self: Arc<Self>) {
                panic!("owner wake panic");
            }
        }

        // SAFETY: Tests use this only as an opaque identity.
        let fake_task_ref = unsafe { TaskRef::fake() };
        let awakened_queue = Arc::new(Mutex::new(VecDeque::with_capacity(1)));
        let signal = pin!(WakeSignal::new_with_parts(
            Arc::clone(&awakened_queue),
            Arc::new(AtomicBool::new(false)),
            Waker::from(Arc::new(PanicWake)),
            fake_task_ref
        ));
        let signal = signal.as_ref();

        // SAFETY: The signal remains pinned through the consuming wake.
        let waker = unsafe { signal.waker() };
        testing_aids::assert_panic!(waker.wake());

        assert_eq!(signal.external_waker_count(), 0);
        assert!(signal.can_drop_task_storage());
    }
}