diff --git a/src/corelib/kernel/qeventdispatcher_glib.cpp b/src/corelib/kernel/qeventdispatcher_glib.cpp index 85403bdabb3..58a9dc5e49e 100644 --- a/src/corelib/kernel/qeventdispatcher_glib.cpp +++ b/src/corelib/kernel/qeventdispatcher_glib.cpp @@ -110,10 +110,7 @@ static gboolean timerSourceCheckHelper(GTimerSource *src) || (src->processEventsFlags & QEventLoop::X11ExcludeTimers)) return false; - if (src->timerList.updateCurrentTime() < src->timerList.constFirst()->timeout) - return false; - - return true; + return !src->timerList.hasPendingTimers(); } static gboolean timerSourcePrepare(GSource *source, gint *timeout) diff --git a/src/corelib/kernel/qtimerinfo_unix.cpp b/src/corelib/kernel/qtimerinfo_unix.cpp index 20b34e51c80..165de171716 100644 --- a/src/corelib/kernel/qtimerinfo_unix.cpp +++ b/src/corelib/kernel/qtimerinfo_unix.cpp @@ -33,6 +33,19 @@ steady_clock::time_point QTimerInfoList::updateCurrentTime() return currentTime; } +/*! \internal + Updates the currentTime member to the current time, and returns \c true if + the first timer's timeout is in the future (after currentTime). + + The list is sorted by timeout, thus it's enough to check the first timer only. +*/ +bool QTimerInfoList::hasPendingTimers() +{ + if (isEmpty()) + return false; + return updateCurrentTime() < constFirst()->timeout; +} + /* insert timer info into list */ diff --git a/src/corelib/kernel/qtimerinfo_unix_p.h b/src/corelib/kernel/qtimerinfo_unix_p.h index ecea6006d38..7c23f93c271 100644 --- a/src/corelib/kernel/qtimerinfo_unix_p.h +++ b/src/corelib/kernel/qtimerinfo_unix_p.h @@ -43,7 +43,6 @@ public: QTimerInfoList(); std::chrono::steady_clock::time_point currentTime; - std::chrono::steady_clock::time_point updateCurrentTime(); bool timerWait(timespec &); void timerInsert(QTimerInfo *); @@ -59,12 +58,16 @@ public: QList registeredTimers(QObject *object) const; int activateTimers(); + bool hasPendingTimers(); QList::const_iterator findTimerById(int timerId) const { auto matchesId = [timerId](const QTimerInfo *t) { return t->id == timerId; }; return std::find_if(cbegin(), cend(), matchesId); } + +private: + std::chrono::steady_clock::time_point updateCurrentTime(); }; QT_END_NAMESPACE