pub struct Notify { /* private fields */ }Expand description
Notify a single task to wake up.
Notify provides a basic mechanism to notify a single task of an event.
Notify itself does not carry any data. Instead, it is to be used to signal
another task to perform an operation.
If notify() is called before notified().await, then the next call to
notified().await will complete immediately, consuming one permit. Permits
accumulate: each call to notify() or [notify_n()] adds permits that can
be consumed by subsequent notified().await calls.
§Examples
Basic usage.
use std::sync::Arc;
use async_notify::Notify;
async_global_executor::block_on(async {
let notify = Arc::new(Notify::new());
let notify2 = notify.clone();
async_global_executor::spawn(async move {
notify2.notify();
println!("sent notification");
})
.detach();
println!("received notification");
notify.notified().await;
})Implementations§
Source§impl Notify
Like tokio Notify, this is a runtime independent Notify.
impl Notify
Like tokio Notify, this is a runtime independent Notify.
Sourcepub fn notify(&self)
pub fn notify(&self)
Notifies a waiting task
Adds one permit to this Notify. If a task is currently waiting on
notified().await, that task will be woken and complete. Otherwise,
the permit is stored and the next call to notified().await will
complete immediately. Permits accumulate across multiple notify() calls.
§Examples
use std::sync::Arc;
use async_notify::Notify;
async_global_executor::block_on(async {
let notify = Arc::new(Notify::new());
let notify2 = notify.clone();
async_global_executor::spawn(async move {
notify2.notify();
println!("sent notification");
})
.detach();
println!("received notification");
notify.notified().await;
})Sourcepub fn notify_n(&self, n: NonZeroUsize)
pub fn notify_n(&self, n: NonZeroUsize)
Grants n permits and notifies up to n waiting tasks.
Adds n permits to this Notify. If there are tasks currently waiting
on notified().await, up to n of them will be woken and complete,
each consuming one permit. If no tasks are waiting, the permits are
stored and the next up to n calls to notified().await will complete
immediately.
This is a generalization of notify() which is equivalent to
notify_n(NonZeroUsize::MIN).
Sourcepub fn notify_waiters(&self, n: NonZeroUsize)
pub fn notify_waiters(&self, n: NonZeroUsize)
Wakes up to n waiting tasks to compete for existing permits.
Unlike notify_n(), this does not add any permits. It only wakes
up to n tasks that are waiting on notified().await. Those tasks
will then compete for whatever permits are currently available. At most
one task can consume each available permit; the rest will wait for the
next notification.
Use this when you want to wake multiple waiters to race for a single resource (e.g. thundering herd mitigation).
Sourcepub async fn notified(&self)
pub async fn notified(&self)
Wait for a notification.
Each Notify value holds a number of permits. If a permit is available
from an earlier call to notify() or notify_n(), then
notified().await will complete immediately, consuming one permit.
Otherwise, notified().await waits for a permit to be made available.
This method is cancel safety.