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.
setHwTimer(relativeTimeoutMs). If it is set at time t, it expires at time t + relativeTimeoutMs.handleHwTimer is called. After that the hardware timer stays idle until it is set again.currentTimeMs.currentTimeMs with timeoutMs expires at currentTimeMs + timeoutMs.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.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. TimerQueue()
int addTimer(int timerId, int currentTimeMs, int timeoutMs)
timerId, which expires at currentTimeMs + timeoutMs.timeoutMs if this call set the hardware timer, otherwise returns -1. List<Integer> handleHwTimer(int currentTimeMs)
currentTimeMs, then sets the hardware timer for the earliest remaining software timer, if there is one.timerId first. int getHwTimeLeft(int currentTimeMs)
-1 if the hardware timer is idle.1 ≤ timerId ≤ 1,000,000,000timerId values passed to addTimer are distinct.0 ≤ currentTimeMs ≤ 1,000,000,0001 ≤ timeoutMs ≤ 1,000,000,000currentTimeMs + timeoutMs ≤ 1,000,000,000 for every addTimer call.currentTimeMs never decreases from one call to the next.t, then handleHwTimer(currentTimeMs = t) is called before any other call with currentTimeMs ≥ t. handleHwTimer is never called at any other time.100,000 calls are made in total.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.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.