From 0cd2158533e566170d22dc71df85918f82551124 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 17 Oct 2023 15:37:37 -0700 Subject: 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 --- src/corelib/kernel/qcore_unix.cpp | 10 ++++++---- 1 file 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(timespecToChrono(*ts)); + return int(ms.count()); } -#endif // defined in qpoll.cpp int qt_poll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout_ts); -- cgit v1.2.3