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
///
// 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.
}
#[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.
/// 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.
//
(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())
// 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)
}
}
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 {
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 })
}
}
#[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)>);
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);
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() {
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() {
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;
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());
}
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);
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>>,
}
}
let _u = Unwinder { i: arc2 };
panic!();
- }).join();
+ })
+ .join();
let lock = arc.lock().unwrap();
assert_eq!(*lock, 2);
}