hazard3-sync 0.2.0

no-std, no-alloc synchronization primitives with async support
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
use core::cell::Cell;
#[cfg(target_has_atomic = "32")]
use core::cell::UnsafeCell;
#[cfg(target_has_atomic = "32")]
use core::sync::atomic::AtomicUsize;
#[cfg(target_has_atomic = "32")]
use core::sync::atomic::Ordering::{AcqRel, Acquire, Release};
use core::task::Waker;

use crate::blocking_mutex::Mutex;
use crate::blocking_mutex::raw::RawMutex;

/// Utility struct to register and wake a waker.
/// If a waker is registered, registering another waker will replace the previous one without waking it.
/// Intended to wake a task from an interrupt. Therefore, it is generally not expected,
/// that multiple tasks register try to register a waker simultaneously.
///
/// Most users should prefer [`AtomicWaker`], which uses a small atomic state machine and
/// does **not** enter a critical section on the wake path. See also
/// [`CriticalSectionWaker`](super::CriticalSectionWaker), a non-generic convenience wrapper
/// that hard-codes [`CriticalSectionRawMutex`](crate::blocking_mutex::raw::CriticalSectionRawMutex).
pub struct GenericAtomicWaker<M: RawMutex> {
    waker: Mutex<M, Cell<Option<Waker>>>,
}

impl<M: RawMutex> GenericAtomicWaker<M> {
    /// Create a new `GenericAtomicWaker`.
    pub const fn new(mutex: M) -> Self {
        Self {
            waker: Mutex::const_new(mutex, Cell::new(None)),
        }
    }

    /// Register a waker. Overwrites the previous waker, if any.
    pub fn register(&self, w: &Waker) {
        self.waker.lock(|cell| {
            cell.set(match cell.replace(None) {
                Some(w2) if (w2.will_wake(w)) => Some(w2),
                _ => Some(w.clone()),
            })
        })
    }

    /// Wake the registered waker, if any.
    pub fn wake(&self) {
        self.waker.lock(|cell| {
            if let Some(w) = cell.replace(None) {
                w.wake_by_ref();
                cell.set(Some(w));
            }
        })
    }
}

// Lockless single-slot waker, ported from `futures::task::AtomicWaker` and
// adapted for `no_std`. Coordinates access to a single waker slot via a small
// atomic state machine, so neither `register()` nor `wake()` enters a critical
// section.
//
// Reference: <https://docs.rs/futures/latest/futures/task/struct.AtomicWaker.html>

// The state machine is encoded as a pair of independent bitflags packed into a
// single `AtomicUsize`. They are *not* mutually exclusive: `wake()` uses
// `fetch_or` to set the `WAKING` bit unconditionally, so a `wake()` that races
// an in-flight `register()` legitimately produces the combined state
// `REGISTERING | WAKING` (0b11). All four encodings are valid:
//
// - `WAITING`              (0b00): no operation in flight, waker cell is idle.
// - `REGISTERING`          (0b01): a `register()` call owns the waker cell.
// - `WAKING`               (0b10): a `wake()` call owns the waker cell.
// - `REGISTERING | WAKING` (0b11): a `register()` owns the cell and a
//   concurrent `wake()` has flagged that a wake is pending; the in-flight
//   `register()` is responsible for honoring it before releasing the cell
//   (see `register()` and `wake()` below).
//
// Only the holder of `REGISTERING` *or* `WAKING` (but never both at the same
// time, by virtue of the protocol) may dereference the waker cell.

/// No flags set: waker cell is idle and may be claimed by `register()` or
/// `wake()`.
#[cfg(target_has_atomic = "32")]
const WAITING: usize = 0;
/// Bit set while a `register()` call owns the waker cell.
#[cfg(target_has_atomic = "32")]
const REGISTERING: usize = 0b01;
/// Bit set while a `wake()` call owns the waker cell, or — when combined with
/// `REGISTERING` — to signal an in-flight `register()` that a wake is pending
/// and must be driven before the cell is released.
#[cfg(target_has_atomic = "32")]
const WAKING: usize = 0b10;

