Struct tokio::runtime::time::entry::StateCell

source ·
pub(super) struct StateCell {
    state: AtomicU64,
    result: UnsafeCell<Result<(), Error>>,
    waker: AtomicWaker,
}
Expand description

This structure holds the current shared state of the timer - its scheduled time (if registered), or otherwise the result of the timer completing, as well as the registered waker.

Generally, the StateCell is only permitted to be accessed from two contexts: Either a thread holding the corresponding &mut TimerEntry, or a thread holding the timer driver lock. The write actions on the StateCell amount to passing “ownership” of the StateCell between these contexts; moving a timer from the TimerEntry to the driver requires both holding the &mut TimerEntry and the driver lock, while moving it back (firing the timer) requires only the driver lock.

Fields§

§state: AtomicU64

Holds either the scheduled expiration time for this timer, or (if the timer has been fired and is unregistered), u64::MAX.

§result: UnsafeCell<Result<(), Error>>

If the timer is fired (an Acquire order read on state shows u64::MAX), holds the result that should be returned from polling the timer. Otherwise, the contents are unspecified and reading without holding the driver lock is undefined behavior.

§waker: AtomicWaker

The currently-registered waker

Implementations§

source§

impl StateCell

source

fn new() -> Self

source

fn is_pending(&self) -> bool

source

fn when(&self) -> Option<u64>

Returns the current expiration time, or None if not currently scheduled.

source

fn poll(&self, waker: &Waker) -> Poll<Result<(), Error>>

If the timer is completed, returns the result of the timer. Otherwise, returns None and registers the waker.

source

fn read_state(&self) -> Poll<Result<(), Error>>

source

unsafe fn mark_pending(&self, not_after: u64) -> Result<(), u64>

Marks this timer as being moved to the pending list, if its scheduled time is not after not_after.

If the timer is scheduled for a time after not_after, returns an Err containing the current scheduled time.

SAFETY: Must hold the driver lock.

source

unsafe fn fire(&self, result: Result<(), Error>) -> Option<Waker>

Fires the timer, setting the result to the provided result.

Returns:

  • Some(waker) - if fired and a waker needs to be invoked once the driver lock is released
  • None - if fired and a waker does not need to be invoked, or if already fired

SAFETY: The driver lock must be held.

source

fn set_expiration(&self, timestamp: u64)

Marks the timer as registered (poll will return None) and sets the expiration time.

While this function is memory-safe, it should only be called from a context holding both &mut TimerEntry and the driver lock.

source

fn extend_expiration(&self, new_timestamp: u64) -> Result<(), ()>

Attempts to adjust the timer to a new timestamp.

If the timer has already been fired, is pending firing, or the new timestamp is earlier than the old timestamp, (or occasionally spuriously) returns Err without changing the timer’s state. In this case, the timer must be deregistered and re-registered.

source

pub(super) fn might_be_registered(&self) -> bool

Returns true if the state of this timer indicates that the timer might be registered with the driver. This check is performed with relaxed ordering, but is conservative - if it returns false, the timer is definitely not registered.

Trait Implementations§

source§

impl Debug for StateCell

source§

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

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

impl Default for StateCell

source§

fn default() -> Self

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

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

source§

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 T
where U: TryFrom<T>,

source§

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.