How many microseconds until the first timer should fire. * If there are no timers, -1 is returned. * * Note that's O(N) since time events are unsorted. * Possible optimizations (not needed by Redis so far, but...): * 1) Insert the event in order, so that the nearest is just the head. * Much better but still insertion or deletion of timers is O(N). * 2) Use a skiplist to have this operati
| 254 | * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)). |
| 255 | */ |
| 256 | static int64_t usUntilEarliestTimer(aeEventLoop *eventLoop) { |
| 257 | aeTimeEvent *te = eventLoop->timeEventHead; |
| 258 | if (te == NULL) return -1; |
| 259 | |
| 260 | aeTimeEvent *earliest = NULL; |
| 261 | while (te) { |
| 262 | if (!earliest || te->when < earliest->when) |
| 263 | earliest = te; |
| 264 | te = te->next; |
| 265 | } |
| 266 | |
| 267 | monotime now = getMonotonicUs(); |
| 268 | return (now >= earliest->when) ? 0 : earliest->when - now; |
| 269 | } |
| 270 | |
| 271 | /* Process time events */ |
| 272 | static int processTimeEvents(aeEventLoop *eventLoop) { |