diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2023-10-17 15:37:37 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2023-10-20 17:18:37 -0700 |
commit | 0cd2158533e566170d22dc71df85918f82551124 (patch) | |
tree | 84c9b901cc98a6bad82143d9e29e00ea02bea09e | |
parent | ef76bd02a63953f5d9158265ca520d8b2c5dc8c6 (diff) |
qt_safe_poll: round up when converting to milliseconds for poll(2)
When running on operating systems that don't offer modern APIs, we need
to round up to ensure we don't under-sleep and wake up before the timers
actually expire. In most cases, this is not a big deal because we'd go
right back to sleep and then wake up again.
The problem is that this new sleep would be for a duration of 0
milliseconds, so we'd end up busy-waiting until the timers do expire.
It's for less than 1 ms, but it's still power-consuming.
It's also perceptible when someone uses processEvents(), because we
could end up saying we didn't process anything, despite waiting for more
events. Since that violates the guiding rule for processEvents() (don't
ever use it), I don't consider this a big deal.
Fixes: QTBUG-118199
Pick-to: 6.5 6.6
Change-Id: I79e700614d034281bf55fffd178f0611be96bfa7
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
-rw-r--r-- | src/corelib/kernel/qcore_unix.cpp | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/corelib/kernel/qcore_unix.cpp b/src/corelib/kernel/qcore_unix.cpp index 78d99a382d..b4bdad1ebb 100644 --- a/src/corelib/kernel/qcore_unix.cpp +++ b/src/corelib/kernel/qcore_unix.cpp @@ -107,13 +107,15 @@ static inline bool time_update(struct timespec *tv, const struct timespec &start return tv->tv_sec >= 0; } -#if QT_CONFIG(poll_poll) +[[maybe_unused]] static inline int timespecToMillisecs(const struct timespec *ts) { - return (ts == NULL) ? -1 : - (ts->tv_sec * 1000) + (ts->tv_nsec / 1000000); + using namespace std::chrono; + if (!ts) + return -1; + auto ms = ceil<milliseconds>(timespecToChrono<nanoseconds>(*ts)); + return int(ms.count()); } -#endif // defined in qpoll.cpp int qt_poll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts); |