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>

source

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.

source

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>

source

pub fn lock(self) -> FairMutexGuard<'a, T>

Locks the mutex.

Trait Implementations§

source§

impl<'a, T: ?Sized, R> Drop for Starvation<'a, T, R>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T, R> !RefUnwindSafe for Starvation<'a, T, R>

§

impl<'a, T: ?Sized, R> Send for Starvation<'a, T, R>where T: Send,

§

impl<'a, T: ?Sized, R> Sync for Starvation<'a, T, R>where T: Send,

§

impl<'a, T: ?Sized, R> Unpin for Starvation<'a, T, R>

§

impl<'a, T, R> !UnwindSafe for Starvation<'a, T, R>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.