Skip to content

Improve docs around Once::call_once_force and OnceState. #45429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 81 additions & 13 deletions src/libstd/sync/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ unsafe impl Sync for Once {}
#[stable(feature = "rust1", since = "1.0.0")]
unsafe impl Send for Once {}

/// State yielded to the [`call_once_force`] method which can be used to query
/// whether the [`Once`] was previously poisoned or not.
/// State yielded to [`call_once_force`]’s closure parameter. The state can be
/// used to query the poison status of the [`Once`].
///
/// [`call_once_force`]: struct.Once.html#method.call_once_force
/// [`Once`]: struct.Once.html
Expand Down Expand Up @@ -230,17 +230,50 @@ impl Once {

/// Performs the same function as [`call_once`] except ignores poisoning.
///
/// 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`.
///
/// [`call_once`]: struct.Once.html#method.call_once
/// [`OnceState`]: struct.OnceState.html
///
/// If this `Once` has been poisoned (some initialization panicked) then
/// this function will continue to attempt to call initialization functions
/// until one of them doesn't panic.
/// # Examples
///
/// The closure `f` is yielded a [`OnceState`] structure which can be used to query the
/// state of this `Once` (whether initialization has previously panicked or
/// not).
/// ```
/// #![feature(once_poison)]
///
/// [`OnceState`]: struct.OnceState.html
/// use std::sync::{Once, ONCE_INIT};
/// use std::thread;
///
/// static INIT: Once = ONCE_INIT;
///
/// // 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(|| {});
/// ```
#[unstable(feature = "once_poison", issue = "33577")]
pub fn call_once_force<F>(&'static self, f: F) where F: FnOnce(&OnceState) {
// same as above, just with a different parameter to `call_inner`.
Expand Down Expand Up @@ -386,12 +419,47 @@ impl Drop for Finish {
}

impl OnceState {
/// Returns whether the associated [`Once`] has been poisoned.
///
/// Once an initialization routine for a [`Once`] has panicked it will forever
/// indicate to future forced initialization routines that it is poisoned.
/// Returns whether the associated [`Once`] was poisoned prior to the
/// invocation of the closure passed to [`call_once_force`].
///
/// [`call_once_force`]: struct.Once.html#method.call_once_force
/// [`Once`]: struct.Once.html
///
/// # Examples
///
/// A poisoned `Once`:
///
/// ```
/// #![feature(once_poison)]
///
/// use std::sync::{Once, ONCE_INIT};
/// use std::thread;
///
/// static INIT: Once = ONCE_INIT;
///
/// // 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`:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

///
/// ```
/// #![feature(once_poison)]
///
/// use std::sync::{Once, ONCE_INIT};
///
/// static INIT: Once = ONCE_INIT;
///
/// INIT.call_once_force(|state| {
/// assert!(!state.poisoned());
/// });
#[unstable(feature = "once_poison", issue = "33577")]
pub fn poisoned(&self) -> bool {
self.poisoned
Expand Down