pub struct Notify { /* private fields */ }Expand description
A single-threaded async notification primitive.
notify_one stores one permit when no task is waiting; the next
Notify::notified call consumes it immediately. notify_waiters wakes all
current waiters and does not create a stored permit.
§Differences from Tokio
This primitive is local to one runtime thread and has no public named
Notified future type. Like Tokio’s Notify, it stores at most one permit
for notify_one, but wakeups are only for local tasks; use channels or
thread handles for cross-thread notification.
§Examples
use std::cell::Cell;
use std::rc::Rc;
use runite::sync::Notify;
let notify = Rc::new(Notify::new());
let woke = Rc::new(Cell::new(false));
runite::spawn({
let notify = Rc::clone(¬ify);
let woke = Rc::clone(&woke);
async move {
notify.notified().await;
woke.set(true);
}
});
runite::spawn({
let notify = Rc::clone(¬ify);
async move {
runite::yield_now().await;
notify.notify_one();
}
});
runite::run();
assert!(woke.get());Implementations§
Source§impl Notify
impl Notify
Sourcepub async fn notified(&self)
pub async fn notified(&self)
Waits for a notification.
If notify_one has already stored a permit, this
returns immediately and consumes that permit.
notify_one wakes the oldest waiter first (FIFO). If a future returned by
this method is selected by notify_one but dropped before it completes,
the notification is forwarded to the next waiter (or stored as a permit)
rather than being lost. A future woken by notify_waiters
is a broadcast wake and is not forwarded when dropped.
Sourcepub fn notify_one(&self)
pub fn notify_one(&self)
Wakes one waiting task or stores one permit for the next waiter.
The oldest waiter is woken first (FIFO). If that selected future is dropped before completing, the notification is forwarded to the next waiter or stored as the single permit.
At most one permit is stored; repeated calls before a waiter arrives
still allow only one future notified call to complete
immediately.
Sourcepub fn notify_waiters(&self)
pub fn notify_waiters(&self)
Wakes all tasks that are currently waiting.
This does not store a permit for future waiters. Broadcast wakeups are
not forwarded if a selected notified() future is dropped before it
completes.