pub struct ReentrantRwLock<T: ?Sized> { /* private fields */ }Expand description
A reader-writer lock its holders may lock again, with the bounded
bypass of RwLock.
Both guards give &T, since one owner may hold several at once; the
write side excludes every other owner, so mutate through interior
mutability that is Sync (an atomic, a Mutex), as
readers on other threads share the value. Guards and lock futures
borrow their Owner and stay on its thread.
use nuclei::sync::{Owner, ReentrantRwLock};
use std::sync::atomic::{AtomicU32, Ordering};
let lock = ReentrantRwLock::new(AtomicU32::new(0));
let me = Owner::new();
let write = lock.try_write(&me).expect("free");
let read = lock.try_read(&me).expect("a writer may also read");
write.store(7, Ordering::Relaxed);
drop(write);
assert!(lock.try_write(&Owner::new()).is_none(), "the read is still held");
assert_eq!(read.load(Ordering::Relaxed), 7);Implementations§
Source§impl<T> ReentrantRwLock<T>
impl<T> ReentrantRwLock<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the lock and returns its value.
Source§impl<T: ?Sized> ReentrantRwLock<T>
impl<T: ?Sized> ReentrantRwLock<T>
Sourcepub fn try_read<'a>(
&'a self,
owner: &'a Owner,
) -> Option<ReentrantReadGuard<'a, T>>
pub fn try_read<'a>( &'a self, owner: &'a Owner, ) -> Option<ReentrantReadGuard<'a, T>>
Reads if owner already reads or writes, or if no writer holds the
lock and no handoff is owed.
Sourcepub fn try_write<'a>(
&'a self,
owner: &'a Owner,
) -> Option<ReentrantWriteGuard<'a, T>>
pub fn try_write<'a>( &'a self, owner: &'a Owner, ) -> Option<ReentrantWriteGuard<'a, T>>
Writes if owner already writes, or if nobody holds the lock and
no handoff is owed. None too while owner reads without writing.
Sourcepub fn read<'a>(&'a self, owner: &'a Owner) -> ReentrantRead<'a, T> ⓘ
pub fn read<'a>(&'a self, owner: &'a Owner) -> ReentrantRead<'a, T> ⓘ
Waits to read, at once if owner already reads or writes. A
contended wait is lock-free, not wait-free: see
Progress.
Sourcepub fn write<'a>(&'a self, owner: &'a Owner) -> ReentrantWrite<'a, T> ⓘ
pub fn write<'a>(&'a self, owner: &'a Owner) -> ReentrantWrite<'a, T> ⓘ
Waits to write, at once if owner already writes. Fails with
ReadHeldByOwner when owner reads without writing: the write
would wait for that read, which only owner can end. A contended
wait is lock-free, not wait-free: see Progress.