Skip to main content

std/sync/
once.rs

1//! A "once initialization" primitive
2//!
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.
5
6use crate::fmt;
7use crate::panic::{RefUnwindSafe, UnwindSafe};
8use crate::sys::sync as sys;
9
10/// A low-level synchronization primitive for one-time global execution.
11///
12/// Previously this was the only "execute once" synchronization in `std`.
13/// Other libraries implemented novel synchronizing types with `Once`, like
14/// [`OnceLock<T>`] or [`LazyLock<T, F>`], before those were added to `std`.
15/// `OnceLock<T>` in particular supersedes `Once` in functionality and should
16/// be preferred for the common case where the `Once` is associated with data.
17///
18/// This type can only be constructed with [`Once::new()`].
19///
20/// # Examples
21///
22/// ```
23/// use std::sync::Once;
24///
25/// static START: Once = Once::new();
26///
27/// START.call_once(|| {
28///     // run initialization here
29/// });
30/// ```
31///
32/// [`OnceLock<T>`]: crate::sync::OnceLock
33/// [`LazyLock<T, F>`]: crate::sync::LazyLock
34#[stable(feature = "rust1", since = "1.0.0")]
35pub struct Once {
36    inner: sys::Once,
37}
38
39#[stable(feature = "sync_once_unwind_safe", since = "1.59.0")]
40impl UnwindSafe for Once {}
41
42#[stable(feature = "sync_once_unwind_safe", since = "1.59.0")]
43impl RefUnwindSafe for Once {}
44
45/// State yielded to [`Once::call_once_force()`]’s closure parameter. The state
46/// can be used to query the poison status of the [`Once`].
47#[stable(feature = "once_poison", since = "1.51.0")]
48pub struct OnceState {
49    pub(crate) inner: sys::OnceState,
50}
51
52/// Used for the internal implementation of `sys::sync::once` on different platforms and the
53/// [`LazyLock`](crate::sync::LazyLock) implementation.
54pub(crate) enum OnceExclusiveState {
55    Incomplete,
56    Poisoned,
57    Complete,
58}
59
60/// Initialization value for static [`Once`] values.
61///
62/// # Examples
63///
64/// ```
65/// use std::sync::{Once, ONCE_INIT};
66///
67/// static START: Once = ONCE_INIT;
68/// ```
69#[stable(feature = "rust1", since = "1.0.0")]
70#[deprecated(
71    since = "1.38.0",
72    note = "the `Once::new()` function is now preferred",
73    suggestion = "Once::new()"
74)]
75pub const ONCE_INIT: Once = Once::new();
76
77impl Once {
78    /// Creates a new `Once` value.
79    #[inline]
80    #[stable(feature = "once_new", since = "1.2.0")]
81    #[rustc_const_stable(feature = "const_once_new", since = "1.32.0")]
82    #[must_use]
83    pub const fn new() -> Once {
84        Once { inner: sys::Once::new() }
85    }
86
87    /// Creates a new `Once` value that starts already completed.
88    #[inline]
89    #[must_use]
90    pub(crate) const fn new_complete() -> Once {
91        Once { inner: sys::Once::new_complete() }
92    }
93
94    /// Performs an initialization routine once and only once. The given closure
95    /// will be executed if this is the first time `call_once` has been called,
96    /// and otherwise the routine will *not* be invoked.
97    ///
98    /// This method will block the calling thread if another initialization
99    /// routine is currently running.
100    ///
101    /// When this function returns, it is guaranteed that some initialization
102    /// has run and completed (it might not be the closure specified). It is also
103    /// guaranteed that any memory writes performed by the executed closure can
104    /// be reliably observed by other threads at this point (there is a
105    /// happens-before relation between the closure and code executing after the
106    /// return).
107    ///
108    /// If the given closure recursively invokes `call_once` on the same [`Once`]
109    /// instance, the exact behavior is not specified: allowed outcomes are
110    /// a panic or a deadlock.
111    ///
112    /// # Examples
113    ///
114    /// ```
115    /// use std::sync::Once;
116    ///
117    /// static mut VAL: usize = 0;
118    /// static INIT: Once = Once::new();
119    ///
120    /// // Accessing a `static mut` is unsafe much of the time, but if we do so
121    /// // in a synchronized fashion (e.g., write once or read all) then we're
122    /// // good to go!
123    /// //
124    /// // This function will only call `expensive_computation` once, and will
125    /// // otherwise always return the value returned from the first invocation.
126    /// fn get_cached_val() -> usize {
127    ///     unsafe {
128    ///         INIT.call_once(|| {
129    ///             VAL = expensive_computation();
130    ///         });
131    ///         VAL
132    ///     }
133    /// }
134    ///
135    /// fn expensive_computation() -> usize {
136    ///     // ...
137    /// # 2
138    /// }
139    /// ```
140    ///
141    /// # Panics
142    ///
143    /// The closure `f` will only be executed once even if this is called
144    /// concurrently amongst many threads. If that closure panics, however, then
145    /// it will *poison* this [`Once`] instance, causing all future invocations of
146    /// `call_once` to also panic.
147    ///
148    /// This is similar to [poisoning with mutexes][poison], but this mechanism
149    /// is guaranteed to never skip panics within `f`.
150    ///
151    /// [poison]: struct.Mutex.html#poisoning
152    #[inline]
153    #[stable(feature = "rust1", since = "1.0.0")]
154    #[track_caller]
155    #[rustc_should_not_be_called_on_const_items]
156    pub fn call_once<F>(&self, f: F)
157    where
158        F: FnOnce(),
159    {
160        // Fast path check
161        if self.inner.is_completed() {
162            return;
163        }
164
165        let mut f = Some(f);
166        self.inner.call(false, &mut |_| f.take().unwrap()());
167    }
168
169    /// Performs the same function as [`call_once()`] except ignores poisoning.
170    ///
171    /// Unlike [`call_once()`], if this [`Once`] has been poisoned (i.e., a previous
172    /// call to [`call_once()`] or [`call_once_force()`] caused a panic), calling
173    /// [`call_once_force()`] will still invoke the closure `f` and will _not_
174    /// result in an immediate panic. If `f` panics, the [`Once`] will remain
175    /// in a poison state. If `f` does _not_ panic, the [`Once`] will no
176    /// longer be in a poison state and all future calls to [`call_once()`] or
177    /// [`call_once_force()`] will be no-ops.
178    ///
179    /// The closure `f` is yielded a [`OnceState`] structure which can be used
180    /// to query the poison status of the [`Once`].
181    ///
182    /// [`call_once()`]: Once::call_once
183    /// [`call_once_force()`]: Once::call_once_force
184    ///
185    /// # Examples
186    ///
187    /// ```
188    /// use std::sync::Once;
189    /// use std::thread;
190    ///
191    /// static INIT: Once = Once::new();
192    ///
193    /// // poison the once
194    /// let handle = thread::spawn(|| {
195    ///     INIT.call_once(|| panic!());
196    /// });
197    /// assert!(handle.join().is_err());
198    ///
199    /// // poisoning propagates
200    /// let handle = thread::spawn(|| {
201    ///     INIT.call_once(|| {});
202    /// });
203    /// assert!(handle.join().is_err());
204    ///
205    /// // call_once_force will still run and reset the poisoned state
206    /// INIT.call_once_force(|state| {
207    ///     assert!(state.is_poisoned());
208    /// });
209    ///
210    /// // once any success happens, we stop propagating the poison
211    /// INIT.call_once(|| {});
212    /// ```
213    #[inline]
214    #[stable(feature = "once_poison", since = "1.51.0")]
215    #[rustc_should_not_be_called_on_const_items]
216    pub fn call_once_force<F>(&self, f: F)
217    where
218        F: FnOnce(&OnceState),
219    {
220        // Fast path check
221        if self.inner.is_completed() {
222            return;
223        }
224
225        let mut f = Some(f);
226        self.inner.call(true, &mut |p| f.take().unwrap()(p));
227    }
228
229    /// Returns `true` if some [`call_once()`] call has completed
230    /// successfully. Specifically, `is_completed` will return false in
231    /// the following situations:
232    ///   * [`call_once()`] was not called at all,
233    ///   * [`call_once()`] was called, but has not yet completed,
234    ///   * the [`Once`] instance is poisoned
235    ///
236    /// This function returning `false` does not mean that [`Once`] has not been
237    /// executed. For example, it may have been executed in the time between
238    /// when `is_completed` starts executing and when it returns, in which case
239    /// the `false` return value would be stale (but still permissible).
240    ///
241    /// [`call_once()`]: Once::call_once
242    ///
243    /// # Examples
244    ///
245    /// ```
246    /// use std::sync::Once;
247    ///
248    /// static INIT: Once = Once::new();
249    ///
250    /// assert_eq!(INIT.is_completed(), false);
251    /// INIT.call_once(|| {
252    ///     assert_eq!(INIT.is_completed(), false);
253    /// });
254    /// assert_eq!(INIT.is_completed(), true);
255    /// ```
256    ///
257    /// ```
258    /// use std::sync::Once;
259    /// use std::thread;
260    ///
261    /// static INIT: Once = Once::new();
262    ///
263    /// assert_eq!(INIT.is_completed(), false);
264    /// let handle = thread::spawn(|| {
265    ///     INIT.call_once(|| panic!());
266    /// });
267    /// assert!(handle.join().is_err());
268    /// assert_eq!(INIT.is_completed(), false);
269    /// ```
270    #[stable(feature = "once_is_completed", since = "1.43.0")]
271    #[inline]
272    pub fn is_completed(&self) -> bool {
273        self.inner.is_completed()
274    }
275
276    /// Blocks the current thread until initialization has completed.
277    ///
278    /// # Example
279    ///
280    /// ```rust
281    /// use std::sync::Once;
282    /// use std::thread;
283    ///
284    /// static READY: Once = Once::new();
285    ///
286    /// let thread = thread::spawn(|| {
287    ///     READY.wait();
288    ///     println!("everything is ready");
289    /// });
290    ///
291    /// READY.call_once(|| println!("performing setup"));
292    /// ```
293    ///
294    /// # Panics
295    ///
296    /// If this [`Once`] has been poisoned because an initialization closure has
297    /// panicked, this method will also panic. Use [`wait_force`](Self::wait_force)
298    /// if this behavior is not desired.
299    #[stable(feature = "once_wait", since = "1.86.0")]
300    #[rustc_should_not_be_called_on_const_items]
301    pub fn wait(&self) {
302        if !self.inner.is_completed() {
303            self.inner.wait(false);
304        }
305    }
306
307    /// Blocks the current thread until initialization has completed, ignoring
308    /// poisoning.
309    ///
310    /// If this [`Once`] has been poisoned, this function blocks until it
311    /// becomes completed, unlike [`Once::wait()`], which panics in this case.
312    #[stable(feature = "once_wait", since = "1.86.0")]
313    #[rustc_should_not_be_called_on_const_items]
314    pub fn wait_force(&self) {
315        if !self.inner.is_completed() {
316            self.inner.wait(true);
317        }
318    }
319
320    /// Returns the current state of the `Once` instance.
321    ///
322    /// Since this takes a mutable reference, no initialization can currently
323    /// be running, so the state must be either "incomplete", "poisoned" or
324    /// "complete".
325    #[inline]
326    pub(crate) fn state(&mut self) -> OnceExclusiveState {
327        self.inner.state()
328    }
329
330    /// Sets current state of the `Once` instance.
331    ///
332    /// Since this takes a mutable reference, no initialization can currently
333    /// be running, so the state must be either "incomplete", "poisoned" or
334    /// "complete".
335    #[inline]
336    pub(crate) fn set_state(&mut self, new_state: OnceExclusiveState) {
337        self.inner.set_state(new_state);
338    }
339}
340
341#[stable(feature = "std_debug", since = "1.16.0")]
342impl fmt::Debug for Once {
343    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
344        f.debug_struct("Once").finish_non_exhaustive()
345    }
346}
347
348impl OnceState {
349    /// Returns `true` if the associated [`Once`] was poisoned prior to the
350    /// invocation of the closure passed to [`Once::call_once_force()`].
351    ///
352    /// # Examples
353    ///
354    /// A poisoned [`Once`]:
355    ///
356    /// ```
357    /// use std::sync::Once;
358    /// use std::thread;
359    ///
360    /// static INIT: Once = Once::new();
361    ///
362    /// // poison the once
363    /// let handle = thread::spawn(|| {
364    ///     INIT.call_once(|| panic!());
365    /// });
366    /// assert!(handle.join().is_err());
367    ///
368    /// INIT.call_once_force(|state| {
369    ///     assert!(state.is_poisoned());
370    /// });
371    /// ```
372    ///
373    /// An unpoisoned [`Once`]:
374    ///
375    /// ```
376    /// use std::sync::Once;
377    ///
378    /// static INIT: Once = Once::new();
379    ///
380    /// INIT.call_once_force(|state| {
381    ///     assert!(!state.is_poisoned());
382    /// });
383    #[stable(feature = "once_poison", since = "1.51.0")]
384    #[inline]
385    pub fn is_poisoned(&self) -> bool {
386        self.inner.is_poisoned()
387    }
388
389    /// Poison the associated [`Once`] without explicitly panicking.
390    // NOTE: This is currently only exposed for `OnceLock`.
391    #[inline]
392    pub(crate) fn poison(&self) {
393        self.inner.poison();
394    }
395}
396
397#[stable(feature = "std_debug", since = "1.16.0")]
398impl fmt::Debug for OnceState {
399    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
400        f.debug_struct("OnceState").field("poisoned", &self.is_poisoned()).finish()
401    }
402}