Struct tokio::sync::task::atomic_waker::AtomicWaker
source · pub(crate) struct AtomicWaker {
state: AtomicUsize,
waker: UnsafeCell<Option<Waker>>,
}
Expand description
A synchronization primitive for task waking.
AtomicWaker
will coordinate concurrent wakes with the consumer
potentially “waking” the underlying task. This is useful in scenarios
where a computation completes in another thread and wants to wake the
consumer, but the consumer is in the process of being migrated to a new
logical task.
Consumers should call register
before checking the result of a computation
and producers should call wake
after producing the computation (this
differs from the usual thread::park
pattern). It is also permitted for
wake
to be called before register
. This results in a no-op.
A single AtomicWaker
may be reused for any number of calls to register
or
wake
.
Fields§
§state: AtomicUsize
§waker: UnsafeCell<Option<Waker>>
Implementations§
source§impl AtomicWaker
impl AtomicWaker
sourcepub(crate) fn new() -> AtomicWaker
pub(crate) fn new() -> AtomicWaker
Create an AtomicWaker
sourcepub(crate) fn register_by_ref(&self, waker: &Waker)
pub(crate) fn register_by_ref(&self, waker: &Waker)
Registers the provided waker to be notified on calls to wake
.
The new waker will take place of any previous wakers that were registered
by previous calls to register
. Any calls to wake
that happen after
a call to register
(as defined by the memory ordering rules), will
wake the register
caller’s task.
It is safe to call register
with multiple other threads concurrently
calling wake
. This will result in the register
caller’s current
task being woken once.
This function is safe to call concurrently, but this is generally a bad
idea. Concurrent calls to register
will attempt to register different
tasks to be woken. One of the callers will win and have its task set,
but there is no guarantee as to which caller will succeed.
fn do_register<W>(&self, waker: W)where
W: WakerRef,
sourcepub(crate) fn wake(&self)
pub(crate) fn wake(&self)
Wakes the task that last called register
.
If register
has not been called yet, then this does nothing.
sourcepub(crate) fn take_waker(&self) -> Option<Waker>
pub(crate) fn take_waker(&self) -> Option<Waker>
Attempts to take the Waker
value out of the AtomicWaker
with the
intention that the caller will wake the task later.