Struct kernel::sync::CondVar

source ·
pub struct CondVar { /* private fields */ }
Expand description

A conditional variable.

Exposes the kernel’s struct wait_queue_head as a condition variable. It allows the caller to atomically release the given lock and go to sleep. It reacquires the lock when it wakes up. And it wakes up when notified by another thread (via CondVar::notify_one or CondVar::notify_all) or because the thread received a signal. It may also wake up spuriously.

Instances of CondVar need a lock class and to be pinned. The recommended way to create such instances is with the pin_init and new_condvar macros.

Examples

The following is an example of using a condvar with a mutex:

use kernel::sync::{CondVar, Mutex};
use kernel::{new_condvar, new_mutex};

#[pin_data]
pub struct Example {
    #[pin]
    value: Mutex<u32>,

    #[pin]
    value_changed: CondVar,
}

/// Waits for `e.value` to become `v`.
fn wait_for_value(e: &Example, v: u32) {
    let mut guard = e.value.lock();
    while *guard != v {
        e.value_changed.wait(&mut guard);
    }
}

/// Increments `e.value` and notifies all potential waiters.
fn increment(e: &Example) {
    *e.value.lock() += 1;
    e.value_changed.notify_all();
}

/// Allocates a new boxed `Example`.
fn new_example() -> Result<Pin<Box<Example>>> {
    Box::pin_init(pin_init!(Example {
        value <- new_mutex!(0),
        value_changed <- new_condvar!(),
    }))
}

Implementations§

source§

impl CondVar

source

pub fn new( name: &'static CStr, key: &'static LockClassKey ) -> impl PinInit<Self>

Constructs a new condvar initialiser.

source

pub fn wait<T: ?Sized, B: Backend>(&self, guard: &mut Guard<'_, T, B>)

Releases the lock and waits for a notification in uninterruptible mode.

Atomically releases the given lock (whose ownership is proven by the guard) and puts the thread to sleep, reacquiring the lock on wake up. It wakes up when notified by CondVar::notify_one or CondVar::notify_all. Note that it may also wake up spuriously.

source

pub fn wait_interruptible<T: ?Sized, B: Backend>( &self, guard: &mut Guard<'_, T, B> ) -> bool

Releases the lock and waits for a notification in interruptible mode.

Similar to CondVar::wait, except that the wait is interruptible. That is, the thread may wake up due to signals. It may also wake up spuriously.

Returns whether there is a signal pending.

source

pub fn notify_one(&self)

Wakes a single waiter up, if any.

This is not ‘sticky’ in the sense that if no thread is waiting, the notification is lost completely (as opposed to automatically waking up the next waiter).

source

pub fn notify_all(&self)

Wakes all waiters up, if any.

This is not ‘sticky’ in the sense that if no thread is waiting, the notification is lost completely (as opposed to automatically waking up the next waiter).

Trait Implementations§

Auto Trait Implementations§

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, E> Init<T, E> for T

source§

unsafe fn __init(self, slot: *mut T) -> Result<(), E>

Initializes slot. Read more
source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
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, E> PinInit<T, E> for T

source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E>

Initializes slot. Read more
source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
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.