diff options
| author | Eduard Burtescu <edy.burt@gmail.com> | 2015-05-27 11:18:36 +0300 |
|---|---|---|
| committer | Eduard Burtescu <edy.burt@gmail.com> | 2015-05-27 11:19:03 +0300 |
| commit | 377b0900aede976b2d37a499bbd7b62c2e39b358 (patch) | |
| tree | b4a5a4431d36ed1a4e0a39c7d2ef2563ecac9bf4 /src/libstd/sync/once.rs | |
| parent | 6e8e4f847c2ea02fec021ea15dfb2de6beac797a (diff) | |
| download | rust-377b0900aede976b2d37a499bbd7b62c2e39b358.tar.gz rust-377b0900aede976b2d37a499bbd7b62c2e39b358.zip | |
Use `const fn` to abstract away the contents of UnsafeCell & friends.
Diffstat (limited to 'src/libstd/sync/once.rs')
| -rw-r--r-- | src/libstd/sync/once.rs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/libstd/sync/once.rs b/src/libstd/sync/once.rs index 57baedaad9c..269affff208 100644 --- a/src/libstd/sync/once.rs +++ b/src/libstd/sync/once.rs @@ -16,8 +16,8 @@ use prelude::v1::*; use isize; -use sync::atomic::{AtomicIsize, Ordering, ATOMIC_ISIZE_INIT}; -use sync::{StaticMutex, MUTEX_INIT}; +use sync::atomic::{AtomicIsize, Ordering}; +use sync::StaticMutex; /// A synchronization primitive which can be used to run a one-time global /// initialization. Useful for one-time initialization for FFI or related @@ -44,13 +44,19 @@ pub struct Once { /// Initialization value for static `Once` values. #[stable(feature = "rust1", since = "1.0.0")] -pub const ONCE_INIT: Once = Once { - mutex: MUTEX_INIT, - cnt: ATOMIC_ISIZE_INIT, - lock_cnt: ATOMIC_ISIZE_INIT, -}; +pub const ONCE_INIT: Once = Once::new(); impl Once { + /// Creates a new `Once` value. + #[unstable(feature = "std_misc")] + pub const fn new() -> Once { + Once { + mutex: StaticMutex::new(), + cnt: AtomicIsize::new(0), + lock_cnt: AtomicIsize::new(0), + } + } + /// Performs an initialization routine once and only once. The given closure /// will be executed if this is the first time `call_once` has been called, /// and otherwise the routine will *not* be invoked. @@ -129,12 +135,12 @@ mod tests { use prelude::v1::*; use thread; - use super::{ONCE_INIT, Once}; + use super::Once; use sync::mpsc::channel; #[test] fn smoke_once() { - static O: Once = ONCE_INIT; + static O: Once = Once::new(); let mut a = 0; O.call_once(|| a += 1); assert_eq!(a, 1); @@ -144,7 +150,7 @@ mod tests { #[test] fn stampede_once() { - static O: Once = ONCE_INIT; + static O: Once = Once::new(); static mut run: bool = false; let (tx, rx) = channel(); |
