summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2023-02-22 09:19:59 -0800
committerThiago Macieira <thiago.macieira@intel.com>2023-04-01 07:49:40 -1000
commit15d6244ec35c5bbdc5ed76eb787f68932f78eed6 (patch)
treeec84ee487b885967f8bdf8683ad30d31d507e19f
parentb5fe88a6e5251251d0f65b2f9cb5b28c01cc3b63 (diff)
QElapsedTimer: remove the runtime check for the monotonic clock
There is no supported OS with a "maybe" monotonic clock. All of the OSes we currently support either always have a monotonic clock in the first place or never have one. There's only one OS in the latter category and that's INTEGRITY, lacking even the CLOCK_MONOTONIC constant. Change-Id: Ieec322d73c1e40ad95c8fffd17463531dae2c5bc Reviewed-by: Ahmad Samir <a.samirh78@gmail.com> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io>
-rw-r--r--src/corelib/kernel/qelapsedtimer_unix.cpp65
-rw-r--r--src/corelib/thread/qwaitcondition_unix.cpp8
2 files changed, 12 insertions, 61 deletions
diff --git a/src/corelib/kernel/qelapsedtimer_unix.cpp b/src/corelib/kernel/qelapsedtimer_unix.cpp
index f11ebc00a4..d7a81babb8 100644
--- a/src/corelib/kernel/qelapsedtimer_unix.cpp
+++ b/src/corelib/kernel/qelapsedtimer_unix.cpp
@@ -39,13 +39,9 @@ QT_BEGIN_NAMESPACE
* 0 monotonic clock might be supported, runtime check is needed
* >1 (such as 200809L) monotonic clock is always supported
*
- * The unixCheckClockType() function will return the clock to use: either
- * CLOCK_MONOTONIC or CLOCK_REALTIME. In the case the POSIX option has a value
- * of zero, then this function stores a static that contains the clock to be
- * used.
- *
- * There's one extra case, which is when CLOCK_REALTIME isn't defined. When
- * that's the case, we'll emulate the clock_gettime function with gettimeofday.
+ * Since Qt 6.6, we no longer perform any runtime checks and instead enforce
+ * the use of the monotonic clock in all OSes that have the CLOCK_MONOTONIC
+ * macro and use of the POSIX realtime clock functions.
*
* Conforming to:
* POSIX.1b-1993 section "Clocks and Timers"
@@ -54,59 +50,14 @@ QT_BEGIN_NAMESPACE
* see http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html
*/
-#if !defined(CLOCK_REALTIME)
-# define CLOCK_REALTIME 0
-static inline void qt_clock_gettime(int, struct timespec *ts)
-{
- // support clock_gettime with gettimeofday
- struct timeval tv;
- gettimeofday(&tv, 0);
- ts->tv_sec = tv.tv_sec;
- ts->tv_nsec = tv.tv_usec * 1000;
-}
-
-static inline int regularClock()
-{
- return 0;
-}
-#else
-static inline void qt_clock_gettime(clockid_t clock, struct timespec *ts)
-{
- clock_gettime(clock, ts);
-}
-
-static inline clock_t regularClockCheck()
+static constexpr clockid_t regularClock()
{
- struct timespec regular_clock_resolution;
- int r = -1;
-
-# ifdef CLOCK_MONOTONIC
- // try the monotonic clock
- r = clock_getres(CLOCK_MONOTONIC, &regular_clock_resolution);
-
-# ifdef Q_OS_LINUX
- // Despite glibc claiming that we should check at runtime, the Linux kernel
- // always supports the monotonic clock
- Q_ASSERT(r == 0);
+#ifdef CLOCK_MONOTONIC
return CLOCK_MONOTONIC;
-# endif
-
- if (r == 0)
- return CLOCK_MONOTONIC;
-# endif
-
- // no monotonic, try the realtime clock
- r = clock_getres(CLOCK_REALTIME, &regular_clock_resolution);
- Q_ASSERT(r == 0);
+#else
return CLOCK_REALTIME;
-}
-
-static inline clock_t regularClock()
-{
- static const clock_t clock = regularClockCheck();
- return clock;
-}
#endif
+}
bool QElapsedTimer::isMonotonic() noexcept
{
@@ -121,7 +72,7 @@ QElapsedTimer::ClockType QElapsedTimer::clockType() noexcept
static inline void do_gettime(qint64 *sec, qint64 *frac)
{
timespec ts;
- qt_clock_gettime(regularClock(), &ts);
+ clock_gettime(regularClock(), &ts);
*sec = ts.tv_sec;
*frac = ts.tv_nsec;
}
diff --git a/src/corelib/thread/qwaitcondition_unix.cpp b/src/corelib/thread/qwaitcondition_unix.cpp
index 84534e9de1..43b8c086b1 100644
--- a/src/corelib/thread/qwaitcondition_unix.cpp
+++ b/src/corelib/thread/qwaitcondition_unix.cpp
@@ -44,13 +44,13 @@ void qt_initialize_pthread_cond(pthread_cond_t *cond, const char *where)
pthread_condattr_t condattr;
pthread_condattr_init(&condattr);
-#if (_POSIX_MONOTONIC_CLOCK-0 >= 0)
+#ifdef CLOCK_MONOTONIC
#if defined(Q_OS_ANDROID)
- if (local_condattr_setclock && QElapsedTimer::clockType() == QElapsedTimer::MonotonicClock)
+ if (local_condattr_setclock)
local_condattr_setclock(&condattr, CLOCK_MONOTONIC);
#elif !defined(Q_OS_DARWIN)
- if (QElapsedTimer::clockType() == QElapsedTimer::MonotonicClock)
- pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
+ // Darwin doesn't have this function
+ pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
#endif
#endif
report_error(pthread_cond_init(cond, &condattr), where, "cv init");