nexus-async-rt 0.4.3

Single-threaded async executor with pre-allocated task storage
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
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
//! Task storage: header + future/output union in a contiguous allocation.
//!
//! Each task is a `Task<F>` struct. The raw pointer to the allocation
//! IS the task handle — no index layer, no separate metadata store.
//!
//! The waker holds the raw pointer directly. `wake()` sets `QUEUED`
//! and pushes the pointer to the ready queue. Zero allocations.
//!
//! Tasks can be allocated via Box (default) or slab (power user).
//! The `free_fn` in the header knows how to deallocate regardless
//! of which allocator was used.
//!
//! ## Packed state word
//!
//! All task state (flags + refcount) is packed into a single `AtomicUsize`:
//!
//! ```text
//! bits 0-5:   flags (COMPLETED, QUEUED, HAS_JOIN, ABORTED, OUTPUT_TAKEN, SLAB_ALLOCATED)
//! bits 6+:    refcount (shifted by 6)
//! ```
//!
//! This eliminates the SIGABRT race where `Executor::drop` reads
//! `ref_count` and `is_completed` as separate atomics and a cross-thread
//! waker can decrement the refcount between those reads.
//!
//! The state word naturally converges to `TERMINAL = COMPLETED = 1`
//! when all refs are decremented and all transient flags are cleared.
//! The free check is one comparison: `state == TERMINAL`.
//!
//! ## Union storage
//!
//! The slot at `storage_offset` holds either `F` (the future) or `T` (the output),
//! never both. While running, `F` is live. When the future completes,
//! `poll_join` drops `F` in place and writes `T` to the same bytes.
//! `drop_fn` is overwritten from `drop_fn::<F>` to `drop_output::<T>`
//! so subsequent cleanup targets the correct type.

use std::cell::UnsafeCell;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use std::task::{Context, Poll, Waker};

// =============================================================================
// Packed state word — constants
// =============================================================================

/// Task has completed (future returned Ready or was aborted).
const COMPLETED: usize = 1 << 0;
/// Task is in a ready queue (dedup flag).
const QUEUED: usize = 1 << 1;
/// JoinHandle exists for this task.
const HAS_JOIN: usize = 1 << 2;
/// abort() was called.
const ABORTED: usize = 1 << 3;
/// JoinHandle consumed the output via poll.
const OUTPUT_TAKEN: usize = 1 << 4;
/// Task was allocated from the slab (permanent flag, set at spawn).
const SLAB_ALLOCATED: usize = 1 << 5;
/// Mask for all flag bits (0-5).
const FLAG_MASK: usize = 0b11_1111;
/// One reference count unit (bit 6).
const REF_ONE: usize = 1 << 6;
/// Mask for refcount bits (6+).
#[allow(dead_code)]
const REF_MASK: usize = !FLAG_MASK;

/// Lifecycle flags: must be cleared before a task can reach terminal.
/// QUEUED: someone needs to pop this task from a queue.
/// HAS_JOIN: a JoinHandle still exists and must be dropped.
const LIFECYCLE_MASK: usize = QUEUED | HAS_JOIN;

/// Inert flags: permanent metadata or historical — don't block terminal.
/// SLAB_ALLOCATED: permanent, set at spawn.
/// ABORTED: historical, set on abort — no cleanup gated on clearing it.
/// OUTPUT_TAKEN: historical, set when output read — same.
const INERT_MASK: usize = SLAB_ALLOCATED | ABORTED | OUTPUT_TAKEN;

/// What to do when a ref_dec or complete_and_unref produces a terminal state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FreeAction {
    /// Task still has outstanding refs or unchecked flags. No action.
    Retain,
    /// Box-allocated terminal. Free from any thread via free_task.
    FreeBox,
    /// Slab-allocated terminal. Route to executor thread for slab free.
    FreeSlab,
}

// =============================================================================
// Task layout
// =============================================================================

/// Header size in bytes. Must match the layout of `Task<F>` before the
/// `future` field.
pub const TASK_HEADER_SIZE: usize = 64;

