Skip to content

Commit 6aba6f9

Browse files
committed
Allow to check if sync::Once is initialized
1 parent 4dae470 commit 6aba6f9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/libstd/sync/once.rs

+41
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,47 @@ impl Once {
295295
});
296296
}
297297

298+
/// Returns true if some `call_once` call has completed
299+
/// successfuly. Specifically, `is_completed` will return false in
300+
/// the following situtations:
301+
/// * `call_once` was not called at all,
302+
/// * `call_once` was called, but has not yet completed,
303+
/// * the `Once` instance is poisoned
304+
///
305+
/// # Examples
306+
///
307+
/// ```
308+
/// #![feature(once_is_completed)]
309+
/// use std::sync::Once;
310+
///
311+
/// static INIT: Once = Once::new();
312+
///
313+
/// assert_eq!(INIT.is_completed(), false);
314+
/// INIT.call_once(|| {
315+
/// assert_eq!(INIT.is_completed(), false);
316+
/// });
317+
/// assert_eq!(INIT.is_completed(), true);
318+
/// ```
319+
///
320+
/// ```
321+
/// #![feature(once_is_completed)]
322+
/// use std::sync::Once;
323+
/// use std::thread;
324+
///
325+
/// static INIT: Once = Once::new();
326+
///
327+
/// assert_eq!(INIT.is_completed(), false);
328+
/// let handle = thread::spawn(|| {
329+
/// INIT.call_once(|| panic!());
330+
/// });
331+
/// assert!(handle.join().is_err());
332+
/// assert_eq!(INIT.is_completed(), false);
333+
/// ```
334+
#[unstable(feature = "once_is_completed", issue = "42")]
335+
pub fn is_completed(&self) -> bool {
336+
self.state.load(Ordering::Acquire) == COMPLETE
337+
}
338+
298339
// This is a non-generic function to reduce the monomorphization cost of
299340
// using `call_once` (this isn't exactly a trivial or small implementation).
300341
//

0 commit comments

Comments
 (0)