Skip to main content

ReentrantRwLock

Struct ReentrantRwLock 

Source
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>

Source

pub const fn new(value: T) -> Self

An unlocked lock holding value.

Source

pub fn into_inner(self) -> T

Consumes the lock and returns its value.

Source§

impl<T: ?Sized> ReentrantRwLock<T>

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get_mut(&mut self) -> &mut T

The value, borrowed mutably; &mut self proves no guard exists.

Trait Implementations§

Source§

impl<T: ?Sized> Debug for ReentrantRwLock<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for ReentrantRwLock<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ?Sized + Send> Send for ReentrantRwLock<T>

Source§

impl<T: ?Sized + Send + Sync> Sync for ReentrantRwLock<T>

Auto Trait Implementations§

§

impl<T> !Freeze for ReentrantRwLock<T>

§

impl<T> !RefUnwindSafe for ReentrantRwLock<T>

§

impl<T> !UnwindSafe for ReentrantRwLock<T>

§

impl<T> Unpin for ReentrantRwLock<T>
where T: Unpin + ?Sized,

§

impl<T> UnsafeUnpin for ReentrantRwLock<T>
where T: UnsafeUnpin + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.