/// Task header + storage in a contiguous allocation. `repr(C)` for
/// deterministic layout.
///
/// `S` is the storage type — either just `F` (fire-and-forget) or a union
/// of `F` and `T` (joinable). The header is always 64 bytes regardless of `S`.
///
/// Layout (64-bit):
/// ```text
/// offset  0: poll_fn       (8B, fn pointer — polls the future)
/// offset  8: drop_fn       (8B, fn pointer — drops F or T in place)
/// offset 16: free_fn       (8B, fn pointer — deallocates the task storage)
/// offset 24: state         (8B, AtomicUsize — packed flags + refcount)
/// offset 32: cross_next    (8B, AtomicPtr — intrusive cross-thread wake queue)
/// offset 40: join_waker    (16B, UnsafeCell<Option<Waker>>)
/// offset 56: storage_offset (2B, u16 — byte offset to storage field)
/// offset 58: _pad          (2B)
/// offset 60: tracker_key   (4B, u32 — index in Executor::all_tasks slab)
/// offset 64: storage       (S bytes — future F or union { F, T })
/// ```
#[repr(C)]
pub(crate) struct Task<S> {
    /// Polls the future. Receives the task base pointer.
    poll_fn: unsafe fn(*mut u8, &mut Context<'_>) -> Poll<()>,
    /// Drops the value at `storage_offset` (future F or output T). Receives base pointer.
    drop_fn: unsafe fn(*mut u8),
    /// Deallocates the task storage.
    free_fn: unsafe fn(*mut u8),
    /// Packed state word: flags (bits 0-5) + refcount (bits 6+).
    state: AtomicUsize,
    /// Intrusive next pointer for the cross-thread wake queue.
    cross_next: AtomicPtr<u8>,
    /// Waker for the task awaiting this JoinHandle.
    join_waker: UnsafeCell<Option<Waker>>,
    /// Byte offset from task base to the storage field.
    /// Set at construction from `offset_of!(Task<S>, storage)`.
    storage_offset: u16,
    /// Padding for alignment.
    _pad: [u8; 2],
    /// Index into the Executor's `all_tasks` slab.
    tracker_key: u32,
    storage: S,
}

/// Union storage for joinable tasks. Sized to fit both the future F
/// and the output T in the same allocation.
#[repr(C)]
pub(crate) union FutureOrOutput<F, T> {
    pub(crate) future: std::mem::ManuallyDrop<F>,
    pub(crate) output: std::mem::ManuallyDrop<T>,
}

// Static assertion: header layout matches TASK_HEADER_SIZE.
const _: () = {
    assert!(std::mem::size_of::<Task<()>>() == TASK_HEADER_SIZE);
};

impl<F: Future<Output = ()> + 'static> Task<F> {
    /// Construct a fire-and-forget task (no JoinHandle) with Box-based free.
    ///
    /// Used internally for tests and low-level task construction.
    /// `ref_count = 1` (executor only), `HAS_JOIN` not set.
    ///
    /// # Why `Output = ()` is required
    ///
    /// This uses `poll_join::<F>` which writes T at offset 64 after dropping F.
    /// The storage is `F` (not `FutureOrOutput<F, T>`), so it's only sized for F.
    /// With `T = ()` (ZST), the write is zero-size and the `drop_fn` overwrite
    /// to `drop_output::<()>` is a no-op. Relaxing this bound to non-ZST T
    /// would write T into storage not sized for it — UB.
    #[cfg(test)]
    #[inline]
    pub(crate) fn new_boxed(future: F, tracker_key: u32) -> Self {
        Self {
            poll_fn: poll_join::<F>,
            drop_fn: drop_future::<F>,
            free_fn: box_free::<F>,
            state: AtomicUsize::new(REF_ONE),
            cross_next: AtomicPtr::new(std::ptr::null_mut()),
            join_waker: UnsafeCell::new(None),
            storage_offset: std::mem::offset_of!(Task<F>, storage) as u16,
            tracker_key,
            _pad: [0; 2],
            storage: future,
        }
    }
}

/// Allocate a joinable Box task and return the raw pointer.
///
/// The task has `ref_count = 2` (executor + JoinHandle) and `HAS_JOIN` set.
/// The allocation is sized for `max(size_of::<F>(), size_of::<T>())` via
/// the `FutureOrOutput<F, T>` union.
pub(crate) fn box_spawn_joinable<F>(future: F, tracker_key: u32) -> *mut u8
where
    F: Future + 'static,
    F::Output: 'static,
{
    type Storage<F> = FutureOrOutput<F, <F as Future>::Output>;

    let task: Task<Storage<F>> = Task {
        poll_fn: poll_join::<F>,
        drop_fn: drop_future_in_union::<F>,
        free_fn: box_free::<Storage<F>>,
        state: AtomicUsize::new(HAS_JOIN | (2 * REF_ONE)),
        cross_next: AtomicPtr::new(std::ptr::null_mut()),
        join_waker: UnsafeCell::new(None),
        storage_offset: std::mem::offset_of!(Task<Storage<F>>, storage) as u16,
        tracker_key,
        _pad: [0; 2],
        storage: FutureOrOutput {
            future: std::mem::ManuallyDrop::new(future),
        },
    };
    Box::into_raw(Box::new(task)) as *mut u8
}

/// Construct a joinable task for slab allocation.
///
/// Returns the task struct to be copied into a slab slot. Uses the
/// `FutureOrOutput<F, T>` union so the allocation fits both.
pub(crate) fn new_joinable_slab<F>(
    future: F,
    tracker_key: u32,
    free_fn: unsafe fn(*mut u8),
) -> Task<FutureOrOutput<F, F::Output>>
where
    F: Future + 'static,
    F::Output: 'static,
{
    Task {
        poll_fn: poll_join::<F>,
        drop_fn: drop_future_in_union::<F>,
        free_fn,
        state: AtomicUsize::new(HAS_JOIN | SLAB_ALLOCATED | (2 * REF_ONE)),
        cross_next: AtomicPtr::new(std::ptr::null_mut()),
        join_waker: UnsafeCell::new(None),
        storage_offset: std::mem::offset_of!(Task<FutureOrOutput<F, F::Output>>, storage) as u16,
        tracker_key,
        _pad: [0; 2],
        storage: FutureOrOutput {
            future: std::mem::ManuallyDrop::new(future),
        },
    }
}

// =============================================================================
// Task handle — raw pointer operations
// =============================================================================

/// Opaque task identifier. Wraps the raw pointer to the task.
/// The pointer is stable for the task's lifetime.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct TaskId(pub(crate) *mut u8);

impl TaskId {
    /// Returns the raw pointer to the task.
    #[allow(dead_code)]
    pub(crate) fn as_ptr(&self) -> *mut u8 {
        self.0
    }
}

// =============================================================================
// JoinHandle
// =============================================================================

/// Handle to a spawned task. Await to get the result.
///
/// Dropping the handle detaches the task — it continues running but the
/// output is dropped when the task completes. Use [`abort()`](Self::abort)
/// to cancel the task.
///
/// `JoinHandle` is `!Send` and `!Sync` — it must stay on the executor thread.
#[must_use = "dropping a JoinHandle detaches the task — await it or call .abort()"]
pub struct JoinHandle<T> {
    ptr: *mut u8,
    _marker: PhantomData<T>,
    _not_send: PhantomData<*const ()>, // !Send + !Sync
}

