proxima-core 0.1.0

Foundation primitives for the proxima workspace: error, ring, arena, time, markers
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
//! Sticky one-shot level signal — the cancellation / shutdown scope
//! primitive.
//!
//! A [`Signal`] is a *level*, not an event: it transitions once
//! (unfired → fired) and stays fired, so a late observer polls an
//! already-fired signal and resolves immediately — the
//! notify-before-wait lost-wakeup class cannot happen by construction.
//!
//! Scopes nest by merge, not by tree: [`Signal::child`] observes every
//! ancestor level plus its own, so firing a parent cancels the whole
//! subtree while firing a child leaves the parent untouched. A handle
//! is a flat `Vec<Arc<Level>>` (depth = real scope nesting: process →
//! listener → connection → request), `is_fired` is one relaxed pass of
//! atomic loads, and there is no node graph to refcount.

// Only the racy primitives (the fired flag + the waiters mutex) swap to
// loom's instrumented equivalents under `--features loom`, matching
// `ring/mpsc.rs`'s own convention (see that module's doc comment): loom
// explores interleavings of the types it instruments, so `Arc` — plain
// refcounting, not part of the fire/register/drop race — stays `std::sync::Arc`
// unconditionally. Rewiring it to `loom::sync::Arc` would also break the
// `Arc<[Arc<Level>]>` unsized-slice construction (`Arc::from([..])`), which
// loom's mock `Arc` does not support (no `CoerceUnsized`; see
// `loom::sync::Arc::from_std`'s doc comment) — a real API gap, not a choice.
#[cfg(feature = "loom")]
use loom::sync::Mutex;
#[cfg(feature = "loom")]
use loom::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
#[cfg(not(feature = "loom"))]
use std::sync::Mutex;
#[cfg(not(feature = "loom"))]
use std::sync::atomic::{AtomicBool, Ordering};
use std::task::{Context, Poll, Waker};

use core::future::Future;
use core::pin::Pin;

use futures::task::AtomicWaker;
use smallvec::SmallVec;

/// One awaiting observer's waker cell. Each [`Fired`] future owns one slot
/// and registers it with every unfired level; re-polls update the slot in
/// place, so a select loop re-polling never grows the registries.
///
/// Backed by [`AtomicWaker`] (from `futures`, RISC reuse per principle 1)
/// rather than a hand-rolled `Mutex<Option<Waker>>`: it is lock-free (no
/// blocking mutex on the register/wake path, per principle 21) and its
/// `register`-then-caller-checks-again contract is the exact same
/// lost-wakeup-avoidance shape `Fired::poll` already implements.
struct WakerSlot(AtomicWaker);

impl WakerSlot {
    fn new() -> Self {
        Self(AtomicWaker::new())
    }

    fn register(&self, waker: &Waker) {
        self.0.register(waker);
    }

    fn wake(&self) {
        self.0.wake();
    }
}

/// A level's set of pending waiters. Inline-capacity `SmallVec` (cap sized by
/// `sized::SIGNAL_WAITERS_INLINE_CAP`, principle 12): the common case — a
/// handful of concurrent `Fired` futures awaiting one scope level — never
/// touches the allocator, in place of the previous unconditionally-heap Vec.
/// A burst past the inline cap spills to a heap-backed tail, same as Vec
/// always did — no waiter is ever refused registration.
type WaiterRegistry = SmallVec<[Arc<WakerSlot>; crate::sized::SIGNAL_WAITERS_INLINE_CAP]>;

struct Level {
    fired: AtomicBool,
    waiters: Mutex<WaiterRegistry>,
}

impl Level {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            fired: AtomicBool::new(false),
            waiters: Mutex::new(SmallVec::new()),
        })
    }

    // a panic while some other thread held the registry poisons it, but the
    // registry is just a list of waker slots — structurally intact either way.
    // Recovering through into_inner is what keeps a poisoned lock from turning
    // a shutdown signal into a silent hang. Same handling as time::drivers::mock.
    fn waiters(&self) -> impl core::ops::DerefMut<Target = WaiterRegistry> + '_ {
        self.waiters.lock().unwrap_or_else(|err| err.into_inner())
    }

    fn fire(&self) {
        if self.fired.swap(true, Ordering::AcqRel) {
            return;
        }
        let drained = core::mem::take(&mut *self.waiters());
        for slot in drained {
            slot.wake();
        }
    }

    fn is_fired(&self) -> bool {
        self.fired.load(Ordering::Acquire)
    }
}

/// Cloneable handle onto a scope's cancellation level. See the module
/// docs for the level/merge model.
///
/// The level list is a shared slice, not an owned `Vec`: clone (the
/// hot per-request/per-record operation) is one atomic increment and
/// zero allocation; only `new`/`child` (scope creation) allocate.
#[derive(Clone)]
pub struct Signal {
    levels: Arc<[Arc<Level>]>,
}