/// Utility struct to register and wake a waker.
///
/// Single-slot waker coordinated by an atomic state machine. Unlike
/// [`CriticalSectionWaker`](super::CriticalSectionWaker), `wake()` does **not**
/// enter a critical section, which avoids unnecessary jitter on higher-priority
/// interrupts and improves wake latency for time-critical async drivers.
///
/// If a waker is registered, registering a different waker replaces the
/// previous one and wakes it, giving the displaced task a chance to
/// re-register itself. This matches the semantics of
/// [`WakerRegistration`](super::WakerRegistration); note that
/// [`GenericAtomicWaker`] and
/// [`CriticalSectionWaker`](super::CriticalSectionWaker) instead replace the
/// previous waker *without* waking it.
///
/// Intended to wake a single task from an interrupt handler.
///
/// On targets without 32-bit atomic CAS support (e.g. Xtensa S2, AVR, RV32I),
/// `AtomicWaker` is a type alias for
/// [`CriticalSectionWaker`](super::CriticalSectionWaker), since the lockless
/// state machine relies on `compare_exchange`/`swap`/`fetch_or`.
#[cfg(target_has_atomic = "32")]
pub struct AtomicWaker {
    state: AtomicUsize,
    waker: UnsafeCell<Option<Waker>>,
}

// SAFETY: All access to the `waker` cell is serialized through the `state`
// machine: only the holder of the REGISTERING or WAKING flag may dereference
// the cell.
#[cfg(target_has_atomic = "32")]
unsafe impl Send for AtomicWaker {}
#[cfg(target_has_atomic = "32")]
unsafe impl Sync for AtomicWaker {}

#[cfg(target_has_atomic = "32")]
impl AtomicWaker {
    /// Create a new `AtomicWaker`.
    pub const fn new() -> Self {
        Self {
            state: AtomicUsize::new(WAITING),
            waker: UnsafeCell::new(None),
        }
    }

    /// Register a waker.
    ///
    /// If a different waker was previously registered, it is replaced and
    /// woken so the displaced task can re-register itself if it is still
    /// interested. This matches
    /// [`WakerRegistration::register`](super::WakerRegistration::register).
    ///
    /// If a [`wake`](Self::wake) call is in flight when this is called, the
    /// supplied waker is woken directly to schedule a re-poll.
    pub fn register(&self, waker: &Waker) {
        match self
            .state
            .compare_exchange(WAITING, REGISTERING, Acquire, Acquire)
            .unwrap_or_else(|x| x)
        {
            WAITING => {
                // The waker evicted from the cell (if any), to be woken
                // *after* we release REGISTERING so the wake cannot re-enter
                // while we still own the cell.
                let evicted: Option<Waker>;
                // The newly-registered waker, taken back out in the handoff
                // path because a racing `wake()` transferred wake duty to us.
                let pending_wake: Option<Waker>;

                // SAFETY: We hold the REGISTERING flag; no other call can
                // touch the waker cell while we own it.
                unsafe {
                    evicted = match &*self.waker.get() {
                        Some(old) if old.will_wake(waker) => None,
                        _ => (*self.waker.get()).replace(waker.clone()),
                    };

                    // Try to release the cell by clearing REGISTERING. This
                    // CAS only succeeds if no `wake()` raced us; otherwise the
                    // observed state is REGISTERING | WAKING (0b11) and the
                    // CAS fails — that failure is precisely the handoff
                    // signal from `wake()`.
                    let res = self.state.compare_exchange(REGISTERING, WAITING, AcqRel, Acquire);
                    pending_wake = match res {
                        Ok(_) => None,
                        Err(_) => {
                            // A concurrent `wake()` observed our REGISTERING
                            // bit and intentionally left the WAKING bit set
                            // instead of touching the waker cell (which we
                            // own). The wake duty has been transferred to us:
                            // take the waker out and clear both bits in a
                            // single `swap` so a new `register()` / `wake()`
                            // cycle can begin. We drive the wake outside the
                            // unsafe block, below.
                            //
                            // This is what makes the "missed wake" race
                            // impossible: the wake is delivered exactly once,
                            // either by `wake()` itself (no race) or by
                            // `register()` here (race), but never dropped.
                            let w = (*self.waker.get()).take().unwrap();
                            self.state.swap(WAITING, AcqRel);
                            Some(w)
                        }
                    };
                }

                // Cell ownership has been released. It is now safe to invoke
                // user wakers, which may re-enter `register()` / `wake()` on
                // this same `AtomicWaker`.
                if let Some(w) = pending_wake {
                    w.wake();
                }
                if let Some(w) = evicted {
                    w.wake();
                }
            }
            WAKING => {
                // A wake is currently in flight. Schedule the supplied waker
                // directly so the task does not miss the wakeup.
                waker.wake_by_ref();
            }
            state => {
                // A concurrent `register` is in flight. The single-waiter
                // contract is being violated; drop our call (memory-safe).
                debug_assert!(state == REGISTERING || state == REGISTERING | WAKING);
            }
        }
    }

