AsyncEvent
Owns manual-reset signal state without a hidden runtime or application loop. Set publishes and wakes every pending wait; observations never consume signal. Reset affects future waits, never revokes committed notifications. Published readonly borrows can operate concurrently; moving owner authority needs external synchronization. Wait explicitly blocks; WaitAsync suspends on retained native registration. No fairness promise or automatic reset exists.
Module std.sync
import std.sync;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
AsyncEvent
pub final class AsyncEventOwns manual-reset signal state without a hidden runtime or application loop. Set publishes and wakes every pending wait; observations never consume signal. Reset affects future waits, never revokes committed notifications. Published readonly borrows can operate concurrently; moving owner authority needs external synchronization. Wait explicitly blocks; WaitAsync suspends on retained native registration. No fairness promise or automatic reset exists.
Example
module example.async_event;
from std.sync import AsyncEvent;
from std.task import Task, CancellationSource;
async fn Main() -> Task<i32> {
let Event = {
// Keep writable result storage through the explicit noncopy payload transfer.
var EventCreation = AsyncEvent.TryCreate();
switch EventCreation {
case .Ok { Value = Value: mut AsyncEvent& } => break move(Value);
case .Err => return 1;
}
};
let First = Event.WaitAsync(); let Second = Event.WaitAsync();
if Event.Set() is .Err { Error = _ } { panic; }
if Event.Reset() is .Err { Error = _ } { panic; }
// Complete both receiver borrows before any recoverable early exit.
let FirstResult = await First;
let SecondResult = await Second;
switch FirstResult { case .Ok => {} case .Err => return 2; }
switch SecondResult { case .Ok => {} case .Err => return 3; }
// Wait is deliberately blocking. Publish the manual-reset signal first,
// then prove the observation leaves it set before restoring the old state.
if Event.Set() is .Err { Error = _ } { panic; }
if Event.Wait() is .Err { Error = _ } { return 8; }
switch Event.TryWait() {
case .Ok { Value = Ready } => { if !Ready { return 9; } }
case .Err => return 10;
}
if Event.Reset() is .Err { Error = _ } { panic; }
switch Event.TryWait() { case .Ok { Value = Ready } => { if Ready { return 4; } } case .Err => return 5; }
Event.Close();
switch await Event.WaitAsync() {
case .Ok => return 6;
case .Err { Error = Error } => { if !(Error is .Closed) { return 7; } }
}
return 0;
}AsyncEvent.init
pub init()Creates empty ownership without allocating.
AsyncEvent.init
pub init(Source: readonly Self&) = delete;Rejects copying unique authority. Source: Existing owner.
AsyncEvent.init
pub init(Source: mut Self&&)Transfers authority and leaves Source inert. Source: Transferred owner.
AsyncEvent.operator
pub fn operator =(Source: readonly Self&) -> void = delete;Rejects copying unique authority. Source: Existing owner. Return: No runtime result; this assignment is deleted and cannot be invoked.
AsyncEvent.operator
pub fn operator =(Source: mut Self&&)Closes old ownership then transfers Source; exact self-move is unchanged. Source: Transferred owner left inert. Return: No value after closing and releasing displaced authority, then transferring Source; exact self-move is unchanged.
AsyncEvent.IsEmpty
pub IsEmpty: bool;Observes empty/moved authority; closed ownership is not empty.
AsyncEvent.TryCreate
pub static fn TryCreate(InitiallySignaled: bool) -> Result<Self, AsyncEventError>Allocates/initializes native manual-reset event ownership. InitiallySignaled: Initial persistent signal state, false by default. Return: Owned event or portable setup error.
AsyncEvent.TryCreate
pub static fn TryCreate() -> Result<Self, AsyncEventError>Supplies the omitted arguments to TryCreate. Note: Defaults: InitiallySignaled = false. Return: The full synchronous operation's owned success value or typed failure.
AsyncEvent.Wait
pub readonly fn Wait() -> Result<void, AsyncEventError>Blocks this thread until signal/close commits, without consuming signal. Return: Success or portable failure.
AsyncEvent.TryWait
pub readonly fn TryWait() -> Result<bool, AsyncEventError>Observes signal without waiting for it or consuming it. Return: True if signaled, false if unsignaled, otherwise failure.
AsyncEvent.WaitAsync
pub readonly async fn WaitAsync(Token: CancellationToken) -> Task<Result<void, AsyncEventError>>Waits without blocking a worker while pending. Token: Owned retained cancellation observation, inert by default. Return: Unique task preserving exact signal/cancel/close winner. Receiver backing remains borrowed until completion; no receiver access follows suspension. Set/Reset after native commit cannot revoke successful observation.
AsyncEvent.WaitAsync
pub readonly fn WaitAsync() -> Task<Result<void, AsyncEventError>>Supplies the omitted arguments to WaitAsync. Note: Defaults: Token = CancellationToken(). Return: Exact full-operation result; no additional async frame is created.
AsyncEvent.Set
pub readonly fn Set() -> Result<void, AsyncEventError>Publishes signal and commits every pending observation, idempotently. Return: Success or portable error.
AsyncEvent.Reset
pub readonly fn Reset() -> Result<void, AsyncEventError>Clears signal for future waits; committed observations remain successful. Return: Success or portable error.
AsyncEvent.Close
pub readonly fn Close() -> voidCloses admission and wakes pending waits; empty-inert and idempotent. Return: No value; never joins pending tasks.