summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qtimerinfo_unix.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2022-02-10 20:06:07 -0800
committerThiago Macieira <thiago.macieira@intel.com>2022-02-11 13:02:35 -0800
commit47cf674477473a05390df4a218fd6b3867c6e923 (patch)
treeb751a30bf7ce841af16711a0e9291f1c80c699a2 /src/corelib/kernel/qtimerinfo_unix.cpp
parent407d076124d42a1f4911e139ffb2d77a117ef277 (diff)
QTimerInfo/Unix: Fix roundToMillisecond when it's already rounded
Don't add 1 ms when it's rounded to 1 ms. Found when running unit tests on QNX, where the clock did not update between two subsequent calls to clock_gettime(). All of this ought to be refactored to use std::chrono::nanoseconds. Fixes: QTBUG-100438 Pick-to: 6.2 6.3 Change-Id: I47dc6426c33d3a66dec946ae3589694745ed1835 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/kernel/qtimerinfo_unix.cpp')
-rw-r--r--src/corelib/kernel/qtimerinfo_unix.cpp11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/corelib/kernel/qtimerinfo_unix.cpp b/src/corelib/kernel/qtimerinfo_unix.cpp
index db4d4b1188..3b1696647b 100644
--- a/src/corelib/kernel/qtimerinfo_unix.cpp
+++ b/src/corelib/kernel/qtimerinfo_unix.cpp
@@ -197,15 +197,22 @@ inline timespec operator+(const timespec &t1, int ms)
return t2 += ms;
}
-static timespec roundToMillisecond(timespec val)
+static constexpr timespec roundToMillisecond(timespec val)
{
// always round up
// worst case scenario is that the first trigger of a 1-ms timer is 0.999 ms late
int ns = val.tv_nsec % (1000 * 1000);
- val.tv_nsec += 1000 * 1000 - ns;
+ if (ns)
+ val.tv_nsec += 1000 * 1000 - ns;
return normalizedTimespec(val);
}
+static_assert(roundToMillisecond({0, 0}) == timespec{0, 0});
+static_assert(roundToMillisecond({0, 1}) == timespec{0, 1'000'000});
+static_assert(roundToMillisecond({0, 999'999}) == timespec{0, 1'000'000});
+static_assert(roundToMillisecond({0, 1'000'000}) == timespec{0, 1'000'000});
+static_assert(roundToMillisecond({0, 999'999'999}) == timespec{1, 0});
+static_assert(roundToMillisecond({1, 0}) == timespec{1, 0});
#ifdef QTIMERINFO_DEBUG
QDebug operator<<(QDebug s, timeval tv)