Skip to main content

Waiter

Struct Waiter 

Source
pub struct Waiter { /* private fields */ }
Expand description

Blocks threads until a condition holds or a deadline passes on a clock.

Clones share one waiter, so threads can wait on it while others notify it. Code that changes the state a condition reads calls Waiter::notify_all after the change.

use darkbio_clock::Clock;
use std::sync::{Arc, Mutex};
use std::thread;

let waiter = Clock::real().waiter();
let slot = Arc::new(Mutex::new(None));
let taker = thread::spawn({
    let (waiter, slot) = (waiter.clone(), slot.clone());
    move || waiter.wait_until(None, || slot.lock().unwrap().take())
});
*slot.lock().unwrap() = Some(42);
waiter.notify_all();
assert_eq!(taker.join().unwrap(), Some(42));

Implementations§

Source§

impl Waiter

Source

pub fn notify_all(&self)

Wakes every thread waiting on this waiter to check its condition again. Call it after each change that can make a condition ready, with or without holding the lock that change took.

Source

pub fn wait_until<T>( &self, deadline: Option<Instant>, ready: impl FnMut() -> Option<T>, ) -> Option<T>

Blocks until ready returns a value or the clock reaches deadline.

Every check calls ready first and compares the deadline after, so a value that is ready wins over an expired deadline. The wait returns None when a check finds ready empty and the deadline reached. Without a deadline, only a value ends the wait. A plain delay passes a ready that always returns None.

A check follows each notification and each advance of a paused clock, and sometimes a wakeup where nothing changed. Changes that land together share one check, so ready can run fewer times than there were changes.

ready runs on the calling thread with no lock of the clock or the waiter held. It may take its own locks, notify, or advance a clock, and should return promptly.

Trait Implementations§

Source§

impl Clone for Waiter

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Waiter

Source§

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

Shows the clock, never the wakeup bookkeeping.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.