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
| 540 | * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)). |
| 541 | */ |
| 542 | static int64_t usUntilEarliestTimer(aeEventLoop *eventLoop) { |
| 543 | aeTimeEvent *te = eventLoop->timeEventHead; |
| 544 | if (te == NULL) return -1; |
| 545 | |
| 546 | aeTimeEvent *earliest = NULL; |
| 547 | while (te) { |
| 548 | if (!earliest || te->when < earliest->when) |
| 549 | earliest = te; |
| 550 | te = te->next; |
| 551 | } |
| 552 | |
| 553 | monotime now = getMonotonicUs(); |
| 554 | return (now >= earliest->when) ? 0 : earliest->when - now; |
| 555 | } |
| 556 | |
| 557 | /* Process time events */ |
| 558 | static int processTimeEvents(aeEventLoop *eventLoop) { |