Struct maitake::sync::spin::RwLock

pub struct RwLock<T>
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 spin::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::spin::RwLock;

let lock = RwLock::new(5);
§

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

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

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

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>

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

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

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::spin::RwLock::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.read(), 10);
§

impl<T> RwLock<T>

pub fn into_inner(self) -> T

Consumes this RwLock, returning the guarded data.

Trait Implementations§

§

impl<T> Debug for RwLock<T>
where T: Debug,

§

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

Formats the value using the given formatter. Read more
§

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

§

fn default() -> RwLock<T>

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> Send for RwLock<T>
where T: Send + ?Sized,

§

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

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