Jump to a problem, path or page
Jump to a problem, path or page
01 · Develop the design
Use a min-heap of monotonic deadlines and generation-tagged timer records. A dispatcher condition loop claims due callbacks under lock and invokes them outside the lock with explicit cancellation semantics.
Stage 1
Separate record and heap entry
A timer record has ID, current generation and lifecycle. Heap entries carry deadline, tie-break sequence and generation.
Stage 2
Pick a cancellation boundary
In this exercise, successful cancel applies before the dispatcher claims the callback. After claim it reports TooLate rather than promising nonexecution.
Stage 3
Recheck after wakeup
Use a predicate loop against the monotonic clock. Spurious wakeups or canceled head entries do not mean a timer is due.
02 · Work through the method
03 · Justify the choices
What if a new earlier timer is inserted?
Wake the dispatcher so it recomputes the next wait
The current sleep target is invalid when a nearer deadline becomes active.
How do stale heap entries from reschedule behave?
Skip entries whose generation no longer matches the active record
Generation guards separate logical timer identity from obsolete heap entries.
Where should callback invocation occur?
Outside the scheduler mutex with isolated error handling
User code can block or reschedule; holding the state lock risks deadlock and stalls other callers.
04 · Plan the evidence