/**************************************************************************** ** ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the QtCore module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ // ask for the latest POSIX, just in case #define _POSIX_C_SOURCE 200809L #include "qelapsedtimer.h" #include #include #include #include "private/qcore_unix_p.h" #if defined(QT_NO_CLOCK_MONOTONIC) || defined(QT_BOOTSTRAPPED) // turn off the monotonic clock # ifdef _POSIX_MONOTONIC_CLOCK # undef _POSIX_MONOTONIC_CLOCK # endif # define _POSIX_MONOTONIC_CLOCK -1 #endif QT_BEGIN_NAMESPACE #if (_POSIX_MONOTONIC_CLOCK-0 != 0) static const bool monotonicClockChecked = true; static const bool monotonicClockAvailable = _POSIX_MONOTONIC_CLOCK > 0; #else static int monotonicClockChecked = false; static int monotonicClockAvailable = false; #endif #define load_acquire(x) ((volatile const int&)(x)) #define store_release(x,v) ((volatile int&)(x) = (v)) static void unixCheckClockType() { #if (_POSIX_MONOTONIC_CLOCK-0 == 0) if (Q_LIKELY(load_acquire(monotonicClockChecked))) return; # if defined(_SC_MONOTONIC_CLOCK) // detect if the system support monotonic timers long x = sysconf(_SC_MONOTONIC_CLOCK); store_release(monotonicClockAvailable, x >= 200112L); # endif store_release(monotonicClockChecked, true); #endif } static inline qint64 fractionAdjustment() { return 1000*1000ull; } bool QElapsedTimer::isMonotonic() Q_DECL_NOTHROW { unixCheckClockType(); return monotonicClockAvailable; } QElapsedTimer::ClockType QElapsedTimer::clockType() Q_DECL_NOTHROW { unixCheckClockType(); return monotonicClockAvailable ? MonotonicClock : SystemTime; } static inline void do_gettime(qint64 *sec, qint64 *frac) { #if (_POSIX_MONOTONIC_CLOCK-0 >= 0) unixCheckClockType(); if (Q_LIKELY(monotonicClockAvailable)) { timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); *sec = ts.tv_sec; *frac = ts.tv_nsec; return; } #endif #ifdef CLOCK_REALTIME // even if we don't have a monotonic clock, // we can use clock_gettime -> nanosecond resolution timespec ts; clock_gettime(CLOCK_REALTIME, &ts); *sec = ts.tv_sec; *frac = ts.tv_nsec; #else // use gettimeofday timeval tv; ::gettimeofday(&tv, 0); *sec = tv.tv_sec; *frac = tv.tv_usec * 1000; #endif } // used in qcore_unix.cpp and qeventdispatcher_unix.cpp timeval qt_gettime() Q_DECL_NOTHROW { qint64 sec, frac; do_gettime(&sec, &frac); timeval tv; tv.tv_sec = sec; tv.tv_usec = frac / 1000; return tv; } void qt_nanosleep(timespec amount) { // We'd like to use clock_nanosleep. // // But clock_nanosleep is from POSIX.1-2001 and both are *not* // affected by clock changes when using relative sleeps, even for // CLOCK_REALTIME. // // nanosleep is POSIX.1-1993 int r; EINTR_LOOP(r, nanosleep(&amount, &amount)); } static qint64 elapsedAndRestart(qint64 sec, qint64 frac, qint64 *nowsec, qint64 *nowfrac) { do_gettime(nowsec, nowfrac); sec = *nowsec - sec; frac = *nowfrac - frac; return sec * Q_INT64_C(1000) + frac / fractionAdjustment(); } void QElapsedTimer::start() Q_DECL_NOTHROW { do_gettime(&t1, &t2); } qint64 QElapsedTimer::restart() Q_DECL_NOTHROW { return elapsedAndRestart(t1, t2, &t1, &t2); } qint64 QElapsedTimer::nsecsElapsed() const Q_DECL_NOTHROW { qint64 sec, frac; do_gettime(&sec, &frac); sec = sec - t1; frac = frac - t2; return sec * Q_INT64_C(1000000000) + frac; } qint64 QElapsedTimer::elapsed() const Q_DECL_NOTHROW { qint64 sec, frac; return elapsedAndRestart(t1, t2, &sec, &frac); } qint64 QElapsedTimer::msecsSinceReference() const Q_DECL_NOTHROW { return t1 * Q_INT64_C(1000) + t2 / fractionAdjustment(); } qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const Q_DECL_NOTHROW { qint64 secs = other.t1 - t1; qint64 fraction = other.t2 - t2; return secs * Q_INT64_C(1000) + fraction / fractionAdjustment(); } qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const Q_DECL_NOTHROW { return other.t1 - t1; } bool operator<(const QElapsedTimer &v1, const QElapsedTimer &v2) Q_DECL_NOTHROW { return v1.t1 < v2.t1 || (v1.t1 == v2.t1 && v1.t2 < v2.t2); } QT_END_NAMESPACE