diff options
Diffstat (limited to 'src/libstd/sync')
| -rw-r--r-- | src/libstd/sync/condvar.rs | 47 | ||||
| -rw-r--r-- | src/libstd/sync/mpsc/blocking.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sync/mutex.rs | 25 | ||||
| -rw-r--r-- | src/libstd/sync/once.rs | 26 | ||||
| -rw-r--r-- | src/libstd/sync/rwlock.rs | 25 |
5 files changed, 77 insertions, 50 deletions
diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index 8da917916e5..f2c389f9426 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -10,7 +10,7 @@ use prelude::v1::*; -use sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; +use sync::atomic::{AtomicUsize, Ordering}; use sync::{mutex, MutexGuard, PoisonError}; use sys_common::condvar as sys; use sys_common::mutex as sys_mutex; @@ -84,10 +84,7 @@ pub struct StaticCondvar { /// Constant initializer for a statically allocated condition variable. #[unstable(feature = "static_condvar", reason = "may be merged with Condvar in the future")] -pub const CONDVAR_INIT: StaticCondvar = StaticCondvar { - inner: sys::CONDVAR_INIT, - mutex: ATOMIC_USIZE_INIT, -}; +pub const CONDVAR_INIT: StaticCondvar = StaticCondvar::new(); impl Condvar { /// Creates a new condition variable which is ready to be waited on and @@ -96,7 +93,7 @@ impl Condvar { pub fn new() -> Condvar { Condvar { inner: box StaticCondvar { - inner: unsafe { sys::Condvar::new() }, + inner: sys::Condvar::new(), mutex: AtomicUsize::new(0), } } @@ -234,6 +231,16 @@ impl Drop for Condvar { } impl StaticCondvar { + /// Creates a new condition variable + #[unstable(feature = "static_condvar", + reason = "may be merged with Condvar in the future")] + pub const fn new() -> StaticCondvar { + StaticCondvar { + inner: sys::Condvar::new(), + mutex: AtomicUsize::new(0), + } + } + /// Blocks the current thread until this condition variable receives a /// notification. /// @@ -388,10 +395,10 @@ impl StaticCondvar { mod tests { use prelude::v1::*; - use super::{StaticCondvar, CONDVAR_INIT}; + use super::StaticCondvar; use sync::mpsc::channel; - use sync::{StaticMutex, MUTEX_INIT, Condvar, Mutex, Arc}; - use sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; + use sync::{StaticMutex, Condvar, Mutex, Arc}; + use sync::atomic::{AtomicUsize, Ordering}; use thread; use time::Duration; use u32; @@ -405,7 +412,7 @@ mod tests { #[test] fn static_smoke() { - static C: StaticCondvar = CONDVAR_INIT; + static C: StaticCondvar = StaticCondvar::new(); C.notify_one(); C.notify_all(); unsafe { C.destroy(); } @@ -413,8 +420,8 @@ mod tests { #[test] fn notify_one() { - static C: StaticCondvar = CONDVAR_INIT; - static M: StaticMutex = MUTEX_INIT; + static C: StaticCondvar = StaticCondvar::new(); + static M: StaticMutex = StaticMutex::new(); let g = M.lock().unwrap(); let _t = thread::spawn(move|| { @@ -464,8 +471,8 @@ mod tests { #[test] fn wait_timeout_ms() { - static C: StaticCondvar = CONDVAR_INIT; - static M: StaticMutex = MUTEX_INIT; + static C: StaticCondvar = StaticCondvar::new(); + static M: StaticMutex = StaticMutex::new(); let g = M.lock().unwrap(); let (g, _no_timeout) = C.wait_timeout_ms(g, 1).unwrap(); @@ -483,9 +490,9 @@ mod tests { #[test] fn wait_timeout_with() { - static C: StaticCondvar = CONDVAR_INIT; - static M: StaticMutex = MUTEX_INIT; - static S: AtomicUsize = ATOMIC_USIZE_INIT; + static C: StaticCondvar = StaticCondvar::new(); + static M: StaticMutex = StaticMutex::new(); + static S: AtomicUsize = AtomicUsize::new(0); let g = M.lock().unwrap(); let (g, success) = C.wait_timeout_with(g, Duration::new(0, 1000), |_| { @@ -530,9 +537,9 @@ mod tests { #[test] #[should_panic] fn two_mutexes() { - static M1: StaticMutex = MUTEX_INIT; - static M2: StaticMutex = MUTEX_INIT; - static C: StaticCondvar = CONDVAR_INIT; + static M1: StaticMutex = StaticMutex::new(); + static M2: StaticMutex = StaticMutex::new(); + static C: StaticCondvar = StaticCondvar::new(); let mut g = M1.lock().unwrap(); let _t = thread::spawn(move|| { diff --git a/src/libstd/sync/mpsc/blocking.rs b/src/libstd/sync/mpsc/blocking.rs index 2e4155ea351..0e5a9859116 100644 --- a/src/libstd/sync/mpsc/blocking.rs +++ b/src/libstd/sync/mpsc/blocking.rs @@ -11,7 +11,7 @@ //! Generic support for building blocking abstractions. use thread::{self, Thread}; -use sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering}; +use sync::atomic::{AtomicBool, Ordering}; use sync::Arc; use marker::{Sync, Send}; use mem; @@ -41,7 +41,7 @@ impl !Sync for WaitToken {} pub fn tokens() -> (WaitToken, SignalToken) { let inner = Arc::new(Inner { thread: thread::current(), - woken: ATOMIC_BOOL_INIT, + woken: AtomicBool::new(false), }); let wait_token = WaitToken { inner: inner.clone(), diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index f9ed7c863d1..fd22d723ebd 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -178,17 +178,14 @@ impl<'a, T: ?Sized> !marker::Send for MutexGuard<'a, T> {} /// other mutex constants. #[unstable(feature = "std_misc", reason = "may be merged with Mutex in the future")] -pub const MUTEX_INIT: StaticMutex = StaticMutex { - lock: sys::MUTEX_INIT, - poison: poison::FLAG_INIT, -}; +pub const MUTEX_INIT: StaticMutex = StaticMutex::new(); impl<T> Mutex<T> { /// Creates a new mutex in an unlocked state ready for use. #[stable(feature = "rust1", since = "1.0.0")] pub fn new(t: T) -> Mutex<T> { Mutex { - inner: box MUTEX_INIT, + inner: box StaticMutex::new(), data: UnsafeCell::new(t), } } @@ -271,9 +268,19 @@ impl<T: ?Sized + fmt::Debug + 'static> fmt::Debug for Mutex<T> { struct Dummy(UnsafeCell<()>); unsafe impl Sync for Dummy {} -static DUMMY: Dummy = Dummy(UnsafeCell { value: () }); +static DUMMY: Dummy = Dummy(UnsafeCell::new(())); impl StaticMutex { + /// Creates a new mutex in an unlocked state ready for use. + #[unstable(feature = "std_misc", + reason = "may be merged with Mutex in the future")] + pub const fn new() -> StaticMutex { + StaticMutex { + lock: sys::Mutex::new(), + poison: poison::Flag::new(), + } + } + /// Acquires this lock, see `Mutex::lock` #[inline] #[unstable(feature = "std_misc", @@ -365,7 +372,7 @@ mod tests { use prelude::v1::*; use sync::mpsc::channel; - use sync::{Arc, Mutex, StaticMutex, MUTEX_INIT, Condvar}; + use sync::{Arc, Mutex, StaticMutex, Condvar}; use thread; struct Packet<T: Send>(Arc<(Mutex<T>, Condvar)>); @@ -382,7 +389,7 @@ mod tests { #[test] fn smoke_static() { - static M: StaticMutex = MUTEX_INIT; + static M: StaticMutex = StaticMutex::new(); unsafe { drop(M.lock().unwrap()); drop(M.lock().unwrap()); @@ -392,7 +399,7 @@ mod tests { #[test] fn lots_and_lots() { - static M: StaticMutex = MUTEX_INIT; + static M: StaticMutex = StaticMutex::new(); static mut CNT: u32 = 0; const J: u32 = 1000; const K: u32 = 3; 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(); diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index 36f6fbf3b72..e7c3d744c17 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -102,10 +102,7 @@ pub struct StaticRwLock { /// Constant initialization for a statically-initialized rwlock. #[unstable(feature = "std_misc", reason = "may be merged with RwLock in the future")] -pub const RW_LOCK_INIT: StaticRwLock = StaticRwLock { - lock: sys::RWLOCK_INIT, - poison: poison::FLAG_INIT, -}; +pub const RW_LOCK_INIT: StaticRwLock = StaticRwLock::new(); /// RAII structure used to release the shared read access of a lock when /// dropped. @@ -142,7 +139,7 @@ impl<T> RwLock<T> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn new(t: T) -> RwLock<T> { - RwLock { inner: box RW_LOCK_INIT, data: UnsafeCell::new(t) } + RwLock { inner: box StaticRwLock::new(), data: UnsafeCell::new(t) } } } @@ -280,9 +277,19 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> { struct Dummy(UnsafeCell<()>); unsafe impl Sync for Dummy {} -static DUMMY: Dummy = Dummy(UnsafeCell { value: () }); +static DUMMY: Dummy = Dummy(UnsafeCell::new(())); impl StaticRwLock { + /// Creates a new rwlock. + #[unstable(feature = "std_misc", + reason = "may be merged with RwLock in the future")] + pub const fn new() -> StaticRwLock { + StaticRwLock { + lock: sys::RWLock::new(), + poison: poison::Flag::new(), + } + } + /// Locks this rwlock with shared read access, blocking the current thread /// until it can be acquired. /// @@ -420,7 +427,7 @@ mod tests { use rand::{self, Rng}; use sync::mpsc::channel; use thread; - use sync::{Arc, RwLock, StaticRwLock, TryLockError, RW_LOCK_INIT}; + use sync::{Arc, RwLock, StaticRwLock, TryLockError}; #[test] fn smoke() { @@ -433,7 +440,7 @@ mod tests { #[test] fn static_smoke() { - static R: StaticRwLock = RW_LOCK_INIT; + static R: StaticRwLock = StaticRwLock::new(); drop(R.read().unwrap()); drop(R.write().unwrap()); drop((R.read().unwrap(), R.read().unwrap())); @@ -443,7 +450,7 @@ mod tests { #[test] fn frob() { - static R: StaticRwLock = RW_LOCK_INIT; + static R: StaticRwLock = StaticRwLock::new(); const N: usize = 10; const M: usize = 1000; |
