Struct ksched::sync::RwLock[][src]

pub struct RwLock<T: ?Sized> { /* fields omitted */ }
Expand description

An async reader-writer lock.

This type of lock allows multiple readers or one writer at any point in time.

The locking strategy is write-preferring, which means writers are never starved. Releasing a write lock wakes the next blocked reader and the next blocked writer.

Examples

use ksched::sync::RwLock;

let lock = RwLock::new(5);

// Multiple read locks can be held at a time.
let r1 = lock.read().await;
let r2 = lock.read().await;
assert_eq!(*r1, 5);
assert_eq!(*r2, 5);
drop((r1, r2));

// Only one write lock can be held at a time.
let mut w = lock.write().await;
*w += 1;
assert_eq!(*w, 6);

Implementations

impl<T> RwLock<T>[src]

pub const fn new(value: T) -> RwLock<T>[src]

Creates a new reader-writer lock.

Examples

use ksched::sync::RwLock;

let lock = RwLock::new(0);

pub fn into_inner(self) -> T[src]

Unwraps the lock and returns the inner value.

Examples

use ksched::sync::RwLock;

let lock = RwLock::new(5);
assert_eq!(lock.into_inner(), 5);

impl<T: ?Sized> RwLock<T>[src]

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

Attempts to acquire a read lock.

If a read lock could not be acquired at this time, then None is returned. Otherwise, a guard is returned that releases the lock when dropped.

See also read

pub async fn read(&self) -> RwLockReadGuard<'_, T>[src]

Acquires a read lock.

Returns a guard that releases the lock when dropped.

Note that attempts to acquire a read lock will block if there are also concurrent attempts to acquire a write lock.

Examples

use ksched::sync::RwLock;

let lock = RwLock::new(1);

let reader = lock.read().await;
assert_eq!(*reader, 1);

assert!(lock.try_read().is_some());

pub fn try_upgradable_read(&self) -> Option<RwLockUpgradableReadGuard<'_, T>>[src]

Attempts to acquire a read lock with the possiblity to upgrade to a write lock.

If a read lock could not be acquired at this time, then None is returned. Otherwise, a guard is returned that releases the lock when dropped.

Upgradable read lock reserves the right to be upgraded to a write lock, which means there can be at most one upgradable read lock at a time.

See also upgradable_read

pub async fn upgradable_read(&self) -> RwLockUpgradableReadGuard<'_, T>[src]

Attempts to acquire a read lock with the possiblity to upgrade to a write lock.

Returns a guard that releases the lock when dropped.

Upgradable read lock reserves the right to be upgraded to a write lock, which means there can be at most one upgradable read lock at a time.

Note that attempts to acquire an upgradable read lock will block if there are concurrent attempts to acquire another upgradable read lock or a write lock.

Examples

use ksched::sync::{RwLock, RwLockUpgradableReadGuard};

let lock = RwLock::new(1);

let reader = lock.upgradable_read().await;
assert_eq!(*reader, 1);
assert_eq!(*lock.try_read().unwrap(), 1);

let mut writer = RwLockUpgradableReadGuard::upgrade(reader).await;
*writer = 2;

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

Attempts to acquire a write lock.

If a write lock could not be acquired at this time, then None is returned. Otherwise, a guard is returned that releases the lock when dropped.

See also write

pub async fn write(&self) -> RwLockWriteGuard<'_, T>[src]

Acquires a write lock.

Returns a guard that releases the lock when dropped.

Examples

use ksched::sync::RwLock;

let lock = RwLock::new(1);

let writer = lock.write().await;
assert!(lock.try_read().is_none());

pub fn get_mut(&mut self) -> &mut T[src]

Returns a mutable reference to the inner value.

Since this call borrows the lock mutably, no actual locking takes place. The mutable borrow statically guarantees no locks exist.

Examples

use ksched::sync::RwLock;

let mut lock = RwLock::new(1);

*lock.get_mut() = 2;
assert_eq!(*lock.read().await, 2);

Trait Implementations

impl<T: Debug + ?Sized> Debug for RwLock<T>[src]

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

Formats the value using the given formatter. Read more

impl<T: Default + ?Sized> Default for RwLock<T>[src]

fn default() -> RwLock<T>[src]

Returns the “default value” for a type. Read more

impl<T> From<T> for RwLock<T>[src]

fn from(val: T) -> RwLock<T>[src]

Performs the conversion.

impl<T: Send + ?Sized> Send for RwLock<T>[src]

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

Auto Trait Implementations

impl<T> !RefUnwindSafe for RwLock<T>

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

impl<T> !UnwindSafe for RwLock<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<!> for T[src]

pub fn from(t: !) -> T[src]

Performs the conversion.

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.