tokio_timer::timer

Struct Timer

Source
pub struct Timer<T, N = SystemNow> { /* private fields */ }
Expand description

Timer implementation that drives Delay, Interval, and Timeout.

A Timer instance tracks the state necessary for managing time and notifying the Delay instances once their deadlines are reached.

It is expected that a single Timer instance manages many individual Delay instances. The Timer implementation is thread-safe and, as such, is able to handle callers from across threads.

Callers do not use Timer directly to create Delay instances. Instead, Handle is used. A handle for the timer instance is obtained by calling handle. Handle is the type that implements Clone and is Send + Sync.

After creating the Timer instance, the caller must repeatedly call turn. The timer will perform no work unless turn is called repeatedly.

The Timer has a resolution of one millisecond. Any unit of time that falls between milliseconds are rounded up to the next millisecond.

When the Timer instance is dropped, any outstanding Delay instance that has not elapsed will be notified with an error. At this point, calling poll on the Delay instance will result in Err being returned.

§Implementation

Timer is based on the paper by Varghese and Lauck.

A hashed timing wheel is a vector of slots, where each slot handles a time slice. As time progresses, the timer walks over the slot for the current instant, and processes each entry for that slot. When the timer reaches the end of the wheel, it starts again at the beginning.

The Timer implementation maintains six wheels arranged in a set of levels. As the levels go up, the slots of the associated wheel represent larger intervals of time. At each level, the wheel has 64 slots. Each slot covers a range of time equal to the wheel at the lower level. At level zero, each slot represents one millisecond of time.

The wheels are:

  • Level 0: 64 x 1 millisecond slots.
  • Level 1: 64 x 64 millisecond slots.
  • Level 2: 64 x ~4 second slots.
  • Level 3: 64 x ~4 minute slots.
  • Level 4: 64 x ~4 hour slots.
  • Level 5: 64 x ~12 day slots.

When the timer processes entries at level zero, it will notify all the Delay instances as their deadlines have been reached. For all higher levels, all entries will be redistributed across the wheel at the next level down. Eventually, as time progresses, entries will Delay instances will either be canceled (dropped) or their associated entries will reach level zero and be notified.

Implementations§

Source§

impl<T> Timer<T>
where T: Park,

Source

pub fn new(park: T) -> Self

Create a new Timer instance that uses park to block the current thread.

Once the timer has been created, a handle can be obtained using handle. The handle is used to create Delay instances.

Use default when constructing a Timer using the default park instance.

Source§

impl<T, N> Timer<T, N>

Source

pub fn get_park(&self) -> &T

Returns a reference to the underlying Park instance.

Source

pub fn get_park_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying Park instance.

Source§

impl<T, N> Timer<T, N>
where T: Park, N: Now,

Source

pub fn new_with_now(park: T, now: N) -> Self

Create a new Timer instance that uses park to block the current thread and now to get the current Instant.

Specifying the source of time is useful when testing.

Source

pub fn handle(&self) -> Handle

Returns a handle to the timer.

The Handle is how Delay instances are created. The Delay instances can either be created directly or the Handle instance can be passed to with_default, setting the timer as the default timer for the execution context.

Source

pub fn turn(&mut self, max_wait: Option<Duration>) -> Result<Turn, T::Error>

Performs one iteration of the timer loop.

This function must be called repeatedly in order for the Timer instance to make progress. This is where the work happens.

The Timer will use the Park instance that was specified in new to block the current thread until the next Delay instance elapses. One call to turn results in at most one call to park.park().

§Return

On success, Ok(Turn) is returned, where Turn is a placeholder type that currently does nothing but may, in the future, have functions add to provide information about the call to turn.

If the call to park.park() fails, then Err is returned with the error.

Trait Implementations§

Source§

impl<T: Debug, N: Debug> Debug for Timer<T, N>

Source§

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

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

impl Default for Timer<ParkThread, SystemNow>

Source§

fn default() -> Self

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

impl<T, N> Drop for Timer<T, N>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, N> Park for Timer<T, N>
where T: Park, N: Now,

Source§

type Unpark = <T as Park>::Unpark

Unpark handle type for the Park implementation.
Source§

type Error = <T as Park>::Error

Error returned by park
Source§

fn unpark(&self) -> Self::Unpark

Get a new Unpark handle associated with this Park instance.
Source§

fn park(&mut self) -> Result<(), Self::Error>

Block the current thread unless or until the token is available. Read more
Source§

fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error>

Park the current thread for at most duration. Read more

Auto Trait Implementations§

§

impl<T, N> Freeze for Timer<T, N>
where T: Freeze, N: Freeze,

§

impl<T, N = Clock> !RefUnwindSafe for Timer<T, N>

§

impl<T, N> Send for Timer<T, N>
where T: Send, N: Send,

§

impl<T, N> Sync for Timer<T, N>
where T: Sync, N: Sync,

§

impl<T, N> Unpin for Timer<T, N>
where T: Unpin, N: Unpin,

§

impl<T, N = Clock> !UnwindSafe for Timer<T, N>

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.