pub struct RwLock<T, Lock = RwSpinlock>where
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 blocking::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§
§impl<T> RwLock<T>
impl<T> RwLock<T>
pub const fn new(data: T) -> RwLock<T>
pub const fn new(data: T) -> RwLock<T>
Creates a new, unlocked RwLock<T>
protecting the provided data
.
Examples
use maitake_sync::blocking::RwLock;
let lock = RwLock::new(5);
pub 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.
§impl<T, Lock> RwLock<T, Lock>
impl<T, Lock> RwLock<T, Lock>
pub fn read(&self) -> RwLockReadGuard<'_, T, Lock>
pub fn read(&self) -> RwLockReadGuard<'_, T, Lock>
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.
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T, Lock>>
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T, Lock>>
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.
pub fn write(&self) -> RwLockWriteGuard<'_, T, Lock>
pub fn write(&self) -> RwLockWriteGuard<'_, T, Lock>
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.
pub 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.
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T, Lock>>
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T, Lock>>
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.
pub 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::blocking::RwLock::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.read(), 10);
§impl<T, Lock> RwLock<T, Lock>where
Lock: RawRwLock,
impl<T, Lock> RwLock<T, Lock>where
Lock: RawRwLock,
pub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes this RwLock
, returning the guarded data.
Trait Implementations§
§impl<T> From<T> for RwLock<T>
impl<T> From<T> for RwLock<T>
§fn from(t: T) -> RwLock<T>
fn from(t: T) -> RwLock<T>
Creates a new instance of an RwLock<T>
which is unlocked.
This is equivalent to RwLock::new
.