3 * Provides a class for managing a timestamp (seconds & milliseconds.)
6 // Written by Curtis Olson, started December 1998.
8 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 # include <simgear_config.h>
31 #include <simgear/compiler.h>
36 #ifdef HAVE_SYS_TIMEB_H
37 # include <sys/timeb.h> // for ftime() and struct timeb
40 # include <unistd.h> // for gettimeofday() and the _POSIX_TIMERS define
42 #ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
46 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
52 # if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
56 # include <mmsystem.h>
59 #include "timestamp.hxx"
61 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
62 static clockid_t getClockId()
64 #if defined(_POSIX_MONOTONIC_CLOCK)
65 static clockid_t clockid = CLOCK_MONOTONIC;
66 static bool firstTime = true;
71 // For the first time test if the monotonic clock is available.
72 // If so use this, if not use the realtime clock.
74 if (-1 == clock_gettime(clockid, &ts) && errno == EINVAL)
75 clockid = CLOCK_REALTIME;
78 return CLOCK_REALTIME;
84 void SGTimeStamp::stamp() {
89 _nsec = ( t - ( _sec * 1000 ) ) * 1000 * 1000;
90 #elif defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
92 clock_gettime(getClockId(), &ts);
95 #elif defined( HAVE_GETTIMEOFDAY )
96 struct timeval current;
97 gettimeofday(¤t, NULL);
98 _sec = current.tv_sec;
99 _nsec = current.tv_usec * 1000;
100 #elif defined( HAVE_GETLOCALTIME )
102 GetLocalTime(¤t);
103 _sec = current.wSecond;
104 _nsec = current.wMilliseconds * 1000 * 1000;
105 #elif defined( HAVE_FTIME )
106 struct timeb current;
109 _nsec = current.millitm * 1000 * 1000;
115 // sleep based timing loop.
117 // Calling sleep, even usleep() on linux is less accurate than
118 // we like, but it does free up the cpu for other tasks during
119 // the sleep so it is desirable. Because of the way sleep()
120 // is implemented in consumer operating systems like windows
121 // and linux, you almost always sleep a little longer than the
124 // To combat the problem of sleeping too long, we calculate the
125 // desired wait time and shorten it by 2000us (2ms) to avoid
126 // [hopefully] over-sleep'ing. The 2ms value was arrived at
127 // via experimentation. We follow this up at the end with a
128 // simple busy-wait loop to get the final pause timing exactly
131 // Assuming we don't oversleep by more than 2000us, this
132 // should be a reasonable compromise between sleep based
133 // waiting, and busy waiting.
135 // Usually posix timer resolutions are low enough that we
136 // could just leave this to the operating system today.
137 // The day where the busy loop was introduced in flightgear,
138 // the usual kernels still had just about 10ms (=HZ for
139 // the timer tick) accuracy which is too bad to catch 60Hz...
140 bool SGTimeStamp::sleepUntil(const SGTimeStamp& abstime)
142 // FreeBSD is missing clock_nanosleep, see
143 // https://wiki.freebsd.org/FreeBSD_and_Standards
144 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS) && !defined(__FreeBSD__)
145 SGTimeStamp abstimeForSleep = abstime;
147 // Always undersleep by resolution of the clock
149 if (-1 != clock_getres(getClockId(), &ts)) {
150 abstimeForSleep -= SGTimeStamp::fromSecNSec(ts.tv_sec, ts.tv_nsec);
152 abstimeForSleep -= SGTimeStamp::fromSecMSec(0, 2);
155 ts.tv_sec = abstimeForSleep._sec;
156 ts.tv_nsec = abstimeForSleep._nsec;
158 int ret = clock_nanosleep(getClockId(), TIMER_ABSTIME, &ts, NULL);
159 if (-1 == ret && errno != EINTR)
165 // The busy loop for the rest
166 SGTimeStamp currentTime;
169 } while (currentTime < abstime);
175 SGTimeStamp currentTime;
177 if (abstime <= currentTime)
180 SGTimeStamp abstimeForSleep = abstime - SGTimeStamp::fromSecMSec(0, 2);
181 for (;abstimeForSleep < currentTime;) {
182 SGTimeStamp timeDiff = abstimeForSleep - currentTime;
183 if (timeDiff < SGTimeStamp::fromSecMSec(0, 1))
185 // Don't know, but may be win32 has something better today??
186 Sleep(static_cast<DWORD>(timeDiff.toMSecs()));
191 // Follow by a busy loop
192 while (currentTime < abstime) {
200 SGTimeStamp currentTime;
202 if (abstime <= currentTime)
205 SGTimeStamp abstimeForSleep = abstime - SGTimeStamp::fromSecMSec(0, 2);
206 for (;abstimeForSleep < currentTime;) {
207 SGTimeStamp timeDiff = abstimeForSleep - currentTime;
208 if (timeDiff < SGTimeStamp::fromSecUSec(0, 1))
210 // Its documented that some systems bail out on usleep for longer than 1s
211 // since we recheck the current time anyway just wait for
212 // less than a second multiple times
213 bool truncated = false;
214 if (SGTimeStamp::fromSec(1) < timeDiff) {
215 timeDiff = SGTimeStamp::fromSec(1);
218 int ret = usleep(static_cast<int>(timeDiff.toUSecs()));
219 if (-1 == ret && errno != EINTR)
221 if (ret == 0 && !truncated)
227 // Follow by a busy loop
228 while (currentTime < abstime) {
237 bool SGTimeStamp::sleepFor(const SGTimeStamp& reltime)
239 // see comment above regarding FreeBSD
240 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS) && !defined(__FreeBSD__)
242 ts.tv_sec = reltime._sec;
243 ts.tv_nsec = reltime._nsec;
246 int ret = clock_nanosleep(getClockId(), 0, &ts, &rem);
247 if (-1 == ret && errno != EINTR)
251 // Use the remainder for the next cycle.
256 if (reltime < SGTimeStamp::fromSecMSec(0, 1))
258 // Don't know, but may be win32 has something better today??
259 Sleep(static_cast<DWORD>(reltime.toMSecs()));
261 #elif defined __APPLE__
262 // the generic version below behaves badly on Mac; use nanosleep directly,
263 // similar to the POSIX timers version
265 ts.tv_sec = reltime._sec;
266 ts.tv_nsec = reltime._nsec;
270 int ret = nanosleep(&ts, &rem);
271 if (-1 == ret && errno != EINTR)
275 // Use the remainder for the next cycle.
284 SGTimeStamp currentTime;
286 for (;abstime < currentTime;) {
287 SGTimeStamp timeDiff = abstime - currentTime;
288 if (timeDiff < SGTimeStamp::fromSecUSec(0, 1))
290 // Its documented that some systems bail out on usleep for longer than 1s
291 // since we recheck the current time anyway just wait for
292 // less than a second multiple times
293 bool truncated = false;
294 if (SGTimeStamp::fromSec(1) < timeDiff) {
295 timeDiff = SGTimeStamp::fromSec(1);
298 int ret = usleep(static_cast<int>(timeDiff.toUSecs()));
299 if (-1 == ret && errno != EINTR)
301 if (ret == 0 && !truncated)
311 int SGTimeStamp::elapsedMSec() const
316 return static_cast<int>((now - *this).toMSecs());