ae.c: Fix delay until next timer event.

This fix was written by Anthony LaTorre.
The old code mis-calculated the amount of time to wait till next event.
This commit is contained in:
antirez 2016-04-04 14:08:16 +02:00
parent ace780c002
commit b9feef9ae8

View File

@ -368,19 +368,22 @@ int aeProcessEvents(aeEventLoop *eventLoop, int flags)
if (shortest) { if (shortest) {
long now_sec, now_ms; long now_sec, now_ms;
/* Calculate the time missing for the nearest
* timer to fire. */
aeGetTime(&now_sec, &now_ms); aeGetTime(&now_sec, &now_ms);
tvp = &tv; tvp = &tv;
tvp->tv_sec = shortest->when_sec - now_sec;
if (shortest->when_ms < now_ms) { /* How many milliseconds we need to wait for the next
tvp->tv_usec = ((shortest->when_ms+1000) - now_ms)*1000; * time event to fire? */
tvp->tv_sec --; long long ms =
(shortest->when_sec - now_sec)*1000 +
shortest->when_ms - now_ms;
if (ms > 0) {
tvp->tv_sec = ms/1000;
tvp->tv_usec = (ms % 1000)*1000;
} else { } else {
tvp->tv_usec = (shortest->when_ms - now_ms)*1000; tvp->tv_sec = 0;
tvp->tv_usec = 0;
} }
if (tvp->tv_sec < 0) tvp->tv_sec = 0;
if (tvp->tv_usec < 0) tvp->tv_usec = 0;
} else { } else {
/* If we have to check for events but need to return /* If we have to check for events but need to return
* ASAP because of AE_DONT_WAIT we need to set the timeout * ASAP because of AE_DONT_WAIT we need to set the timeout