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., Mutex
es.
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§
sourcetype Guard: Deref<Target = T>
type Guard: Deref<Target = T>
The immutable “guard” type returned by the Self::lock()
function.
sourcetype GuardMut: DerefMut<Target = T>
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§
sourcefn lock(&'t self) -> Self::Guard
fn lock(&'t self) -> Self::Guard
Obtain the lock in a blocking fashion, returning an immutable guard that dereferences into the inner data.
sourcefn try_lock(&'t self) -> Option<Self::Guard>
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
.
sourcefn lock_mut(&'t self) -> Self::GuardMut
fn lock_mut(&'t self) -> Self::GuardMut
Obtain the lock in a blocking fashion, returning a mutable guard that dereferences into the inner data.
sourcefn try_lock_mut(&'t self) -> Option<Self::GuardMut>
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
.
Implementations on Foreign Types§
source§impl<'t, T> Lockable<'t, T> for IrqSafeMutex<T>where
T: 't,
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
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) -> &mut T
source§impl<'t, T> Lockable<'t, T> for IrqSafeRwLock<T>where
T: 't,
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>
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) -> &mut T
source§impl<'t, T> Lockable<'t, T> for RwLock<T>where
T: 't + ?Sized,
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>
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) -> &mut T
source§impl<'t, T> Lockable<'t, T> for Mutex<T>where
T: 't + ?Sized,
impl<'t, T> Lockable<'t, T> for Mutex<T>where T: 't + ?Sized,
Implement Lockable
for [spin::Mutex
].