pub struct Mutex<T> { /* private fields */ }
Expand description
Wrapper around a std::sync::Mutex
which uses a thread local variable in order to check for
lock hierarchy violations in debug builds.
See the crate level documentation for more general information.
use lock_hierarchy::Mutex;
let mutex_a = Mutex::new(()); // Level 0
let mutex_b = Mutex::with_level((), 0); // also level 0
// Fine, first mutex in thread
let _guard_a = mutex_a.lock().unwrap();
// Would panic, lock hierarchy violation
// let _guard_b = mutex_b.lock().unwrap();
Implementations§
Source§impl<T> Mutex<T>
impl<T> Mutex<T>
Sourcepub fn new(t: T) -> Self
pub fn new(t: T) -> Self
Creates lock with level 0. Use this constructor if you want to get an error in debug builds every time you acquire another lock while holding this one.
Sourcepub fn with_level(t: T, level: u32) -> Self
pub fn with_level(t: T, level: u32) -> Self
Creates a lock and assigns it a level in the lock hierarchy. Higher levels must be acquired first if locks are to be held simultaneously. This way we can ensure locks are always acquired in the same order. This prevents deadlocks.
Sourcepub fn lock(&self) -> LockResult<MutexGuard<'_, T>>
pub fn lock(&self) -> LockResult<MutexGuard<'_, T>>
Sourcepub fn get_mut(&mut self) -> LockResult<&mut T>
pub fn get_mut(&mut self) -> LockResult<&mut T>
Sourcepub fn into_inner(self) -> LockResult<T>
pub fn into_inner(self) -> LockResult<T>
Trait Implementations§
Auto Trait Implementations§
impl<T> !Freeze for Mutex<T>
impl<T> RefUnwindSafe for Mutex<T>
impl<T> Send for Mutex<T>where
T: Send,
impl<T> Sync for Mutex<T>where
T: Send,
impl<T> Unpin for Mutex<T>where
T: Unpin,
impl<T> UnwindSafe for Mutex<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more