pub struct RwLock<T> { /* private fields */ }Expand description
A wrapper around a reader-writer lock that tracks operations for deadlock detection
The RwLock provides the same API as a standard reader-writer lock but also notifies the detector on lock/unlock operations.
Implementations§
Source§impl<T> RwLock<T>
impl<T> RwLock<T>
Sourcepub fn creator_thread_id(&self) -> ThreadId
pub fn creator_thread_id(&self) -> ThreadId
Get the creator thread ID
Sourcepub fn read(&self) -> RwLockReadGuard<'_, T>
pub fn read(&self) -> RwLockReadGuard<'_, T>
Acquire a shared (read) lock, tracking the attempt and acquisition
Uses two-phase locking protocol to eliminate race conditions between deadlock detection and lock acquisition.
§Returns
A guard which releases the lock when dropped
Sourcepub fn write(&self) -> RwLockWriteGuard<'_, T>
pub fn write(&self) -> RwLockWriteGuard<'_, T>
Acquire an exclusive (write) lock, tracking the attempt and acquisition
Uses two-phase locking protocol to eliminate race conditions between deadlock detection and lock acquisition.
§Returns
A guard which releases the lock when dropped
Sourcepub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
Try to acquire a shared (read) lock, tracking the attempt
Uses atomic detection to ensure deadlock detection and acquisition happen together.
Sourcepub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
Try to acquire an exclusive (write) lock, tracking the attempt
Uses atomic detection to ensure deadlock detection and acquisition happen together.
Sourcepub fn into_inner(self) -> Twhere
T: Sized,
pub fn into_inner(self) -> Twhere
T: Sized,
Consumes this RwLock, returning the underlying data
§Example
use deloxide::RwLock;
let lock = RwLock::new(String::from("hello"));
let s = lock.into_inner();
assert_eq!(s, "hello");Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying data
Since this call borrows the RwLock mutably, no actual locking needs to take place – the mutable borrow statically guarantees no locks exist.
§Example
use deloxide::RwLock;
let mut lock = RwLock::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.read(), 10);