Expand description
Injected clock and timer queue.
A TimerQueue holds pending one-shot and periodic timers; the event loop
asks it which timers are due and how long until the next one. Time comes
from an injected Clock: SystemClock in production, ManualClock
in tests. Instant::now() is only called inside SystemClock — all
timer logic operates on the opaque u64 millisecond values the clock
yields, so tests can advance time deterministically without sleeps.
Two structural choices differ from the original:
-
No re-entrancy dance.
TimerQueue::collect_expiredgathers due ids into aVecand returns them; the caller dispatches afterwards, so there is no re-entrant mutation of the queue to guard against. Two invariants are preserved: a singlenow_msvalue is used for the whole pass, and a periodic timer fires at most once percollect_expiredcall (even if overdue by several periods — it reschedules forward pastnow_msviacalc_next_expires_at). -
The clock lives in the event loop, not the queue.
now_msis passed intoTimerQueue::collect_expired,TimerQueue::time_until_next, andTimerQueue::set_timerby the caller, which keeps the queue easy to test and givesset_timerthe absolute expiry it needs for the reschedule grid arithmetic.
Guide: The event loop in depth.
§Turbo Vision heritage
Ports TTimerQueue (ttimerqu.cpp, system.h) and its time source
THardwareInfo::getTickCountMs. The original marked each timer with a
per-invocation collect-id so a callback could mutate the list mid-iteration,
and stored the clock inside the queue; tvision-rs replaces the callback with a
returned id list and injects the clock from the event loop (deviations D9 and
D11).
Structs§
- Manual
Clock - Test clock. Holds the current time in a
Cellso&selfcan both read the time (via theClocktrait) and advance it (viaset/advance). - System
Clock - Production clock. Captures an
Instantat construction and returns elapsed ms on each call. - TimerId
- A handle to a pending timer. Monotonically-increasing id allocated by
TimerQueue;Copy + Eq + Hashso callers can store it and compare it in event handlers. - Timer
Queue - A queue of pending one-shot and periodic timers.
Traits§
- Clock
- Monotonic millisecond clock.
Functions§
- calc_
next_ expires_ at - Catch-up-aware reschedule for periodic timers.