Struct maitake_sync::spin::RwLock
source · pub struct RwLock<T: ?Sized> { /* private fields */ }
Expand description
A spinlock-based readers-writer lock.
This type of lock allows a number of readers or at most one writer at any point in time. The write portion of this lock typically allows modification of the underlying data (exclusive access) and the read portion of this lock typically allows for read-only access (shared access).
In comparison, a spin::Mutex
does not distinguish between readers or writers
that acquire the lock, therefore blocking any threads waiting for the lock to
become available. An RwLock
will allow any number of readers to acquire the
lock as long as a writer is not holding the lock.
§Fairness
This is not a fair reader-writer lock.
§Loom-specific behavior
When cfg(loom)
is enabled, this mutex will use Loom’s simulated atomics,
checked UnsafeCell
, and simulated spin loop hints.
Implementations§
source§impl<T: ?Sized> RwLock<T>
impl<T: ?Sized> RwLock<T>
sourcepub fn read(&self) -> RwLockReadGuard<'_, T>
pub fn read(&self) -> RwLockReadGuard<'_, T>
Locks this RwLock
for shared read access, spinning until it can be
acquired.
The calling CPU core will spin (with an exponential backoff) until there are no more writers which hold the lock. There may be other readers currently inside the lock when this method returns. This method does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.
Returns an RAII guard which will release this thread’s shared access once it is dropped.
sourcepub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
Attempts to acquire this RwLock
for shared read access.
If the access could not be granted at this time, this method returns
None
. Otherwise, Some
(
RwLockReadGuard
)
containing a RAII
guard is returned. The shared access is released when it is dropped.
This function does not spin.
sourcepub fn write(&self) -> RwLockWriteGuard<'_, T>
pub fn write(&self) -> RwLockWriteGuard<'_, T>
Locks this RwLock
for exclusive write access, spinning until write
access can be acquired.
This function will not return while other writers or other readers currently have access to the lock.
Returns an RAII guard which will drop the write access of this RwLock
when dropped.
sourcepub fn reader_count(&self) -> usize
pub fn reader_count(&self) -> usize
Returns the current number of readers holding a read lock.
§Note
This method is not synchronized with attempts to increment the reader count, and its value may become out of date as soon as it is read. This is not intended to be used for synchronization purposes! It is intended only for debugging purposes or for use as a heuristic.
sourcepub fn has_writer(&self) -> bool
pub fn has_writer(&self) -> bool
Returns true
if there is currently a writer holding a write lock.
§Note
This method is not synchronized its value may become out of date as soon as it is read. This is not intended to be used for synchronization purposes! It is intended only for debugging purposes or for use as a heuristic.
sourcepub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
Attempts to acquire this RwLock
for exclusive write access.
If the access could not be granted at this time, this method returns
None
. Otherwise, Some
(
RwLockWriteGuard
)
containing a
RAII guard is returned. The write access is released when it is dropped.
This function does not spin.
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.
§Examples
let mut lock = maitake_sync::spin::RwLock::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.read(), 10);
source§impl<T> RwLock<T>
impl<T> RwLock<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes this RwLock
, returning the guarded data.
Trait Implementations§
source§impl<T> From<T> for RwLock<T>
impl<T> From<T> for RwLock<T>
source§fn from(t: T) -> Self
fn from(t: T) -> Self
Creates a new instance of an RwLock<T>
which is unlocked.
This is equivalent to RwLock::new
.