summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Stone <daniel@fooishbar.org>2006-10-25 23:55:43 +0300
committerDaniel Stone <daniels@endtroducing.fooishbar.org>2006-10-26 00:25:38 +0300
commitdfac36e56a4c1dbccb7b302738dbf60985cc12ba (patch)
treed756b835aac2d6682ead88fbcdf999db97b616e8
parentd04e2545a7238133692aa4501d24e5fdae30df08 (diff)
WaitForSomething: allow time to rewind
If time rewinds dramatically, reset all the timers to fix their expiry. (cherry picked from d3e57faffee63df1424a209d0418d3a712f91ae6 commit)
-rw-r--r--os/WaitFor.c34
1 files changed, 31 insertions, 3 deletions
diff --git a/os/WaitFor.c b/os/WaitFor.c
index 045767809..896fdf15d 100644
--- a/os/WaitFor.c
+++ b/os/WaitFor.c
@@ -119,11 +119,13 @@ mffs(fd_mask mask)
struct _OsTimerRec {
OsTimerPtr next;
CARD32 expires;
+ CARD32 delta;
OsTimerCallback callback;
pointer arg;
};
static void DoTimer(OsTimerPtr timer, CARD32 now, OsTimerPtr *prev);
+static void CheckAllTimers(CARD32 now);
static OsTimerPtr timers = NULL;
/*****************
@@ -200,6 +202,11 @@ WaitForSomething(int *pClientsReady)
{
now = GetTimeInMillis();
timeout = timers->expires - now;
+ /* time has rewound. reset the timers. */
+ if (timeout > timers->delta) {
+ CheckAllTimers(now);
+ timeout = timers->expires - now;
+ }
if (timeout < 0)
timeout = 0;
waittime.tv_sec = timeout / MILLI_PER_SECOND;
@@ -426,6 +433,20 @@ ANYSET(FdMask *src)
}
#endif
+/* If time has rewound, re-run every affected timer.
+ * TimerForce will change timer->next, but it will _generally_ only
+ * promote timers in the list, meaning that we should still be
+ * walking every timer. */
+static void
+CheckAllTimers(CARD32 now)
+{
+ OsTimerPtr timer;
+
+ for (timer = timers; timer; timer = timer->next) {
+ if (timer->expires - now > timer->delta)
+ TimerForce(timer);
+ }
+}
static void
DoTimer(OsTimerPtr timer, CARD32 now, OsTimerPtr *prev)
@@ -467,8 +488,13 @@ TimerSet(OsTimerPtr timer, int flags, CARD32 millis,
}
if (!millis)
return timer;
- if (!(flags & TimerAbsolute))
+ if (flags & TimerAbsolute) {
+ timer->delta = millis - now;
+ }
+ else {
+ timer->delta = millis;
millis += now;
+ }
timer->expires = millis;
timer->callback = func;
timer->arg = arg;
@@ -481,8 +507,10 @@ TimerSet(OsTimerPtr timer, int flags, CARD32 millis,
}
for (prev = &timers;
*prev && (int) ((*prev)->expires - millis) <= 0;
- prev = &(*prev)->next)
- ;
+ prev = &(*prev)->next) {
+ if ((*prev)->expires - now > (*prev)->delta)
+ CheckAllTimers(now);
+ }
timer->next = *prev;
*prev = timer;
return timer;