pub struct Notify { /* private fields */ }Expand description
A notify primitive for signaling events.
Notify provides a mechanism for tasks to wait for events and for
other tasks to signal those events. It is similar to a condition
variable but designed for async/await.
§Example
let notify = Notify::new();
// Spawn a task that waits for notification
let fut = async {
notify.notified().await;
println!("notified!");
};
// Later, signal the waiter
notify.notify_one();Implementations§
Source§impl Notify
impl Notify
Sourcepub fn notified(&self) -> Notified<'_> ⓘ
pub fn notified(&self) -> Notified<'_> ⓘ
Returns a future that completes when this Notify is notified.
The returned future is cancel-safe: if dropped before completion, the waiter is cleanly removed.
§Example
use asupersync::sync::Notify;
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
let notify = Arc::new(Notify::new());
let ready = Arc::new(AtomicBool::new(false));
let signaler = {
let notify = Arc::clone(¬ify);
let ready = Arc::clone(&ready);
std::thread::spawn(move || {
ready.store(true, Ordering::Release);
notify.notify_one();
})
};
notify.notified().await;
assert!(ready.load(Ordering::Acquire));
signaler.join().expect("signaler thread panicked");Sourcepub async fn wait_until<F>(&self, predicate: F)
pub async fn wait_until<F>(&self, predicate: F)
Waits until predicate returns true, re-checking it after every wake.
The predicate is evaluated before parking and again after each
notification, so callers can pair a state transition with
notify_one() / notify_waiters() without a separate check-then-park
race window.
§Example
use asupersync::sync::Notify;
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
let notify = Arc::new(Notify::new());
let ready = Arc::new(AtomicBool::new(false));
let signaler = {
let notify = Arc::clone(¬ify);
let ready = Arc::clone(&ready);
std::thread::spawn(move || {
ready.store(true, Ordering::Release);
notify.notify_one();
})
};
notify
.wait_until(|| ready.load(Ordering::Acquire))
.await;
assert!(ready.load(Ordering::Acquire));
signaler.join().expect("signaler thread panicked");Sourcepub fn notify_one(&self) -> bool
pub fn notify_one(&self) -> bool
Notifies one waiting task.
If no task is currently waiting, the notification is stored and
will be delivered to the next task that calls notified().await.
If multiple tasks are waiting, exactly one will be woken.
Returns true when an active waiter was selected and woken, or
false when no waiter was available and the notification was stored.
Sourcepub fn notify_waiters(&self)
pub fn notify_waiters(&self)
Notifies all waiting tasks.
This wakes all tasks that are currently waiting. Tasks that start waiting after this call will not be affected.
Sourcepub fn waiter_count(&self) -> usize
pub fn waiter_count(&self) -> usize
Returns the number of tasks currently waiting.
Trait Implementations§
Auto Trait Implementations§
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
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more