|
1 | 1 | //! Helpers built around pointer-sized atomics. |
2 | | -use core::sync::atomic::{AtomicUsize, Ordering}; |
3 | | - |
4 | | -// This structure represents a lazily initialized static usize value. Useful |
5 | | -// when it is preferable to just rerun initialization instead of locking. |
6 | | -// unsync_init will invoke an init() function until it succeeds, then return the |
7 | | -// cached value for future calls. |
8 | | -// |
9 | | -// unsync_init supports init() "failing". If the init() method returns UNINIT, |
10 | | -// that value will be returned as normal, but will not be cached. |
11 | | -// |
12 | | -// Users should only depend on the _value_ returned by init() functions. |
13 | | -// Specifically, for the following init() function: |
14 | | -// fn init() -> usize { |
15 | | -// a(); |
16 | | -// let v = b(); |
17 | | -// c(); |
18 | | -// v |
19 | | -// } |
20 | | -// the effects of c() or writes to shared memory will not necessarily be |
21 | | -// observed and additional synchronization methods may be needed. |
22 | | -pub(crate) struct LazyUsize(AtomicUsize); |
23 | | - |
24 | | -impl LazyUsize { |
25 | | - // The initialization is not completed. |
26 | | - const UNINIT: usize = usize::MAX; |
27 | | - |
28 | | - pub const fn new() -> Self { |
29 | | - Self(AtomicUsize::new(Self::UNINIT)) |
30 | | - } |
31 | | - |
32 | | - // Runs the init() function at most once, returning the value of some run of |
33 | | - // init(). Multiple callers can run their init() functions in parallel. |
34 | | - // init() should always return the same value, if it succeeds. |
35 | | - pub fn unsync_init(&self, init: impl FnOnce() -> usize) -> usize { |
36 | | - #[cold] |
37 | | - fn do_init(this: &LazyUsize, init: impl FnOnce() -> usize) -> usize { |
38 | | - let val = init(); |
39 | | - this.0.store(val, Ordering::Relaxed); |
40 | | - val |
41 | | - } |
42 | | - |
43 | | - // Relaxed ordering is fine, as we only have a single atomic variable. |
44 | | - let val = self.0.load(Ordering::Relaxed); |
45 | | - if val != Self::UNINIT { |
46 | | - val |
47 | | - } else { |
48 | | - do_init(self, init) |
| 2 | +use core::{ |
| 3 | + ptr, |
| 4 | + sync::atomic::{AtomicPtr, AtomicUsize, Ordering}, |
| 5 | +}; |
| 6 | + |
| 7 | +macro_rules! lazy_atomic { |
| 8 | + ($name:ident $(<$($gen:ident),+>)?, $atomic:ty, $value:ty, $uninit:expr) => { |
| 9 | + /// Lazily initialized static value backed by a single atomic. |
| 10 | + /// |
| 11 | + /// `unsync_init` will invoke `init` until it returns a value other than |
| 12 | + /// the sentinel `UNINIT`, then cache that value for subsequent calls. |
| 13 | + /// Multiple callers may race to run `init`; only the returned value is |
| 14 | + /// guaranteed to be observed, not any side effects. |
| 15 | + pub(crate) struct $name$(<$($gen),+>)?($atomic); |
| 16 | + |
| 17 | + impl<$($($gen),+)? > $name$(<$($gen),+>)? { |
| 18 | + const UNINIT: $value = $uninit; |
| 19 | + |
| 20 | + pub const fn new() -> Self { |
| 21 | + Self(<$atomic>::new(Self::UNINIT)) |
| 22 | + } |
| 23 | + |
| 24 | + #[cold] |
| 25 | + fn do_init(&self, init: impl FnOnce() -> $value) -> $value { |
| 26 | + let val = init(); |
| 27 | + self.0.store(val, Ordering::Relaxed); |
| 28 | + val |
| 29 | + } |
| 30 | + |
| 31 | + #[inline] |
| 32 | + pub fn unsync_init(&self, init: impl FnOnce() -> $value) -> $value { |
| 33 | + // Relaxed ordering is fine, as we only have a single atomic variable. |
| 34 | + let val = self.0.load(Ordering::Relaxed); |
| 35 | + if val != Self::UNINIT { |
| 36 | + val |
| 37 | + } else { |
| 38 | + self.do_init(init) |
| 39 | + } |
| 40 | + } |
49 | 41 | } |
50 | | - } |
| 42 | + }; |
51 | 43 | } |
52 | 44 |
|
53 | | -// Identical to LazyUsize except with bool instead of usize. |
| 45 | +lazy_atomic!(LazyUsize, AtomicUsize, usize, usize::MAX); |
| 46 | +lazy_atomic!(LazyPtr<T>, AtomicPtr<T>, *mut T, ptr::dangling_mut()); |
| 47 | + |
| 48 | +/// Lazily initializes a cached bool; reuses `LazyUsize` to avoid sentinel |
| 49 | +/// issues with `AtomicBool`. |
54 | 50 | pub(crate) struct LazyBool(LazyUsize); |
55 | 51 |
|
56 | 52 | impl LazyBool { |
|
0 commit comments