Struct Notify
struct Notify { ... }
Notifies 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.
A Notify can be thought of as a Semaphore starting with 0 permits. The
notified().await method waits for a permit to become available, and
notify_one() sets a permit if there currently are no available
permits.
The synchronization details of Notify are similar to
thread::park and Thread::unpark from std. A Notify
value contains a single permit. notified().await waits for the permit to
be made available, consumes the permit, and resumes. notify_one() sets
the permit, waking a pending task if there is one.
If notify_one() is called before notified().await, then the next
call to notified().await will complete immediately, consuming the permit.
Any subsequent calls to notified().await will wait for a new permit.
If notify_one() is called multiple times before notified().await,
only a single permit is stored. The next call to notified().await will
complete immediately, but the one after will wait for a new permit.
Examples
Basic usage.
use Notify;
use Arc;
async
Unbound multi-producer single-consumer (mpsc) channel.
No wakeups can be lost when using this channel because the call to
notify_one() will store a permit in the Notify, which the following call
to notified() will consume.
use Notify;
use VecDeque;
use Mutex;
Unbound multi-producer multi-consumer (mpmc) channel.
The call to enable is important because otherwise if you have two
calls to recv and two calls to send in parallel, the following could
happen:
- Both calls to
try_recvreturnNone. - Both new elements are added to the vector.
- The
notify_onemethod is called twice, adding only a single permit to theNotify. - Both calls to
recvreach theNotifiedfuture. One of them consumes the permit, and the other sleeps forever.
By adding the Notified futures to the list by calling enable before
try_recv, the notify_one calls in step three would remove the
futures from the list and mark them notified instead of adding a permit
to the Notify. This ensures that both futures are woken.
Notice that this failure can only happen if there are two concurrent calls
to recv. This is why the mpsc example above does not require a call to
enable.
use Notify;
use VecDeque;
use Mutex;
Implementations
impl Notify
fn new() -> NotifyCreate a new
Notify, initialized without a permit.Examples
use Notify; let notify = new;const fn const_new() -> NotifyCreate a new
Notify, initialized without a permit.When using the
tracingunstable feature, aNotifycreated withconst_newwill not be instrumented. As such, it will not be visible intokio-console. Instead,Notify::newshould be used to create an instrumented object if that is needed.Examples
use Notify; static NOTIFY: Notify = const_new;fn notified(self: &Self) -> Notified<'_>Wait for a notification.
Equivalent to:
async fn notified(&self);Each
Notifyvalue holds a single permit. If a permit is available from an earlier call tonotify_one(), thennotified().awaitwill complete immediately, consuming that permit. Otherwise,notified().awaitwaits for a permit to be made available by the next call tonotify_one().The
Notifiedfuture is not guaranteed to receive wakeups from calls tonotify_one()if it has not yet been polled. See the documentation forNotified::enable()for more details.The
Notifiedfuture is guaranteed to receive wakeups fromnotify_waiters()as soon as it has been created, even if it has not yet been polled.Cancel safety
This method uses a queue to fairly distribute notifications in the order they were requested. Cancelling a call to
notifiedmakes you lose your place in the queue.Examples
use Notify; use Arc; asyncfn notify_one(self: &Self)Notifies the first waiting task.
If a task is currently waiting, that task is notified. Otherwise, a permit is stored in this
Notifyvalue and the next call tonotified().awaitwill complete immediately consuming the permit made available by this call tonotify_one().At most one permit may be stored by
Notify. Many sequential calls tonotify_onewill result in a single permit being stored. The next call tonotified().awaitwill complete immediately, but the one after that will wait.Examples
use Notify; use Arc; asyncfn notify_last(self: &Self)Notifies the last waiting task.
This function behaves similar to
notify_one. The only difference is that it wakes the most recently added waiter instead of the oldest waiter.Check the
notify_one()documentation for more info and examples.fn notify_waiters(self: &Self)Notifies all waiting tasks.
If a task is currently waiting, that task is notified. Unlike with
notify_one(), no permit is stored to be used by the next call tonotified().await. The purpose of this method is to notify all already registered waiters. Registering for notification is done by acquiring an instance of theNotifiedfuture via callingnotified().Examples
use Notify; use Arc; async
impl Debug for Notify
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Default for Notify
fn default() -> Notify
impl Freeze for Notify
impl RefUnwindSafe for Notify
impl Send for Notify
impl Sync for Notify
impl Unpin for Notify
impl UnsafeUnpin for Notify
impl UnwindSafe for Notify
impl<T> Any for Notify
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Notify
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Notify
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> From for Notify
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for Notify
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Notify
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Notify
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>