impl Signal {
    #[must_use]
    pub fn new() -> Self {
        Self {
            levels: Arc::from([Level::new()]),
        }
    }

    /// A nested scope: observes every ancestor level plus a fresh one
    /// of its own. Firing the child never touches the ancestors.
    #[must_use]
    pub fn child(&self) -> Self {
        let mut levels = Vec::with_capacity(self.levels.len() + 1);
        levels.extend(self.levels.iter().cloned());
        levels.push(Level::new());
        Self {
            levels: Arc::from(levels),
        }
    }

    /// Fire this handle's own scope level (idempotent). Ancestor
    /// levels are never fired through a child handle.
    pub fn fire(&self) {
        if let Some(own) = self.levels.last() {
            own.fire();
        }
    }

    /// True once this scope or any ancestor scope has fired.
    #[must_use]
    pub fn is_fired(&self) -> bool {
        self.levels.iter().any(|level| level.is_fired())
    }

    /// Resolves when this scope or any ancestor fires; resolves
    /// immediately if that already happened (sticky level).
    #[must_use]
    pub fn fired(&self) -> Fired {
        Fired {
            levels: self.levels.clone(),
            slot: OnceLock::new(),
        }
    }

    /// Fire-on-drop guard for tying a scope's lifetime to a value
    /// (e.g. cancel a request's subtree when its driver unwinds).
    #[must_use]
    pub fn guard(self) -> SignalGuard {
        SignalGuard {
            signal: self,
            armed: true,
        }
    }
}

impl Default for Signal {
    fn default() -> Self {
        Self::new()
    }
}

impl core::fmt::Debug for Signal {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        formatter
            .debug_struct("Signal")
            .field("depth", &self.levels.len())
            .field("fired", &self.is_fired())
            .finish()
    }
}

/// Future returned by [`Signal::fired`]. Owns its level handles, so it
/// is `'static` and can outlive the handle it came from.
pub struct Fired {
    levels: Arc<[Arc<Level>]>,
    slot: OnceLock<Arc<WakerSlot>>,
}

impl Fired {
    /// Poll through a shared reference, for an owner that must keep this
    /// registration alive across abandoned call futures (`FanIn` drops a
    /// `Pending` call every scan). The waker slot lives in `self`, so it is
    /// unregistered only when the `Fired` itself drops; a re-poll refreshes
    /// the slot's waker in place. Prefer `.await` / `Future::poll` when the
    /// future is polled to completion by one owner.
    pub fn poll_shared(&self, cx: &mut Context<'_>) -> Poll<()> {
        // register-then-check closes the lost-wakeup race: fire() sets
        // the flag before draining waiters, so a fire that slipped past
        // our registration is visible in the check below.
        let slot = self.slot.get_or_init(|| {
            let slot = Arc::new(WakerSlot::new());
            for level in self.levels.iter() {
                level.waiters().push(slot.clone());
            }
            slot
        });
        slot.register(cx.waker());
        if self.levels.iter().any(|level| level.is_fired()) {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }
}

impl Future for Fired {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        self.poll_shared(cx)
    }
}

impl Drop for Fired {
    fn drop(&mut self) {
        let Some(slot) = self.slot.take() else {
            return;
        };
        for level in self.levels.iter() {
            level
                .waiters()
                .retain(|candidate| !Arc::ptr_eq(candidate, &slot));
        }
    }
}

/// Fires the wrapped signal's own level on drop, unless disarmed.
pub struct SignalGuard {
    signal: Signal,
    armed: bool,
}

impl SignalGuard {
    #[must_use]
    pub fn signal(&self) -> &Signal {
        &self.signal
    }

    /// Consume the guard without firing — the success path, where the
    /// scope outlives the guard. Returns the signal untouched.
    pub fn disarm(mut self) -> Signal {
        self.armed = false;
        self.signal.clone()
    }
}

impl Drop for SignalGuard {
    fn drop(&mut self) {
        if self.armed {
            self.signal.fire();
        }
    }
}

