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>
bb10
Thiago Macieira 2023-10-17 15:37:37 -07:00
parent ef76bd02a6
commit 0cd2158533
1 changed files with 6 additions and 4 deletions

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