2014-01-16 19:57:59 -08:00
|
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
|
//! 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.
|
|
|
|
|
|
2016-03-17 19:01:50 -07:00
|
|
|
|
// A "once" is a relatively simple primitive, and it's also typically provided
|
|
|
|
|
// by the OS as well (see `pthread_once` or `InitOnceExecuteOnce`). The OS
|
|
|
|
|
// primitives, however, tend to have surprising restrictions, such as the Unix
|
|
|
|
|
// one doesn't allow an argument to be passed to the function.
|
|
|
|
|
//
|
|
|
|
|
// As a result, we end up implementing it ourselves in the standard library.
|
|
|
|
|
// This also gives us the opportunity to optimize the implementation a bit which
|
|
|
|
|
// should help the fast path on call sites. Consequently, let's explain how this
|
|
|
|
|
// primitive works now!
|
|
|
|
|
//
|
|
|
|
|
// So to recap, the guarantees of a Once are that it will call the
|
|
|
|
|
// initialization closure at most once, and it will never return until the one
|
|
|
|
|
// that's running has finished running. This means that we need some form of
|
|
|
|
|
// blocking here while the custom callback is running at the very least.
|
|
|
|
|
// Additionally, we add on the restriction of **poisoning**. Whenever an
|
|
|
|
|
// initialization closure panics, the Once enters a "poisoned" state which means
|
|
|
|
|
// that all future calls will immediately panic as well.
|
|
|
|
|
//
|
2018-08-06 12:34:00 +02:00
|
|
|
|
// So to implement this, one might first reach for a `Mutex`, but those cannot
|
|
|
|
|
// be put into a `static`. It also gets a lot harder with poisoning to figure
|
|
|
|
|
// out when the mutex needs to be deallocated because it's not after the closure
|
|
|
|
|
// finishes, but after the first successful closure finishes.
|
2016-03-17 19:01:50 -07:00
|
|
|
|
//
|
|
|
|
|
// All in all, this is instead implemented with atomics and lock-free
|
|
|
|
|
// operations! Whee! Each `Once` has one word of atomic state, and this state is
|
|
|
|
|
// CAS'd on to determine what to do. There are four possible state of a `Once`:
|
|
|
|
|
//
|
|
|
|
|
// * Incomplete - no initialization has run yet, and no thread is currently
|
|
|
|
|
// using the Once.
|
|
|
|
|
// * Poisoned - some thread has previously attempted to initialize the Once, but
|
|
|
|
|
// it panicked, so the Once is now poisoned. There are no other
|
|
|
|
|
// threads currently accessing this Once.
|
|
|
|
|
// * Running - some thread is currently attempting to run initialization. It may
|
|
|
|
|
// succeed, so all future threads need to wait for it to finish.
|
|
|
|
|
// Note that this state is accompanied with a payload, described
|
|
|
|
|
// below.
|
|
|
|
|
// * Complete - initialization has completed and all future calls should finish
|
|
|
|
|
// immediately.
|
|
|
|
|
//
|
|
|
|
|
// With 4 states we need 2 bits to encode this, and we use the remaining bits
|
|
|
|
|
// in the word we have allocated as a queue of threads waiting for the thread
|
|
|
|
|
// responsible for entering the RUNNING state. This queue is just a linked list
|
|
|
|
|
// of Waiter nodes which is monotonically increasing in size. Each node is
|
|
|
|
|
// allocated on the stack, and whenever the running closure finishes it will
|
|
|
|
|
// consume the entire queue and notify all waiters they should try again.
|
|
|
|
|
//
|
|
|
|
|
// You'll find a few more details in the implementation, but that's the gist of
|
|
|
|
|
// it!
|
|
|
|
|
|
2016-11-25 13:21:49 -05:00
|
|
|
|
use fmt;
|
2016-03-17 19:01:50 -07:00
|
|
|
|
use marker;
|
2016-06-24 20:54:52 +02:00
|
|
|
|
use ptr;
|
2016-03-17 19:01:50 -07:00
|
|
|
|
use sync::atomic::{AtomicUsize, AtomicBool, Ordering};
|
|
|
|
|
use thread::{self, Thread};
|
2014-01-16 19:57:59 -08: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
|
2017-03-27 16:10:44 -04:00
|
|
|
|
/// functionality. This type can only be constructed with the [`ONCE_INIT`]
|
2018-05-24 14:08:47 +02:00
|
|
|
|
/// value or the equivalent [`Once::new`] constructor.
|
2014-01-16 19:57:59 -08:00
|
|
|
|
///
|
2017-03-27 16:10:44 -04:00
|
|
|
|
/// [`ONCE_INIT`]: constant.ONCE_INIT.html
|
2018-05-24 14:08:47 +02:00
|
|
|
|
/// [`Once::new`]: struct.Once.html#method.new
|
2017-03-27 16:10:44 -04: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 {
|
2016-03-17 19:01:50 -07:00
|
|
|
|
// This `state` word is actually an encoded version of just a pointer to a
|
|
|
|
|
// `Waiter`, so we add the `PhantomData` appropriately.
|
|
|
|
|
state: AtomicUsize,
|
|
|
|
|
_marker: marker::PhantomData<*mut Waiter>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The `PhantomData` of a raw pointer removes these two auto traits, but we
|
|
|
|
|
// enforce both below in the implementation so this should be safe to add.
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
unsafe impl Sync for Once {}
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
unsafe impl Send for Once {}
|
|
|
|
|
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// State yielded to [`call_once_force`]’s closure parameter. The state can be
|
|
|
|
|
/// used to query the poison status of the [`Once`].
|
2017-03-27 16:10:44 -04:00
|
|
|
|
///
|
|
|
|
|
/// [`call_once_force`]: struct.Once.html#method.call_once_force
|
|
|
|
|
/// [`Once`]: struct.Once.html
|
2016-05-11 21:01:29 -04:00
|
|
|
|
#[unstable(feature = "once_poison", issue = "33577")]
|
2016-11-25 13:21:49 -05:00
|
|
|
|
#[derive(Debug)]
|
2016-03-17 19:01:50 -07:00
|
|
|
|
pub struct OnceState {
|
|
|
|
|
poisoned: bool,
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 16:10:44 -04:00
|
|
|
|
/// Initialization value for static [`Once`] values.
|
|
|
|
|
///
|
|
|
|
|
/// [`Once`]: struct.Once.html
|
|
|
|
|
///
|
|
|
|
|
/// # 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")]
|
2015-05-27 11:18:36 +03:00
|
|
|
|
pub const ONCE_INIT: Once = Once::new();
|
2014-01-16 19:57:59 -08:00
|
|
|
|
|
2016-03-17 19:01:50 -07:00
|
|
|
|
// Four states that a Once can be in, encoded into the lower bits of `state` in
|
|
|
|
|
// the Once structure.
|
|
|
|
|
const INCOMPLETE: usize = 0x0;
|
|
|
|
|
const POISONED: usize = 0x1;
|
|
|
|
|
const RUNNING: usize = 0x2;
|
|
|
|
|
const COMPLETE: usize = 0x3;
|
|
|
|
|
|
|
|
|
|
// Mask to learn about the state. All other bits are the queue of waiters if
|
|
|
|
|
// this is in the RUNNING state.
|
|
|
|
|
const STATE_MASK: usize = 0x3;
|
|
|
|
|
|
|
|
|
|
// Representation of a node in the linked list of waiters in the RUNNING state.
|
|
|
|
|
struct Waiter {
|
|
|
|
|
thread: Option<Thread>,
|
|
|
|
|
signaled: AtomicBool,
|
|
|
|
|
next: *mut Waiter,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper struct used to clean up after a closure call with a `Drop`
|
|
|
|
|
// implementation to also run on panic.
|
2018-07-10 22:47:59 -04:00
|
|
|
|
struct Finish<'a> {
|
2016-03-17 19:01:50 -07:00
|
|
|
|
panicked: bool,
|
2018-07-10 22:47:59 -04:00
|
|
|
|
me: &'a Once,
|
2016-03-17 19:01:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-16 19:57:59 -08:00
|
|
|
|
impl Once {
|
2015-05-27 11:18:36 +03:00
|
|
|
|
/// Creates a new `Once` value.
|
2015-06-10 18:44:11 -07:00
|
|
|
|
#[stable(feature = "once_new", since = "1.2.0")]
|
2015-05-27 11:18:36 +03:00
|
|
|
|
pub const fn new() -> Once {
|
|
|
|
|
Once {
|
2016-03-17 19:01:50 -07:00
|
|
|
|
state: AtomicUsize::new(INCOMPLETE),
|
|
|
|
|
_marker: marker::PhantomData,
|
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
|
2015-04-28 21:07:21 +02:00
|
|
|
|
/// has run and completed (it may not be the closure specified). It is also
|
|
|
|
|
/// 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
|
|
|
|
///
|
2018-08-19 15:30:23 +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
|
|
|
|
|
/// // in a synchronized fashion (e.g. write once or read all) then we're
|
|
|
|
|
/// // 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
|
|
|
|
|
/// it will *poison* this `Once` instance, causing all future invocations of
|
|
|
|
|
/// `call_once` to also panic.
|
|
|
|
|
///
|
|
|
|
|
/// This is similar to [poisoning with mutexes][poison].
|
|
|
|
|
///
|
|
|
|
|
/// [poison]: struct.Mutex.html#poisoning
|
2015-01-23 21:48:20 -08:00
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-07-10 22:47:59 -04:00
|
|
|
|
pub fn call_once<F>(&self, f: F) where F: FnOnce() {
|
2018-08-06 16:31:04 +03:00
|
|
|
|
// Fast path check
|
|
|
|
|
if self.is_completed() {
|
|
|
|
|
return;
|
2014-05-14 10:23:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 19:01:50 -07:00
|
|
|
|
let mut f = Some(f);
|
|
|
|
|
self.call_inner(false, &mut |_| f.take().unwrap()());
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-27 16:10:44 -04:00
|
|
|
|
/// Performs the same function as [`call_once`] except ignores poisoning.
|
|
|
|
|
///
|
2017-10-21 09:20:25 -04: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_one_force` will no-op.
|
|
|
|
|
///
|
|
|
|
|
/// The closure `f` is yielded a [`OnceState`] structure which can be used
|
|
|
|
|
/// to query the poison status of the `Once`.
|
|
|
|
|
///
|
2017-03-27 16:10:44 -04:00
|
|
|
|
/// [`call_once`]: struct.Once.html#method.call_once
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// [`OnceState`]: struct.OnceState.html
|
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
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(once_poison)]
|
2017-03-27 16:10:44 -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| {
|
|
|
|
|
/// assert!(state.poisoned());
|
|
|
|
|
/// });
|
|
|
|
|
///
|
|
|
|
|
/// // once any success happens, we stop propagating the poison
|
|
|
|
|
/// INIT.call_once(|| {});
|
|
|
|
|
/// ```
|
2016-05-11 21:01:29 -04:00
|
|
|
|
#[unstable(feature = "once_poison", issue = "33577")]
|
2018-07-10 22:47:59 -04: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
|
|
|
|
|
if self.is_completed() {
|
|
|
|
|
return;
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 19:01:50 -07:00
|
|
|
|
let mut f = Some(f);
|
|
|
|
|
self.call_inner(true, &mut |p| {
|
|
|
|
|
f.take().unwrap()(&OnceState { poisoned: p })
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 13:52:52 +03:00
|
|
|
|
/// Returns true if some `call_once` call has completed
|
|
|
|
|
/// successfuly. Specifically, `is_completed` will return false in
|
|
|
|
|
/// the following situtations:
|
|
|
|
|
/// * `call_once` was not called at all,
|
|
|
|
|
/// * `call_once` was called, but has not yet completed,
|
|
|
|
|
/// * the `Once` instance is poisoned
|
|
|
|
|
///
|
2018-08-06 16:31:04 +03:00
|
|
|
|
/// It is also possible that immediately after `is_completed`
|
|
|
|
|
/// returns false, some other thread finishes executing
|
|
|
|
|
/// `call_once`.
|
|
|
|
|
///
|
2018-08-03 13:52:52 +03:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(once_is_completed)]
|
|
|
|
|
/// 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);
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(once_is_completed)]
|
|
|
|
|
/// 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);
|
|
|
|
|
/// ```
|
2018-10-07 12:00:41 +02:00
|
|
|
|
#[unstable(feature = "once_is_completed", issue = "54890")]
|
2018-09-29 11:07:48 +03:00
|
|
|
|
#[inline]
|
2018-08-03 13:52:52 +03:00
|
|
|
|
pub fn is_completed(&self) -> bool {
|
2018-08-06 16:31:04 +03:00
|
|
|
|
// An `Acquire` load is enough because that makes all the initialization
|
|
|
|
|
// operations visible to us, and, this being a fast path, weaker
|
|
|
|
|
// ordering helps with performance. This `Acquire` synchronizes with
|
|
|
|
|
// `SeqCst` operations on the slow path.
|
2018-08-03 13:52:52 +03:00
|
|
|
|
self.state.load(Ordering::Acquire) == COMPLETE
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 19:01:50 -07:00
|
|
|
|
// This is a non-generic function to reduce the monomorphization cost of
|
|
|
|
|
// using `call_once` (this isn't exactly a trivial or small implementation).
|
|
|
|
|
//
|
|
|
|
|
// Additionally, this is tagged with `#[cold]` as it should indeed be cold
|
|
|
|
|
// and it helps let LLVM know that calls to this function should be off the
|
|
|
|
|
// fast path. Essentially, this should help generate more straight line code
|
|
|
|
|
// in LLVM.
|
|
|
|
|
//
|
|
|
|
|
// Finally, this takes an `FnMut` instead of a `FnOnce` because there's
|
|
|
|
|
// currently no way to take an `FnOnce` and call it via virtual dispatch
|
|
|
|
|
// without some allocation overhead.
|
|
|
|
|
#[cold]
|
2018-07-10 22:47:59 -04:00
|
|
|
|
fn call_inner(&self,
|
2016-03-17 19:01:50 -07:00
|
|
|
|
ignore_poisoning: bool,
|
2018-07-10 20:35:36 +02:00
|
|
|
|
init: &mut dyn FnMut(bool)) {
|
2018-08-06 16:31:04 +03:00
|
|
|
|
|
|
|
|
|
// This cold path uses SeqCst consistently because the
|
|
|
|
|
// performance difference really does not matter there, and
|
|
|
|
|
// SeqCst minimizes the chances of something going wrong.
|
2016-03-17 19:01:50 -07:00
|
|
|
|
let mut state = self.state.load(Ordering::SeqCst);
|
|
|
|
|
|
|
|
|
|
'outer: loop {
|
|
|
|
|
match state {
|
|
|
|
|
// If we're complete, then there's nothing to do, we just
|
|
|
|
|
// jettison out as we shouldn't run the closure.
|
|
|
|
|
COMPLETE => return,
|
|
|
|
|
|
|
|
|
|
// If we're poisoned and we're not in a mode to ignore
|
|
|
|
|
// poisoning, then we panic here to propagate the poison.
|
|
|
|
|
POISONED if !ignore_poisoning => {
|
|
|
|
|
panic!("Once instance has previously been poisoned");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise if we see a poisoned or otherwise incomplete state
|
|
|
|
|
// we will attempt to move ourselves into the RUNNING state. If
|
|
|
|
|
// we succeed, then the queue of waiters starts at null (all 0
|
|
|
|
|
// bits).
|
|
|
|
|
POISONED |
|
|
|
|
|
INCOMPLETE => {
|
|
|
|
|
let old = self.state.compare_and_swap(state, RUNNING,
|
|
|
|
|
Ordering::SeqCst);
|
|
|
|
|
if old != state {
|
|
|
|
|
state = old;
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run the initialization routine, letting it know if we're
|
|
|
|
|
// poisoned or not. The `Finish` struct is then dropped, and
|
|
|
|
|
// the `Drop` implementation here is responsible for waking
|
|
|
|
|
// up other waiters both in the normal return and panicking
|
|
|
|
|
// case.
|
|
|
|
|
let mut complete = Finish {
|
|
|
|
|
panicked: true,
|
|
|
|
|
me: self,
|
|
|
|
|
};
|
|
|
|
|
init(state == POISONED);
|
|
|
|
|
complete.panicked = false;
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All other values we find should correspond to the RUNNING
|
|
|
|
|
// state with an encoded waiter list in the more significant
|
|
|
|
|
// bits. We attempt to enqueue ourselves by moving us to the
|
|
|
|
|
// head of the list and bail out if we ever see a state that's
|
|
|
|
|
// not RUNNING.
|
|
|
|
|
_ => {
|
|
|
|
|
assert!(state & STATE_MASK == RUNNING);
|
|
|
|
|
let mut node = Waiter {
|
|
|
|
|
thread: Some(thread::current()),
|
|
|
|
|
signaled: AtomicBool::new(false),
|
2016-06-24 20:54:52 +02:00
|
|
|
|
next: ptr::null_mut(),
|
2016-03-17 19:01:50 -07:00
|
|
|
|
};
|
|
|
|
|
let me = &mut node as *mut Waiter as usize;
|
|
|
|
|
assert!(me & STATE_MASK == 0);
|
|
|
|
|
|
|
|
|
|
while state & STATE_MASK == RUNNING {
|
|
|
|
|
node.next = (state & !STATE_MASK) as *mut Waiter;
|
|
|
|
|
let old = self.state.compare_and_swap(state,
|
|
|
|
|
me | RUNNING,
|
|
|
|
|
Ordering::SeqCst);
|
|
|
|
|
if old != state {
|
|
|
|
|
state = old;
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Once we've enqueued ourselves, wait in a loop.
|
2017-02-15 23:26:29 +01:00
|
|
|
|
// Afterwards reload the state and continue with what we
|
2016-03-17 19:01:50 -07:00
|
|
|
|
// were doing from before.
|
|
|
|
|
while !node.signaled.load(Ordering::SeqCst) {
|
|
|
|
|
thread::park();
|
|
|
|
|
}
|
|
|
|
|
state = self.state.load(Ordering::SeqCst);
|
|
|
|
|
continue 'outer
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
2016-03-17 19:01:50 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-16 19:57:59 -08: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 {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
f.pad("Once { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-10 22:47:59 -04:00
|
|
|
|
impl<'a> Drop for Finish<'a> {
|
2016-03-17 19:01:50 -07:00
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
// Swap out our state with however we finished. We should only ever see
|
|
|
|
|
// an old state which was RUNNING.
|
|
|
|
|
let queue = if self.panicked {
|
|
|
|
|
self.me.state.swap(POISONED, Ordering::SeqCst)
|
|
|
|
|
} else {
|
|
|
|
|
self.me.state.swap(COMPLETE, Ordering::SeqCst)
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(queue & STATE_MASK, RUNNING);
|
|
|
|
|
|
|
|
|
|
// Decode the RUNNING to a list of waiters, then walk that entire list
|
|
|
|
|
// and wake them up. Note that it is crucial that after we store `true`
|
|
|
|
|
// in the node it can be free'd! As a result we load the `thread` to
|
|
|
|
|
// signal ahead of time and then unpark it after the store.
|
|
|
|
|
unsafe {
|
|
|
|
|
let mut queue = (queue & !STATE_MASK) as *mut Waiter;
|
|
|
|
|
while !queue.is_null() {
|
|
|
|
|
let next = (*queue).next;
|
|
|
|
|
let thread = (*queue).thread.take().unwrap();
|
|
|
|
|
(*queue).signaled.store(true, Ordering::SeqCst);
|
|
|
|
|
thread.unpark();
|
|
|
|
|
queue = next;
|
|
|
|
|
}
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-17 19:01:50 -07:00
|
|
|
|
impl OnceState {
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// Returns whether the associated [`Once`] was poisoned prior to the
|
|
|
|
|
/// invocation of the closure passed to [`call_once_force`].
|
2017-03-27 16:10:44 -04:00
|
|
|
|
///
|
2017-10-21 09:20:25 -04:00
|
|
|
|
/// [`call_once_force`]: struct.Once.html#method.call_once_force
|
2017-03-27 16:10:44 -04:00
|
|
|
|
/// [`Once`]: struct.Once.html
|
2017-10-21 09:20:25 -04:00
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// A poisoned `Once`:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(once_poison)]
|
|
|
|
|
///
|
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| {
|
|
|
|
|
/// assert!(state.poisoned());
|
|
|
|
|
/// });
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// An unpoisoned `Once`:
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// #![feature(once_poison)]
|
|
|
|
|
///
|
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| {
|
|
|
|
|
/// assert!(!state.poisoned());
|
|
|
|
|
/// });
|
2016-05-11 21:01:29 -04:00
|
|
|
|
#[unstable(feature = "once_poison", issue = "33577")]
|
2016-03-17 19:01:50 -07:00
|
|
|
|
pub fn poisoned(&self) -> bool {
|
|
|
|
|
self.poisoned
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-22 20:04:48 +00:00
|
|
|
|
#[cfg(all(test, not(target_os = "emscripten")))]
|
2015-04-24 17:30:41 +02:00
|
|
|
|
mod tests {
|
2016-03-17 19:01:50 -07:00
|
|
|
|
use panic;
|
|
|
|
|
use sync::mpsc::channel;
|
2015-02-17 15:10:25 -08:00
|
|
|
|
use thread;
|
2015-05-27 11:18:36 +03:00
|
|
|
|
use super::Once;
|
2014-01-16 19:57:59 -08:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn smoke_once() {
|
2015-05-27 11:18:36 +03:00
|
|
|
|
static O: Once = Once::new();
|
2015-01-25 22:05:03 +01:00
|
|
|
|
let mut a = 0;
|
2014-12-29 15:03:01 -08:00
|
|
|
|
O.call_once(|| a += 1);
|
2014-01-16 19:57:59 -08:00
|
|
|
|
assert_eq!(a, 1);
|
2014-12-29 15:03:01 -08:00
|
|
|
|
O.call_once(|| a += 1);
|
2014-01-16 19:57:59 -08:00
|
|
|
|
assert_eq!(a, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn stampede_once() {
|
2015-05-27 11:18:36 +03:00
|
|
|
|
static O: Once = Once::new();
|
2016-10-14 15:07:18 +03:00
|
|
|
|
static mut RUN: bool = false;
|
2014-01-16 19:57:59 -08:00
|
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
|
let (tx, rx) = channel();
|
2015-02-17 09:47:49 -05:00
|
|
|
|
for _ in 0..10 {
|
2014-03-09 14:58:32 -07:00
|
|
|
|
let tx = tx.clone();
|
2015-02-17 15:10:25 -08:00
|
|
|
|
thread::spawn(move|| {
|
2015-02-17 09:47:49 -05:00
|
|
|
|
for _ in 0..4 { thread::yield_now() }
|
2014-01-16 19:57:59 -08:00
|
|
|
|
unsafe {
|
2014-12-29 15:03:01 -08:00
|
|
|
|
O.call_once(|| {
|
2016-10-14 15:07:18 +03:00
|
|
|
|
assert!(!RUN);
|
|
|
|
|
RUN = true;
|
2014-01-16 19:57:59 -08:00
|
|
|
|
});
|
2016-10-14 15:07:18 +03:00
|
|
|
|
assert!(RUN);
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
2014-12-23 11:53:35 -08:00
|
|
|
|
tx.send(()).unwrap();
|
2015-01-05 21:59:45 -08:00
|
|
|
|
});
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe {
|
2014-12-29 15:03:01 -08:00
|
|
|
|
O.call_once(|| {
|
2016-10-14 15:07:18 +03:00
|
|
|
|
assert!(!RUN);
|
|
|
|
|
RUN = true;
|
2014-01-16 19:57:59 -08:00
|
|
|
|
});
|
2016-10-14 15:07:18 +03:00
|
|
|
|
assert!(RUN);
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 09:47:49 -05:00
|
|
|
|
for _ in 0..10 {
|
2014-12-23 11:53:35 -08:00
|
|
|
|
rx.recv().unwrap();
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-17 19:01:50 -07:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn poison_bad() {
|
|
|
|
|
static O: Once = Once::new();
|
|
|
|
|
|
|
|
|
|
// poison the once
|
2016-04-07 10:42:53 -07:00
|
|
|
|
let t = panic::catch_unwind(|| {
|
2016-03-17 19:01:50 -07:00
|
|
|
|
O.call_once(|| panic!());
|
|
|
|
|
});
|
|
|
|
|
assert!(t.is_err());
|
|
|
|
|
|
|
|
|
|
// poisoning propagates
|
2016-04-07 10:42:53 -07:00
|
|
|
|
let t = panic::catch_unwind(|| {
|
2016-03-17 19:01:50 -07:00
|
|
|
|
O.call_once(|| {});
|
|
|
|
|
});
|
|
|
|
|
assert!(t.is_err());
|
|
|
|
|
|
|
|
|
|
// we can subvert poisoning, however
|
|
|
|
|
let mut called = false;
|
|
|
|
|
O.call_once_force(|p| {
|
|
|
|
|
called = true;
|
|
|
|
|
assert!(p.poisoned())
|
|
|
|
|
});
|
|
|
|
|
assert!(called);
|
|
|
|
|
|
|
|
|
|
// once any success happens, we stop propagating the poison
|
|
|
|
|
O.call_once(|| {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn wait_for_force_to_finish() {
|
|
|
|
|
static O: Once = Once::new();
|
|
|
|
|
|
|
|
|
|
// poison the once
|
2016-04-07 10:42:53 -07:00
|
|
|
|
let t = panic::catch_unwind(|| {
|
2016-03-17 19:01:50 -07:00
|
|
|
|
O.call_once(|| panic!());
|
|
|
|
|
});
|
|
|
|
|
assert!(t.is_err());
|
|
|
|
|
|
|
|
|
|
// make sure someone's waiting inside the once via a force
|
|
|
|
|
let (tx1, rx1) = channel();
|
|
|
|
|
let (tx2, rx2) = channel();
|
|
|
|
|
let t1 = thread::spawn(move || {
|
|
|
|
|
O.call_once_force(|p| {
|
|
|
|
|
assert!(p.poisoned());
|
|
|
|
|
tx1.send(()).unwrap();
|
|
|
|
|
rx2.recv().unwrap();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
rx1.recv().unwrap();
|
|
|
|
|
|
|
|
|
|
// put another waiter on the once
|
|
|
|
|
let t2 = thread::spawn(|| {
|
|
|
|
|
let mut called = false;
|
|
|
|
|
O.call_once(|| {
|
|
|
|
|
called = true;
|
|
|
|
|
});
|
|
|
|
|
assert!(!called);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tx2.send(()).unwrap();
|
|
|
|
|
|
|
|
|
|
assert!(t1.join().is_ok());
|
|
|
|
|
assert!(t2.join().is_ok());
|
|
|
|
|
|
|
|
|
|
}
|
2014-01-16 19:57:59 -08:00
|
|
|
|
}
|