pub unsafe trait Event: Sync {
fn with(f: impl FnOnce(Pin<&Self>));
fn wait(self: Pin<&Self>);
fn set(self: Pin<&Self>);
}
Expand description
An Event represents a single-producer, single-consumer notification synchronization primitive.
A thread creates an Event using Event::with
, makes it available to the producer, and uses
Event::wait
to block the caller thread until Event::set
is called from another thread
or from the same consumer thread prior to Event::wait
.
The same thread which call Event::with
will always be the same thread that waits.
Event::set
will also only be called by a single other “producer” thread, allowing for
implementations to rely on any SPSC-style optimizations. Events are Pinned in case the
implementation requires a stable address or utilizes intrusive memory.
Safety
Event::with
must block until Event::set
is called. Failure to uphold this invariant
could result in undefined behavior at safe use sites.
Required Methods
sourcefn with(f: impl FnOnce(Pin<&Self>))
fn with(f: impl FnOnce(Pin<&Self>))
Construct a pinned Event, allowing the closure to call wait
on the Event reference.
sourcefn wait(self: Pin<&Self>)
fn wait(self: Pin<&Self>)
Blocks the caller until the another thread (or previously on the same thread)
invokes Event::set
.
sourcefn set(self: Pin<&Self>)
fn set(self: Pin<&Self>)
Marks the thread as active and unblocks the waiting thread if any.
Further attempts to call Event::wait
should return immediately.