pub(crate) struct Driver {
park: IoStack,
}
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.
Fields§
§park: IoStack
Parker to delegate to.
Implementations§
source§impl Driver
impl Driver
sourcepub(crate) fn new(park: IoStack, clock: &Clock, shards: u32) -> (Driver, Handle)
pub(crate) fn new(park: IoStack, clock: &Clock, shards: u32) -> (Driver, Handle)
Creates a new Driver
instance that uses park
to block the current
thread and time_source
to get the current time and convert to ticks.
Specifying the source of time is useful when testing.