pub struct RwLock<T>(/* private fields */);
Expand description
Async reader–writer lock.
Powered by async_lock::RwLock, RwLock
provides both fair writer acquisition
and try_read
/ try_write
without waiting (without any runtime-specific dependencies).
Usage:
use commonware_runtime::{Spawner, Runner, Signaler, deterministic, RwLock};
let executor = deterministic::Runner::default();
executor.start(|context| async move {
// Create a new RwLock
let lock = RwLock::new(2);
// many concurrent readers
let r1 = lock.read().await;
let r2 = lock.read().await;
assert_eq!(*r1 + *r2, 4);
// exclusive writer
drop((r1, r2));
let mut w = lock.write().await;
*w += 1;
});
Implementations§
Source§impl<T> RwLock<T>
impl<T> RwLock<T>
Sourcepub async fn read(&self) -> RwLockReadGuard<'_, T>
pub async fn read(&self) -> RwLockReadGuard<'_, T>
Acquire a shared read guard.
Sourcepub async fn write(&self) -> RwLockWriteGuard<'_, T>
pub async fn write(&self) -> RwLockWriteGuard<'_, T>
Acquire an exclusive write guard.
Sourcepub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
Try to get a read guard without waiting.
Sourcepub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
Try to get a write guard without waiting.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume the lock, returning the inner value.
Auto Trait Implementations§
impl<T> !Freeze for RwLock<T>
impl<T> !RefUnwindSafe for RwLock<T>
impl<T> Send for RwLock<T>where
T: Send,
impl<T> Sync for RwLock<T>
impl<T> Unpin for RwLock<T>where
T: Unpin,
impl<T> UnwindSafe for RwLock<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more