RwLock

Struct RwLock 

Source
pub struct RwLock<T>
where T: Sized + Sync,
{ /* private fields */ }
Expand description

A scalable reader-writer lock.

This lock favours reader performance over writers. Each reader thread gets its own “lock” while writers share a single lock.

T represents the underlying type protected by the lock. Calling read() returns a read-guard that can be used to safely read T. Calling write() returns a write-guard that can be used to safely mutate T.

Implementations§

Source§

impl<T> RwLock<T>
where T: Sized + Sync,

Source

pub fn new(t: T) -> Self

Returns a new instance of a RwLock. Default constructs the underlying data structure.

Source

pub fn write(&self, n: usize) -> WriteGuard<'_, T>

Locks the underlying data-structure for writes. The caller can retrieve a mutable reference from the returned WriteGuard.

n is the number of active readers currently using this reader-writer lock.

§Example
    use node_replication::rwlock::RwLock;

    // Create the lock.
    let lock = RwLock::<usize>::default();

    // Acquire the write lock. This returns a guard that can be used
    // to perform writes against the protected data. We need to know
    // the number of concurrent reader threads upfront.
    const N_CONCURRENT_READERS: usize = 32;
    let mut w_guard = lock.write(N_CONCURRENT_READERS);
    *w_guard = 777;
Source

pub fn read(&self, tid: usize) -> ReadGuard<'_, T>

Locks the underlying data-structure for reads. Allows multiple readers to acquire the lock. Blocks until there aren’t any active writers.

§Example
    use node_replication::rwlock::RwLock;

    // Create the lock.
    let lock = RwLock::<usize>::default();

    // Acquire the read lock. This returns a guard that can be used
    // to perform reads against the protected data. We need
    // a thread identifier to acquire this lock.
    const MY_THREAD_ID: usize = 16;
    let r_guard = lock.read(MY_THREAD_ID);
    assert_eq!(0, *r_guard);

Trait Implementations§

Source§

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

Source§

fn default() -> RwLock<T>

Returns a new instance of a RwLock. Default constructs the underlying data structure.

Source§

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

Sync trait allows RwLock to be shared between threads. The read() and write() logic ensures that we will never have threads writing to and reading from the underlying data structure simultaneously.

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