Qt 4.8
qelapsedtimer_unix.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qelapsedtimer.h"
43 #ifdef Q_OS_VXWORKS
44 #include "qfunctions_vxworks.h"
45 #else
46 #include <sys/time.h>
47 #include <time.h>
48 #endif
49 #include <unistd.h>
50 
51 #if defined(QT_NO_CLOCK_MONOTONIC) || defined(QT_BOOTSTRAPPED)
52 // turn off the monotonic clock
53 # ifdef _POSIX_MONOTONIC_CLOCK
54 # undef _POSIX_MONOTONIC_CLOCK
55 # endif
56 # define _POSIX_MONOTONIC_CLOCK -1
57 #endif
58 
60 
61 #if (_POSIX_MONOTONIC_CLOCK-0 != 0)
62 static const bool monotonicClockChecked = true;
63 static const bool monotonicClockAvailable = _POSIX_MONOTONIC_CLOCK > 0;
64 #else
65 static int monotonicClockChecked = false;
66 static int monotonicClockAvailable = false;
67 #endif
68 
69 #ifdef Q_CC_GNU
70 # define is_likely(x) __builtin_expect((x), 1)
71 #else
72 # define is_likely(x) (x)
73 #endif
74 #define load_acquire(x) ((volatile const int&)(x))
75 #define store_release(x,v) ((volatile int&)(x) = (v))
76 
77 static void unixCheckClockType()
78 {
79 #if (_POSIX_MONOTONIC_CLOCK-0 == 0)
81  return;
82 
83 # if defined(_SC_MONOTONIC_CLOCK)
84  // detect if the system support monotonic timers
85  long x = sysconf(_SC_MONOTONIC_CLOCK);
87 # endif
88 
90 #endif
91 }
92 
93 static inline qint64 fractionAdjustment()
94 {
95  // disabled, but otherwise indicates bad usage of QElapsedTimer
96  //Q_ASSERT(monotonicClockChecked);
97 
99  // the monotonic timer is measured in nanoseconds
100  // 1 ms = 1000000 ns
101  return 1000*1000ull;
102  } else {
103  // gettimeofday is measured in microseconds
104  // 1 ms = 1000 us
105  return 1000;
106  }
107 }
108 
110 {
113 }
114 
116 {
119 }
120 
121 static inline void do_gettime(qint64 *sec, qint64 *frac)
122 {
123 #if (_POSIX_MONOTONIC_CLOCK-0 >= 0)
126  timespec ts;
127  clock_gettime(CLOCK_MONOTONIC, &ts);
128  *sec = ts.tv_sec;
129  *frac = ts.tv_nsec;
130  return;
131  }
132 #endif
133  // use gettimeofday
134  struct timeval tv;
135  ::gettimeofday(&tv, 0);
136  *sec = tv.tv_sec;
137  *frac = tv.tv_usec;
138 }
139 
140 // used in qcore_unix.cpp and qeventdispatcher_unix.cpp
141 timeval qt_gettime()
142 {
143  qint64 sec, frac;
144  do_gettime(&sec, &frac);
145 
146  timeval tv;
147  tv.tv_sec = sec;
148  tv.tv_usec = frac;
150  tv.tv_usec /= 1000;
151 
152  return tv;
153 }
154 
156  qint64 *nowsec, qint64 *nowfrac)
157 {
158  do_gettime(nowsec, nowfrac);
159  sec = *nowsec - sec;
160  frac = *nowfrac - frac;
161  return sec * Q_INT64_C(1000) + frac / fractionAdjustment();
162 }
163 
165 {
166  do_gettime(&t1, &t2);
167 }
168 
170 {
171  return elapsedAndRestart(t1, t2, &t1, &t2);
172 }
173 
175 {
176  qint64 sec, frac;
177  do_gettime(&sec, &frac);
178  sec = sec - t1;
179  frac = frac - t2;
181  frac *= 1000;
182  return sec * Q_INT64_C(1000000000) + frac;
183 }
184 
186 {
187  qint64 sec, frac;
188  return elapsedAndRestart(t1, t2, &sec, &frac);
189 }
190 
192 {
193  return t1 * Q_INT64_C(1000) + t2 / fractionAdjustment();
194 }
195 
196 qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const
197 {
198  qint64 secs = other.t1 - t1;
199  qint64 fraction = other.t2 - t2;
200  return secs * Q_INT64_C(1000) + fraction / fractionAdjustment();
201 }
202 
203 qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const
204 {
205  return other.t1 - t1;
206 }
207 
208 bool operator<(const QElapsedTimer &v1, const QElapsedTimer &v2)
209 {
210  return v1.t1 < v2.t1 || (v1.t1 == v2.t1 && v1.t2 < v2.t2);
211 }
212 
#define store_release(x, v)
ClockType
This enum contains the different clock types that QElapsedTimer may use.
Definition: qelapsedtimer.h:56
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
qint64 msecsTo(const QElapsedTimer &other) const
Returns the number of milliseconds between this QElapsedTimer and other.
friend bool Q_CORE_EXPORT operator<(const QElapsedTimer &v1, const QElapsedTimer &v2)
Returns true if v1 was started before v2, false otherwise.
#define load_acquire(x)
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
static bool isMonotonic()
Returns true if this is a monotonic clock, false otherwise.
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qint64 restart()
Restarts the timer and returns the time elapsed since the previous start.
static int monotonicClockAvailable
static int monotonicClockChecked
qint64 nsecsElapsed() const
Returns the number of nanoseconds since this QElapsedTimer was last started.
static qint64 fractionAdjustment()
__int64 qint64
Definition: qglobal.h:942
static ClockType clockType()
Returns the clock type that this QElapsedTimer implementation uses.
#define _POSIX_MONOTONIC_CLOCK
Definition: qcore_unix_p.h:343
static qint64 elapsedAndRestart(qint64 sec, qint64 frac, qint64 *nowsec, qint64 *nowfrac)
#define Q_INT64_C(c)
Definition: qglobal.h:940
static void unixCheckClockType()
qint64 msecsSinceReference() const
Returns the number of milliseconds between last time this QElapsedTimer object was started and its re...
static void do_gettime(qint64 *sec, qint64 *frac)
timeval qt_gettime()
void start()
Starts this timer.
#define is_likely(x)
qint64 secsTo(const QElapsedTimer &other) const
Returns the number of seconds between this QElapsedTimer and other.