Struct RwLock
struct RwLock<T: ?Sized> { ... }
An asynchronous 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).
In comparison, a Mutex does not distinguish between readers or writers
that acquire the lock, therefore causing any tasks waiting for the lock to
become available to yield. An RwLock will allow any number of readers to
acquire the lock as long as a writer is not holding the lock.
The priority policy of Tokio's read-write lock is fair (or
write-preferring), in order to ensure that readers cannot starve
writers. Fairness is ensured using a first-in, first-out queue for the tasks
awaiting the lock; if a task that wishes to acquire the write lock is at the
head of the queue, read locks will not be given out until the write lock has
been released. This is in contrast to the Rust standard library's
std::sync::RwLock, where the priority policy is dependent on the
operating system's implementation.
The type parameter T represents the data that this lock protects. It is
required that T satisfies Send to be shared across threads. The RAII guards
returned from the locking methods implement Deref
(and DerefMut
for the write methods) to allow access to the content of the lock.
Examples
use RwLock;
async
Implementations
impl<T: ?Sized> RwLock<T>
fn new(value: T) -> RwLock<T> where T: SizedCreates a new instance of an
RwLock<T>which is unlocked.Examples
use RwLock; let lock = new;fn with_max_readers(value: T, max_reads: u32) -> RwLock<T> where T: SizedCreates a new instance of an
RwLock<T>which is unlocked and allows a maximum ofmax_readsconcurrent readers.Examples
use RwLock; let lock = with_max_readers;Panics
Panics if
max_readsis more thanu32::MAX >> 3.const fn const_new(value: T) -> RwLock<T> where T: SizedCreates a new instance of an
RwLock<T>which is unlocked.When using the
tracingunstable feature, aRwLockcreated withconst_newwill not be instrumented. As such, it will not be visible intokio-console. Instead,RwLock::newshould be used to create an instrumented object if that is needed.Examples
use RwLock; static LOCK: = const_new;const fn const_with_max_readers(value: T, max_reads: u32) -> RwLock<T> where T: SizedCreates a new instance of an
RwLock<T>which is unlocked and allows a maximum ofmax_readsconcurrent readers.Examples
use RwLock; static LOCK: = const_with_max_readers;async fn read(self: &Self) -> RwLockReadGuard<'_, T>Locks this
RwLockwith shared read access, causing the current task to yield until the lock has been acquired.The calling task will yield until there are no writers which hold the lock. There may be other readers inside the lock when the task resumes.
Note that under the priority policy of
RwLock, read locks are not granted until prior write locks, to prevent starvation. Therefore deadlock may occur if a read lock is held by the current task, a write lock attempt is made, and then a subsequent read lock attempt is made by the current task.Returns an RAII guard which will drop this read access of the
RwLockwhen dropped.Cancel safety
This method uses a queue to fairly distribute locks in the order they were requested. Cancelling a call to
readmakes you lose your place in the queue.Examples
use Arc; use RwLock; asyncfn blocking_read(self: &Self) -> RwLockReadGuard<'_, T>Blockingly locks this
RwLockwith shared read access.This method is intended for use cases where you need to use this rwlock in asynchronous code as well as in synchronous code.
Returns an RAII guard which will drop the read access of this
RwLockwhen dropped.Panics
This function panics if called within an asynchronous execution context.
- If you find yourself in an asynchronous execution context and needing
to call some (synchronous) function which performs one of these
blocking_operations, then consider wrapping that call inside [spawn_blocking()][crate::runtime::Handle::spawn_blocking] (or [block_in_place()][crate::task::block_in_place]).
Examples
use Arc; use RwLock; async- If you find yourself in an asynchronous execution context and needing
to call some (synchronous) function which performs one of these
async fn read_owned(self: Arc<Self>) -> OwnedRwLockReadGuard<T>Locks this
RwLockwith shared read access, causing the current task to yield until the lock has been acquired.The calling task will yield until there are no writers which hold the lock. There may be other readers inside the lock when the task resumes.
This method is identical to
RwLock::read, except that the returned guard references theRwLockwith anArcrather than by borrowing it. Therefore, theRwLockmust be wrapped in anArcto call this method, and the guard will live for the'staticlifetime, as it keeps theRwLockalive by holding anArc.Note that under the priority policy of
RwLock, read locks are not granted until prior write locks, to prevent starvation. Therefore deadlock may occur if a read lock is held by the current task, a write lock attempt is made, and then a subsequent read lock attempt is made by the current task.Returns an RAII guard which will drop this read access of the
RwLockwhen dropped.Cancel safety
This method uses a queue to fairly distribute locks in the order they were requested. Cancelling a call to
read_ownedmakes you lose your place in the queue.Examples
use Arc; use RwLock; asyncfn try_read(self: &Self) -> Result<RwLockReadGuard<'_, T>, TryLockError>Attempts to acquire this
RwLockwith shared read access.If the access couldn't be acquired immediately, returns
TryLockError. Otherwise, an RAII guard is returned which will release read access when dropped.Examples
use Arc; use RwLock; asyncfn try_read_owned(self: Arc<Self>) -> Result<OwnedRwLockReadGuard<T>, TryLockError>Attempts to acquire this
RwLockwith shared read access.If the access couldn't be acquired immediately, returns
TryLockError. Otherwise, an RAII guard is returned which will release read access when dropped.This method is identical to
RwLock::try_read, except that the returned guard references theRwLockwith anArcrather than by borrowing it. Therefore, theRwLockmust be wrapped in anArcto call this method, and the guard will live for the'staticlifetime, as it keeps theRwLockalive by holding anArc.Examples
use Arc; use RwLock; asyncasync fn write(self: &Self) -> RwLockWriteGuard<'_, T>Locks this
RwLockwith exclusive write access, causing the current task to yield until the lock has been acquired.The calling task will yield while other writers or readers currently have access to the lock.
Returns an RAII guard which will drop the write access of this
RwLockwhen dropped.Cancel safety
This method uses a queue to fairly distribute locks in the order they were requested. Cancelling a call to
writemakes you lose your place in the queue.Examples
use RwLock; asyncfn blocking_write(self: &Self) -> RwLockWriteGuard<'_, T>Blockingly locks this
RwLockwith exclusive write access.This method is intended for use cases where you need to use this rwlock in asynchronous code as well as in synchronous code.
Returns an RAII guard which will drop the write access of this
RwLockwhen dropped.Panics
This function panics if called within an asynchronous execution context.
- If you find yourself in an asynchronous execution context and needing
to call some (synchronous) function which performs one of these
blocking_operations, then consider wrapping that call inside [spawn_blocking()][crate::runtime::Handle::spawn_blocking] (or [block_in_place()][crate::task::block_in_place]).
Examples
use Arc; use ; async- If you find yourself in an asynchronous execution context and needing
to call some (synchronous) function which performs one of these
async fn write_owned(self: Arc<Self>) -> OwnedRwLockWriteGuard<T>Locks this
RwLockwith exclusive write access, causing the current task to yield until the lock has been acquired.The calling task will yield while other writers or readers currently have access to the lock.
This method is identical to
RwLock::write, except that the returned guard references theRwLockwith anArcrather than by borrowing it. Therefore, theRwLockmust be wrapped in anArcto call this method, and the guard will live for the'staticlifetime, as it keeps theRwLockalive by holding anArc.Returns an RAII guard which will drop the write access of this
RwLockwhen dropped.Cancel safety
This method uses a queue to fairly distribute locks in the order they were requested. Cancelling a call to
write_ownedmakes you lose your place in the queue.Examples
use Arc; use RwLock; asyncfn try_write(self: &Self) -> Result<RwLockWriteGuard<'_, T>, TryLockError>Attempts to acquire this
RwLockwith exclusive write access.If the access couldn't be acquired immediately, returns
TryLockError. Otherwise, an RAII guard is returned which will release write access when dropped.Examples
use RwLock; asyncfn try_write_owned(self: Arc<Self>) -> Result<OwnedRwLockWriteGuard<T>, TryLockError>Attempts to acquire this
RwLockwith exclusive write access.If the access couldn't be acquired immediately, returns
TryLockError. Otherwise, an RAII guard is returned which will release write access when dropped.This method is identical to
RwLock::try_write, except that the returned guard references theRwLockwith anArcrather than by borrowing it. Therefore, theRwLockmust be wrapped in anArcto call this method, and the guard will live for the'staticlifetime, as it keeps theRwLockalive by holding anArc.Examples
use Arc; use RwLock; asyncfn get_mut(self: &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.Examples
use RwLock;fn into_inner(self: Self) -> T where T: SizedConsumes the lock, returning the underlying data.
impl<T> Any for RwLock<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for RwLock<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for RwLock<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Debug for RwLock<T>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<T> Default for RwLock<T>
fn default() -> Self
impl<T> Freeze for RwLock<T>
impl<T> From for RwLock<T>
fn from(t: never) -> T
impl<T> From for RwLock<T>
fn from(s: T) -> Self
impl<T> From for RwLock<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for RwLock<T>
impl<T> Send for RwLock<T>
impl<T> Sync for RwLock<T>
impl<T> Unpin for RwLock<T>
impl<T> UnsafeUnpin for RwLock<T>
impl<T> UnwindSafe for RwLock<T>
impl<T, U> Into for RwLock<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for RwLock<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for RwLock<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>