494. Design Software Timer Queue on a Single Hardware Timer
Asked in
Software Timer Queue on a Single Hardware Timer

An embedded device runs a simple operating system that has only one hardware timer. Build a timer queue that runs many software timers on top of this single hardware timer.

You do not call real hardware. Your class only keeps track of the hardware timer's state.

Hardware Timer

  • The hardware timer is set with setHwTimer(relativeTimeoutMs). If it is set at time t, it expires at time t + relativeTimeoutMs.
  • At most one hardware timer can be pending. Setting it again before it expires replaces the pending one with the new relative value, and the old one never fires.
  • When the pending hardware timer expires, the interrupt callback handleHwTimer is called. After that the hardware timer stays idle until it is set again.

Timer Queue Rules

  • All times are in milliseconds. Every call receives the current time currentTimeMs.
  • A software timer added at currentTimeMs with timeoutMs expires at currentTimeMs + timeoutMs.
  • When a software timer is added, set the hardware timer with setHwTimer(timeoutMs) only if the hardware timer is idle or the new timer expires strictly earlier than the pending hardware timer. Otherwise, leave the hardware timer unchanged.
  • When the hardware timer expires, fire and remove every software timer that expires at or before currentTimeMs. Then, if software timers are left, set the hardware timer with setHwTimer(earliestExpiryTime - currentTimeMs), where earliestExpiryTime is the earliest expiry time among the remaining timers.

Methods

Constructor

TimerQueue()
  • Creates an empty timer queue. The hardware timer starts idle.

Add a Software Timer

int addTimer(int timerId, int currentTimeMs, int timeoutMs)
  • Adds software timer timerId, which expires at currentTimeMs + timeoutMs.
  • Updates the hardware timer using the rules above.
  • Returns timeoutMs if this call set the hardware timer, otherwise returns -1.

Hardware Timer Interrupt

List<Integer> handleHwTimer(int currentTimeMs)
  • Called when the pending hardware timer expires.
  • Fires and removes every software timer that expires at or before currentTimeMs, then sets the hardware timer for the earliest remaining software timer, if there is one.
  • Returns the ids of the fired timers sorted by expiry time. Timers with equal expiry times are sorted by smaller timerId first.

Hardware Timer Time Left

int getHwTimeLeft(int currentTimeMs)
  • Returns the milliseconds left before the pending hardware timer expires, or -1 if the hardware timer is idle.

Constraints

  • 1 ≤ timerId ≤ 1,000,000,000
  • All timerId values passed to addTimer are distinct.
  • 0 ≤ currentTimeMs ≤ 1,000,000,000
  • 1 ≤ timeoutMs ≤ 1,000,000,000
  • currentTimeMs + timeoutMs ≤ 1,000,000,000 for every addTimer call.
  • currentTimeMs never decreases from one call to the next.
  • If the pending hardware timer expires at time t, then handleHwTimer(currentTimeMs = t) is called before any other call with currentTimeMs ≥ t. handleHwTimer is never called at any other time.
  • At most 100,000 calls are made in total.

Examples

Example 1

  • TimerQueue timerQueue = new TimerQueue();
  • timerQueue.addTimer(timerId = 7, currentTimeMs = 0, timeoutMs = 50) returns 50. The hardware timer was idle, so it is set and expires at 50.
  • timerQueue.addTimer(timerId = 3, currentTimeMs = 10, timeoutMs = 60) returns -1. Timer 3 expires at 70, which is not earlier than 50.
  • timerQueue.addTimer(timerId = 9, currentTimeMs = 20, timeoutMs = 10) returns 10. Timer 9 expires at 30, earlier than 50, so the hardware timer is reset to expire at 30.
  • timerQueue.getHwTimeLeft(currentTimeMs = 25) returns 5.
  • timerQueue.handleHwTimer(currentTimeMs = 30) returns [9]. Timers 7 (at 50) and 3 (at 70) are left, so the hardware timer is set with 20 and expires at 50.
  • timerQueue.getHwTimeLeft(currentTimeMs = 40) returns 10.
  • timerQueue.handleHwTimer(currentTimeMs = 50) returns [7]. The hardware timer is set with 20 and expires at 70.
  • timerQueue.handleHwTimer(currentTimeMs = 70) returns [3]. No timers are left, so the hardware timer is idle.
  • timerQueue.getHwTimeLeft(currentTimeMs = 80) returns -1.

Example 2

  • TimerQueue timerQueue = new TimerQueue();
  • timerQueue.addTimer(timerId = 12, currentTimeMs = 100, timeoutMs = 40) returns 40. The hardware timer expires at 140.
  • timerQueue.addTimer(timerId = 5, currentTimeMs = 110, timeoutMs = 30) returns -1. Timer 5 also expires at 140, which is not strictly earlier.
  • timerQueue.addTimer(timerId = 8, currentTimeMs = 120, timeoutMs = 50) returns -1. Timer 8 expires at 170.
  • timerQueue.handleHwTimer(currentTimeMs = 140) returns [5, 12]. Both expire at 140, so the smaller id comes first. The hardware timer is set with 30 and expires at 170.
  • timerQueue.addTimer(timerId = 2, currentTimeMs = 150, timeoutMs = 20) returns -1. Timer 2 expires at 170, the same as the hardware timer.
  • timerQueue.getHwTimeLeft(currentTimeMs = 160) returns 10.
  • timerQueue.handleHwTimer(currentTimeMs = 170) returns [2, 8]. No timers are left, so the hardware timer is idle.
  • timerQueue.addTimer(timerId = 4, currentTimeMs = 175, timeoutMs = 5) returns 5. The hardware timer was idle, so it is set and expires at 180.
  • timerQueue.getHwTimeLeft(currentTimeMs = 178) returns 2.


Please use Laptop/Desktop or any other large screen to add/edit code.