Struct RwLock
pub struct RwLock<R, T: ?Sized> { /* private fields */ }
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 threads 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.
Implementations
impl<R, T> RwLock<R, T>
const fn from_raw(raw_rwlock: R, val: T) -> RwLock<R, T>Creates a new new instance of an
RwLock<T>based on a pre-existingRawRwLock<T>.const fn const_new(raw_rwlock: R, val: T) -> RwLock<R, T>Creates a new new instance of an
RwLock<T>based on a pre-existingRawRwLock<T>.This allows creating a
RwLock<T>in a constant context on stable Rust.This method is a legacy alias for
from_raw.
impl<R: RawRwLock, T> RwLock<R, T>
const fn new(val: T) -> RwLock<R, T>Creates a new instance of an
RwLock<T>which is unlocked.fn into_inner(self) -> TConsumes this
RwLock, returning the underlying data.
impl<R: RawRwLock, T: ?Sized> RwLock<R, T>
unsafe fn make_read_guard_unchecked(&self) -> RwLockReadGuard<'_, R, T>Creates a new
RwLockReadGuardwithout checking if the lock is held.Safety
This method must only be called if the thread logically holds a read lock.
This function does not increment the read count of the lock. Calling this function when a guard has already been produced is undefined behaviour unless the guard was forgotten with
mem::forget.unsafe fn make_write_guard_unchecked(&self) -> RwLockWriteGuard<'_, R, T>Creates a new
RwLockReadGuardwithout checking if the lock is held.Safety
This method must only be called if the thread logically holds a write lock.
Calling this function when a guard has already been produced is undefined behaviour unless the guard was forgotten with
mem::forget.fn read(&self) -> RwLockReadGuard<'_, R, T>Locks this
RwLockwith 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.
Note that attempts to recursively acquire a read lock on a
RwLockwhen the current thread already holds one may result in a deadlock.Returns an RAII guard which will release this thread's shared access once it is dropped.
fn try_read(&self) -> Option<RwLockReadGuard<'_, R, T>>Attempts to acquire this
RwLockwith shared read access.If the access could not be granted at this time, then
Noneis returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.This function does not block.
fn write(&self) -> RwLockWriteGuard<'_, R, T>Locks this
RwLockwith 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
RwLockwhen dropped.fn try_write(&self) -> Option<RwLockWriteGuard<'_, R, T>>Attempts to lock this
RwLockwith exclusive write access.If the lock could not be acquired at this time, then
Noneis returned. Otherwise, an RAII guard is returned which will release the lock when it is dropped.This function does not block.
fn get_mut(&mut self) -> &mut TReturns a mutable reference to the underlying data.
Since this call borrows the
RwLockmutably, no actual locking needs to take place---the mutable borrow statically guarantees no locks exist.fn is_locked(&self) -> boolChecks whether this
RwLockis currently locked in any way.fn is_locked_exclusive(&self) -> boolCheck if this
RwLockis currently exclusively locked.unsafe fn force_unlock_read(&self)Forcibly unlocks a read lock.
This is useful when combined with
mem::forgetto hold a lock without the need to maintain aRwLockReadGuardobject alive, for example when dealing with FFI.Safety
This method must only be called if the current thread logically owns a
RwLockReadGuardbut that guard has be discarded usingmem::forget. Behavior is undefined if a rwlock is read-unlocked when not read-locked.unsafe fn force_unlock_write(&self)Forcibly unlocks a write lock.
This is useful when combined with
mem::forgetto hold a lock without the need to maintain aRwLockWriteGuardobject alive, for example when dealing with FFI.Safety
This method must only be called if the current thread logically owns a
RwLockWriteGuardbut that guard has be discarded usingmem::forget. Behavior is undefined if a rwlock is write-unlocked when not write-locked.unsafe fn raw(&self) -> &RReturns the underlying raw reader-writer lock object.
Note that you will most likely need to import the
RawRwLocktrait fromlock_apito be able to call functions on the raw reader-writer lock.Safety
This method is unsafe because it allows unlocking a mutex while still holding a reference to a lock guard.
fn data_ptr(&self) -> *mut TReturns a raw pointer to the underlying data.
This is useful when combined with
mem::forgetto hold a lock without the need to maintain aRwLockReadGuardorRwLockWriteGuardobject alive, for example when dealing with FFI.Safety
You must ensure that there are no data races when dereferencing the returned pointer, for example if the current thread logically owns a
RwLockReadGuardorRwLockWriteGuardbut that guard has been discarded usingmem::forget.
impl<R: RawRwLockFair, T: ?Sized> RwLock<R, T>
unsafe fn force_unlock_read_fair(&self)Forcibly unlocks a read lock using a fair unlock protocol.
This is useful when combined with
mem::forgetto hold a lock without the need to maintain aRwLockReadGuardobject alive, for example when dealing with FFI.Safety
This method must only be called if the current thread logically owns a
RwLockReadGuardbut that guard has be discarded usingmem::forget. Behavior is undefined if a rwlock is read-unlocked when not read-locked.unsafe fn force_unlock_write_fair(&self)Forcibly unlocks a write lock using a fair unlock protocol.
This is useful when combined with
mem::forgetto hold a lock without the need to maintain aRwLockWriteGuardobject alive, for example when dealing with FFI.Safety
This method must only be called if the current thread logically owns a
RwLockWriteGuardbut that guard has be discarded usingmem::forget. Behavior is undefined if a rwlock is write-unlocked when not write-locked.
impl<R: RawRwLockRecursive, T: ?Sized> RwLock<R, T>
fn read_recursive(&self) -> RwLockReadGuard<'_, R, T>Locks this
RwLockwith 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.
Unlike
read, this method is guaranteed to succeed without blocking if another read lock is held at the time of the call. This allows a thread to recursively lock aRwLock. However using this method can cause writers to starve since readers no longer block if a writer is waiting for the lock.Returns an RAII guard which will release this thread's shared access once it is dropped.
fn try_read_recursive(&self) -> Option<RwLockReadGuard<'_, R, T>>Attempts to acquire this
RwLockwith shared read access.If the access could not be granted at this time, then
Noneis returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.This method is guaranteed to succeed if another read lock is held at the time of the call. See the documentation for
read_recursivefor details.This function does not block.
impl<R: RawRwLockRecursiveTimed, T: ?Sized> RwLock<R, T>
fn try_read_recursive_for(&self, timeout: R::Duration) -> Option<RwLockReadGuard<'_, R, T>>Attempts to acquire this
RwLockwith shared read access until a timeout is reached.If the access could not be granted before the timeout expires, then
Noneis returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.This method is guaranteed to succeed without blocking if another read lock is held at the time of the call. See the documentation for
read_recursivefor details.fn try_read_recursive_until(&self, timeout: R::Instant) -> Option<RwLockReadGuard<'_, R, T>>Attempts to acquire this
RwLockwith shared read access until a timeout is reached.If the access could not be granted before the timeout expires, then
Noneis returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.
impl<R: RawRwLockTimed, T: ?Sized> RwLock<R, T>
fn try_read_for(&self, timeout: R::Duration) -> Option<RwLockReadGuard<'_, R, T>>Attempts to acquire this
RwLockwith shared read access until a timeout is reached.If the access could not be granted before the timeout expires, then
Noneis returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.fn try_read_until(&self, timeout: R::Instant) -> Option<RwLockReadGuard<'_, R, T>>Attempts to acquire this
RwLockwith shared read access until a timeout is reached.If the access could not be granted before the timeout expires, then
Noneis returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.fn try_write_for(&self, timeout: R::Duration) -> Option<RwLockWriteGuard<'_, R, T>>Attempts to acquire this
RwLockwith exclusive write access until a timeout is reached.If the access could not be granted before the timeout expires, then
Noneis returned. Otherwise, an RAII guard is returned which will release the exclusive access when it is dropped.fn try_write_until(&self, timeout: R::Instant) -> Option<RwLockWriteGuard<'_, R, T>>Attempts to acquire this
RwLockwith exclusive write access until a timeout is reached.If the access could not be granted before the timeout expires, then
Noneis returned. Otherwise, an RAII guard is returned which will release the exclusive access when it is dropped.
impl<R: RawRwLockUpgrade, T: ?Sized> RwLock<R, T>
unsafe fn make_upgradable_guard_unchecked(&self) -> RwLockUpgradableReadGuard<'_, R, T>Creates a new
RwLockUpgradableReadGuardwithout checking if the lock is held.Safety
This method must only be called if the thread logically holds an upgradable read lock.
This function does not increment the read count of the lock. Calling this function when a guard has already been produced is undefined behaviour unless the guard was forgotten with
mem::forget.fn upgradable_read(&self) -> RwLockUpgradableReadGuard<'_, R, T>Locks this
RwLockwith upgradable read access, blocking the current thread until it can be acquired.The calling thread will be blocked until there are no more writers or other upgradable reads which hold the lock. There may be other readers currently inside the lock when this method returns.
Returns an RAII guard which will release this thread's shared access once it is dropped.
fn try_upgradable_read(&self) -> Option<RwLockUpgradableReadGuard<'_, R, T>>Attempts to acquire this
RwLockwith upgradable read access.If the access could not be granted at this time, then
Noneis returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.This function does not block.
impl<R: RawRwLockUpgradeTimed, T: ?Sized> RwLock<R, T>
fn try_upgradable_read_for(&self, timeout: R::Duration) -> Option<RwLockUpgradableReadGuard<'_, R, T>>Attempts to acquire this
RwLockwith upgradable read access until a timeout is reached.If the access could not be granted before the timeout expires, then
Noneis returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.fn try_upgradable_read_until(&self, timeout: R::Instant) -> Option<RwLockUpgradableReadGuard<'_, R, T>>Attempts to acquire this
RwLockwith upgradable read access until a timeout is reached.If the access could not be granted before the timeout expires, then
Noneis returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.
Trait Implementations
impl<R: RawRwLock + Send, T: ?Sized + Send> Send for RwLock<R, T>
impl<R: RawRwLock + Sync, T: ?Sized + Send + Sync> Sync for RwLock<R, T>
impl<R: RawRwLock, T> From<T> for RwLock<R, T>
fn from(t: T) -> RwLock<R, T>
impl<R: RawRwLock, T: ?Sized + Debug> Debug for RwLock<R, T>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<R: RawRwLock, T: ?Sized + Default> Default for RwLock<R, T>
fn default() -> RwLock<R, T>
Auto Trait Implementations
impl<R, T> !Freeze for RwLock<R, T>
impl<R, T> !RefUnwindSafe for RwLock<R, T>
impl<R, T> Unpin for RwLock<R, T>
where
R: Unpin,
UnsafeCell<T>: Unpin,
T: ?Sized,
impl<R, T> UnsafeUnpin for RwLock<R, T>
where
R: UnsafeUnpin,
UnsafeCell<T>: UnsafeUnpin,
T: ?Sized,
impl<R, T> UnwindSafe for RwLock<R, T>
where
R: UnwindSafe,
UnsafeCell<T>: UnwindSafe,
T: ?Sized,
Blanket Implementations
impl<T> Any for RwLock<R, T>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for RwLock<R, T>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for RwLock<R, T>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for RwLock<R, T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for RwLock<R, T>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for RwLock<R, T>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for RwLock<R, T>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>