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>
impl<D> Buildable for TimeDriver<D>
Source§fn build(this: RuntimeBuilder<Self>) -> Result<Runtime<TimeDriver<D>>>
fn build(this: RuntimeBuilder<Self>) -> Result<Runtime<TimeDriver<D>>>
Build the runtime