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

source

pub(crate) fn new() -> AtomicWaker

Create an AtomicWaker

source

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.

source

fn do_register<W>(&self, waker: W)where W: WakerRef,

source

pub(crate) fn wake(&self)

Wakes the task that last called register.

If register has not been called yet, then this does nothing.

source

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.

Trait Implementations§

source§

impl Debug for AtomicWaker

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for AtomicWaker

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl RefUnwindSafe for AtomicWaker

source§

impl Send for AtomicWaker

source§

impl Sync for AtomicWaker

source§

impl UnwindSafe for AtomicWaker

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.