diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:04 -0500 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:47 -0500 |
| commit | a06baa56b95674fc626b3c3fd680d6a65357fe60 (patch) | |
| tree | cd9d867c2ca3cff5c1d6b3bd73377c44649fb075 /src/libstd/sync | |
| parent | 8eb7c58dbb7b32701af113bc58722d0d1fefb1eb (diff) | |
| download | rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.tar.gz rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.zip | |
Format the world
Diffstat (limited to 'src/libstd/sync')
| -rw-r--r-- | src/libstd/sync/condvar.rs | 97 | ||||
| -rw-r--r-- | src/libstd/sync/mod.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sync/mutex.rs | 62 | ||||
| -rw-r--r-- | src/libstd/sync/rwlock.rs | 70 |
4 files changed, 121 insertions, 112 deletions
diff --git a/src/libstd/sync/condvar.rs b/src/libstd/sync/condvar.rs index 65ce19f2a1b..5a4cb14b72d 100644 --- a/src/libstd/sync/condvar.rs +++ b/src/libstd/sync/condvar.rs @@ -127,10 +127,7 @@ impl Condvar { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn new() -> Condvar { - let mut c = Condvar { - inner: box sys::Condvar::new(), - mutex: AtomicUsize::new(0), - }; + let mut c = Condvar { inner: box sys::Condvar::new(), mutex: AtomicUsize::new(0) }; unsafe { c.inner.init(); } @@ -196,19 +193,14 @@ impl Condvar { /// } /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) - -> LockResult<MutexGuard<'a, T>> { + pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> { let poisoned = unsafe { let lock = mutex::guard_lock(&guard); self.verify(lock); self.inner.wait(lock); mutex::guard_poison(&guard).get() }; - if poisoned { - Err(PoisonError::new(guard)) - } else { - Ok(guard) - } + if poisoned { Err(PoisonError::new(guard)) } else { Ok(guard) } } /// Blocks the current thread until this condition variable receives a @@ -258,17 +250,20 @@ impl Condvar { /// let _guard = cvar.wait_until(lock.lock().unwrap(), |started| { *started }).unwrap(); /// ``` #[unstable(feature = "wait_until", issue = "47960")] - pub fn wait_until<'a, T, F>(&self, mut guard: MutexGuard<'a, T>, - mut condition: F) - -> LockResult<MutexGuard<'a, T>> - where F: FnMut(&mut T) -> bool { + pub fn wait_until<'a, T, F>( + &self, + mut guard: MutexGuard<'a, T>, + mut condition: F, + ) -> LockResult<MutexGuard<'a, T>> + where + F: FnMut(&mut T) -> bool, + { while !condition(&mut *guard) { guard = self.wait(guard)?; } Ok(guard) } - /// Waits on this condition variable for a notification, timing out after a /// specified duration. /// @@ -324,12 +319,13 @@ impl Condvar { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[rustc_deprecated(since = "1.6.0", reason = "replaced by `std::sync::Condvar::wait_timeout`")] - pub fn wait_timeout_ms<'a, T>(&self, guard: MutexGuard<'a, T>, ms: u32) - -> LockResult<(MutexGuard<'a, T>, bool)> { + pub fn wait_timeout_ms<'a, T>( + &self, + guard: MutexGuard<'a, T>, + ms: u32, + ) -> LockResult<(MutexGuard<'a, T>, bool)> { let res = self.wait_timeout(guard, Duration::from_millis(ms as u64)); - poison::map_result(res, |(a, b)| { - (a, !b.timed_out()) - }) + poison::map_result(res, |(a, b)| (a, !b.timed_out())) } /// Waits on this condition variable for a notification, timing out after a @@ -396,20 +392,18 @@ impl Condvar { /// } /// ``` #[stable(feature = "wait_timeout", since = "1.5.0")] - pub fn wait_timeout<'a, T>(&self, guard: MutexGuard<'a, T>, - dur: Duration) - -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> { + pub fn wait_timeout<'a, T>( + &self, + guard: MutexGuard<'a, T>, + dur: Duration, + ) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> { let (poisoned, result) = unsafe { let lock = mutex::guard_lock(&guard); self.verify(lock); let success = self.inner.wait_timeout(lock, dur); (mutex::guard_poison(&guard).get(), WaitTimeoutResult(!success)) }; - if poisoned { - Err(PoisonError::new((guard, result))) - } else { - Ok((guard, result)) - } + if poisoned { Err(PoisonError::new((guard, result))) } else { Ok((guard, result)) } } /// Waits on this condition variable for a notification, timing out after a @@ -469,10 +463,15 @@ impl Condvar { /// // access the locked mutex via result.0 /// ``` #[unstable(feature = "wait_timeout_until", issue = "47960")] - pub fn wait_timeout_until<'a, T, F>(&self, mut guard: MutexGuard<'a, T>, - dur: Duration, mut condition: F) - -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> - where F: FnMut(&mut T) -> bool { + pub fn wait_timeout_until<'a, T, F>( + &self, + mut guard: MutexGuard<'a, T>, + dur: Duration, + mut condition: F, + ) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> + where + F: FnMut(&mut T) -> bool, + { let start = Instant::now(); loop { if condition(&mut *guard) { @@ -581,8 +580,10 @@ impl Condvar { // Anything else and we're using more than one mutex on this cvar, // which is currently disallowed. - _ => panic!("attempted to use a condition variable with two \ - mutexes"), + _ => panic!( + "attempted to use a condition variable with two \ + mutexes" + ), } } } @@ -611,10 +612,10 @@ impl Drop for Condvar { #[cfg(test)] mod tests { + use crate::sync::atomic::{AtomicBool, Ordering}; /// #![feature(wait_until)] use crate::sync::mpsc::channel; - use crate::sync::{Condvar, Mutex, Arc}; - use crate::sync::atomic::{AtomicBool, Ordering}; + use crate::sync::{Arc, Condvar, Mutex}; use crate::thread; use crate::time::Duration; use crate::u64; @@ -635,7 +636,7 @@ mod tests { let c2 = c.clone(); let g = m.lock().unwrap(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { let _g = m2.lock().unwrap(); c2.notify_one(); }); @@ -653,7 +654,7 @@ mod tests { for _ in 0..N { let data = data.clone(); let tx = tx.clone(); - thread::spawn(move|| { + thread::spawn(move || { let &(ref lock, ref cond) = &*data; let mut cnt = lock.lock().unwrap(); *cnt += 1; @@ -687,7 +688,7 @@ mod tests { let pair2 = pair.clone(); // Inside of our lock, spawn a new thread, and then wait for it to start. - thread::spawn(move|| { + thread::spawn(move || { let &(ref lock, ref cvar) = &*pair2; let mut started = lock.lock().unwrap(); *started = true; @@ -697,9 +698,7 @@ mod tests { // Wait for the thread to start up. let &(ref lock, ref cvar) = &*pair; - let guard = cvar.wait_until(lock.lock().unwrap(), |started| { - *started - }); + let guard = cvar.wait_until(lock.lock().unwrap(), |started| *started); assert!(*guard.unwrap()); } @@ -731,7 +730,7 @@ mod tests { let c = Arc::new(Condvar::new()); let g = m.lock().unwrap(); - let (_g, wait) = c.wait_timeout_until(g, Duration::from_millis(1), |_| { false }).unwrap(); + let (_g, wait) = c.wait_timeout_until(g, Duration::from_millis(1), |_| false).unwrap(); // no spurious wakeups. ensure it timed-out assert!(wait.timed_out()); } @@ -743,7 +742,7 @@ mod tests { let c = Arc::new(Condvar::new()); let g = m.lock().unwrap(); - let (_g, wait) = c.wait_timeout_until(g, Duration::from_millis(0), |_| { true }).unwrap(); + let (_g, wait) = c.wait_timeout_until(g, Duration::from_millis(0), |_| true).unwrap(); // ensure it didn't time-out even if we were not given any time. assert!(!wait.timed_out()); } @@ -764,9 +763,9 @@ mod tests { *started = true; cvar.notify_one(); }); - let (g2, wait) = c.wait_timeout_until(g, Duration::from_millis(u64::MAX), |&mut notified| { - notified - }).unwrap(); + let (g2, wait) = c + .wait_timeout_until(g, Duration::from_millis(u64::MAX), |&mut notified| notified) + .unwrap(); // ensure it didn't time-out even if we were not given any time. assert!(!wait.timed_out()); assert!(*g2); @@ -820,7 +819,7 @@ mod tests { let c2 = c.clone(); let mut g = m.lock().unwrap(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { let _g = m2.lock().unwrap(); c2.notify_one(); }); diff --git a/src/libstd/sync/mod.rs b/src/libstd/sync/mod.rs index fd6e46fd61d..b6699910b07 100644 --- a/src/libstd/sync/mod.rs +++ b/src/libstd/sync/mod.rs @@ -166,9 +166,9 @@ pub use self::mutex::{Mutex, MutexGuard}; #[allow(deprecated)] pub use self::once::{Once, OnceState, ONCE_INIT}; #[stable(feature = "rust1", since = "1.0.0")] -pub use crate::sys_common::poison::{PoisonError, TryLockError, TryLockResult, LockResult}; -#[stable(feature = "rust1", since = "1.0.0")] pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}; +#[stable(feature = "rust1", since = "1.0.0")] +pub use crate::sys_common::poison::{LockResult, PoisonError, TryLockError, TryLockResult}; pub mod mpsc; diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index e90da699060..6eeddc28512 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -4,7 +4,7 @@ use crate::mem; use crate::ops::{Deref, DerefMut}; use crate::ptr; use crate::sys_common::mutex as sys; -use crate::sys_common::poison::{self, TryLockError, TryLockResult, LockResult}; +use crate::sys_common::poison::{self, LockResult, TryLockError, TryLockResult}; /// A mutual exclusion primitive useful for protecting shared data /// @@ -122,9 +122,9 @@ pub struct Mutex<T: ?Sized> { // these are the only places where `T: Send` matters; all other // functionality works fine on a single thread. #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<T: ?Sized + Send> Send for Mutex<T> { } +unsafe impl<T: ?Sized + Send> Send for Mutex<T> {} #[stable(feature = "rust1", since = "1.0.0")] -unsafe impl<T: ?Sized + Send> Sync for Mutex<T> { } +unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {} /// An RAII implementation of a "scoped lock" of a mutex. When this structure is /// dropped (falls out of scope), the lock will be unlocked. @@ -148,9 +148,9 @@ pub struct MutexGuard<'a, T: ?Sized + 'a> { } #[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized> !Send for MutexGuard<'_, T> { } +impl<T: ?Sized> !Send for MutexGuard<'_, T> {} #[stable(feature = "mutexguard", since = "1.19.0")] -unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> { } +unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {} impl<T> Mutex<T> { /// Creates a new mutex in an unlocked state ready for use. @@ -309,7 +309,10 @@ impl<T: ?Sized> Mutex<T> { /// assert_eq!(mutex.into_inner().unwrap(), 0); /// ``` #[stable(feature = "mutex_into_inner", since = "1.6.0")] - pub fn into_inner(self) -> LockResult<T> where T: Sized { + pub fn into_inner(self) -> LockResult<T> + where + T: Sized, + { // We know statically that there are no outstanding references to // `self` so there's no need to lock the inner mutex. // @@ -323,7 +326,7 @@ impl<T: ?Sized> Mutex<T> { (ptr::read(inner), ptr::read(poison), ptr::read(data)) }; mem::forget(self); - inner.destroy(); // Keep in sync with the `Drop` impl. + inner.destroy(); // Keep in sync with the `Drop` impl. drop(inner); poison::map_result(poison.borrow(), |_| data.into_inner()) @@ -354,7 +357,7 @@ impl<T: ?Sized> Mutex<T> { // We know statically that there are no other references to `self`, so // there's no need to lock the inner mutex. let data = unsafe { &mut *self.data.get() }; - poison::map_result(self.poison.borrow(), |_| data ) + poison::map_result(self.poison.borrow(), |_| data) } } @@ -396,7 +399,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> { Ok(guard) => f.debug_struct("Mutex").field("data", &&*guard).finish(), Err(TryLockError::Poisoned(err)) => { f.debug_struct("Mutex").field("data", &&**err.get_ref()).finish() - }, + } Err(TryLockError::WouldBlock) => { struct LockedPlaceholder; impl fmt::Debug for LockedPlaceholder { @@ -413,12 +416,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> { impl<'mutex, T: ?Sized> MutexGuard<'mutex, T> { unsafe fn new(lock: &'mutex Mutex<T>) -> LockResult<MutexGuard<'mutex, T>> { - poison::map_result(lock.poison.borrow(), |guard| { - MutexGuard { - lock: lock, - poison: guard, - } - }) + poison::map_result(lock.poison.borrow(), |guard| MutexGuard { lock: lock, poison: guard }) } } @@ -473,9 +471,9 @@ pub fn guard_poison<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a poison::Fla #[cfg(all(test, not(target_os = "emscripten")))] mod tests { - use crate::sync::mpsc::channel; - use crate::sync::{Arc, Mutex, Condvar}; use crate::sync::atomic::{AtomicUsize, Ordering}; + use crate::sync::mpsc::channel; + use crate::sync::{Arc, Condvar, Mutex}; use crate::thread; struct Packet<T>(Arc<(Mutex<T>, Condvar)>); @@ -507,10 +505,16 @@ mod tests { for _ in 0..K { let tx2 = tx.clone(); let m2 = m.clone(); - thread::spawn(move|| { inc(&m2); tx2.send(()).unwrap(); }); + thread::spawn(move || { + inc(&m2); + tx2.send(()).unwrap(); + }); let tx2 = tx.clone(); let m2 = m.clone(); - thread::spawn(move|| { inc(&m2); tx2.send(()).unwrap(); }); + thread::spawn(move || { + inc(&m2); + tx2.send(()).unwrap(); + }); } drop(tx); @@ -557,7 +561,8 @@ mod tests { let _ = thread::spawn(move || { let _lock = m2.lock().unwrap(); panic!("test panic in inner thread to poison mutex"); - }).join(); + }) + .join(); assert!(m.is_poisoned()); match Arc::try_unwrap(m).unwrap().into_inner() { @@ -580,7 +585,8 @@ mod tests { let _ = thread::spawn(move || { let _lock = m2.lock().unwrap(); panic!("test panic in inner thread to poison mutex"); - }).join(); + }) + .join(); assert!(m.is_poisoned()); match Arc::try_unwrap(m).unwrap().get_mut() { @@ -594,7 +600,7 @@ mod tests { let packet = Packet(Arc::new((Mutex::new(false), Condvar::new()))); let packet2 = Packet(packet.0.clone()); let (tx, rx) = channel(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { // wait until parent gets in rx.recv().unwrap(); let &(ref lock, ref cvar) = &*packet2.0; @@ -646,10 +652,11 @@ mod tests { let arc = Arc::new(Mutex::new(1)); assert!(!arc.is_poisoned()); let arc2 = arc.clone(); - let _ = thread::spawn(move|| { + let _ = thread::spawn(move || { let lock = arc2.lock().unwrap(); assert_eq!(*lock, 2); - }).join(); + }) + .join(); assert!(arc.lock().is_err()); assert!(arc.is_poisoned()); } @@ -661,7 +668,7 @@ mod tests { let arc = Arc::new(Mutex::new(1)); let arc2 = Arc::new(Mutex::new(arc)); let (tx, rx) = channel(); - let _t = thread::spawn(move|| { + let _t = thread::spawn(move || { let lock = arc2.lock().unwrap(); let lock2 = lock.lock().unwrap(); assert_eq!(*lock2, 1); @@ -674,7 +681,7 @@ mod tests { fn test_mutex_arc_access_in_unwind() { let arc = Arc::new(Mutex::new(1)); let arc2 = arc.clone(); - let _ = thread::spawn(move|| -> () { + let _ = thread::spawn(move || -> () { struct Unwinder { i: Arc<Mutex<i32>>, } @@ -685,7 +692,8 @@ mod tests { } let _u = Unwinder { i: arc2 }; panic!(); - }).join(); + }) + .join(); let lock = arc.lock().unwrap(); assert_eq!(*lock, 2); } diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index c217291a42e..2ff36133a7c 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -360,7 +360,10 @@ impl<T: ?Sized> RwLock<T> { /// assert_eq!(lock.into_inner().unwrap(), "modified"); /// ``` #[stable(feature = "rwlock_into_inner", since = "1.6.0")] - pub fn into_inner(self) -> LockResult<T> where T: Sized { + pub fn into_inner(self) -> LockResult<T> + where + T: Sized, + { // We know statically that there are no outstanding references to // `self` so there's no need to lock the inner lock. // @@ -426,7 +429,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> { Ok(guard) => f.debug_struct("RwLock").field("data", &&*guard).finish(), Err(TryLockError::Poisoned(err)) => { f.debug_struct("RwLock").field("data", &&**err.get_ref()).finish() - }, + } Err(TryLockError::WouldBlock) => { struct LockedPlaceholder; impl fmt::Debug for LockedPlaceholder { @@ -461,24 +464,16 @@ impl<T> From<T> for RwLock<T> { } impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> { - unsafe fn new(lock: &'rwlock RwLock<T>) - -> LockResult<RwLockReadGuard<'rwlock, T>> { - poison::map_result(lock.poison.borrow(), |_| { - RwLockReadGuard { - lock: lock, - } - }) + unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockReadGuard<'rwlock, T>> { + poison::map_result(lock.poison.borrow(), |_| RwLockReadGuard { lock: lock }) } } impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> { - unsafe fn new(lock: &'rwlock RwLock<T>) - -> LockResult<RwLockWriteGuard<'rwlock, T>> { - poison::map_result(lock.poison.borrow(), |guard| { - RwLockWriteGuard { - lock: lock, - poison: guard, - } + unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockWriteGuard<'rwlock, T>> { + poison::map_result(lock.poison.borrow(), |guard| RwLockWriteGuard { + lock: lock, + poison: guard, }) } } @@ -486,9 +481,7 @@ impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> { #[stable(feature = "std_debug", since = "1.16.0")] impl<T: fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("RwLockReadGuard") - .field("lock", &self.lock) - .finish() + f.debug_struct("RwLockReadGuard").field("lock", &self.lock).finish() } } @@ -502,9 +495,7 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'_, T> { #[stable(feature = "std_debug", since = "1.16.0")] impl<T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("RwLockWriteGuard") - .field("lock", &self.lock) - .finish() + f.debug_struct("RwLockWriteGuard").field("lock", &self.lock).finish() } } @@ -543,7 +534,9 @@ impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> { #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> { fn drop(&mut self) { - unsafe { self.lock.inner.read_unlock(); } + unsafe { + self.lock.inner.read_unlock(); + } } } @@ -551,17 +544,19 @@ impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> { impl<T: ?Sized> Drop for RwLockWriteGuard<'_, T> { fn drop(&mut self) { self.lock.poison.done(&self.poison); - unsafe { self.lock.inner.write_unlock(); } + unsafe { + self.lock.inner.write_unlock(); + } } } #[cfg(all(test, not(target_os = "emscripten")))] mod tests { - use rand::{self, Rng}; + use crate::sync::atomic::{AtomicUsize, Ordering}; use crate::sync::mpsc::channel; - use crate::thread; use crate::sync::{Arc, RwLock, TryLockError}; - use crate::sync::atomic::{AtomicUsize, Ordering}; + use crate::thread; + use rand::{self, Rng}; #[derive(Eq, PartialEq, Debug)] struct NonCopy(i32); @@ -609,7 +604,8 @@ mod tests { let _: Result<(), _> = thread::spawn(move || { let _lock = arc2.write().unwrap(); panic!(); - }).join(); + }) + .join(); assert!(arc.read().is_err()); } @@ -621,7 +617,8 @@ mod tests { let _: Result<(), _> = thread::spawn(move || { let _lock = arc2.write().unwrap(); panic!(); - }).join(); + }) + .join(); assert!(arc.write().is_err()); assert!(arc.is_poisoned()); } @@ -633,7 +630,8 @@ mod tests { let _: Result<(), _> = thread::spawn(move || { let _lock = arc2.read().unwrap(); panic!(); - }).join(); + }) + .join(); let lock = arc.read().unwrap(); assert_eq!(*lock, 1); } @@ -644,7 +642,8 @@ mod tests { let _: Result<(), _> = thread::spawn(move || { let _lock = arc2.read().unwrap(); panic!() - }).join(); + }) + .join(); let lock = arc.write().unwrap(); assert_eq!(*lock, 1); } @@ -703,7 +702,8 @@ mod tests { } let _u = Unwinder { i: arc2 }; panic!(); - }).join(); + }) + .join(); let lock = arc.read().unwrap(); assert_eq!(*lock, 2); } @@ -766,7 +766,8 @@ mod tests { let _ = thread::spawn(move || { let _lock = m2.write().unwrap(); panic!("test panic in inner thread to poison RwLock"); - }).join(); + }) + .join(); assert!(m.is_poisoned()); match Arc::try_unwrap(m).unwrap().into_inner() { @@ -789,7 +790,8 @@ mod tests { let _ = thread::spawn(move || { let _lock = m2.write().unwrap(); panic!("test panic in inner thread to poison RwLock"); - }).join(); + }) + .join(); assert!(m.is_poisoned()); match Arc::try_unwrap(m).unwrap().get_mut() { |
