From f900b675047c2e595a270b2a65edf8a9731b252a Mon Sep 17 00:00:00 2001 From: Norwegian Rock Cat Date: Mon, 20 Jul 2009 10:08:52 +0200 Subject: Get monotonic time working on Mac OS X for corelib programs. Mac OS X does not provide POSIX monotonic timers. Instead it does provide a Mach call to get the absolute time (a.k.a., number of CPU ticks) for the next timer event. This gets us around the bug in select(2) on Mac, that it doesn't wakeup when the times been changed. Of course, if you used the GUI event dispatcher, which is based on CFRunLoopTimers, this is not an issue, but if you really just need corelib, it's a bear to bring in the other stuff. Thanks to the nice guys at Parallels for the basics of the patch! Task-number: 237384 Reviewed-by: Bradley T. Hughes --- src/corelib/kernel/qeventdispatcher_unix.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/corelib/kernel/qeventdispatcher_unix.cpp b/src/corelib/kernel/qeventdispatcher_unix.cpp index 0eeea04..9deb78f 100644 --- a/src/corelib/kernel/qeventdispatcher_unix.cpp +++ b/src/corelib/kernel/qeventdispatcher_unix.cpp @@ -59,6 +59,10 @@ # include #endif +#ifdef Q_OS_MAC +#include +#endif + QT_BEGIN_NAMESPACE Q_CORE_EXPORT bool qt_disable_lowpriority_timers=false; @@ -259,7 +263,7 @@ int QEventDispatcherUNIXPrivate::doSelect(QEventLoop::ProcessEventsFlags flags, QTimerInfoList::QTimerInfoList() { -#if (_POSIX_MONOTONIC_CLOCK-0 <= 0) +#if (_POSIX_MONOTONIC_CLOCK-0 <= 0) && !defined(Q_OS_MAC) useMonotonicTimers = false; # if (_POSIX_MONOTONIC_CLOCK == 0) @@ -287,6 +291,9 @@ QTimerInfoList::QTimerInfoList() msPerTick = 0; } #else +# if defined(Q_OS_MAC) + useMonotonicTimers = true; +# endif // using monotonic timers unconditionally getTime(currentTime); #endif @@ -335,7 +342,19 @@ bool QTimerInfoList::timeChanged(timeval *delta) void QTimerInfoList::getTime(timeval &t) { -#if !defined(QT_NO_CLOCK_MONOTONIC) && !defined(QT_BOOTSTRAPPED) +#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); + t.tv_sec = nsecs * 1e-9; + t.tv_usec = nsecs * 1e-3 - (t.tv_sec * 1e6); + return; + } +#elif !defined(QT_NO_CLOCK_MONOTONIC) && !defined(QT_BOOTSTRAPPED) if (useMonotonicTimers) { timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); -- cgit v0.12