1 //! A "once initialization" primitive
3 //! This primitive is meant to be used to run one-time initialization. An
4 //! example use case would be for initializing an FFI library.
6 // A "once" is a relatively simple primitive, and it's also typically provided
7 // by the OS as well (see `pthread_once` or `InitOnceExecuteOnce`). The OS
8 // primitives, however, tend to have surprising restrictions, such as the Unix
9 // one doesn't allow an argument to be passed to the function.
11 // As a result, we end up implementing it ourselves in the standard library.
12 // This also gives us the opportunity to optimize the implementation a bit which
13 // should help the fast path on call sites. Consequently, let's explain how this
14 // primitive works now!
16 // So to recap, the guarantees of a Once are that it will call the
17 // initialization closure at most once, and it will never return until the one
18 // that's running has finished running. This means that we need some form of
19 // blocking here while the custom callback is running at the very least.
20 // Additionally, we add on the restriction of **poisoning**. Whenever an
21 // initialization closure panics, the Once enters a "poisoned" state which means
22 // that all future calls will immediately panic as well.
24 // So to implement this, one might first reach for a `Mutex`, but those cannot
25 // be put into a `static`. It also gets a lot harder with poisoning to figure
26 // out when the mutex needs to be deallocated because it's not after the closure
27 // finishes, but after the first successful closure finishes.
29 // All in all, this is instead implemented with atomics and lock-free
30 // operations! Whee! Each `Once` has one word of atomic state, and this state is
31 // CAS'd on to determine what to do. There are four possible state of a `Once`:
33 // * Incomplete - no initialization has run yet, and no thread is currently
35 // * Poisoned - some thread has previously attempted to initialize the Once, but
36 // it panicked, so the Once is now poisoned. There are no other
37 // threads currently accessing this Once.
38 // * Running - some thread is currently attempting to run initialization. It may
39 // succeed, so all future threads need to wait for it to finish.
40 // Note that this state is accompanied with a payload, described
42 // * Complete - initialization has completed and all future calls should finish
45 // With 4 states we need 2 bits to encode this, and we use the remaining bits
46 // in the word we have allocated as a queue of threads waiting for the thread
47 // responsible for entering the RUNNING state. This queue is just a linked list
48 // of Waiter nodes which is monotonically increasing in size. Each node is
49 // allocated on the stack, and whenever the running closure finishes it will
50 // consume the entire queue and notify all waiters they should try again.
52 // You'll find a few more details in the implementation, but that's the gist of
56 // When running `Once` we deal with multiple atomics:
57 // `Once.state_and_queue` and an unknown number of `Waiter.signaled`.
58 // * `state_and_queue` is used (1) as a state flag, (2) for synchronizing the
59 // result of the `Once`, and (3) for synchronizing `Waiter` nodes.
60 // - At the end of the `call_inner` function we have to make sure the result
61 // of the `Once` is acquired. So every load which can be the only one to
62 // load COMPLETED must have at least Acquire ordering, which means all
64 // - `WaiterQueue::Drop` is the only place that may store COMPLETED, and
65 // must do so with Release ordering to make the result available.
66 // - `wait` inserts `Waiter` nodes as a pointer in `state_and_queue`, and
67 // needs to make the nodes available with Release ordering. The load in
68 // its `compare_exchange` can be Relaxed because it only has to compare
69 // the atomic, not to read other data.
70 // - `WaiterQueue::Drop` must see the `Waiter` nodes, so it must load
71 // `state_and_queue` with Acquire ordering.
72 // - There is just one store where `state_and_queue` is used only as a
73 // state flag, without having to synchronize data: switching the state
74 // from INCOMPLETE to RUNNING in `call_inner`. This store can be Relaxed,
75 // but the read has to be Acquire because of the requirements mentioned
77 // * `Waiter.signaled` is both used as a flag, and to protect a field with
78 // interior mutability in `Waiter`. `Waiter.thread` is changed in
79 // `WaiterQueue::Drop` which then sets `signaled` with Release ordering.
80 // After `wait` loads `signaled` with Acquire and sees it is true, it needs to
81 // see the changes to drop the `Waiter` struct correctly.
82 // * There is one place where the two atomics `Once.state_and_queue` and
83 // `Waiter.signaled` come together, and might be reordered by the compiler or
84 // processor. Because both use Acquire ordering such a reordering is not
85 // allowed, so no need for SeqCst.
87 #[cfg(all(test, not(target_os = "emscripten")))]
90 use crate::cell::Cell;
93 use crate::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
94 use crate::thread::{self, Thread};
96 /// A synchronization primitive which can be used to run a one-time global
97 /// initialization. Useful for one-time initialization for FFI or related
98 /// functionality. This type can only be constructed with [`Once::new()`].
103 /// use std::sync::Once;
105 /// static START: Once = Once::new();
107 /// START.call_once(|| {
108 /// // run initialization here
111 #[stable(feature = "rust1", since = "1.0.0")]
113 // `state_and_queue` is actually a pointer to a `Waiter` with extra state
114 // bits, so we add the `PhantomData` appropriately.
115 state_and_queue: AtomicUsize,
116 _marker: marker::PhantomData<*const Waiter>,
119 // The `PhantomData` of a raw pointer removes these two auto traits, but we
120 // enforce both below in the implementation so this should be safe to add.
121 #[stable(feature = "rust1", since = "1.0.0")]
122 unsafe impl Sync for Once {}
123 #[stable(feature = "rust1", since = "1.0.0")]
124 unsafe impl Send for Once {}
126 /// State yielded to [`Once::call_once_force()`]’s closure parameter. The state
127 /// can be used to query the poison status of the [`Once`].
128 #[stable(feature = "once_poison", since = "1.51.0")]
130 pub struct OnceState {
132 set_state_on_drop_to: Cell<usize>,
135 /// Initialization value for static [`Once`] values.
140 /// use std::sync::{Once, ONCE_INIT};
142 /// static START: Once = ONCE_INIT;
144 #[stable(feature = "rust1", since = "1.0.0")]
147 reason = "the `new` function is now preferred",
148 suggestion = "Once::new()"
150 pub const ONCE_INIT: Once = Once::new();
152 // Four states that a Once can be in, encoded into the lower bits of
153 // `state_and_queue` in the Once structure.
154 const INCOMPLETE: usize = 0x0;
155 const POISONED: usize = 0x1;
156 const RUNNING: usize = 0x2;
157 const COMPLETE: usize = 0x3;
159 // Mask to learn about the state. All other bits are the queue of waiters if
160 // this is in the RUNNING state.
161 const STATE_MASK: usize = 0x3;
163 // Representation of a node in the linked list of waiters, used while in the
165 // Note: `Waiter` can't hold a mutable pointer to the next thread, because then
166 // `wait` would both hand out a mutable reference to its `Waiter` node, and keep
167 // a shared reference to check `signaled`. Instead we hold shared references and
168 // use interior mutability.
169 #[repr(align(4))] // Ensure the two lower bits are free to use as state bits.
171 thread: Cell<Option<Thread>>,
172 signaled: AtomicBool,
176 // Head of a linked list of waiters.
177 // Every node is a struct on the stack of a waiting thread.
178 // Will wake up the waiters when it gets dropped, i.e. also on panic.
179 struct WaiterQueue<'a> {
180 state_and_queue: &'a AtomicUsize,
181 set_state_on_drop_to: usize,
185 /// Creates a new `Once` value.
187 #[stable(feature = "once_new", since = "1.2.0")]
188 #[rustc_const_stable(feature = "const_once_new", since = "1.32.0")]
189 pub const fn new() -> Once {
190 Once { state_and_queue: AtomicUsize::new(INCOMPLETE), _marker: marker::PhantomData }
193 /// Performs an initialization routine once and only once. The given closure
194 /// will be executed if this is the first time `call_once` has been called,
195 /// and otherwise the routine will *not* be invoked.
197 /// This method will block the calling thread if another initialization
198 /// routine is currently running.
200 /// When this function returns, it is guaranteed that some initialization
201 /// has run and completed (it may not be the closure specified). It is also
202 /// guaranteed that any memory writes performed by the executed closure can
203 /// be reliably observed by other threads at this point (there is a
204 /// happens-before relation between the closure and code executing after the
207 /// If the given closure recursively invokes `call_once` on the same [`Once`]
208 /// instance the exact behavior is not specified, allowed outcomes are
209 /// a panic or a deadlock.
214 /// use std::sync::Once;
216 /// static mut VAL: usize = 0;
217 /// static INIT: Once = Once::new();
219 /// // Accessing a `static mut` is unsafe much of the time, but if we do so
220 /// // in a synchronized fashion (e.g., write once or read all) then we're
223 /// // This function will only call `expensive_computation` once, and will
224 /// // otherwise always return the value returned from the first invocation.
225 /// fn get_cached_val() -> usize {
227 /// INIT.call_once(|| {
228 /// VAL = expensive_computation();
234 /// fn expensive_computation() -> usize {
242 /// The closure `f` will only be executed once if this is called
243 /// concurrently amongst many threads. If that closure panics, however, then
244 /// it will *poison* this [`Once`] instance, causing all future invocations of
245 /// `call_once` to also panic.
247 /// This is similar to [poisoning with mutexes][poison].
249 /// [poison]: struct.Mutex.html#poisoning
250 #[stable(feature = "rust1", since = "1.0.0")]
251 pub fn call_once<F>(&self, f: F)
256 if self.is_completed() {
261 self.call_inner(false, &mut |_| f.take().unwrap()());
264 /// Performs the same function as [`call_once()`] except ignores poisoning.
266 /// Unlike [`call_once()`], if this [`Once`] has been poisoned (i.e., a previous
267 /// call to [`call_once()`] or [`call_once_force()`] caused a panic), calling
268 /// [`call_once_force()`] will still invoke the closure `f` and will _not_
269 /// result in an immediate panic. If `f` panics, the [`Once`] will remain
270 /// in a poison state. If `f` does _not_ panic, the [`Once`] will no
271 /// longer be in a poison state and all future calls to [`call_once()`] or
272 /// [`call_once_force()`] will be no-ops.
274 /// The closure `f` is yielded a [`OnceState`] structure which can be used
275 /// to query the poison status of the [`Once`].
277 /// [`call_once()`]: Once::call_once
278 /// [`call_once_force()`]: Once::call_once_force
283 /// use std::sync::Once;
286 /// static INIT: Once = Once::new();
288 /// // poison the once
289 /// let handle = thread::spawn(|| {
290 /// INIT.call_once(|| panic!());
292 /// assert!(handle.join().is_err());
294 /// // poisoning propagates
295 /// let handle = thread::spawn(|| {
296 /// INIT.call_once(|| {});
298 /// assert!(handle.join().is_err());
300 /// // call_once_force will still run and reset the poisoned state
301 /// INIT.call_once_force(|state| {
302 /// assert!(state.is_poisoned());
305 /// // once any success happens, we stop propagating the poison
306 /// INIT.call_once(|| {});
308 #[stable(feature = "once_poison", since = "1.51.0")]
309 pub fn call_once_force<F>(&self, f: F)
311 F: FnOnce(&OnceState),
314 if self.is_completed() {
319 self.call_inner(true, &mut |p| f.take().unwrap()(p));
322 /// Returns `true` if some [`call_once()`] call has completed
323 /// successfully. Specifically, `is_completed` will return false in
324 /// the following situations:
325 /// * [`call_once()`] was not called at all,
326 /// * [`call_once()`] was called, but has not yet completed,
327 /// * the [`Once`] instance is poisoned
329 /// This function returning `false` does not mean that [`Once`] has not been
330 /// executed. For example, it may have been executed in the time between
331 /// when `is_completed` starts executing and when it returns, in which case
332 /// the `false` return value would be stale (but still permissible).
334 /// [`call_once()`]: Once::call_once
339 /// use std::sync::Once;
341 /// static INIT: Once = Once::new();
343 /// assert_eq!(INIT.is_completed(), false);
344 /// INIT.call_once(|| {
345 /// assert_eq!(INIT.is_completed(), false);
347 /// assert_eq!(INIT.is_completed(), true);
351 /// use std::sync::Once;
354 /// static INIT: Once = Once::new();
356 /// assert_eq!(INIT.is_completed(), false);
357 /// let handle = thread::spawn(|| {
358 /// INIT.call_once(|| panic!());
360 /// assert!(handle.join().is_err());
361 /// assert_eq!(INIT.is_completed(), false);
363 #[stable(feature = "once_is_completed", since = "1.43.0")]
365 pub fn is_completed(&self) -> bool {
366 // An `Acquire` load is enough because that makes all the initialization
367 // operations visible to us, and, this being a fast path, weaker
368 // ordering helps with performance. This `Acquire` synchronizes with
369 // `Release` operations on the slow path.
370 self.state_and_queue.load(Ordering::Acquire) == COMPLETE
373 // This is a non-generic function to reduce the monomorphization cost of
374 // using `call_once` (this isn't exactly a trivial or small implementation).
376 // Additionally, this is tagged with `#[cold]` as it should indeed be cold
377 // and it helps let LLVM know that calls to this function should be off the
378 // fast path. Essentially, this should help generate more straight line code
381 // Finally, this takes an `FnMut` instead of a `FnOnce` because there's
382 // currently no way to take an `FnOnce` and call it via virtual dispatch
383 // without some allocation overhead.
385 fn call_inner(&self, ignore_poisoning: bool, init: &mut dyn FnMut(&OnceState)) {
386 let mut state_and_queue = self.state_and_queue.load(Ordering::Acquire);
388 match state_and_queue {
390 POISONED if !ignore_poisoning => {
391 // Panic to propagate the poison.
392 panic!("Once instance has previously been poisoned");
394 POISONED | INCOMPLETE => {
395 // Try to register this thread as the one RUNNING.
396 let exchange_result = self.state_and_queue.compare_exchange(
402 if let Err(old) = exchange_result {
403 state_and_queue = old;
406 // `waiter_queue` will manage other waiting threads, and
407 // wake them up on drop.
408 let mut waiter_queue = WaiterQueue {
409 state_and_queue: &self.state_and_queue,
410 set_state_on_drop_to: POISONED,
412 // Run the initialization function, letting it know if we're
414 let init_state = OnceState {
415 poisoned: state_and_queue == POISONED,
416 set_state_on_drop_to: Cell::new(COMPLETE),
419 waiter_queue.set_state_on_drop_to = init_state.set_state_on_drop_to.get();
423 // All other values must be RUNNING with possibly a
424 // pointer to the waiter queue in the more significant bits.
425 assert!(state_and_queue & STATE_MASK == RUNNING);
426 wait(&self.state_and_queue, state_and_queue);
427 state_and_queue = self.state_and_queue.load(Ordering::Acquire);
434 fn wait(state_and_queue: &AtomicUsize, mut current_state: usize) {
435 // Note: the following code was carefully written to avoid creating a
436 // mutable reference to `node` that gets aliased.
438 // Don't queue this thread if the status is no longer running,
439 // otherwise we will not be woken up.
440 if current_state & STATE_MASK != RUNNING {
444 // Create the node for our current thread.
446 thread: Cell::new(Some(thread::current())),
447 signaled: AtomicBool::new(false),
448 next: (current_state & !STATE_MASK) as *const Waiter,
450 let me = &node as *const Waiter as usize;
452 // Try to slide in the node at the head of the linked list, making sure
453 // that another thread didn't just replace the head of the linked list.
454 let exchange_result = state_and_queue.compare_exchange(
460 if let Err(old) = exchange_result {
465 // We have enqueued ourselves, now lets wait.
466 // It is important not to return before being signaled, otherwise we
467 // would drop our `Waiter` node and leave a hole in the linked list
468 // (and a dangling reference). Guard against spurious wakeups by
469 // reparking ourselves until we are signaled.
470 while !node.signaled.load(Ordering::Acquire) {
471 // If the managing thread happens to signal and unpark us before we
472 // can park ourselves, the result could be this thread never gets
473 // unparked. Luckily `park` comes with the guarantee that if it got
474 // an `unpark` just before on an unparked thread it does not park.
481 #[stable(feature = "std_debug", since = "1.16.0")]
482 impl fmt::Debug for Once {
483 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
484 f.debug_struct("Once").finish_non_exhaustive()
488 impl Drop for WaiterQueue<'_> {
490 // Swap out our state with however we finished.
491 let state_and_queue =
492 self.state_and_queue.swap(self.set_state_on_drop_to, Ordering::AcqRel);
494 // We should only ever see an old state which was RUNNING.
495 assert_eq!(state_and_queue & STATE_MASK, RUNNING);
497 // Walk the entire linked list of waiters and wake them up (in lifo
498 // order, last to register is first to wake up).
500 // Right after setting `node.signaled = true` the other thread may
501 // free `node` if there happens to be has a spurious wakeup.
502 // So we have to take out the `thread` field and copy the pointer to
504 let mut queue = (state_and_queue & !STATE_MASK) as *const Waiter;
505 while !queue.is_null() {
506 let next = (*queue).next;
507 let thread = (*queue).thread.take().unwrap();
508 (*queue).signaled.store(true, Ordering::Release);
509 // ^- FIXME (maybe): This is another case of issue #55005
510 // `store()` has a potentially dangling ref to `signaled`.
519 /// Returns `true` if the associated [`Once`] was poisoned prior to the
520 /// invocation of the closure passed to [`Once::call_once_force()`].
524 /// A poisoned [`Once`]:
527 /// use std::sync::Once;
530 /// static INIT: Once = Once::new();
532 /// // poison the once
533 /// let handle = thread::spawn(|| {
534 /// INIT.call_once(|| panic!());
536 /// assert!(handle.join().is_err());
538 /// INIT.call_once_force(|state| {
539 /// assert!(state.is_poisoned());
543 /// An unpoisoned [`Once`]:
546 /// use std::sync::Once;
548 /// static INIT: Once = Once::new();
550 /// INIT.call_once_force(|state| {
551 /// assert!(!state.is_poisoned());
553 #[stable(feature = "once_poison", since = "1.51.0")]
554 pub fn is_poisoned(&self) -> bool {
558 /// Poison the associated [`Once`] without explicitly panicking.
559 // NOTE: This is currently only exposed for the `lazy` module
560 pub(crate) fn poison(&self) {
561 self.set_state_on_drop_to.set(POISONED);