diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2010-02-18 12:56:46 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2010-03-17 10:54:16 (GMT) |
commit | 49d949b644b94fbd6c1946ffddcc1b8675ffa9ef (patch) | |
tree | ae95853f8c0401ba159cd522464b48948eb88278 /src/corelib | |
parent | ca0def08d1ec2575546f7bfa3759f24288ba8e2a (diff) | |
download | Qt-49d949b644b94fbd6c1946ffddcc1b8675ffa9ef.zip Qt-49d949b644b94fbd6c1946ffddcc1b8675ffa9ef.tar.gz Qt-49d949b644b94fbd6c1946ffddcc1b8675ffa9ef.tar.bz2 |
Make qcore_unix.cpp share code with QTimestamp
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/kernel/qcore_unix.cpp | 65 | ||||
-rw-r--r-- | src/corelib/kernel/qcore_unix_p.h | 4 | ||||
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_unix.cpp | 9 | ||||
-rw-r--r-- | src/corelib/tools/qtimestamp_mac.cpp | 25 | ||||
-rw-r--r-- | src/corelib/tools/qtimestamp_unix.cpp | 62 |
5 files changed, 68 insertions, 97 deletions
diff --git a/src/corelib/kernel/qcore_unix.cpp b/src/corelib/kernel/qcore_unix.cpp index 0fd965b..9146923 100644 --- a/src/corelib/kernel/qcore_unix.cpp +++ b/src/corelib/kernel/qcore_unix.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "qcore_unix_p.h" +#include "qtimestamp.h" #ifndef Q_OS_VXWORKS # if !defined(Q_OS_HPUX) || defined(__ia64) @@ -56,74 +57,12 @@ #include <mach/mach_time.h> #endif -#if !defined(QT_NO_CLOCK_MONOTONIC) -# if defined(QT_BOOTSTRAPPED) -# define QT_NO_CLOCK_MONOTONIC -# endif -#endif - QT_BEGIN_NAMESPACE -bool qt_gettime_is_monotonic() -{ -#if (_POSIX_MONOTONIC_CLOCK-0 > 0) || defined(Q_OS_MAC) - return true; -#else - static int returnValue = 0; - - if (returnValue == 0) { -# if (_POSIX_MONOTONIC_CLOCK-0 < 0) - returnValue = -1; -# elif (_POSIX_MONOTONIC_CLOCK == 0) - // detect if the system support monotonic timers - long x = sysconf(_SC_MONOTONIC_CLOCK); - returnValue = (x >= 200112L) ? 1 : -1; -# endif - } - - return returnValue != -1; -#endif -} - -timeval qt_gettime() -{ - timeval tv; -#if defined(Q_OS_MAC) - static mach_timebase_info_data_t info = {0,0}; - if (info.denom == 0) - mach_timebase_info(&info); - - uint64_t cpu_time = mach_absolute_time(); - uint64_t nsecs = cpu_time * (info.numer / info.denom); - tv.tv_sec = nsecs / 1000000000ull; - tv.tv_usec = (nsecs / 1000) - (tv.tv_sec * 1000000); - return tv; -#elif (_POSIX_MONOTONIC_CLOCK-0 > 0) - timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - tv.tv_sec = ts.tv_sec; - tv.tv_usec = ts.tv_nsec / 1000; - return tv; -#else -# if !defined(QT_NO_CLOCK_MONOTONIC) && !defined(QT_BOOTSTRAPPED) - if (qt_gettime_is_monotonic()) { - timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - tv.tv_sec = ts.tv_sec; - tv.tv_usec = ts.tv_nsec / 1000; - return tv; - } -# endif - // use gettimeofday - ::gettimeofday(&tv, 0); - return tv; -#endif -} - static inline bool time_update(struct timeval *tv, const struct timeval &start, const struct timeval &timeout) { - if (!qt_gettime_is_monotonic()) { + if (!QTimestamp::isMonotonic()) { // we cannot recalculate the timeout without a monotonic clock as the time may have changed return false; } diff --git a/src/corelib/kernel/qcore_unix_p.h b/src/corelib/kernel/qcore_unix_p.h index 297b25d..59d66f7 100644 --- a/src/corelib/kernel/qcore_unix_p.h +++ b/src/corelib/kernel/qcore_unix_p.h @@ -314,8 +314,8 @@ static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options) # define _POSIX_MONOTONIC_CLOCK -1 #endif -bool qt_gettime_is_monotonic(); -timeval qt_gettime(); +timeval qt_gettime(); // in qtimestamp_mac.cpp or qtimestamp_unix.cpp + Q_CORE_EXPORT int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept, const struct timeval *tv); diff --git a/src/corelib/kernel/qeventdispatcher_unix.cpp b/src/corelib/kernel/qeventdispatcher_unix.cpp index a141887..7a671d1 100644 --- a/src/corelib/kernel/qeventdispatcher_unix.cpp +++ b/src/corelib/kernel/qeventdispatcher_unix.cpp @@ -45,6 +45,7 @@ #include "qpair.h" #include "qsocketnotifier.h" #include "qthread.h" +#include "qtimestamp.h" #include "qeventdispatcher_unix_p.h" #include <private/qthread_p.h> @@ -311,12 +312,10 @@ int QEventDispatcherUNIXPrivate::doSelect(QEventLoop::ProcessEventsFlags flags, QTimerInfoList::QTimerInfoList() { - currentTime = qt_gettime(); - #if (_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC) - if (!qt_gettime_is_monotonic()) { + if (!QTimestamp::isMonotonic()) { // not using monotonic timers, initialize the timeChanged() machinery - previousTime = currentTime; + previousTime = qt_gettime(); tms unused; previousTicks = times(&unused); @@ -393,7 +392,7 @@ bool QTimerInfoList::timeChanged(timeval *delta) void QTimerInfoList::repairTimersIfNeeded() { - if (qt_gettime_is_monotonic()) + if (QTimestamp::isMonotonic()) return; timeval delta; if (timeChanged(&delta)) diff --git a/src/corelib/tools/qtimestamp_mac.cpp b/src/corelib/tools/qtimestamp_mac.cpp index bdc91d1..e1a7d79 100644 --- a/src/corelib/tools/qtimestamp_mac.cpp +++ b/src/corelib/tools/qtimestamp_mac.cpp @@ -53,20 +53,35 @@ bool QTimestamp::isMonotonic() } static mach_timebase_info_data_t info = {0,0}; -static qint64 absoluteToMSecs(qint64 cpuTime) +static qint64 absoluteToNSecs(qint64 cpuTime) { if (info.denom == 0) mach_timebase_info(&info); qint64 nsecs = cpuTime * info.numer / info.denom; - return nsecs / (1000*1000ull); + return nsecs; +} + +static qint64 absoluteToMSecs(qint64 cpuTime) +{ + return absoluteToNSecs(cpuTime) / 1000000; } static qint64 msecsToAbsolute(qint64 ms) { if (info.denom == 0) mach_timebase_info(&info); - qint64 nsecs = ms * 1000000ull; - return nsecs * info.denom / info.numer; + return ms * 1000000 * info.denom / info.numer; +} + +timeval qt_gettime() +{ + timeval tv; + + uint64_t cpu_time = mach_absolute_time(); + uint64_t nsecs = absoluteToNSecs(cpu_time); + tv.tv_sec = nsecs / 1000000000ull; + tv.tv_usec = (nsecs / 1000) - (tv.tv_sec * 1000000); + return tv; } void QTimestamp::start() @@ -98,7 +113,7 @@ qint64 QTimestamp::secsTo(const QTimestamp &other) const void QTimestamp::addSecs(int secs) { - t1 += secs * 1000; + t1 += msecsToAbsolute(secs * 1000); } bool operator<(const QTimestamp &v1, const QTimestamp &v2) diff --git a/src/corelib/tools/qtimestamp_unix.cpp b/src/corelib/tools/qtimestamp_unix.cpp index b82259f..dbf49c1 100644 --- a/src/corelib/tools/qtimestamp_unix.cpp +++ b/src/corelib/tools/qtimestamp_unix.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include "qtimestamp.h" +#include "qpair.h" #include <sys/time.h> #include <time.h> #include <unistd.h> @@ -52,6 +53,19 @@ QT_BEGIN_NAMESPACE +static qint64 fractionAdjustment() +{ + if (QTimestamp::isMonotonic()) { + // 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; + } +} + bool QTimestamp::isMonotonic() { #if (_POSIX_MONOTONIC_CLOCK-0 > 0) @@ -73,44 +87,48 @@ bool QTimestamp::isMonotonic() #endif } -static qint64 fractionAdjustment() -{ - if (QTimestamp::isMonotonic()) { - // 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; - } -} - -void QTimestamp::start() +static inline QPair<long, long> do_gettime() { #if (_POSIX_MONOTONIC_CLOCK-0 > 0) timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); - t1 = ts.tv_sec; - t2 = ts.tv_nsec; + return qMakePair(ts.tv_sec, ts.tv_nsec); #else # if !defined(QT_NO_CLOCK_MONOTONIC) && !defined(QT_BOOTSTRAPPED) - if (isMonotonic()) { + if (QTimestamp::isMonotonic()) { timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); - t1 = ts.tv_sec; - t2 = ts.tv_nsec; - return; + return qMakePair(ts.tv_sec, ts.tv_nsec); } # endif // use gettimeofday timeval tv; ::gettimeofday(&tv, 0); - t1 = tv.tv_sec; - t2 = tv.tv_usec; + return qMakePair(tv.tv_sec, tv.tv_usec); #endif } +// used in qcore_unix.cpp and qeventdispatcher_unix.cpp +timeval qt_gettime() +{ + QPair<long, long> r = do_gettime(); + + timeval tv; + tv.tv_sec = r.first; + tv.tv_usec = r.second; + if (QTimestamp::isMonotonic()) + tv.tv_usec /= 1000; + + return tv; +} + +void QTimestamp::start() +{ + QPair<long, long> r = do_gettime(); + t1 = r.first; + t2 = r.second; +} + qint64 QTimestamp::elapsed() const { QTimestamp now; |