RwLock

Struct RwLock 

Source
pub struct RwLock<T> { /* private fields */ }
Expand description

A wrapper around a reader-writer lock that tracks operations for deadlock detection

The RwLock provides the same API as a standard reader-writer lock but also notifies the detector on lock/unlock operations.

Implementations§

Source§

impl<T> RwLock<T>

Source

pub fn new(value: T) -> Self

Create a new tracked RwLock with a unique ID

§Arguments
  • value - The initial value to store in the lock
§Returns

A new RwLock wrapping the provided value

§Example
use deloxide::RwLock;
let lock = RwLock::new(42);
Source

pub fn id(&self) -> LockId

Get the unique ID of this lock

Source

pub fn creator_thread_id(&self) -> ThreadId

Get the creator thread ID

Source

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

Acquire a shared (read) lock, tracking the attempt and acquisition

Uses two-phase locking protocol to eliminate race conditions between deadlock detection and lock acquisition.

§Returns

A guard which releases the lock when dropped

Source

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

Acquire an exclusive (write) lock, tracking the attempt and acquisition

Uses two-phase locking protocol to eliminate race conditions between deadlock detection and lock acquisition.

§Returns

A guard which releases the lock when dropped

Source

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

Try to acquire a shared (read) lock, tracking the attempt

Uses atomic detection to ensure deadlock detection and acquisition happen together.

Source

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

Try to acquire an exclusive (write) lock, tracking the attempt

Uses atomic detection to ensure deadlock detection and acquisition happen together.

Source

pub fn into_inner(self) -> T
where T: Sized,

Consumes this RwLock, returning the underlying data

§Example
use deloxide::RwLock;

let lock = RwLock::new(String::from("hello"));
let s = lock.into_inner();
assert_eq!(s, "hello");
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.

§Example
use deloxide::RwLock;

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

Trait Implementations§

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> Drop for RwLock<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
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

Auto Trait Implementations§

§

impl<T> !Freeze for RwLock<T>

§

impl<T> !RefUnwindSafe for RwLock<T>

§

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

§

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

§

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

§

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

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

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

impl<T> ErasedDestructor for T
where T: 'static,