Trait lockable::Lockable

source ·
pub trait Lockable<'t, T: 't + ?Sized> {
    type Guard: Deref<Target = T>;
    type GuardMut: DerefMut<Target = T>;

    // Required methods
    fn lock(&'t self) -> Self::Guard;
    fn try_lock(&'t self) -> Option<Self::Guard>;
    fn lock_mut(&'t self) -> Self::GuardMut;
    fn try_lock_mut(&'t self) -> Option<Self::GuardMut>;
    fn is_locked(&self) -> bool;
    fn get_mut(&'t mut self) -> &'t mut T;
}
Expand description

A trait representing types that can be locked, e.g., Mutexes.

It also can represent types like RwLock (read-write lock) that allow multiple concurrent readers but only one concurrent writer.

Note: an optional design choice would be to remove the generic T parameter and instead assign it as an associated type, e.g., type Inner: 't.

Required Associated Types§

source

type Guard: Deref<Target = T>

The immutable “guard” type returned by the Self::lock() function.

source

type GuardMut: DerefMut<Target = T>

The mutable “guard” type returned by the Self::lock_mut() function.

For locks like RwLock that differentiate between read-only and read-write locks, this should be set to the read-write guard type. For locks like Mutex that only have one locking function, this should be set to the same type as Self::Guard.

Required Methods§

source

fn lock(&'t self) -> Self::Guard

Obtain the lock in a blocking fashion, returning an immutable guard that dereferences into the inner data.

source

fn try_lock(&'t self) -> Option<Self::Guard>

Attempt to obtain the lock in a non-blocking fashion, returning an immutable guard that dereferences into the inner data.

If the lock is already locked, this returns None.

source

fn lock_mut(&'t self) -> Self::GuardMut

Obtain the lock in a blocking fashion, returning a mutable guard that dereferences into the inner data.

source

fn try_lock_mut(&'t self) -> Option<Self::GuardMut>

Attempt to obtain the lock in a non-blocking fashion, returning a mutable guard that dereferences into the inner data.

If the lock is already locked, this returns None.

source

fn is_locked(&self) -> bool

Returns true if this lock is currently locked.

For types like RwLock that can lock in a read-only or read-write manner, this should return true if the singular writer lock is obtained, and false if only the reader lock is obtained.

source

fn get_mut(&'t mut self) -> &'t mut T

Returns a mutable reference to the underlying data.

Implementations on Foreign Types§

source§

impl<'t, T> Lockable<'t, T> for IrqSafeMutex<T>where T: 't,

Implement Lockable for [sync_irq::IrqSafeMutex].

§

type Guard = MutexGuard<'t, T, DisableIrq>

§

type GuardMut = <Mutex<T, DisableIrq> as Lockable<'t, T>>::Guard

source§

fn lock(&'t self) -> Self::Guard

source§

fn try_lock(&'t self) -> Option<Self::Guard>

source§

fn lock_mut(&'t self) -> Self::GuardMut

source§

fn try_lock_mut(&'t self) -> Option<Self::GuardMut>

source§

fn is_locked(&self) -> bool

source§

fn get_mut(&'t mut self) -> &mut T

source§

impl<'t, T> Lockable<'t, T> for IrqSafeRwLock<T>where T: 't,

Implement Lockable for [sync_irq::IrqSafeRwLock].

§

type Guard = RwLockReadGuard<'t, T, DisableIrq>

§

type GuardMut = RwLockWriteGuard<'t, T, DisableIrq>

source§

fn lock(&'t self) -> Self::Guard

source§

fn try_lock(&'t self) -> Option<Self::Guard>

source§

fn lock_mut(&'t self) -> Self::GuardMut

source§

fn try_lock_mut(&'t self) -> Option<Self::GuardMut>

source§

fn is_locked(&self) -> bool

source§

fn get_mut(&'t mut self) -> &mut T

source§

impl<'t, T> Lockable<'t, T> for RwLock<T>where T: 't + ?Sized,

Implement Lockable for [spin::RwLock].

§

type Guard = RwLockReadGuard<'t, T>

§

type GuardMut = RwLockWriteGuard<'t, T, Spin>

source§

fn lock(&'t self) -> Self::Guard

source§

fn try_lock(&'t self) -> Option<Self::Guard>

source§

fn lock_mut(&'t self) -> Self::GuardMut

source§

fn try_lock_mut(&'t self) -> Option<Self::GuardMut>

source§

fn is_locked(&self) -> bool

source§

fn get_mut(&'t mut self) -> &mut T

source§

impl<'t, T> Lockable<'t, T> for Mutex<T>where T: 't + ?Sized,

Implement Lockable for [spin::Mutex].

§

type Guard = MutexGuard<'t, T>

§

type GuardMut = <Mutex<T, Spin> as Lockable<'t, T>>::Guard

source§

fn lock(&'t self) -> Self::Guard

source§

fn try_lock(&'t self) -> Option<Self::Guard>

source§

fn lock_mut(&'t self) -> Self::GuardMut

source§

fn try_lock_mut(&'t self) -> Option<Self::GuardMut>

source§

fn is_locked(&self) -> bool

source§

fn get_mut(&'t mut self) -> &mut T

Implementors§