// the fired flag + waiters mutex are cfg-swapped to loom under `--features
// loom` (see the note at the top of this module) — those only work inside an
// actual loom::model(...) closure, which these plain #[test] functions don't
// provide. Same gate ring/mpsc.rs and ring/bounded.rs already carry; the loom
// coverage for this module is tests/loom_signal.rs.
#[cfg(all(test, not(feature = "loom")))]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use std::panic::{AssertUnwindSafe, catch_unwind};
    use std::sync::atomic::AtomicUsize;
    use std::task::Wake;

    use super::*;

    struct CountingWaker(AtomicUsize);

    impl Wake for CountingWaker {
        fn wake(self: Arc<Self>) {
            self.0.fetch_add(1, Ordering::Relaxed);
        }
    }

    fn counting_waker() -> (Arc<CountingWaker>, Waker) {
        let counter = Arc::new(CountingWaker(AtomicUsize::new(0)));
        let waker = Waker::from(counter.clone());
        (counter, waker)
    }

    #[test]
    fn fire_is_sticky_and_idempotent() {
        let signal = Signal::new();
        assert!(!signal.is_fired());
        signal.fire();
        signal.fire();
        assert!(signal.is_fired());
    }

    #[test]
    fn late_observer_resolves_on_first_poll() {
        let signal = Signal::new();
        signal.fire();
        let mut fired = signal.fired();
        let (_, waker) = counting_waker();
        let mut cx = Context::from_waker(&waker);
        assert_eq!(Pin::new(&mut fired).poll(&mut cx), Poll::Ready(()));
    }

    #[test]
    fn registered_waiter_is_woken_by_fire() {
        let signal = Signal::new();
        let mut fired = signal.fired();
        let (counter, waker) = counting_waker();
        let mut cx = Context::from_waker(&waker);
        assert_eq!(Pin::new(&mut fired).poll(&mut cx), Poll::Pending);
        signal.fire();
        assert_eq!(counter.0.load(Ordering::Relaxed), 1);
        assert_eq!(Pin::new(&mut fired).poll(&mut cx), Poll::Ready(()));
    }

    #[test]
    fn child_observes_parent_fire() {
        let parent = Signal::new();
        let child = parent.child();
        parent.fire();
        assert!(child.is_fired());
    }

    #[test]
    fn parent_is_untouched_by_child_fire() {
        let parent = Signal::new();
        let child = parent.child();
        child.fire();
        assert!(child.is_fired());
        assert!(!parent.is_fired());
    }

    #[test]
    fn grandchild_observes_root_through_the_merge() {
        let root = Signal::new();
        let grandchild = root.child().child();
        root.fire();
        assert!(grandchild.is_fired());
    }

    #[test]
    fn guard_fires_own_level_on_drop() {
        let signal = Signal::new();
        let observer = signal.clone();
        {
            let _guard = signal.guard();
            assert!(!observer.is_fired());
        }
        assert!(observer.is_fired());
    }

    #[test]
    fn disarmed_guard_does_not_fire_on_drop() {
        let signal = Signal::new();
        let observer = signal.clone();
        let guard = signal.guard();
        let returned = guard.disarm();
        assert!(!observer.is_fired());
        assert!(!returned.is_fired());
    }

    #[test]
    fn dropping_a_pending_waiter_unregisters_its_slot() {
        let signal = Signal::new();
        let (_, waker) = counting_waker();
        let mut cx = Context::from_waker(&waker);
        {
            let mut fired = signal.fired();
            assert_eq!(Pin::new(&mut fired).poll(&mut cx), Poll::Pending);
            assert_eq!(signal.levels[0].waiters.lock().unwrap().len(), 1);
        }
        assert_eq!(signal.levels[0].waiters.lock().unwrap().len(), 0);
    }

    // a shutdown signal that silently declines to fire is a hang, so a panic
    // elsewhere must not be able to disarm it. The registry is only a list of
    // waker slots, so recovering the poisoned guard is sound.
    #[test]
    fn a_poisoned_waiter_registry_still_wakes_its_waiters() {
        let signal = Signal::new();
        let mut fired = signal.fired();
        let (counter, waker) = counting_waker();
        let mut cx = Context::from_waker(&waker);
        assert_eq!(Pin::new(&mut fired).poll(&mut cx), Poll::Pending);

        let poisoner = signal.clone();
        let outcome = catch_unwind(AssertUnwindSafe(|| {
            let _held = poisoner.levels[0].waiters.lock().expect("uncontended lock");
            panic!("poison the waiter registry");
        }));
        assert!(outcome.is_err(), "the poisoning closure must have unwound");
        assert!(signal.levels[0].waiters.is_poisoned());

        signal.fire();

        assert_eq!(
            counter.0.load(Ordering::Relaxed),
            1,
            "a poisoned registry must still deliver the wakeup"
        );
        assert_eq!(Pin::new(&mut fired).poll(&mut cx), Poll::Ready(()));
    }

    #[test]
    fn repolling_does_not_grow_the_waiter_list() {
        let signal = Signal::new();
        let mut fired = signal.fired();
        let (_, waker) = counting_waker();
        let mut cx = Context::from_waker(&waker);
        for _ in 0..64 {
            assert_eq!(Pin::new(&mut fired).poll(&mut cx), Poll::Pending);
        }
        assert_eq!(signal.levels[0].waiters.lock().unwrap().len(), 1);
    }
}