Skip to main content

Notify

Struct Notify 

Source
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(&notify);
    let woke = Rc::clone(&woke);
    async move {
        notify.notified().await;
        woke.set(true);
    }
});

runite::spawn({
    let notify = Rc::clone(&notify);
    async move {
        runite::yield_now().await;
        notify.notify_one();
    }
});

runite::run();

assert!(woke.get());

Implementations§

Source§

impl Notify

Source

pub fn new() -> Self

Creates a notification primitive with no stored permit.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Default for Notify

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl !Freeze for Notify

§

impl !RefUnwindSafe for Notify

§

impl !Send for Notify

§

impl !Sync for Notify

§

impl !UnwindSafe for Notify

§

impl Unpin for Notify

§

impl UnsafeUnpin for Notify

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more