diff options
author | antirez <antirez@gmail.com> | 2016-04-04 14:08:16 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2016-05-02 08:42:22 +0200 |
commit | 29645f1f44926ac2662ed768fe1e369775a30b66 (patch) | |
tree | 24e497cf94f20d0ac07271ff7f642986510bea99 | |
parent | aa79c1f1c1d288e4cc2c0f4689ad01488678bf9d (diff) | |
download | redis-29645f1f44926ac2662ed768fe1e369775a30b66.tar.gz |
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.
-rw-r--r-- | src/ae.c | 21 |
1 files changed, 12 insertions, 9 deletions
@@ -368,19 +368,22 @@ int aeProcessEvents(aeEventLoop *eventLoop, int flags) if (shortest) { long now_sec, now_ms; - /* Calculate the time missing for the nearest - * timer to fire. */ aeGetTime(&now_sec, &now_ms); tvp = &tv; - tvp->tv_sec = shortest->when_sec - now_sec; - if (shortest->when_ms < now_ms) { - tvp->tv_usec = ((shortest->when_ms+1000) - now_ms)*1000; - tvp->tv_sec --; + + /* How many milliseconds we need to wait for the next + * time event to fire? */ + 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 { - 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 { /* If we have to check for events but need to return * ASAP because of AE_DONT_WAIT we need to set the timeout |