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
impl Waiter
Sourcepub fn notify_all(&self)
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.
Sourcepub fn wait_until<T>(
&self,
deadline: Option<Instant>,
ready: impl FnMut() -> Option<T>,
) -> Option<T>
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.