diff options
| author | Aleksey Kladov <aleksey.kladov@gmail.com> | 2018-08-03 13:52:52 +0300 |
|---|---|---|
| committer | Aleksey Kladov <aleksey.kladov@gmail.com> | 2018-08-03 15:49:23 +0300 |
| commit | 6aba6f9184e0e738664c219c58feadb70f967f33 (patch) | |
| tree | 142019e9f7d662e6192809c86dca9749270ab13b /src/libstd/sync | |
| parent | 4dae47051315793d4d4d043b4b8b03c51e3dc877 (diff) | |
| download | rust-6aba6f9184e0e738664c219c58feadb70f967f33.tar.gz rust-6aba6f9184e0e738664c219c58feadb70f967f33.zip | |
Allow to check if sync::Once is initialized
Diffstat (limited to 'src/libstd/sync')
| -rw-r--r-- | src/libstd/sync/once.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/libstd/sync/once.rs b/src/libstd/sync/once.rs index 10282ecb658..3ca6a135345 100644 --- a/src/libstd/sync/once.rs +++ b/src/libstd/sync/once.rs @@ -295,6 +295,47 @@ impl Once { }); } + /// 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 + /// + /// # 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); + /// ``` + #[unstable(feature = "once_is_completed", issue = "42")] + pub fn is_completed(&self) -> bool { + self.state.load(Ordering::Acquire) == COMPLETE + } + // This is a non-generic function to reduce the monomorphization cost of // using `call_once` (this isn't exactly a trivial or small implementation). // |
