Skip to main content

Notify

Struct Notify 

Source
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.

Source

pub const fn new() -> Self

Create a Notify

Source

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;
})
Source

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).

Source

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).

Source

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.

Trait Implementations§

Source§

impl<T: Deref<Target = Notify>> AsRef<Notify> for NotifyStream<T>

Source§

fn as_ref(&self) -> &Notify

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Notify

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Notify

Source§

fn default() -> Notify

Returns the “default value” for a type. Read more

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 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.