Trait lockable::Lockable

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

    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§

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

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§

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

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.

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

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.

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.

Returns a mutable reference to the underlying data.

Implementations on Foreign Types§

Implement Lockable for [spin::Mutex].

Implement Lockable for [spin::RwLock].

Implement Lockable for [irq_safety::MutexIrqSafe].

Implement Lockable for [irq_safety::RwLockIrqSafe].

Implementors§