OSDN Git Service

QT_NO_CLOCK_MONOTONIC case optimizations
authorIvailo Monev <xakepa10@laimg.moc>
Sun, 8 Dec 2019 14:47:37 +0000 (14:47 +0000)
committerIvailo Monev <xakepa10@laimg.moc>
Sun, 8 Dec 2019 14:47:37 +0000 (14:47 +0000)
Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
src/core/tools/qelapsedtimer_unix.cpp

index 2ea3a6c..291f1b9 100644 (file)
@@ -46,38 +46,45 @@ static const bool monotonicClockAvailable = (sysconf(_SC_MONOTONIC_CLOCK) >= 200
 static const bool monotonicClockAvailable = (_POSIX_MONOTONIC_CLOCK > 0);
 #  else
 #    define QT_NO_CLOCK_MONOTONIC
-static const bool monotonicClockAvailable = false;
 #  endif
 #endif
 
 static inline qint64 fractionAdjustment()
 {
-    // disabled, but otherwise indicates bad usage of QElapsedTimer
-    //Q_ASSERT(monotonicClockChecked);
-
+#ifndef QT_NO_CLOCK_MONOTONIC
     if (Q_LIKELY(monotonicClockAvailable)) {
         // the monotonic timer is measured in nanoseconds
         // 1 ms = 1000000 ns
         return 1000*1000ull;
-    } else {
-        // gettimeofday is measured in microseconds
-        // 1 ms = 1000 us
-        return 1000;
     }
+#endif
+    // gettimeofday is measured in microseconds
+    // 1 ms = 1000 us
+    return 1000;
 }
 
 bool QElapsedTimer::isMonotonic()
 {
+#ifndef QT_NO_CLOCK_MONOTONIC
     return monotonicClockAvailable;
+#else
+    return false;
+#endif
 }
 
 QElapsedTimer::ClockType QElapsedTimer::clockType()
 {
-    return monotonicClockAvailable ? MonotonicClock : SystemTime;
+#ifndef QT_NO_CLOCK_MONOTONIC
+    if (Q_LIKELY(monotonicClockAvailable)) {
+        return QElapsedTimer::MonotonicClock;
+    }
+#endif
+    return QElapsedTimer::SystemTime;
 }
 
 static inline void do_gettime(qint64 *sec, qint64 *frac)
 {
+#ifndef QT_NO_CLOCK_MONOTONIC
     if (Q_LIKELY(monotonicClockAvailable)) {
         timespec ts;
         clock_gettime(CLOCK_MONOTONIC, &ts);
@@ -85,6 +92,7 @@ static inline void do_gettime(qint64 *sec, qint64 *frac)
         *frac = ts.tv_nsec;
         return;
     }
+#endif
     // use gettimeofday
     struct timeval tv;
     ::gettimeofday(&tv, Q_NULLPTR);
@@ -100,9 +108,11 @@ timeval qt_gettime()
 
     timeval tv;
     tv.tv_sec = sec;
+#ifndef QT_NO_CLOCK_MONOTONIC
     if (Q_LIKELY(monotonicClockAvailable))
         tv.tv_usec = frac / 1000;
     else
+#endif
         tv.tv_usec = frac;
 
     return tv;
@@ -133,8 +143,10 @@ qint64 QElapsedTimer::nsecsElapsed() const
     do_gettime(&sec, &frac);
     sec = sec - t1;
     frac = frac - t2;
+#ifndef QT_NO_CLOCK_MONOTONIC
     if (Q_UNLIKELY(!monotonicClockAvailable))
         frac *= 1000;
+#endif
     return sec * Q_INT64_C(1000000000) + frac;
 }