summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@nokia.com>2010-02-18 12:56:46 (GMT)
committerThiago Macieira <thiago.macieira@nokia.com>2010-03-17 10:54:16 (GMT)
commit49d949b644b94fbd6c1946ffddcc1b8675ffa9ef (patch)
treeae95853f8c0401ba159cd522464b48948eb88278 /src/corelib/tools
parentca0def08d1ec2575546f7bfa3759f24288ba8e2a (diff)
downloadQt-49d949b644b94fbd6c1946ffddcc1b8675ffa9ef.zip
Qt-49d949b644b94fbd6c1946ffddcc1b8675ffa9ef.tar.gz
Qt-49d949b644b94fbd6c1946ffddcc1b8675ffa9ef.tar.bz2
Make qcore_unix.cpp share code with QTimestamp
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qtimestamp_mac.cpp25
-rw-r--r--src/corelib/tools/qtimestamp_unix.cpp62
2 files changed, 60 insertions, 27 deletions
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;