2014-01-16 19:57:59 -08:00
|
|
|
|
//! A "once initialization" primitive
|
|
|
|
|
//!
|
|
|
|
|
//! This primitive is meant to be used to run one-time initialization. An
|
|
|
|
|
//! example use case would be for initializing an FFI library.
|
|
|
|
|
|
2020-08-27 13:45:01 +00:00
|
|
|
|
#[cfg(all(test, not(target_os = "emscripten")))]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
|
use crate::fmt;
|
2021-12-20 11:49:47 -08:00
|
|
|
|
use crate::panic::{RefUnwindSafe, UnwindSafe};
|
2022-09-04 18:47:59 +02:00
|
|
|
|
use crate::sys_common::once as sys;
|
2022-03-22 01:24:55 -04:00
|
|
|
|
|
2014-06-10 17:43:22 -07:00
|
|
|
|
/// A synchronization primitive which can be used to run a one-time global
|
|
|
|
|
/// initialization. Useful for one-time initialization for FFI or related
|
2020-09-20 18:37:05 +02:00
|
|
|
|
/// functionality. This type can only be constructed with [`Once::new()`].
|
2014-01-16 19:57:59 -08:00
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
|
/// # Examples
|
2014-01-16 19:57:59 -08:00
|
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
|
/// ```
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// use std::sync::Once;
|
2014-01-16 19:57:59 -08:00
|
|
|
|
///
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// static START: Once = Once::new();
|
2014-06-10 17:43:22 -07:00
|
|
|
|
///
|
2014-12-29 15:03:01 -08:00
|
|
|
|
/// START.call_once(|| {
|
2014-10-10 21:59:10 -07:00
|
|
|
|
/// // run initialization here
|
|
|
|
|
/// });
|
2014-01-16 19:57:59 -08:00
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-01-16 19:57:59 -08:00
|
|
|
|
pub struct Once {
|
2022-09-04 18:47:59 +02:00
|
|
|
|
inner: sys::Once,
|
2016-03-17 19:01:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 11:49:47 -08:00
|
|
|
|
#[stable(feature = "sync_once_unwind_safe", since = "1.59.0")]
|
2021-11-05 18:27:54 +00:00
|
|
|
|
impl UnwindSafe for Once {}
|
|
|
|
|
|
2021-12-20 11:49:47 -08:00
|
|
|
|
#[stable(feature = "sync_once_unwind_safe", since = "1.59.0")]
|
|
|
|
|
impl RefUnwindSafe for Once {}
|
|
|
|
|
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// State yielded to [`Once::call_once_force()`]’s closure parameter. The state
|
|
|
|
|
/// can be used to query the poison status of the [`Once`].
|
2021-02-04 11:13:03 +01:00
|
|
|
|
#[stable(feature = "once_poison", since = "1.51.0")]
|
2016-03-17 19:01:50 -07:00
|
|
|
|
pub struct OnceState {
|
2022-09-04 18:47:59 +02:00
|
|
|
|
pub(crate) inner: sys::OnceState,
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 16:16:03 +01:00
|
|
|
|
pub(crate) enum ExclusiveState {
|
|
|
|
|
Incomplete,
|
|
|
|
|
Poisoned,
|
|
|
|
|
Complete,
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 16:10:44 -04:00
|
|
|
|
/// Initialization value for static [`Once`] values.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::sync::{Once, ONCE_INIT};
|
|
|
|
|
///
|
|
|
|
|
/// static START: Once = ONCE_INIT;
|
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-04-07 21:20:32 -04:00
|
|
|
|
#[deprecated(
|
2019-06-11 18:57:48 -07:00
|
|
|
|
since = "1.38.0",
|
2022-04-07 21:20:32 -04:00
|
|
|
|
note = "the `new` function is now preferred",
|
2019-11-09 12:46:17 +01:00
|
|
|
|
suggestion = "Once::new()"
|
2019-06-11 18:57:48 -07:00
|
|
|
|
)]
|
2015-05-27 11:18:36 +03:00
|
|
|
|
pub const ONCE_INIT: Once = Once::new();
|
2014-01-16 19:57:59 -08:00
|
|
|
|
|
|
|
|
|
impl Once {
|
2015-05-27 11:18:36 +03:00
|
|
|
|
/// Creates a new `Once` value.
|
2020-09-12 17:11:47 +02:00
|
|
|
|
#[inline]
|
2015-06-10 18:44:11 -07:00
|
|
|
|
#[stable(feature = "once_new", since = "1.2.0")]
|
2019-12-18 12:00:59 -05:00
|
|
|
|
#[rustc_const_stable(feature = "const_once_new", since = "1.32.0")]
|
2021-10-10 02:44:26 -04:00
|
|
|
|
#[must_use]
|
2015-05-27 11:18:36 +03:00
|
|
|
|
pub const fn new() -> Once {
|
2022-09-04 18:47:59 +02:00
|
|
|
|
Once { inner: sys::Once::new() }
|
2015-05-27 11:18:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
|
/// Performs an initialization routine once and only once. The given closure
|
2014-12-29 15:03:01 -08:00
|
|
|
|
/// will be executed if this is the first time `call_once` has been called,
|
|
|
|
|
/// and otherwise the routine will *not* be invoked.
|
2014-01-16 19:57:59 -08:00
|
|
|
|
///
|
2015-05-09 00:12:29 +09:00
|
|
|
|
/// This method will block the calling thread if another initialization
|
2014-01-16 19:57:59 -08:00
|
|
|
|
/// routine is currently running.
|
|
|
|
|
///
|
|
|
|
|
/// When this function returns, it is guaranteed that some initialization
|
2021-07-23 19:14:28 -04:00
|
|
|
|
/// has run and completed (it might not be the closure specified). It is also
|
2015-04-28 21:07:21 +02:00
|
|
|
|
/// guaranteed that any memory writes performed by the executed closure can
|
2015-05-09 00:12:29 +09:00
|
|
|
|
/// be reliably observed by other threads at this point (there is a
|
2015-04-28 21:07:21 +02:00
|
|
|
|
/// happens-before relation between the closure and code executing after the
|
|
|
|
|
/// return).
|
2016-03-17 19:01:50 -07:00
|
|
|
|
///
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// If the given closure recursively invokes `call_once` on the same [`Once`]
|
2018-08-03 14:18:06 +03:00
|
|
|
|
/// instance the exact behavior is not specified, allowed outcomes are
|
|
|
|
|
/// a panic or a deadlock.
|
|
|
|
|
///
|
2016-03-17 19:01:50 -07:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// use std::sync::Once;
|
2016-03-17 19:01:50 -07:00
|
|
|
|
///
|
|
|
|
|
/// static mut VAL: usize = 0;
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// static INIT: Once = Once::new();
|
2016-03-17 19:01:50 -07:00
|
|
|
|
///
|
|
|
|
|
/// // Accessing a `static mut` is unsafe much of the time, but if we do so
|
2018-11-27 02:59:49 +00:00
|
|
|
|
/// // in a synchronized fashion (e.g., write once or read all) then we're
|
2016-03-17 19:01:50 -07:00
|
|
|
|
/// // good to go!
|
|
|
|
|
/// //
|
|
|
|
|
/// // This function will only call `expensive_computation` once, and will
|
|
|
|
|
/// // otherwise always return the value returned from the first invocation.
|
|
|
|
|
/// fn get_cached_val() -> usize {
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// INIT.call_once(|| {
|
|
|
|
|
/// VAL = expensive_computation();
|
|
|
|
|
/// });
|
|
|
|
|
/// VAL
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn expensive_computation() -> usize {
|
|
|
|
|
/// // ...
|
|
|
|
|
/// # 2
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// The closure `f` will only be executed once if this is called
|
|
|
|
|
/// concurrently amongst many threads. If that closure panics, however, then
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// it will *poison* this [`Once`] instance, causing all future invocations of
|
2016-03-17 19:01:50 -07:00
|
|
|
|
/// `call_once` to also panic.
|
|
|
|
|
///
|
|
|
|
|
/// This is similar to [poisoning with mutexes][poison].
|
|
|
|
|
///
|
|
|
|
|
/// [poison]: struct.Mutex.html#poisoning
|
2022-09-04 18:47:59 +02:00
|
|
|
|
#[inline]
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-02-22 08:37:52 +09:00
|
|
|
|
#[track_caller]
|
2019-11-09 12:46:17 +01:00
|
|
|
|
pub fn call_once<F>(&self, f: F)
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(),
|
|
|
|
|
{
|
2018-08-06 16:31:04 +03:00
|
|
|
|
// Fast path check
|
2022-09-04 18:47:59 +02:00
|
|
|
|
if self.inner.is_completed() {
|
2018-08-06 16:31:04 +03:00
|
|
|
|
return;
|
2014-05-14 10:23:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 19:01:50 -07:00
|
|
|
|
let mut f = Some(f);
|
2022-09-04 18:47:59 +02:00
|
|
|
|
self.inner.call(false, &mut |_| f.take().unwrap()());
|
2016-03-17 19:01:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// Performs the same function as [`call_once()`] except ignores poisoning.
|
2017-03-27 16:10:44 -04:00
|
|
|
|
///
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// Unlike [`call_once()`], if this [`Once`] has been poisoned (i.e., a previous
|
|
|
|
|
/// call to [`call_once()`] or [`call_once_force()`] caused a panic), calling
|
|
|
|
|
/// [`call_once_force()`] will still invoke the closure `f` and will _not_
|
|
|
|
|
/// result in an immediate panic. If `f` panics, the [`Once`] will remain
|
|
|
|
|
/// in a poison state. If `f` does _not_ panic, the [`Once`] will no
|
|
|
|
|
/// longer be in a poison state and all future calls to [`call_once()`] or
|
|
|
|
|
/// [`call_once_force()`] will be no-ops.
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
|
|
|
|
/// The closure `f` is yielded a [`OnceState`] structure which can be used
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// to query the poison status of the [`Once`].
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// [`call_once()`]: Once::call_once
|
|
|
|
|
/// [`call_once_force()`]: Once::call_once_force
|
2016-03-17 19:01:50 -07:00
|
|
|
|
///
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// # Examples
|
2016-03-17 19:01:50 -07:00
|
|
|
|
///
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// ```
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// use std::sync::Once;
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// use std::thread;
|
|
|
|
|
///
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// static INIT: Once = Once::new();
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
|
|
|
|
/// // poison the once
|
|
|
|
|
/// let handle = thread::spawn(|| {
|
|
|
|
|
/// INIT.call_once(|| panic!());
|
|
|
|
|
/// });
|
|
|
|
|
/// assert!(handle.join().is_err());
|
|
|
|
|
///
|
|
|
|
|
/// // poisoning propagates
|
|
|
|
|
/// let handle = thread::spawn(|| {
|
|
|
|
|
/// INIT.call_once(|| {});
|
|
|
|
|
/// });
|
|
|
|
|
/// assert!(handle.join().is_err());
|
|
|
|
|
///
|
|
|
|
|
/// // call_once_force will still run and reset the poisoned state
|
|
|
|
|
/// INIT.call_once_force(|state| {
|
2021-02-04 11:13:03 +01:00
|
|
|
|
/// assert!(state.is_poisoned());
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// });
|
|
|
|
|
///
|
|
|
|
|
/// // once any success happens, we stop propagating the poison
|
|
|
|
|
/// INIT.call_once(|| {});
|
|
|
|
|
/// ```
|
2022-09-04 18:47:59 +02:00
|
|
|
|
#[inline]
|
2021-02-04 11:13:03 +01:00
|
|
|
|
#[stable(feature = "once_poison", since = "1.51.0")]
|
2019-11-09 12:46:17 +01:00
|
|
|
|
pub fn call_once_force<F>(&self, f: F)
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(&OnceState),
|
|
|
|
|
{
|
2018-08-06 16:31:04 +03:00
|
|
|
|
// Fast path check
|
2022-09-04 18:47:59 +02:00
|
|
|
|
if self.inner.is_completed() {
|
2018-08-06 16:31:04 +03:00
|
|
|
|
return;
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 19:01:50 -07:00
|
|
|
|
let mut f = Some(f);
|
2022-09-04 18:47:59 +02:00
|
|
|
|
self.inner.call(true, &mut |p| f.take().unwrap()(p));
|
2016-03-17 19:01:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// Returns `true` if some [`call_once()`] call has completed
|
2018-10-22 18:21:55 +02:00
|
|
|
|
/// successfully. Specifically, `is_completed` will return false in
|
|
|
|
|
/// the following situations:
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// * [`call_once()`] was not called at all,
|
|
|
|
|
/// * [`call_once()`] was called, but has not yet completed,
|
|
|
|
|
/// * the [`Once`] instance is poisoned
|
2018-08-03 13:52:52 +03:00
|
|
|
|
///
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// This function returning `false` does not mean that [`Once`] has not been
|
2020-02-07 21:53:22 -08:00
|
|
|
|
/// executed. For example, it may have been executed in the time between
|
|
|
|
|
/// when `is_completed` starts executing and when it returns, in which case
|
|
|
|
|
/// the `false` return value would be stale (but still permissible).
|
2018-08-06 16:31:04 +03:00
|
|
|
|
///
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// [`call_once()`]: Once::call_once
|
|
|
|
|
///
|
2018-08-03 13:52:52 +03:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::sync::Once;
|
|
|
|
|
///
|
|
|
|
|
/// static INIT: Once = Once::new();
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(INIT.is_completed(), false);
|
|
|
|
|
/// INIT.call_once(|| {
|
|
|
|
|
/// assert_eq!(INIT.is_completed(), false);
|
|
|
|
|
/// });
|
|
|
|
|
/// assert_eq!(INIT.is_completed(), true);
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::sync::Once;
|
|
|
|
|
/// use std::thread;
|
|
|
|
|
///
|
|
|
|
|
/// static INIT: Once = Once::new();
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(INIT.is_completed(), false);
|
|
|
|
|
/// let handle = thread::spawn(|| {
|
|
|
|
|
/// INIT.call_once(|| panic!());
|
|
|
|
|
/// });
|
|
|
|
|
/// assert!(handle.join().is_err());
|
|
|
|
|
/// assert_eq!(INIT.is_completed(), false);
|
|
|
|
|
/// ```
|
2020-03-15 10:19:26 +01:00
|
|
|
|
#[stable(feature = "once_is_completed", since = "1.43.0")]
|
2018-09-29 11:07:48 +03:00
|
|
|
|
#[inline]
|
2018-08-03 13:52:52 +03:00
|
|
|
|
pub fn is_completed(&self) -> bool {
|
2022-09-04 18:47:59 +02:00
|
|
|
|
self.inner.is_completed()
|
2019-10-23 10:01:22 +02:00
|
|
|
|
}
|
2023-01-26 16:16:03 +01:00
|
|
|
|
|
|
|
|
|
/// Returns the current state of the `Once` instance.
|
|
|
|
|
///
|
|
|
|
|
/// Since this takes a mutable reference, no initialization can currently
|
|
|
|
|
/// be running, so the state must be either "incomplete", "poisoned" or
|
|
|
|
|
/// "complete".
|
|
|
|
|
#[inline]
|
|
|
|
|
pub(crate) fn state(&mut self) -> ExclusiveState {
|
|
|
|
|
self.inner.state()
|
|
|
|
|
}
|
2019-10-23 10:01:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-29 13:31:47 +00:00
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
2016-11-25 13:21:49 -05:00
|
|
|
|
impl fmt::Debug for Once {
|
2019-03-01 09:34:11 +01:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-04-05 13:31:11 +02:00
|
|
|
|
f.debug_struct("Once").finish_non_exhaustive()
|
2016-11-25 13:21:49 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 19:01:50 -07:00
|
|
|
|
impl OnceState {
|
2019-02-09 22:16:58 +00:00
|
|
|
|
/// Returns `true` if the associated [`Once`] was poisoned prior to the
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// invocation of the closure passed to [`Once::call_once_force()`].
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// A poisoned [`Once`]:
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// use std::sync::Once;
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// use std::thread;
|
|
|
|
|
///
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// static INIT: Once = Once::new();
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
|
|
|
|
/// // poison the once
|
|
|
|
|
/// let handle = thread::spawn(|| {
|
|
|
|
|
/// INIT.call_once(|| panic!());
|
|
|
|
|
/// });
|
|
|
|
|
/// assert!(handle.join().is_err());
|
|
|
|
|
///
|
|
|
|
|
/// INIT.call_once_force(|state| {
|
2021-02-04 11:13:03 +01:00
|
|
|
|
/// assert!(state.is_poisoned());
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// });
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
2020-09-18 11:09:36 +02:00
|
|
|
|
/// An unpoisoned [`Once`]:
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// use std::sync::Once;
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
2018-05-24 14:09:42 +02:00
|
|
|
|
/// static INIT: Once = Once::new();
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
|
|
|
|
/// INIT.call_once_force(|state| {
|
2021-02-04 11:13:03 +01:00
|
|
|
|
/// assert!(!state.is_poisoned());
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// });
|
2021-02-04 11:13:03 +01:00
|
|
|
|
#[stable(feature = "once_poison", since = "1.51.0")]
|
2022-09-04 18:47:59 +02:00
|
|
|
|
#[inline]
|
2021-02-04 11:13:03 +01:00
|
|
|
|
pub fn is_poisoned(&self) -> bool {
|
2022-09-04 18:47:59 +02:00
|
|
|
|
self.inner.is_poisoned()
|
2016-03-17 19:01:50 -07:00
|
|
|
|
}
|
2020-06-30 18:27:21 +10:00
|
|
|
|
|
|
|
|
|
/// Poison the associated [`Once`] without explicitly panicking.
|
2022-09-04 18:47:59 +02:00
|
|
|
|
// NOTE: This is currently only exposed for `OnceLock`.
|
|
|
|
|
#[inline]
|
2020-06-30 18:27:21 +10:00
|
|
|
|
pub(crate) fn poison(&self) {
|
2022-09-04 18:47:59 +02:00
|
|
|
|
self.inner.poison();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "std_debug", since = "1.16.0")]
|
|
|
|
|
impl fmt::Debug for OnceState {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("OnceState").field("poisoned", &self.is_poisoned()).finish()
|
2020-06-30 18:27:21 +10:00
|
|
|
|
}
|
2016-03-17 19:01:50 -07:00
|
|
|
|
}
|