Struct TimeDriver

Source
pub struct TimeDriver<D: 'static> { /* private fields */ }
Expand description

Time implementation that drives Sleep, Interval, and Timeout.

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

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

After creating the Driver instance, the caller must repeatedly call park or park_timeout. The time driver will perform no work unless park or park_timeout is called repeatedly.

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

When an instance is dropped, any outstanding Sleep instance that has not elapsed will be notified with an error. At this point, calling poll on the Sleep instance will result in panic.

§Implementation

The time driver 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 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 Sleep 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 with Sleep instances will either be canceled (dropped) or their associated entries will reach level zero and be notified.

Trait Implementations§

Source§

impl<D> Buildable for TimeDriver<D>
where D: Buildable + Driver,

Source§

fn build(this: RuntimeBuilder<Self>) -> Result<Runtime<TimeDriver<D>>>

Build the runtime

Source§

impl<D: Debug + 'static> Debug for TimeDriver<D>

Source§

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

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

impl<D> Driver for TimeDriver<D>
where D: Driver + 'static,

Source§

type Unpark = <D as Driver>::Unpark

The struct to wake thread from another.
Source§

fn with<R>(&self, f: impl FnOnce() -> R) -> R

Run with driver TLS.
Source§

fn submit(&self) -> Result<()>

Submit ops to kernel and process returned events.
Source§

fn park(&self) -> Result<()>

Wait infinitely and process returned events.
Source§

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

Wait with timeout and process returned events.
Source§

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

Get Unpark.
Source§

impl<D> Drop for TimeDriver<D>
where D: 'static,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<D> Freeze for TimeDriver<D>
where D: Freeze,

§

impl<D> !RefUnwindSafe for TimeDriver<D>

§

impl<D> !Send for TimeDriver<D>

§

impl<D> !Sync for TimeDriver<D>

§

impl<D> Unpin for TimeDriver<D>
where D: Unpin,

§

impl<D> !UnwindSafe for TimeDriver<D>

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.