Jump to a problem, path or page
Jump to a problem, path or page
01 · Frame the problem
Design a single-process timer scheduler for one-shot callbacks. Tasks can cancel or reschedule while the dispatcher waits. Callbacks may throw or schedule more timers.
Timer semantics depend on monotonic deadlines and a precise cancellation boundary. Analyse rescheduling, stale heap entries and callbacks that reenter the scheduler before choosing the dispatcher mechanism.
02 · Find the design pressure
Begin with a deadline heap and generation-tagged timer records. Removing arbitrary heap entries is avoidable if stale generations are rejected. Claim callbacks under lock and invoke them outside it, distinguishing cancellation before start from work already running.
03 · Define success
04 · Identify the correctness boundary
Heap entries carry identity and generation, allowing cancelled/rescheduled stale entries to be ignored. Claim callbacks under the lock but invoke them outside it so callbacks can safely schedule more timers. Cancellation distinguishes not-yet-started work from a callback already executing; it cannot rewind an external side effect.
05 · Make it concrete
Timer T is scheduled for 10, then rescheduled for 20. Its old heap entry at 10 is discarded by generation comparison. If cancellation races callback claiming, the contract determines which wins. A callback adding an immediate timer does not deadlock by reentering a held scheduler lock.