impl<T: 'static> Future for JoinHandle<T> {
    type Output = T;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
        let ptr = self.ptr;

        // SAFETY: ptr is valid — JoinHandle holds a ref (refcount >= 1).
        if unsafe { is_completed(ptr) } {
            let s = unsafe { state_load(ptr) };
            assert!(s & ABORTED == 0, "polled JoinHandle after task was aborted");
            // SAFETY: Task completed, so poll_join already transitioned the union
            // from F to T. The output is live at storage_offset. ptr::read moves
            // it out (bitwise copy). OUTPUT_TAKEN prevents double-read.
            let output_ptr = unsafe { ptr.add(storage_offset(ptr)) };
            let value = unsafe { std::ptr::read(output_ptr.cast::<T>()) };
            unsafe { set_output_taken(ptr) };
            Poll::Ready(value)
        } else {
            // SAFETY: Task still running, single-threaded — safe to write waker.
            unsafe { set_join_waker(ptr, cx.waker().clone()) };
            Poll::Pending
        }
    }
}

impl<T> JoinHandle<T> {
    pub(crate) fn new(ptr: *mut u8) -> Self {
        Self {
            ptr,
            _marker: PhantomData,
            _not_send: PhantomData,
        }
    }

    /// Returns `true` if the task has completed (output is ready).
    pub fn is_finished(&self) -> bool {
        unsafe { is_completed(self.ptr) }
    }

    /// Abort the task and consume the handle.
    ///
    /// The future is dropped on the next poll cycle. Consumes the handle
    /// so it cannot be awaited after abort — this is enforced at the type
    /// level rather than via a runtime panic.
    ///
    /// Returns `true` if the task was still running, `false` if it had
    /// already completed (output is dropped by `JoinHandle::drop`).
    #[must_use = "returns whether the task was still running"]
    pub fn abort(self) -> bool {
        let ptr = self.ptr;
        let was_running = !unsafe { is_completed(ptr) };
        if was_running {
            unsafe { set_aborted(ptr) };
        }
        // self is consumed — Drop runs, which clears HAS_JOIN,
        // takes the join waker, and decrements refcount.
        was_running
    }
}

impl<T> Drop for JoinHandle<T> {
    fn drop(&mut self) {
        let ptr = self.ptr;
        // SAFETY: ptr is valid — JoinHandle holds a ref (refcount >= 1).
        let s = unsafe { state_load(ptr) };

        if (s & COMPLETED != 0) && (s & OUTPUT_TAKEN == 0) && (s & ABORTED == 0) {
            // Task completed but output was never read — drop it.
            // SAFETY: poll_join overwrote drop_fn to drop_output::<T>,
            // so this drops the output T (not the future F).
            unsafe { drop_task_future(ptr) };
        }

        // Clear HAS_JOIN so complete_task knows nobody is waiting.
        // Take the join waker to release the parent task's refcount.
        unsafe { clear_has_join(ptr) };
        let _ = unsafe { take_join_waker(ptr) };

        // Release our reference. If terminal, push to deferred free.
        match unsafe { ref_dec(ptr) } {
            FreeAction::Retain => {}
            FreeAction::FreeBox | FreeAction::FreeSlab => {
                // Executor thread — slab TLS available. Free via deferred path.
                unsafe { defer_free_slot(ptr) };
            }
        }
    }
}

/// Push a task to the deferred free list, or free immediately if outside poll.
///
/// # Safety
///
/// `ptr` must point to a completed task in TERMINAL state.
unsafe fn defer_free_slot(ptr: *mut u8) {
    unsafe { crate::waker::defer_free(ptr) };
}

// =============================================================================
// Packed state accessor functions
// =============================================================================

/// Get the raw state value.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
unsafe fn state_load(ptr: *mut u8) -> usize {
    // SAFETY: state is AtomicUsize at offset 24 in repr(C) Task.
    unsafe { &*ptr.add(24).cast::<AtomicUsize>() }.load(Ordering::Acquire)
}

/// Get a reference to the state atomic.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
unsafe fn state_ref(ptr: *mut u8) -> &'static AtomicUsize {
    // SAFETY: state is AtomicUsize at offset 24 in repr(C) Task.
    // 'static is a lie — the caller must not outlive the task.
    unsafe { &*ptr.add(24).cast::<AtomicUsize>() }
}

/// Read the `tracker_key` from a task pointer.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn tracker_key(ptr: *mut u8) -> u32 {
    // SAFETY: tracker_key is at offset 60 in repr(C) Task.
    unsafe { *(ptr.add(60).cast::<u32>()) }
}

/// Increment the waker refcount. Called on waker clone.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn ref_inc(ptr: *mut u8) {
    let state = unsafe { state_ref(ptr) };
    let prev = state.fetch_add(REF_ONE, Ordering::Relaxed);
    debug_assert!((prev & REF_MASK) > 0, "ref_inc on zero refcount");
}

/// Decrement the refcount. Returns `FreeAction` indicating whether
/// a terminal state was produced and what kind of allocation it is.
///
/// # Safety
///
/// `ptr` must point to a live (or completed) `Task<F>`.
#[inline]
pub(crate) unsafe fn ref_dec(ptr: *mut u8) -> FreeAction {
    let state = unsafe { state_ref(ptr) };
    let prev = state.fetch_sub(REF_ONE, Ordering::AcqRel);
    debug_assert!((prev & REF_MASK) >= REF_ONE, "ref_dec on zero refcount");

    // Was this the last ref?
    if (prev & REF_MASK) != REF_ONE {
        return FreeAction::Retain;
    }

    // Last ref. Check: COMPLETED must be set, lifecycle flags must be clear.
    // ABORTED, OUTPUT_TAKEN, SLAB_ALLOCATED are inert — don't block terminal.
    let flags = prev & FLAG_MASK;
    if (flags & COMPLETED == 0) || (flags & LIFECYCLE_MASK != 0) {
        return FreeAction::Retain;
    }
    if flags & SLAB_ALLOCATED != 0 {
        FreeAction::FreeSlab
    } else {
        FreeAction::FreeBox
    }
}

/// Read the refcount.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn ref_count(ptr: *mut u8) -> usize {
    (unsafe { state_load(ptr) } & REF_MASK) >> 6
}

