Struct maitake::sync::blocking::RwLock

pub struct RwLock<T, Lock = RwSpinlock>
where T: ?Sized,
{ /* private fields */ }
Expand description

A spinlock-based readers-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 blocking::Mutex does not distinguish between readers or writers that acquire the lock, therefore blocking any threads waiting for the lock to become available. An RwLock will allow any number of readers to acquire the lock as long as a writer is not holding the lock.

Fairness

This is not a fair reader-writer lock.

Loom-specific behavior

When cfg(loom) is enabled, this mutex will use Loom’s simulated atomics, checked UnsafeCell, and simulated spin loop hints.

Implementations§

§

impl<T> RwLock<T>

pub const fn new(data: T) -> RwLock<T>

Creates a new, unlocked RwLock<T> protecting the provided data.

Examples
use maitake_sync::blocking::RwLock;

let lock = RwLock::new(5);

pub fn reader_count(&self) -> usize

Returns the current number of readers holding a read lock.

Note

This method is not synchronized with attempts to increment the reader count, and its value may become out of date as soon as it is read. This is not intended to be used for synchronization purposes! It is intended only for debugging purposes or for use as a heuristic.

§

impl<T, Lock> RwLock<T, Lock>
where Lock: RawRwLock, T: ?Sized,

pub fn read(&self) -> RwLockReadGuard<'_, T, Lock>

Locks this RwLock for shared read access, spinning until it can be acquired.

The calling CPU core will spin (with an exponential backoff) until there are no more writers which hold the lock. There may be other readers currently inside the lock when this method returns. This method does not provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.

Returns an RAII guard which will release this thread’s shared access once it is dropped.

pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T, Lock>>

Attempts to acquire this RwLock for shared read access.

If the access could not be granted at this time, this method returns None. Otherwise, Some(RwLockReadGuard) containing a RAII guard is returned. The shared access is released when it is dropped.

This function does not spin.

pub fn write(&self) -> RwLockWriteGuard<'_, T, Lock>

Locks this RwLock for exclusive write access, spinning until write access can be acquired.

This function will not return while other writers or other readers currently have access to the lock.

Returns an RAII guard which will drop the write access of this RwLock when dropped.

pub fn has_writer(&self) -> bool

Returns true if there is currently a writer holding a write lock.

Note

This method is not synchronized its value may become out of date as soon as it is read. This is not intended to be used for synchronization purposes! It is intended only for debugging purposes or for use as a heuristic.

pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T, Lock>>

Attempts to acquire this RwLock for exclusive write access.

If the access could not be granted at this time, this method returns None. Otherwise, Some(RwLockWriteGuard) containing a RAII guard is returned. The write access is released when it is dropped.

This function does not spin.

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

Returns a mutable reference to the underlying data.

Since this call borrows the RwLock mutably, no actual locking needs to take place – the mutable borrow statically guarantees no locks exist.

Examples
let mut lock = maitake_sync::blocking::RwLock::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.read(), 10);
§

impl<T, Lock> RwLock<T, Lock>
where Lock: RawRwLock,

pub fn into_inner(self) -> T

Consumes this RwLock, returning the guarded data.

Trait Implementations§

§

impl<T, Lock> Debug for RwLock<T, Lock>
where T: Debug, Lock: Debug + RawRwLock,

§

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

Formats the value using the given formatter. Read more
§

impl<T, Lock> Default for RwLock<T, Lock>
where T: Default, Lock: Default,

§

fn default() -> RwLock<T, Lock>

Creates a new RwLock<T>, with the Default value for T.

§

impl<T> From<T> for RwLock<T>

§

fn from(t: T) -> RwLock<T>

Creates a new instance of an RwLock<T> which is unlocked. This is equivalent to RwLock::new.

§

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

§

impl<T, Lock> Sync for RwLock<T, Lock>
where T: Send + Sync + ?Sized, Lock: Sync,

Auto Trait Implementations§

§

impl<T, Lock = RwSpinlock> !RefUnwindSafe for RwLock<T, Lock>

§

impl<T: ?Sized, Lock> Unpin for RwLock<T, Lock>
where Lock: Unpin, T: Unpin,

§

impl<T: ?Sized, Lock> UnwindSafe for RwLock<T, Lock>
where Lock: UnwindSafe, T: UnwindSafe,

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<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

source§

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

§

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

§

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more