Struct spin::mutex::fair::Starvation
source · pub struct Starvation<'a, T: ?Sized + 'a, R> { /* private fields */ }
Available on crate features
mutex
and fair_mutex
only.Expand description
A handle that indicates that we have been trying to acquire the lock for a while.
This handle is used to prevent starvation.
Implementations§
source§impl<'a, T: ?Sized, R> Starvation<'a, T, R>
impl<'a, T: ?Sized, R> Starvation<'a, T, R>
sourcepub fn try_lock_fair(self) -> Result<FairMutexGuard<'a, T>, Self>
pub fn try_lock_fair(self) -> Result<FairMutexGuard<'a, T>, Self>
Attempts the lock the mutex if we are the only starving user.
This allows another user to lock the mutex if they are starving as well.
sourcepub fn try_lock(self) -> Result<FairMutexGuard<'a, T>, Self>
pub fn try_lock(self) -> Result<FairMutexGuard<'a, T>, Self>
Attempts to lock the mutex.
If the lock is currently held by another thread, this will return None
.
Example
let lock = spin::mutex::FairMutex::<_>::new(42);
// Lock the mutex to simulate it being used by another user.
let guard1 = lock.lock();
// Try to lock the mutex.
let guard2 = lock.try_lock();
assert!(guard2.is_none());
// Wait for a while.
wait_for_a_while();
// We are now starved, indicate as such.
let starve = lock.starve();
// Once the lock is released, another user trying to lock it will
// fail.
drop(guard1);
let guard3 = lock.try_lock();
assert!(guard3.is_none());
// However, we will be able to lock it.
let guard4 = starve.try_lock();
assert!(guard4.is_ok());
source§impl<'a, T: ?Sized, R: RelaxStrategy> Starvation<'a, T, R>
impl<'a, T: ?Sized, R: RelaxStrategy> Starvation<'a, T, R>
sourcepub fn lock(self) -> FairMutexGuard<'a, T>
pub fn lock(self) -> FairMutexGuard<'a, T>
Locks the mutex.