/// Atomically set COMPLETED and decrement the executor's reference.
/// Returns `FreeAction` indicating whether a terminal state was produced.
///
/// This is the key atomic operation that eliminates the race between
/// `set_completed` and `ref_dec` that caused the SIGABRT.
///
/// # Safety
///
/// `ptr` must point to a live, not-yet-completed `Task<F>`.
#[inline]
pub(crate) unsafe fn complete_and_unref(ptr: *mut u8) -> FreeAction {
    let state = unsafe { state_ref(ptr) };
    // Atomically: set COMPLETED (add 1 to bit 0) + dec refcount (sub REF_ONE)
    // Net subtraction = REF_ONE - COMPLETED.
    let prev = state.fetch_sub(REF_ONE - COMPLETED, Ordering::AcqRel);
    debug_assert!(prev & COMPLETED == 0, "double complete");
    debug_assert!(
        (prev & REF_MASK) >= REF_ONE,
        "complete_and_unref on zero refcount"
    );
    // prev had COMPLETED=0. Last ref if prev had exactly REF_ONE.
    // Lifecycle flags (QUEUED, HAS_JOIN) must be clear.
    // Inert flags (ABORTED, OUTPUT_TAKEN, SLAB_ALLOCATED) don't matter.
    if (prev & REF_MASK) != REF_ONE {
        return FreeAction::Retain;
    }
    let flags = prev & FLAG_MASK;
    if flags & LIFECYCLE_MASK != 0 {
        return FreeAction::Retain;
    }
    if flags & SLAB_ALLOCATED != 0 {
        FreeAction::FreeSlab
    } else {
        FreeAction::FreeBox
    }
}

/// Check if the state is TERMINAL (safe to free).
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn is_terminal(ptr: *mut u8) -> bool {
    let s = unsafe { state_load(ptr) };
    // Strip inert flags (SLAB_ALLOCATED, ABORTED, OUTPUT_TAKEN).
    // What remains must be exactly COMPLETED with zero refcount.
    (s & !INERT_MASK) == COMPLETED
}

/// Read the is_completed flag.
///
/// # Safety
///
/// `ptr` must point to a (possibly completed) `Task<F>`.
#[inline]
pub(crate) unsafe fn is_completed(ptr: *mut u8) -> bool {
    (unsafe { state_load(ptr) }) & COMPLETED != 0
}

/// Read the is_queued flag.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn is_queued(ptr: *mut u8) -> bool {
    (unsafe { state_load(ptr) }) & QUEUED != 0
}

/// Set the `is_queued` flag.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn set_queued(ptr: *mut u8, queued: bool) {
    let state = unsafe { state_ref(ptr) };
    if queued {
        state.fetch_or(QUEUED, Ordering::Release);
    } else {
        state.fetch_and(!QUEUED, Ordering::Release);
    }
}

/// Atomically try to set QUEUED from false to true. Returns true if
/// successful (was not queued). Used by cross-thread wakers.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn try_set_queued(ptr: *mut u8) -> bool {
    let state = unsafe { state_ref(ptr) };
    // fetch_or always sets the bit. Check if it was already set.
    let prev = state.fetch_or(QUEUED, Ordering::AcqRel);
    (prev & QUEUED) == 0
}

/// Clear the QUEUED flag.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn clear_queued(ptr: *mut u8) {
    let state = unsafe { state_ref(ptr) };
    state.fetch_and(!QUEUED, Ordering::Release);
}

/// Check if ABORTED flag is set.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn is_aborted(ptr: *mut u8) -> bool {
    (unsafe { state_load(ptr) }) & ABORTED != 0
}

/// Set the ABORTED flag.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn set_aborted(ptr: *mut u8) {
    let state = unsafe { state_ref(ptr) };
    state.fetch_or(ABORTED, Ordering::Release);
}

/// Check if HAS_JOIN flag is set.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn has_join(ptr: *mut u8) -> bool {
    (unsafe { state_load(ptr) }) & HAS_JOIN != 0
}

/// Clear the HAS_JOIN flag.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn clear_has_join(ptr: *mut u8) {
    let state = unsafe { state_ref(ptr) };
    state.fetch_and(!HAS_JOIN, Ordering::Release);
}

/// Set the OUTPUT_TAKEN flag.
///
/// # Safety
///
/// `ptr` must point to a live, completed `Task<F>`. Single-threaded.
#[inline]
unsafe fn set_output_taken(ptr: *mut u8) {
    let state = unsafe { state_ref(ptr) };
    state.fetch_or(OUTPUT_TAKEN, Ordering::Release);
}

/// Get a raw pointer to the `cross_next` atomic pointer.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
#[inline]
pub(crate) unsafe fn cross_next(ptr: *mut u8) -> *const AtomicPtr<u8> {
    // SAFETY: cross_next is at offset 32 in repr(C) Task.
    unsafe { ptr.add(32).cast::<AtomicPtr<u8>>() }
}

/// Read the storage offset from the task header.
///
/// # Safety
///
/// `ptr` must point to a live `Task<S>`.
#[inline]
pub(crate) unsafe fn storage_offset(ptr: *mut u8) -> usize {
    // SAFETY: storage_offset is u16 at offset 56 in repr(C) Task.
    unsafe { *(ptr.add(56).cast::<u16>()) as usize }
}

/// Store a waker for the JoinHandle awaiter.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`. Single-threaded access only.
#[inline]
unsafe fn set_join_waker(ptr: *mut u8, waker: Waker) {
    // SAFETY: join_waker is UnsafeCell<Option<Waker>> at offset 40.
    let cell = unsafe { &*ptr.add(40).cast::<UnsafeCell<Option<Waker>>>() };
    unsafe { *cell.get() = Some(waker) };
}

