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
Change-Id: I79e700614d034281bf55fffd178f0611be96bfa7
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
(cherry picked from commit 0cd2158533e566170d22dc71df85918f82551124)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2023-10-17 15:37:37 -07:00 committed by Qt Cherry-pick Bot
parent 0bc6150289
commit c7bc337341

View File

@ -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);