Struct wait_condition::WaitCondition
source · pub struct WaitCondition<F: WaitConditionFn> { /* private fields */ }
Expand description
A condition variable that allows multiple Task
s to wait for a condition to be met,
upon which other Task
s 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 Task
s by wrapping it in an Arc
.
Implementations§
source§impl<F: Fn() -> bool> WaitCondition<F>
impl<F: Fn() -> bool> WaitCondition<F>
sourcepub fn new(condition_fn: F) -> WaitCondition<F>
pub fn new(condition_fn: F) -> WaitCondition<F>
Create a new WaitCondition
in which Task
s can wait
for a condition to be met, as defined by the given condition_fn
.
sourcepub fn wait(&self)
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.
sourcepub fn condition_satisfied(&self) -> Option<SatisfiedWaitCondition<'_, F>>
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
.