/// Take the join waker (if any).
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`. Single-threaded access only.
#[inline]
pub(crate) unsafe fn take_join_waker(ptr: *mut u8) -> Option<Waker> {
    let cell = unsafe { &*ptr.add(40).cast::<UnsafeCell<Option<Waker>>>() };
    unsafe { (*cell.get()).take() }
}

/// Poll the task's future.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`.
/// The future must not have been dropped.
#[inline]
pub(crate) unsafe fn poll_task(ptr: *mut u8, cx: &mut Context<'_>) -> Poll<()> {
    // SAFETY: poll_fn is at offset 0 in repr(C) Task.
    let poll_fn: unsafe fn(*mut u8, &mut Context<'_>) -> Poll<()> =
        unsafe { *(ptr as *const unsafe fn(*mut u8, &mut Context<'_>) -> Poll<()>) };
    // Pass the task base pointer — the trampoline reads storage_offset.
    unsafe { poll_fn(ptr, cx) }
}

/// Drop the task's future (or output) in place.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`. Must only be called once.
#[inline]
pub(crate) unsafe fn drop_task_future(ptr: *mut u8) {
    // SAFETY: drop_fn is at offset 8 in repr(C) Task.
    let drop_fn: unsafe fn(*mut u8) = unsafe { *(ptr.add(8) as *const unsafe fn(*mut u8)) };
    // Pass base pointer — the trampoline reads storage_offset.
    unsafe { drop_fn(ptr) }
}

/// Call the task's free function to deallocate its storage.
///
/// # Safety
///
/// `ptr` must point to a `Task<F>` whose future has already been dropped.
/// Must only be called once (after state reaches TERMINAL).
#[inline]
pub(crate) unsafe fn free_task(ptr: *mut u8) {
    // SAFETY: free_fn is at offset 16 in repr(C) Task.
    let free_fn: unsafe fn(*mut u8) = unsafe { *(ptr.add(16) as *const unsafe fn(*mut u8)) };
    unsafe { free_fn(ptr) }
}

// =============================================================================
// Type-erased vtable functions
// =============================================================================

/// Poll trampoline for joinable tasks (Output = T).
///
/// On completion: drops F, writes T into the same location, overwrites
/// drop_fn to target T instead of F.
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>`. The future must not have been dropped.
unsafe fn poll_join<F: Future>(ptr: *mut u8, cx: &mut Context<'_>) -> Poll<()>
where
    F::Output: 'static,
{
    // Check if aborted
    if unsafe { is_aborted(ptr) } {
        return Poll::Ready(());
    }

    let future_ptr = unsafe { ptr.add(storage_offset(ptr)) };
    let future = unsafe { Pin::new_unchecked(&mut *future_ptr.cast::<F>()) };
    match future.poll(cx) {
        Poll::Pending => Poll::Pending,
        Poll::Ready(value) => {
            let drop_fn_slot = unsafe { ptr.add(8).cast::<unsafe fn(*mut u8)>() };
            // 1. Overwrite drop_fn to no-op BEFORE dropping F.
            //    If F::drop() panics, this prevents double-drop —
            //    subsequent cleanup calls the no-op instead of
            //    drop_future_in_union on a partially-dropped F.
            //    The output (value) is dropped during unwind (stack-owned).
            unsafe { *drop_fn_slot = drop_noop };
            // 2. Drop the future in place (panic-safe now)
            unsafe { std::ptr::drop_in_place(future_ptr.cast::<F>()) };
            // 3. Write output T into the same location
            unsafe { std::ptr::write(future_ptr.cast::<F::Output>(), value) };
            // 4. Overwrite drop_fn: now drops T instead of F
            unsafe { *drop_fn_slot = drop_output::<F::Output> };
            Poll::Ready(())
        }
    }
}

/// Drop trampoline for futures stored directly (fire-and-forget tasks).
///
/// # Safety
///
/// `ptr` must point to a live `Task<F>` with a live future at `storage_offset`.
#[cfg(test)]
unsafe fn drop_future<F>(ptr: *mut u8) {
    let future_ptr = unsafe { ptr.add(storage_offset(ptr)) };
    unsafe { std::ptr::drop_in_place(future_ptr.cast::<F>()) }
}

/// Drop trampoline for futures stored in FutureOrOutput union.
///
/// # Safety
///
/// `ptr` must point to a `Task<FutureOrOutput<F, T>>` with a live future.
unsafe fn drop_future_in_union<F: Future>(ptr: *mut u8) {
    let storage_ptr = unsafe { ptr.add(storage_offset(ptr)) };
    // The future is at the start of the union (same offset as the union itself).
    unsafe { std::ptr::drop_in_place(storage_ptr.cast::<F>()) }
}

/// No-op drop trampoline. Installed temporarily during the F→T transition
/// in `poll_join` to prevent double-drop if `F::drop()` panics.
///
/// # Safety
///
/// Always safe — does nothing.
unsafe fn drop_noop(_ptr: *mut u8) {}

/// Drop trampoline for output values. Receives the task base pointer.
///
/// Installed by `poll_join` after the future completes, replacing `drop_future`.
///
/// # Safety
///
/// `ptr` must point to a `Task` with a live `T` at `storage_offset`.
unsafe fn drop_output<T>(ptr: *mut u8) {
    let output_ptr = unsafe { ptr.add(storage_offset(ptr)) };
    unsafe { std::ptr::drop_in_place(output_ptr.cast::<T>()) }
}

/// Free function for Box-allocated tasks.
///
/// Deallocates the memory without running destructors — the future/output
/// was already dropped via `drop_task_future`, and the header fields
/// are all trivial. Only the heap allocation needs to be freed.
///
/// # Safety
///
/// `ptr` must have been produced by `Box::into_raw(Box::new(Task<F>))`.
/// The value at offset 64 must already be dropped.
unsafe fn box_free<F>(ptr: *mut u8) {
    // SAFETY: Layout matches what Box::new(Task<F>) allocated.
    let layout = std::alloc::Layout::new::<Task<F>>();
    unsafe { std::alloc::dealloc(ptr, layout) }
}

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

    #[test]
    fn task_header_size() {
        assert_eq!(TASK_HEADER_SIZE, 64);
        assert_eq!(std::mem::size_of::<Task<()>>(), 64);
    }

    #[test]
    fn task_layout_offsets() {
        assert_eq!(std::mem::offset_of!(Task<()>, poll_fn), 0);
        assert_eq!(std::mem::offset_of!(Task<()>, drop_fn), 8);
        assert_eq!(std::mem::offset_of!(Task<()>, free_fn), 16);
        assert_eq!(std::mem::offset_of!(Task<()>, state), 24);
        assert_eq!(std::mem::offset_of!(Task<()>, cross_next), 32);
        assert_eq!(std::mem::offset_of!(Task<()>, join_waker), 40);
        assert_eq!(std::mem::offset_of!(Task<()>, storage_offset), 56);
        assert_eq!(std::mem::offset_of!(Task<()>, _pad), 58);
        assert_eq!(std::mem::offset_of!(Task<()>, tracker_key), 60);
        assert_eq!(std::mem::offset_of!(Task<()>, storage), 64);
    }

    #[test]
    fn task_size_with_future() {
        #[allow(dead_code)]
        struct SmallFuture([u8; 24]);
        impl Future for SmallFuture {
            type Output = ();
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
                Poll::Ready(())
            }
        }

        // 64 byte header + 24 byte future = 88 bytes
        assert_eq!(
            std::mem::size_of::<Task<SmallFuture>>(),
            TASK_HEADER_SIZE + 24
        );
    }

    #[test]
    fn packed_state_fire_and_forget() {
        struct Noop;
        impl Future for Noop {
            type Output = ();
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
                Poll::Ready(())
            }
        }

        let task = Box::new(Task::new_boxed(Noop, 0));
        let ptr = Box::into_raw(task) as *mut u8;

        unsafe {
            // Initial state: 1 ref, no flags
            assert_eq!(ref_count(ptr), 1);
            assert!(!is_completed(ptr));
            assert!(!is_queued(ptr));
            assert!(!has_join(ptr));
            assert!(!is_terminal(ptr));

            // Set and clear queued
            set_queued(ptr, true);
            assert!(is_queued(ptr));
            set_queued(ptr, false);
            assert!(!is_queued(ptr));

            // complete_and_unref with 1 ref → TERMINAL
            drop_task_future(ptr);
            assert!(matches!(complete_and_unref(ptr), FreeAction::FreeBox));
            assert!(is_terminal(ptr));

            free_task(ptr);
        }
    }

    #[test]
    fn packed_state_joinable() {
        struct Noop;
        impl Future for Noop {
            type Output = u64;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
                Poll::Ready(42)
            }
        }

        let ptr = box_spawn_joinable(Noop, 7);
        unsafe {
            assert!(has_join(ptr));
            assert!(!is_aborted(ptr));
            assert_eq!(ref_count(ptr), 2); // executor + JoinHandle
            assert_eq!(tracker_key(ptr), 7);

            // Simulate: handle drops before completion
            clear_has_join(ptr);
            assert!(!has_join(ptr));
            assert!(matches!(ref_dec(ptr), FreeAction::Retain)); // still 1 ref, not completed

            // complete_and_unref → TERMINAL
            drop_task_future(ptr);
            assert!(matches!(complete_and_unref(ptr), FreeAction::FreeBox));
            assert!(is_terminal(ptr));

            free_task(ptr);
        }
    }

    #[test]
    fn packed_state_joinable_completion_before_handle_drop() {
        struct Noop;
        impl Future for Noop {
            type Output = u64;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
                Poll::Ready(42)
            }
        }

        let ptr = box_spawn_joinable(Noop, 0);
        unsafe {
            // complete_and_unref with 2 refs → not terminal
            drop_task_future(ptr);
            assert!(matches!(complete_and_unref(ptr), FreeAction::Retain));
            assert!(is_completed(ptr));
            assert_eq!(ref_count(ptr), 1);

            // Handle drop: clear HAS_JOIN, ref_dec → TERMINAL
            clear_has_join(ptr);
            assert!(matches!(ref_dec(ptr), FreeAction::FreeBox));
            assert!(is_terminal(ptr));

            free_task(ptr);
        }
    }

    #[test]
    fn packed_state_cross_thread_waker_scenario() {
        struct Noop;
        impl Future for Noop {
            type Output = u64;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
                Poll::Ready(42)
            }
        }

        let ptr = box_spawn_joinable(Noop, 0);
        unsafe {
            // Waker clone: ref_inc
            ref_inc(ptr);
            assert_eq!(ref_count(ptr), 3);

            // complete_and_unref: executor releases its ref
            drop_task_future(ptr);
            assert!(matches!(complete_and_unref(ptr), FreeAction::Retain));

            // Handle drop: clear HAS_JOIN, ref_dec
            clear_has_join(ptr);
            assert!(matches!(ref_dec(ptr), FreeAction::Retain)); // still 1 ref (waker)

            // Waker drop: ref_dec → TERMINAL
            assert!(matches!(ref_dec(ptr), FreeAction::FreeBox));
            assert!(is_terminal(ptr));

            free_task(ptr);
        }
    }

    // =========================================================================
    // Panic safety — drop_fn transitions
    // =========================================================================

    /// Future whose Drop impl panics. Used to verify the drop_noop guard
    /// in poll_join prevents double-drop.
    struct PanickingDrop {
        drop_count: *mut u32,
    }

    impl Future for PanickingDrop {
        type Output = u64;
        fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
            Poll::Ready(42)
        }
    }

    impl Drop for PanickingDrop {
        fn drop(&mut self) {
            unsafe { *self.drop_count += 1 };
            panic!("intentional drop panic");
        }
    }

    #[test]
    fn poll_join_panic_in_drop_prevents_double_drop() {
        use std::task::{RawWaker, RawWakerVTable, Waker};

        static NOOP_VTABLE: RawWakerVTable =
            RawWakerVTable::new(|p| RawWaker::new(p, &NOOP_VTABLE), |_| {}, |_| {}, |_| {});
        let waker = unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &NOOP_VTABLE)) };
        let mut cx = Context::from_waker(&waker);

        let mut drop_count: u32 = 0;
        let ptr = box_spawn_joinable(
            PanickingDrop {
                drop_count: &raw mut drop_count,
            },
            0,
        );

        // poll_join completes the future, then drops F — which panics.
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| unsafe {
            poll_task(ptr, &mut cx)
        }));

        // The panic should have been caught.
        assert!(result.is_err(), "expected panic from PanickingDrop");
        // F was dropped exactly once (by poll_join, before the panic propagated).
        assert_eq!(drop_count, 1, "future should be dropped exactly once");

        // drop_fn should now be drop_noop — calling it must NOT double-drop F.
        unsafe { drop_task_future(ptr) };
        assert_eq!(
            drop_count, 1,
            "drop_task_future after panic must be a no-op (drop_noop)"
        );

        // Clean up: dec both refs (executor + JoinHandle), then free.
        unsafe {
            ref_dec(ptr);
            ref_dec(ptr);
            free_task(ptr);
        }
    }

    #[test]
    fn drop_fn_transitions_correctly_on_normal_completion() {
        use std::task::{RawWaker, RawWakerVTable, Waker};

        static NOOP_VTABLE: RawWakerVTable =
            RawWakerVTable::new(|p| RawWaker::new(p, &NOOP_VTABLE), |_| {}, |_| {}, |_| {});
        let waker = unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &NOOP_VTABLE)) };
        let mut cx = Context::from_waker(&waker);

        static mut OUTPUT_DROP_COUNT: u32 = 0;
        struct TrackedOutput;
        impl Drop for TrackedOutput {
            fn drop(&mut self) {
                unsafe { OUTPUT_DROP_COUNT += 1 };
            }
        }

        struct ProduceTracked;
        impl Future for ProduceTracked {
            type Output = TrackedOutput;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<TrackedOutput> {
                Poll::Ready(TrackedOutput)
            }
        }

        let ptr = box_spawn_joinable(ProduceTracked, 0);

        // Poll to completion — F dropped, T written, drop_fn → drop_output.
        let result = unsafe { poll_task(ptr, &mut cx) };
        assert!(result.is_ready());

        // drop_fn should now target T (TrackedOutput).
        unsafe { OUTPUT_DROP_COUNT = 0 };
        unsafe { drop_task_future(ptr) };
        assert_eq!(
            unsafe { OUTPUT_DROP_COUNT },
            1,
            "drop_fn should drop the output exactly once"
        );

        // Clean up.
        unsafe {
            ref_dec(ptr);
            ref_dec(ptr);
            free_task(ptr);
        }
    }

    // =========================================================================
    // Packed state word — SIGABRT root cause regression tests
    // =========================================================================

    #[test]
    fn packed_state_fire_and_forget_terminal() {
        // Box task with 1 ref (no JoinHandle). complete_and_unref → FreeBox.
        // Verify terminal state is exactly TERMINAL_BOX (1).
        struct Noop;
        impl Future for Noop {
            type Output = ();
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
                Poll::Ready(())
            }
        }

        let task = Box::new(Task::new_boxed(Noop, 0));
        let ptr = Box::into_raw(task) as *mut u8;

        unsafe {
            assert_eq!(ref_count(ptr), 1);
            assert!(!has_join(ptr));

            drop_task_future(ptr);
            let action = complete_and_unref(ptr);
            assert_eq!(action, FreeAction::FreeBox);

            let s = state_load(ptr);
            assert_eq!(s, COMPLETED, "terminal state must have COMPLETED set");
            assert_eq!(s, 1);
            assert!(is_terminal(ptr));

            free_task(ptr);
        }
    }

    #[test]
    fn packed_state_slab_flag_terminal() {
        // Task with SLAB_ALLOCATED set. complete_and_unref → FreeSlab.
        // Verify terminal state is exactly TERMINAL_SLAB (33).
        struct Noop;
        impl Future for Noop {
            type Output = u64;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
                Poll::Ready(42)
            }
        }

        // Use new_joinable_slab to get SLAB_ALLOCATED flag set at construction.
        // Provide a free_fn that does Box dealloc (we box it manually below).
        type Storage = FutureOrOutput<Noop, u64>;
        unsafe fn slab_free(ptr: *mut u8) {
            let layout = std::alloc::Layout::new::<Task<Storage>>();
            std::alloc::dealloc(ptr, layout);
        }

        let task = new_joinable_slab(Noop, 0, slab_free);
        let ptr = Box::into_raw(Box::new(task)) as *mut u8;

        unsafe {
            assert_eq!(ref_count(ptr), 2); // executor + JoinHandle
            assert!(has_join(ptr));

            // Simulate handle detach: clear HAS_JOIN + ref_dec
            clear_has_join(ptr);
            assert_eq!(ref_dec(ptr), FreeAction::Retain);
            assert_eq!(ref_count(ptr), 1);

            // Executor completes task
            drop_task_future(ptr);
            let action = complete_and_unref(ptr);
            assert_eq!(action, FreeAction::FreeSlab);

            let s = state_load(ptr);
            assert_eq!(
                s,
                COMPLETED | SLAB_ALLOCATED,
                "terminal state must be COMPLETED | SLAB_ALLOCATED"
            );
            assert_eq!(s, 33);
            assert!(is_terminal(ptr));

            slab_free(ptr);
        }
    }

    #[test]
    fn packed_state_joinable_handle_drops_first() {
        // Joinable task (2 refs + HAS_JOIN). Handle drops first:
        // clear HAS_JOIN → ref_dec → 1 ref remaining.
        // Then complete_and_unref → terminal.
        struct Noop;
        impl Future for Noop {
            type Output = u64;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
                Poll::Ready(42)
            }
        }

        let ptr = box_spawn_joinable(Noop, 0);
        unsafe {
            assert_eq!(ref_count(ptr), 2);
            assert!(has_join(ptr));

            // Handle drops: clear HAS_JOIN, ref_dec
            clear_has_join(ptr);
            assert!(!has_join(ptr));
            assert_eq!(ref_dec(ptr), FreeAction::Retain);
            assert_eq!(ref_count(ptr), 1);
            assert!(!is_terminal(ptr));

            // Executor completes
            drop_task_future(ptr);
            assert_eq!(complete_and_unref(ptr), FreeAction::FreeBox);
            assert!(is_terminal(ptr));

            free_task(ptr);
        }
    }

    #[test]
    fn packed_state_joinable_completion_first_then_handle() {
        // Joinable task. Completion fires first (Retain because 2 refs).
        // Then handle clears HAS_JOIN + ref_dec → FreeBox.
        struct Noop;
        impl Future for Noop {
            type Output = u64;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
                Poll::Ready(42)
            }
        }

        let ptr = box_spawn_joinable(Noop, 0);
        unsafe {
            // complete_and_unref: sets COMPLETED, dec ref → 1 ref remains
            drop_task_future(ptr);
            assert_eq!(complete_and_unref(ptr), FreeAction::Retain);
            assert!(is_completed(ptr));
            assert_eq!(ref_count(ptr), 1);

            // Handle drops: clear HAS_JOIN, ref_dec → terminal
            clear_has_join(ptr);
            assert_eq!(ref_dec(ptr), FreeAction::FreeBox);
            assert!(is_terminal(ptr));

            free_task(ptr);
        }
    }

    #[test]
    fn packed_state_waker_clone_lifecycle() {
        // Joinable task (2 refs). Waker clone adds 3rd ref.
        // complete_and_unref → Retain (2 refs remain, HAS_JOIN still set).
        // Handle drops (clear HAS_JOIN + ref_dec) → Retain (1 ref from waker).
        // Waker drops (ref_dec) → FreeBox.
        struct Noop;
        impl Future for Noop {
            type Output = u64;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
                Poll::Ready(42)
            }
        }

        let ptr = box_spawn_joinable(Noop, 0);
        unsafe {
            // Waker clone: ref_inc
            ref_inc(ptr);
            assert_eq!(ref_count(ptr), 3);

            // Executor completes: complete_and_unref
            drop_task_future(ptr);
            assert_eq!(complete_and_unref(ptr), FreeAction::Retain);
            assert_eq!(ref_count(ptr), 2);

            // Handle drops: clear HAS_JOIN, ref_dec
            clear_has_join(ptr);
            assert_eq!(ref_dec(ptr), FreeAction::Retain);
            assert_eq!(ref_count(ptr), 1);

            // Waker drops: ref_dec → terminal
            assert_eq!(ref_dec(ptr), FreeAction::FreeBox);
            assert!(is_terminal(ptr));

            free_task(ptr);
        }
    }

    #[test]
    fn packed_state_leaked_flag_prevents_terminal() {
        // If HAS_JOIN is NOT cleared before the final ref_dec, the state
        // won't reach terminal (HAS_JOIN is a lifecycle flag that blocks it).
        // Result: Retain. This is safe — the lifecycle flag prevents
        // premature free.
        struct Noop;
        impl Future for Noop {
            type Output = u64;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
                Poll::Ready(42)
            }
        }

        let ptr = box_spawn_joinable(Noop, 0);
        unsafe {
            // complete_and_unref with 2 refs → Retain
            drop_task_future(ptr);
            assert_eq!(complete_and_unref(ptr), FreeAction::Retain);

            // ref_dec WITHOUT clearing HAS_JOIN → still not terminal
            // because HAS_JOIN is a lifecycle flag that blocks terminal.
            assert_eq!(ref_dec(ptr), FreeAction::Retain);
            assert!(!is_terminal(ptr));

            // State is COMPLETED | HAS_JOIN | 0 refs — leaked but safe.
            // In real code this can't happen (JoinHandle::Drop always
            // clears HAS_JOIN), but the packed state correctly prevents
            // a free even if it did.
            let s = state_load(ptr);
            assert_eq!(s & COMPLETED, COMPLETED);
            assert_eq!(s & HAS_JOIN, HAS_JOIN);
            assert_eq!(ref_count(ptr), 0);

            // Clean up: manually clear HAS_JOIN to reach terminal, then free.
            clear_has_join(ptr);
            assert!(is_terminal(ptr));
            free_task(ptr);
        }
    }

    #[test]
    fn packed_state_many_refs_converge() {
        // Clone waker 10 times (ref_inc 10x), complete, then ref_dec 10x.
        // Only the last ref_dec returns FreeBox. All others Retain.
        struct Noop;
        impl Future for Noop {
            type Output = u64;
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u64> {
                Poll::Ready(42)
            }
        }

        let ptr = box_spawn_joinable(Noop, 0);
        unsafe {
            // 10 waker clones: ref 2 → 12
            for _ in 0..10 {
                ref_inc(ptr);
            }
            assert_eq!(ref_count(ptr), 12);

            // Executor completes: ref 12 → 11
            drop_task_future(ptr);
            assert_eq!(complete_and_unref(ptr), FreeAction::Retain);
            assert_eq!(ref_count(ptr), 11);

            // Handle drops: clear HAS_JOIN, ref_dec → 10
            clear_has_join(ptr);
            assert_eq!(ref_dec(ptr), FreeAction::Retain);
            assert_eq!(ref_count(ptr), 10);

            // Drop 9 waker refs — all Retain
            for i in 0..9 {
                assert_eq!(
                    ref_dec(ptr),
                    FreeAction::Retain,
                    "ref_dec #{i} should Retain"
                );
            }
            assert_eq!(ref_count(ptr), 1);

            // Last waker drop → FreeBox
            assert_eq!(ref_dec(ptr), FreeAction::FreeBox);
            assert!(is_terminal(ptr));

            free_task(ptr);
        }
    }
}