If we have two timers running in an application with the same timeout
and started almost at the same time by the code, they would trigger two
sleeps, the second of which very short (under a millisecond).
This causes us to match the Glib and Windows event loops, which round
all timers to millisecond anyway.
Change-Id: I7eb531e02dadf75925c01192b0f33ef3641ae1ea
Reviewed-by: Olivier Goffart <ogoffart@kde.org>
}
}
+static timeval roundToMillisecond(timeval val)
+{
+ // always round up
+ // worst case scenario is that the first trigger of a 1-ms timer is 0.999 ms late
+
+ int us = val.tv_usec % 1000;
+ val.tv_usec += 1000 - us;
+ if (val.tv_usec > 1000000) {
+ val.tv_usec -= 1000000;
+ ++val.tv_sec;
+ }
+ return val;
+}
+
/*
Returns the time to wait for the next timer, or null if no timers
are waiting.
if (currentTime < t->timeout) {
// time to wait
- tm = t->timeout - currentTime;
+ tm = roundToMillisecond(t->timeout - currentTime);
} else {
// no time to wait
tm.tv_sec = 0;