pub struct RwLock<T: ?Sized> { /* private fields */ }
Expand description
A reader-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).
The type parameter T
represents the data that this lock protects. It is
required that T
satisfies Send
to be shared across tasks and Sync
to
allow concurrent access through readers. The RAII guards returned from the
locking methods implement Deref
(and DerefMut
for the write
methods)
to allow access to the contained of the lock.
An RwLockUpgradeableGuard
can be upgraded to a
writable guard through the RwLockUpgradeableGuard::upgrade
RwLockUpgradeableGuard::try_upgrade
functions.
Writable or upgradeable guards can be downgraded through their respective downgrade
functions.
Based on Facebook’s
folly/RWSpinLock.h
.
This implementation is unfair to writers - if the lock always has readers, then no writers will
ever get a chance. Using an upgradeable lock guard can somewhat alleviate this issue as no
new readers are allowed when an upgradeable guard is held, but upgradeable guards can be taken
when there are existing readers. However if the lock is that highly contended and writes are
crucial then this implementation may be a poor choice.
Examples
use spin;
let lock = spin::RwLock::new(5);
// many reader locks can be held at once
{
let r1 = lock.read();
let r2 = lock.read();
assert_eq!(*r1, 5);
assert_eq!(*r2, 5);
} // read locks are dropped at this point
// only one write lock may be held, however
{
let mut w = lock.write();
*w += 1;
assert_eq!(*w, 6);
} // write lock is dropped here
Implementations
sourceimpl<T> RwLock<T>
impl<T> RwLock<T>
sourcepub const fn new(user_data: T) -> RwLock<T>
pub const fn new(user_data: T) -> RwLock<T>
Creates a new spinlock wrapping the supplied data.
May be used statically:
use spin;
static RW_LOCK: spin::RwLock<()> = spin::RwLock::new(());
fn demo() {
let lock = RW_LOCK.read();
// do something with lock
drop(lock);
}
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes this RwLock
, returning the underlying data.
sourceimpl<T: ?Sized> RwLock<T>
impl<T: ?Sized> RwLock<T>
sourcepub fn read(&self) -> RwLockReadGuard<'_, T>
pub fn read(&self) -> RwLockReadGuard<'_, T>
Locks this rwlock with shared read access, blocking the current thread until it can be acquired.
The calling thread will be blocked 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.
let mylock = spin::RwLock::new(0);
{
let mut data = mylock.read();
// The lock is now locked and the data can be read
println!("{}", *data);
// The lock is dropped
}
sourcepub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
Attempt to acquire this lock with shared read access.
This function will never block and will return immediately if read
would otherwise succeed. Returns Some
of an RAII guard which will
release the shared access of this thread when dropped, or None
if the
access could not be granted. This method does not provide any
guarantees with respect to the ordering of whether contentious readers
or writers will acquire the lock first.
let mylock = spin::RwLock::new(0);
{
match mylock.try_read() {
Some(data) => {
// The lock is now locked and the data can be read
println!("{}", *data);
// The lock is dropped
},
None => (), // no cigar
};
}
sourcepub unsafe fn force_read_decrement(&self)
pub unsafe fn force_read_decrement(&self)
Force decrement the reader count.
This is extremely unsafe if there are outstanding RwLockReadGuard
s
live, or if called more times than read
has been called, but can be
useful in FFI contexts where the caller doesn’t know how to deal with
RAII. The underlying atomic operation uses Ordering::Release
.
sourcepub unsafe fn force_write_unlock(&self)
pub unsafe fn force_write_unlock(&self)
Force unlock exclusive write access.
This is extremely unsafe if there are outstanding RwLockWriteGuard
s
live, or if called when there are current readers, but can be useful in
FFI contexts where the caller doesn’t know how to deal with RAII. The
underlying atomic operation uses Ordering::Release
.
sourcepub fn write(&self) -> RwLockWriteGuard<'_, T>
pub fn write(&self) -> RwLockWriteGuard<'_, T>
Lock this rwlock with exclusive write access, blocking the current thread until it 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.
let mylock = spin::RwLock::new(0);
{
let mut data = mylock.write();
// The lock is now locked and the data can be written
*data += 1;
// The lock is dropped
}
sourcepub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
Attempt to lock this rwlock with exclusive write access.
This function does not ever block, and it will return None
if a call
to write
would otherwise block. If successful, an RAII guard is
returned.
let mylock = spin::RwLock::new(0);
{
match mylock.try_write() {
Some(mut data) => {
// The lock is now locked and the data can be written
*data += 1;
// The lock is implicitly dropped
},
None => (), // no cigar
};
}
sourcepub fn upgradeable_read(&self) -> RwLockUpgradeableGuard<'_, T>
pub fn upgradeable_read(&self) -> RwLockUpgradeableGuard<'_, T>
Obtain a readable lock guard that can later be upgraded to a writable lock guard.
Upgrades can be done through the RwLockUpgradeableGuard::upgrade
method.
sourcepub fn try_upgradeable_read(&self) -> Option<RwLockUpgradeableGuard<'_, T>>
pub fn try_upgradeable_read(&self) -> Option<RwLockUpgradeableGuard<'_, T>>
Tries to obtain an upgradeable lock guard.
Trait Implementations
impl<T: ?Sized + Send> Send for RwLock<T>
impl<T: ?Sized + Send + Sync> Sync for RwLock<T>
Auto Trait Implementations
impl<T> !RefUnwindSafe for RwLock<T>
impl<T: ?Sized> Unpin for RwLock<T> where
T: Unpin,
impl<T: ?Sized> UnwindSafe for RwLock<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more