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 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
143 SGTimeStamp abstimeForSleep = abstime;
145 // Always undersleep by resolution of the clock
147 if (-1 != clock_getres(getClockId(), &ts)) {
148 abstimeForSleep -= SGTimeStamp::fromSecNSec(ts.tv_sec, ts.tv_nsec);
150 abstimeForSleep -= SGTimeStamp::fromSecMSec(0, 2);
153 ts.tv_sec = abstimeForSleep._sec;
154 ts.tv_nsec = abstimeForSleep._nsec;
156 int ret = clock_nanosleep(getClockId(), TIMER_ABSTIME, &ts, NULL);
157 if (-1 == ret && errno != EINTR)
163 // The busy loop for the rest
164 SGTimeStamp currentTime;
167 } while (currentTime < abstime);
173 SGTimeStamp currentTime;
175 if (abstime <= currentTime)
178 SGTimeStamp abstimeForSleep = abstime - SGTimeStamp::fromSecMSec(0, 2);
179 for (;abstimeForSleep < currentTime;) {
180 SGTimeStamp timeDiff = abstimeForSleep - currentTime;
181 if (timeDiff < SGTimeStamp::fromSecMSec(0, 1))
183 // Don't know, but may be win32 has something better today??
184 Sleep(static_cast<DWORD>(timeDiff.toMSecs()));
189 // Follow by a busy loop
190 while (currentTime < abstime) {
198 SGTimeStamp currentTime;
200 if (abstime <= currentTime)
203 SGTimeStamp abstimeForSleep = abstime - SGTimeStamp::fromSecMSec(0, 2);
204 for (;abstimeForSleep < currentTime;) {
205 SGTimeStamp timeDiff = abstimeForSleep - currentTime;
206 if (timeDiff < SGTimeStamp::fromSecUSec(0, 1))
208 // Its documented that some systems bail out on usleep for longer than 1s
209 // since we recheck the current time anyway just wait for
210 // less than a second multiple times
211 bool truncated = false;
212 if (SGTimeStamp::fromSec(1) < timeDiff) {
213 timeDiff = SGTimeStamp::fromSec(1);
216 int ret = usleep(static_cast<int>(timeDiff.toUSecs()));
217 if (-1 == ret && errno != EINTR)
219 if (ret == 0 && !truncated)
225 // Follow by a busy loop
226 while (currentTime < abstime) {
235 bool SGTimeStamp::sleepFor(const SGTimeStamp& reltime)
237 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
239 ts.tv_sec = reltime._sec;
240 ts.tv_nsec = reltime._nsec;
243 int ret = clock_nanosleep(getClockId(), 0, &ts, &rem);
244 if (-1 == ret && errno != EINTR)
248 // Use the remainder for the next cycle.
253 if (reltime < SGTimeStamp::fromSecMSec(0, 1))
255 // Don't know, but may be win32 has something better today??
256 Sleep(static_cast<DWORD>(reltime.toMSecs()));
263 SGTimeStamp currentTime;
265 for (;abstime < currentTime;) {
266 SGTimeStamp timeDiff = abstime - currentTime;
267 if (timeDiff < SGTimeStamp::fromSecUSec(0, 1))
269 // Its documented that some systems bail out on usleep for longer than 1s
270 // since we recheck the current time anyway just wait for
271 // less than a second multiple times
272 bool truncated = false;
273 if (SGTimeStamp::fromSec(1) < timeDiff) {
274 timeDiff = SGTimeStamp::fromSec(1);
277 int ret = usleep(static_cast<int>(timeDiff.toUSecs()));
278 if (-1 == ret && errno != EINTR)
280 if (ret == 0 && !truncated)
290 int SGTimeStamp::elapsedMSec() const
295 return static_cast<int>((now - *this).toMSecs());