pub struct ManualResetEvent { /* private fields */ }event only.Expand description
A reusable event that remains set until explicitly reset.
See the module-level documentation for its waiting semantics.
§Synchronization
An unset-to-set transition synchronizes with the waits it releases and with waits first polled
while the event remains set. Memory operations sequenced before set are therefore
visible after those waits complete.
A set call that finds the event already set does not establish this guarantee.
is_set is only a snapshot and cannot replace a wait or support check-then-act.
Implementations§
Source§impl ManualResetEvent
impl ManualResetEvent
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates an unset event.
§Examples
use asyncband::event::ManualResetEvent;
let event = ManualResetEvent::new();
assert!(!event.is_set());Sourcepub const fn with_state(is_set: bool) -> Self
pub const fn with_state(is_set: bool) -> Self
Creates an event with the specified initial state.
§Examples
use asyncband::event::ManualResetEvent;
let ready = ManualResetEvent::with_state(true);
assert!(ready.is_set());Sourcepub fn is_set(&self) -> bool
pub fn is_set(&self) -> bool
Returns whether the event is currently set.
This is a snapshot only; it does not reserve or consume the set state.
§Examples
use asyncband::event::ManualResetEvent;
let event = ManualResetEvent::new();
assert!(!event.is_set());
event.set();
assert!(event.is_set());
event.reset();
assert!(!event.is_set());Sourcepub fn set(&self)
pub fn set(&self)
Sets the event and releases every currently registered wait.
The event remains set until reset is called. Calling set while it is
already set has no effect. No ordering is guaranteed among the released waits.
§Panics
Panics if a registered waker panics. The state transition remains committed, and every remaining registered waker is still notified before the first panic resumes.
§Examples
use asyncband::event::ManualResetEvent;
let event = ManualResetEvent::new();
event.set();
// The event stays ready, so every later wait completes without blocking.
event.wait().await;
event.wait().await;Sourcepub fn reset(&self)
pub fn reset(&self)
Resets the event so new waits block until another set.
Waiters already committed by a preceding set remain ready. Calling reset while the
event is already unset has no effect.
§Examples
use asyncband::event::ManualResetEvent;
let event = ManualResetEvent::with_state(true);
event.wait().await;
// Subsequent waits block again until the next `set`.
event.reset();
assert!(!event.is_set());
event.set();
event.wait().await;Sourcepub async fn wait(&self)
pub async fn wait(&self)
Waits until the event is set.
If the event is already set, the wait completes immediately. Once a set
commits a registered wait, a later reset cannot make that wait pending
again.
§Cancel safety
Dropping a pending wait unregisters only that call; it does not change the event or affect
other waiters. If cancellation races with set, either cancellation unregisters first or
set commits the wait first. A waker already detached by set may still run after the wait
is dropped.
§Examples
use asyncband::event::ManualResetEvent;
let event = ManualResetEvent::new();
let waiter = async {
event.wait().await;
"released"
};
let setter = async { event.set() };
let (released, ()) = tokio::join!(waiter, setter);
assert_eq!(released, "released");Sourcepub async fn wait_owned(self: Arc<Self>)
pub async fn wait_owned(self: Arc<Self>)
Waits until the event is set without borrowing it.
The event must be held in an Arc. The returned future owns that Arc, which makes it
suitable for spawned tasks. Its waiting and cancellation semantics match
wait.
§Examples
use std::sync::Arc;
use asyncband::event::ManualResetEvent;
let event = Arc::new(ManualResetEvent::new());
let waiter = tokio::spawn(event.clone().wait_owned());
event.set();
waiter.await.unwrap();