pub struct WaitCondition<F: WaitConditionFn> { /* private fields */ }
Expand description

A condition variable that allows multiple Tasks to wait for a condition to be met, upon which other Tasks can notify them. This is effectively a convenience wrapper around WaitQueue::wait_until().

The condition is specified as an closure that returns a boolean: true if the condition has been met, false if not.

The condition closure must be a regular Fn that can be repeatedly executed, and should be cheap and quick to execute. Complicated logic should be kept outside of the condition function.

This can be shared across multiple Tasks by wrapping it in an Arc.

Implementations§

source§

impl<F: Fn() -> bool> WaitCondition<F>

source

pub fn new(condition_fn: F) -> WaitCondition<F>

Create a new WaitCondition in which Tasks can wait for a condition to be met, as defined by the given condition_fn.

source

pub fn wait(&self)

Waits for the condition to be true in a blocking fashion that puts the current Task to sleep until it is notified that the condition has been met.

The design of WaitCondition prevents spurious wakeups; Tasks are only allowed to If the Task wakes up spuriously (it is still on the waitqueue), it will be automatically put back to sleep until it is properly woken up. Therefore, there is no need for the caller to check for spurious wakeups.

This function blocks until the Task is woken up through the notify mechanism.

source

pub fn condition_satisfied(&self) -> Option<SatisfiedWaitCondition<'_, F>>

This function should be invoked after the wait condition has been met and you are ready to notify other waiting tasks. The condition function within this WaitCondition object will be run again to ensure it has been met.

If the condition is met, it returns a SatisfiedWaitCondition object that can be used. to notify (wake up) the other tasks waiting on this WaitCondition. If the condition is not met, it returns None.

Auto Trait Implementations§

§

impl<F> !RefUnwindSafe for WaitCondition<F>

§

impl<F> Send for WaitCondition<F>where F: Send,

§

impl<F> Sync for WaitCondition<F>where F: Sync,

§

impl<F> Unpin for WaitCondition<F>where F: Unpin,

§

impl<F> !UnwindSafe for WaitCondition<F>

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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

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.
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.
source§

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

Performs the conversion.