Struct maitake_sync::spin::RwLock

source ·
pub struct RwLock<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§

source§

impl<T> RwLock<T>

source

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

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

§Examples
use maitake_sync::spin::RwLock;

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

impl<T: ?Sized> RwLock<T>

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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);
source§

impl<T> RwLock<T>

source

pub fn into_inner(self) -> T

Consumes this RwLock, returning the guarded data.

Trait Implementations§

source§

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

source§

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

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

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

source§

fn default() -> RwLock<T>

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

source§

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

source§

fn from(t: T) -> Self

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

source§

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

source§

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

Auto Trait Implementations§

§

impl<T> !Freeze for RwLock<T>

§

impl<T> !RefUnwindSafe for RwLock<T>

§

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

§

impl<T> UnwindSafe for RwLock<T>
where T: UnwindSafe + ?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<!> 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.

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.