Struct Condvar

Source
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

Source

pub fn new() -> Condvar

Creates a new condition variable

§Examples
use mea::condvar::Condvar;

let cvar = Condvar::new();
Source

pub fn notify_one(&self)

Wakes up one blocked task on this condvar.

Source

pub fn notify_all(&self)

Wakes up all blocked tasks on this condvar.

Source

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.

Source

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.

Source

pub async fn wait_while<'a, T, F>( &self, guard: MutexGuard<'a, T>, condition: F, ) -> MutexGuard<'a, T>
where F: FnMut(&mut T) -> bool,

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);
Source

pub async fn wait_while_owned<T, F>( &self, guard: OwnedMutexGuard<T>, condition: F, ) -> OwnedMutexGuard<T>
where F: FnMut(&mut T) -> bool,

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);

Trait Implementations§

Source§

impl Debug for Condvar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Condvar

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.