pub struct RwLock<T> { /* private fields */ }
Expand description
Wrapper around a std::sync::RwLock
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::RwLock;
let mutex_a = RwLock::new(()); // Level 0
let mutex_b = RwLock::with_level((), 0); // also level 0
// Fine, first mutex in thread
let _guard_a = mutex_a.read().unwrap();
// Would panic, lock hierarchy violation
// let _guard_b = mutex_b.read().unwrap();
Implementations§
Source§impl<T> RwLock<T>
impl<T> RwLock<T>
Sourcepub fn new(t: T) -> Self
pub fn new(t: T) -> Self
Creates a 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 read(&self) -> LockResult<RwLockReadGuard<'_, T>>
pub fn read(&self) -> LockResult<RwLockReadGuard<'_, T>>
Sourcepub fn write(&self) -> LockResult<RwLockWriteGuard<'_, T>>
pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, 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 RwLock<T>
impl<T> RefUnwindSafe for RwLock<T>
impl<T> Send for RwLock<T>where
T: Send,
impl<T> Sync for RwLock<T>
impl<T> Unpin for RwLock<T>where
T: Unpin,
impl<T> UnwindSafe for RwLock<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