    /// Wake the registered waker, if any.
    ///
    /// A clone of the stored waker is woken after the state is released, so
    /// the stored waker remains registered, matching the semantics of
    /// [`CriticalSectionWaker`](super::CriticalSectionWaker).
    pub fn wake(&self) {
        // `fetch_or` (rather than `compare_exchange`) is load-bearing here.
        // It unconditionally publishes the WAKING bit, even if a `register()`
        // is mid-flight (state == REGISTERING). This is what enables the
        // handoff: the in-flight `register()` will observe the combined
        // REGISTERING | WAKING state on its release-CAS, take ownership of
        // the wake, and drive it.
        //
        // A `compare_exchange(WAITING, WAKING, ...)` here would silently drop
        // the wake whenever it lost the race against `register()`, leaving
        // the task hung — the waker cell holds a stale waker that nothing is
        // about to invoke.
        match self.state.fetch_or(WAKING, AcqRel) {
            WAITING => {
                // No `register()` was in flight, so we now solely own the
                // waker cell via the WAKING bit.
                //
                // SAFETY: We hold the WAKING bit; the protocol guarantees
                // no other call can touch the waker cell while we own it.
                let waker = unsafe { (*self.waker.get()).clone() };
                self.state.swap(WAITING, Release);
                // Wake only after releasing the WAKING bit. Waking can hand
                // control to the woken task before this function returns: an
                // interrupt-mode executor at a higher priority than the
                // interrupt calling `wake()` preempts it right here. If the
                // task then registers again while WAKING is still set,
                // `register()` wakes it at once, the executor polls it again,
                // and the preempted `wake()` never gets to clear the bit: a
                // livelock that starves everything at or below the waking
                // interrupt's priority. `futures`' `AtomicWaker`, which this
                // is ported from, also releases the state before waking.
                if let Some(w) = waker {
                    w.wake();
                }
            }
            _ => {
                // Previous state was REGISTERING (now REGISTERING | WAKING due
                // to `fetch_or` above), WAKING (another wake is in flight), or
                // REGISTERING | WAKING (both). In every case, *somebody else*
                // owns the waker cell and is responsible for delivering the
                // wake:
                //
                // - REGISTERING: the in-flight `register()` will see WAKING
                //   on its release-CAS and wake the registered waker itself.
                // - WAKING / REGISTERING | WAKING: another `wake()` /
                //   `register()` is already going to deliver the wake.
                //
                // So we deliberately do nothing here — including not touching
                // the waker cell, which we do not own.
            }
        }
    }
}

#[cfg(target_has_atomic = "32")]
impl Default for AtomicWaker {
    fn default() -> Self {
        Self::new()
    }
}

/// On targets without 32-bit atomic CAS, fall back to the critical-section
/// based waker. The lockless state machine requires
/// `AtomicUsize::{compare_exchange, swap, fetch_or}`, which are unavailable on
/// such targets (e.g. Xtensa S2, AVR, RV32I).
#[cfg(not(target_has_atomic = "32"))]
pub type AtomicWaker = super::CriticalSectionWaker;

#[cfg(all(test, target_has_atomic = "32"))]
mod tests {
    extern crate std;

    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::task::{Wake, Waker};

    use super::AtomicWaker;

