pub struct RwLock<T>where
T: ?Sized,{ /* private fields */ }
Expand description
An async reader-writer lock.
This type of lock allows multiple readers or one writer at any point in time.
The locking strategy is write-preferring, which means writers are never starved. Releasing a write lock wakes the next blocked reader and the next blocked writer.
Examples
use async_lock::RwLock;
let lock = RwLock::new(5);
// Multiple read locks can be held at a time.
let r1 = lock.read().await;
let r2 = lock.read().await;
assert_eq!(*r1, 5);
assert_eq!(*r2, 5);
drop((r1, r2));
// Only one write lock can be held at a time.
let mut w = lock.write().await;
*w += 1;
assert_eq!(*w, 6);
Implementations§
source§impl<T> RwLock<T>
impl<T> RwLock<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwraps the lock and returns the inner value.
Examples
use async_lock::RwLock;
let lock = RwLock::new(5);
assert_eq!(lock.into_inner(), 5);
sourcepub fn try_read_arc(self: &Arc<RwLock<T>>) -> Option<RwLockReadGuardArc<T>>
pub fn try_read_arc(self: &Arc<RwLock<T>>) -> Option<RwLockReadGuardArc<T>>
Attempts to acquire an an owned, reference-counted read lock.
If a read lock could not be acquired at this time, then None
is returned. Otherwise, a
guard is returned that releases the lock when dropped.
Examples
use std::sync::Arc;
use async_lock::RwLock;
let lock = Arc::new(RwLock::new(1));
let reader = lock.read_arc().await;
assert_eq!(*reader, 1);
assert!(lock.try_read_arc().is_some());
sourcepub fn read_arc<'a>(self: &'a Arc<RwLock<T>>) -> ReadArc<'a, T> ⓘ
pub fn read_arc<'a>(self: &'a Arc<RwLock<T>>) -> ReadArc<'a, T> ⓘ
Acquires an owned, reference-counted read lock.
Returns a guard that releases the lock when dropped.
Note that attempts to acquire a read lock will block if there are also concurrent attempts to acquire a write lock.
Examples
use std::sync::Arc;
use async_lock::RwLock;
let lock = Arc::new(RwLock::new(1));
let reader = lock.read_arc().await;
assert_eq!(*reader, 1);
assert!(lock.try_read_arc().is_some());
source§impl<T> RwLock<T>where
T: ?Sized,
impl<T> RwLock<T>where
T: ?Sized,
sourcepub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>>
Attempts to acquire a read lock.
If a read lock could not be acquired at this time, then None
is returned. Otherwise, a
guard is returned that releases the lock when dropped.
Examples
use async_lock::RwLock;
let lock = RwLock::new(1);
let reader = lock.read().await;
assert_eq!(*reader, 1);
assert!(lock.try_read().is_some());
sourcepub fn read(&self) -> Read<'_, T> ⓘ
pub fn read(&self) -> Read<'_, T> ⓘ
Acquires a read lock.
Returns a guard that releases the lock when dropped.
Note that attempts to acquire a read lock will block if there are also concurrent attempts to acquire a write lock.
Examples
use async_lock::RwLock;
let lock = RwLock::new(1);
let reader = lock.read().await;
assert_eq!(*reader, 1);
assert!(lock.try_read().is_some());
sourcepub fn try_upgradable_read(&self) -> Option<RwLockUpgradableReadGuard<'_, T>>
pub fn try_upgradable_read(&self) -> Option<RwLockUpgradableReadGuard<'_, T>>
Attempts to acquire a read lock with the possiblity to upgrade to a write lock.
If a read lock could not be acquired at this time, then None
is returned. Otherwise, a
guard is returned that releases the lock when dropped.
Upgradable read lock reserves the right to be upgraded to a write lock, which means there can be at most one upgradable read lock at a time.
Examples
use async_lock::{RwLock, RwLockUpgradableReadGuard};
let lock = RwLock::new(1);
let reader = lock.upgradable_read().await;
assert_eq!(*reader, 1);
assert_eq!(*lock.try_read().unwrap(), 1);
let mut writer = RwLockUpgradableReadGuard::upgrade(reader).await;
*writer = 2;
sourcepub fn upgradable_read(&self) -> UpgradableRead<'_, T> ⓘ
pub fn upgradable_read(&self) -> UpgradableRead<'_, T> ⓘ
Acquires a read lock with the possiblity to upgrade to a write lock.
Returns a guard that releases the lock when dropped.
Upgradable read lock reserves the right to be upgraded to a write lock, which means there can be at most one upgradable read lock at a time.
Note that attempts to acquire an upgradable read lock will block if there are concurrent attempts to acquire another upgradable read lock or a write lock.
Examples
use async_lock::{RwLock, RwLockUpgradableReadGuard};
let lock = RwLock::new(1);
let reader = lock.upgradable_read().await;
assert_eq!(*reader, 1);
assert_eq!(*lock.try_read().unwrap(), 1);
let mut writer = RwLockUpgradableReadGuard::upgrade(reader).await;
*writer = 2;
sourcepub fn try_upgradable_read_arc(
self: &Arc<RwLock<T>>
) -> Option<RwLockUpgradableReadGuardArc<T>>
pub fn try_upgradable_read_arc( self: &Arc<RwLock<T>> ) -> Option<RwLockUpgradableReadGuardArc<T>>
Attempts to acquire an owned, reference-counted read lock with the possiblity to upgrade to a write lock.
If a read lock could not be acquired at this time, then None
is returned. Otherwise, a
guard is returned that releases the lock when dropped.
Upgradable read lock reserves the right to be upgraded to a write lock, which means there can be at most one upgradable read lock at a time.
Examples
use std::sync::Arc;
use async_lock::{RwLock, RwLockUpgradableReadGuardArc};
let lock = Arc::new(RwLock::new(1));
let reader = lock.upgradable_read_arc().await;
assert_eq!(*reader, 1);
assert_eq!(*lock.try_read_arc().unwrap(), 1);
let mut writer = RwLockUpgradableReadGuardArc::upgrade(reader).await;
*writer = 2;
sourcepub fn upgradable_read_arc<'a>(
self: &'a Arc<RwLock<T>>
) -> UpgradableReadArc<'a, T> ⓘ
pub fn upgradable_read_arc<'a>( self: &'a Arc<RwLock<T>> ) -> UpgradableReadArc<'a, T> ⓘ
Acquires an owned, reference-counted read lock with the possiblity to upgrade to a write lock.
Returns a guard that releases the lock when dropped.
Upgradable read lock reserves the right to be upgraded to a write lock, which means there can be at most one upgradable read lock at a time.
Note that attempts to acquire an upgradable read lock will block if there are concurrent attempts to acquire another upgradable read lock or a write lock.
Examples
use std::sync::Arc;
use async_lock::{RwLock, RwLockUpgradableReadGuardArc};
let lock = Arc::new(RwLock::new(1));
let reader = lock.upgradable_read_arc().await;
assert_eq!(*reader, 1);
assert_eq!(*lock.try_read_arc().unwrap(), 1);
let mut writer = RwLockUpgradableReadGuardArc::upgrade(reader).await;
*writer = 2;
sourcepub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>>
Attempts to acquire a write lock.
If a write lock could not be acquired at this time, then None
is returned. Otherwise, a
guard is returned that releases the lock when dropped.
Examples
use async_lock::RwLock;
let lock = RwLock::new(1);
assert!(lock.try_write().is_some());
let reader = lock.read().await;
assert!(lock.try_write().is_none());
sourcepub fn write(&self) -> Write<'_, T> ⓘ
pub fn write(&self) -> Write<'_, T> ⓘ
Acquires a write lock.
Returns a guard that releases the lock when dropped.
Examples
use async_lock::RwLock;
let lock = RwLock::new(1);
let writer = lock.write().await;
assert!(lock.try_read().is_none());
sourcepub fn try_write_arc(self: &Arc<RwLock<T>>) -> Option<RwLockWriteGuardArc<T>>
pub fn try_write_arc(self: &Arc<RwLock<T>>) -> Option<RwLockWriteGuardArc<T>>
Attempts to acquire an owned, reference-counted write lock.
If a write lock could not be acquired at this time, then None
is returned. Otherwise, a
guard is returned that releases the lock when dropped.
Examples
use std::sync::Arc;
use async_lock::RwLock;
let lock = Arc::new(RwLock::new(1));
assert!(lock.try_write_arc().is_some());
let reader = lock.read_arc().await;
assert!(lock.try_write_arc().is_none());
sourcepub fn write_arc<'a>(self: &'a Arc<RwLock<T>>) -> WriteArc<'a, T> ⓘ
pub fn write_arc<'a>(self: &'a Arc<RwLock<T>>) -> WriteArc<'a, T> ⓘ
Acquires an owned, reference-counted write lock.
Returns a guard that releases the lock when dropped.
Examples
use std::sync::Arc;
use async_lock::RwLock;
let lock = Arc::new(RwLock::new(1));
let writer = lock.write_arc().await;
assert!(lock.try_read_arc().is_none());
sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the inner value.
Since this call borrows the lock mutably, no actual locking takes place. The mutable borrow statically guarantees no locks exist.
Examples
use async_lock::RwLock;
let mut lock = RwLock::new(1);
*lock.get_mut() = 2;
assert_eq!(*lock.read().await, 2);
Trait Implementations§
impl<T> Send for RwLock<T>
impl<T> 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§
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
§impl<T> FromFd for T
impl<T> FromFd for T
§impl<T> FromFilelike for T
impl<T> FromFilelike for T
§fn from_filelike(owned: OwnedFd) -> T
fn from_filelike(owned: OwnedFd) -> T
Self
from the given filelike object. Read more§fn from_into_filelike<Owned>(owned: Owned) -> Twhere
Owned: IntoFilelike,
fn from_into_filelike<Owned>(owned: Owned) -> Twhere
Owned: IntoFilelike,
Self
from the given filelike object
converted from into_owned
. Read more§impl<T> FromSocketlike for T
impl<T> FromSocketlike for T
§fn from_socketlike(owned: OwnedFd) -> T
fn from_socketlike(owned: OwnedFd) -> T
Self
from the given socketlike object.§fn from_into_socketlike<Owned>(owned: Owned) -> Twhere
Owned: IntoSocketlike,
fn from_into_socketlike<Owned>(owned: Owned) -> Twhere
Owned: IntoSocketlike,
Self
from the given socketlike object
converted from into_owned
.