pub struct Condvar { /* private fields */ }Expand description
A condition variable that allows tasks to wait for a notification.
See the module level documentation for more.
Implementations§
Source§impl Condvar
impl Condvar
Sourcepub fn notify_one(&self)
pub fn notify_one(&self)
Wakes up one blocked task on this condvar.
Sourcepub fn notify_all(&self)
pub fn notify_all(&self)
Wakes up all blocked tasks on this condvar.
Sourcepub async fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T>
pub async fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T>
Yields the current task until this condition variable receives a notification.
Unlike the std equivalent, this does not check that a single mutex is used at runtime. However, as a best practice avoid using with multiple mutexes.
Sourcepub async fn wait_owned<T>(
&self,
guard: OwnedMutexGuard<T>,
) -> OwnedMutexGuard<T>
pub async fn wait_owned<T>( &self, guard: OwnedMutexGuard<T>, ) -> OwnedMutexGuard<T>
Yields the current task until this condition variable receives a notification.
Unlike the std equivalent, this does not check that a single mutex is used at runtime. However, as a best practice avoid using with multiple mutexes.
Sourcepub async fn wait_while<'a, T, F>(
&self,
guard: MutexGuard<'a, T>,
condition: F,
) -> MutexGuard<'a, T>
pub async fn wait_while<'a, T, F>( &self, guard: MutexGuard<'a, T>, condition: F, ) -> MutexGuard<'a, T>
Yields the current task until this condition variable receives a notification and the provided condition becomes false. Spurious wake-ups are ignored and this function will only return once the condition has been met.
use std::sync::Arc;
use mea::condvar::Condvar;
use mea::mutex::Mutex;
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair_clone = pair.clone();
tokio::spawn(async move {
let (lock, cvar) = &*pair_clone;
let mut started = lock.lock().await;
*started = true;
// We notify the condvar that the value has changed.
cvar.notify_one();
});
// Wait for the thread to start up.
let (lock, cvar) = &*pair;
// As long as the value inside the `Mutex<bool>` is `false`, we wait.
let guard = cvar
.wait_while(lock.lock().await, |started| !*started)
.await;
assert!(*guard);Sourcepub async fn wait_while_owned<T, F>(
&self,
guard: OwnedMutexGuard<T>,
condition: F,
) -> OwnedMutexGuard<T>
pub async fn wait_while_owned<T, F>( &self, guard: OwnedMutexGuard<T>, condition: F, ) -> OwnedMutexGuard<T>
Yields the current task until this condition variable receives a notification and the provided condition becomes false. Spurious wake-ups are ignored and this function will only return once the condition has been met.
use std::sync::Arc;
use mea::condvar::Condvar;
use mea::mutex::Mutex;
let pair = (Arc::new(Mutex::new(false)), Arc::new(Condvar::new()));
let pair_clone = pair.clone();
tokio::spawn(async move {
let (lock, cvar) = pair_clone;
let mut started = lock.lock_owned().await;
*started = true;
// We notify the condvar that the value has changed.
cvar.notify_one();
});
// Wait for the thread to start up.
let (lock, cvar) = pair;
// As long as the value inside the `Mutex<bool>` is `false`, we wait.
let guard = cvar
.wait_while_owned(lock.lock_owned().await, |started| !*started)
.await;
assert!(*guard);