    /// Test waker that counts how many times it was woken (via either
    /// `wake` or `wake_by_ref`).
    struct CountingWaker {
        count: AtomicUsize,
    }

    impl CountingWaker {
        fn new() -> Arc<Self> {
            Arc::new(Self {
                count: AtomicUsize::new(0),
            })
        }

        fn count(&self) -> usize {
            self.count.load(Ordering::SeqCst)
        }

        fn waker(self: &Arc<Self>) -> Waker {
            Waker::from(self.clone())
        }
    }

    impl Wake for CountingWaker {
        fn wake(self: Arc<Self>) {
            self.count.fetch_add(1, Ordering::SeqCst);
        }
        fn wake_by_ref(self: &Arc<Self>) {
            self.count.fetch_add(1, Ordering::SeqCst);
        }
    }

    #[test]
    fn register_with_distinct_waker_wakes_evicted() {
        let aw = AtomicWaker::new();
        let a = CountingWaker::new();
        let b = CountingWaker::new();

        aw.register(&a.waker());
        assert_eq!(a.count(), 0);
        assert_eq!(b.count(), 0);

        aw.register(&b.waker());

        // The evicted (a) is woken exactly once; the newly-registered (b) is
        // not woken by `register` alone.
        assert_eq!(a.count(), 1, "evicted waker should be woken on eviction");
        assert_eq!(b.count(), 0, "newly-registered waker must not be woken by register");
    }

    #[test]
    fn register_same_waker_no_spurious_wakes() {
        let aw = AtomicWaker::new();
        let a = CountingWaker::new();
        let w = a.waker();

        aw.register(&w);
        aw.register(&w);

        // `Waker::will_wake` is best-effort: it may return `false` even
        // when both wakers wake the same task (the result depends on
        // toolchain-specific const-promotion of the internal vtable).
        // Either branch is correct:
        //   - fast path taken      → 0 wakes
        //   - eviction + re-register → 1 wake (the evicted clone)
        // What we guard against is *spurious extra* wakes.
        assert!(a.count() <= 1);
    }

    #[test]
    fn wake_after_register_wakes_registered() {
        let aw = AtomicWaker::new();
        let a = CountingWaker::new();

        aw.register(&a.waker());
        aw.wake();

        assert_eq!(a.count(), 1);
    }

    /// A waker whose wake registers it again on the same `AtomicWaker`, as a
    /// task does when an executor that preempts the waking interrupt polls
    /// it before `wake()` has returned. Built by hand on a `static` vtable
    /// and a `static` context, so `will_wake` holds between all its copies
    /// (it does not for `Waker::from(Arc)` under Miri, whose vtables have no
    /// stable address).
    mod reregistering {
        use core::sync::atomic::{AtomicUsize, Ordering};
        use core::task::{RawWaker, RawWakerVTable, Waker};

        use super::AtomicWaker;

        pub static AW: AtomicWaker = AtomicWaker::new();
        pub static WAKES: AtomicUsize = AtomicUsize::new(0);
        static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);

        pub fn waker() -> Waker {
            // SAFETY: the vtable functions touch nothing but the statics.
            unsafe { Waker::from_raw(RawWaker::new(&WAKES as *const AtomicUsize as *const (), &VTABLE)) }
        }

        fn clone(_: *const ()) -> RawWaker {
            RawWaker::new(&WAKES as *const AtomicUsize as *const (), &VTABLE)
        }

        fn wake(_: *const ()) {
            // Bounded, so a regression fails the assertion instead of
            // recursing without end.
            if WAKES.fetch_add(1, Ordering::SeqCst) < 10 {
                AW.register(&waker());
            }
        }

        fn drop(_: *const ()) {}
    }

    #[test]
    fn register_from_within_wake_is_not_woken_again() {
        use reregistering::{AW, WAKES, waker};

        AW.register(&waker());
        AW.wake();

        // One wake. Registering from inside the wake must not see the wake
        // still in flight and wake the task again (and again).
        assert_eq!(WAKES.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn wake_with_no_registered_waker_is_noop() {
        let aw = AtomicWaker::new();
        aw.wake();
        // Just checking we don't panic / UB.
    }
}