]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timestamp.cxx
2189d643a8ec65c5313d2903ea6cbabd417982ce
[simgear.git] / simgear / timing / timestamp.cxx
1 /**
2  * \file timestamp.cxx
3  * Provides a class for managing a timestamp (seconds & milliseconds.)
4  */
5
6 // Written by Curtis Olson, started December 1998.
7 //
8 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
9 //
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <simgear_config.h>
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #include <ctime>
34 #include <cerrno>
35
36 #ifdef HAVE_SYS_TIMEB_H
37 #  include <sys/timeb.h> // for ftime() and struct timeb
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #  include <unistd.h>    // for gettimeofday() and the _POSIX_TIMERS define
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
44 #endif
45
46 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
47 #  include <time.h>
48 #endif
49
50 #ifdef WIN32
51 #  include <windows.h>
52 #  if defined( __CYGWIN__ ) || defined( __CYGWIN32__ )
53 #    define NEAR /* */
54 #    define FAR  /* */
55 #  endif
56 #  include <mmsystem.h>
57 #endif
58
59 #include "timestamp.hxx"
60
61 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
62 static clockid_t getClockId()
63 {
64 #if defined(_POSIX_MONOTONIC_CLOCK)
65     static clockid_t clockid = CLOCK_MONOTONIC;
66     static bool firstTime = true;
67     if (!firstTime)
68         return clockid;
69
70     firstTime = false;
71     // For the first time test if the monotonic clock is available.
72     // If so use this, if not use the realtime clock.
73     struct timespec ts;
74     if (-1 == clock_gettime(clockid, &ts) && errno == EINVAL)
75         clockid = CLOCK_REALTIME;
76     return clockid;
77 #else
78     return CLOCK_REALTIME;
79 #endif
80 }
81
82 #endif
83
84 void SGTimeStamp::stamp() {
85 #ifdef _WIN32
86     unsigned int t;
87     t = timeGetTime();
88     _sec = t / 1000;
89     _nsec = ( t - ( _sec * 1000 ) ) * 1000 * 1000;
90 #elif defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
91     struct timespec ts;
92     clock_gettime(getClockId(), &ts);
93     _sec = ts.tv_sec;
94     _nsec = ts.tv_nsec;
95 #elif defined( HAVE_GETTIMEOFDAY )
96     struct timeval current;
97     gettimeofday(&current, NULL);
98     _sec = current.tv_sec;
99     _nsec = current.tv_usec * 1000;
100 #elif defined( HAVE_GETLOCALTIME )
101     SYSTEMTIME current;
102     GetLocalTime(&current);
103     _sec = current.wSecond;
104     _nsec = current.wMilliseconds * 1000 * 1000;
105 #elif defined( HAVE_FTIME )
106     struct timeb current;
107     ftime(&current);
108     _sec = current.time;
109     _nsec = current.millitm * 1000 * 1000;
110 #else
111 # error Port me
112 #endif
113 }
114
115 // sleep based timing loop.
116 //
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
122 // requested amount.
123 //
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
129 // right.
130 //
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.
134 //
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)
141 {
142 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
143     SGTimeStamp abstimeForSleep = abstime;
144
145     // Always undersleep by resolution of the clock
146     struct timespec ts;
147     if (-1 != clock_getres(getClockId(), &ts)) {
148         abstimeForSleep -= SGTimeStamp::fromSecNSec(ts.tv_sec, ts.tv_nsec);
149     } else {
150         abstimeForSleep -= SGTimeStamp::fromSecMSec(0, 2);
151     }
152
153     ts.tv_sec = abstimeForSleep._sec;
154     ts.tv_nsec = abstimeForSleep._nsec;
155     for (;;) {
156         int ret = clock_nanosleep(getClockId(), TIMER_ABSTIME, &ts, NULL);
157         if (-1 == ret && errno != EINTR)
158             return false;
159         if (ret == 0)
160             break;
161     }
162
163     // The busy loop for the rest
164     SGTimeStamp currentTime;
165     do {
166         currentTime.stamp();
167     } while (currentTime < abstime);
168
169     return true;
170
171 #elif defined _WIN32
172
173     SGTimeStamp currentTime;
174     currentTime.stamp();
175     if (abstime <= currentTime)
176         return true;
177
178     SGTimeStamp abstimeForSleep = abstime - SGTimeStamp::fromSecMSec(0, 2);
179     for (;abstimeForSleep < currentTime;) {
180         SGTimeStamp timeDiff = abstimeForSleep - currentTime;
181         if (timeDiff < SGTimeStamp::fromSecMSec(0, 1))
182             break;
183         // Don't know, but may be win32 has something better today??
184         Sleep(static_cast<DWORD>(timeDiff.toMSecs()));
185
186         currentTime.stamp();
187     }
188
189     // Follow by a busy loop
190     while (currentTime < abstime) {
191         currentTime.stamp();
192     }
193
194     return true;
195
196 #else
197
198     SGTimeStamp currentTime;
199     currentTime.stamp();
200     if (abstime <= currentTime)
201         return true;
202
203     SGTimeStamp abstimeForSleep = abstime - SGTimeStamp::fromSecMSec(0, 2);
204     for (;abstimeForSleep < currentTime;) {
205         SGTimeStamp timeDiff = abstimeForSleep - currentTime;
206         if (timeDiff < SGTimeStamp::fromSecUSec(0, 1))
207             break;
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);
214             truncated = true;
215         }
216         int ret = usleep(static_cast<int>(timeDiff.toUSecs()));
217         if (-1 == ret && errno != EINTR)
218             return false;
219         if (ret == 0 && !truncated)
220             break;
221
222         currentTime.stamp();
223     }
224
225     // Follow by a busy loop
226     while (currentTime < abstime) {
227         currentTime.stamp();
228     }
229
230     return true;
231
232 #endif
233 }
234
235 bool SGTimeStamp::sleepFor(const SGTimeStamp& reltime)
236 {
237 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
238     struct timespec ts;
239     ts.tv_sec = reltime._sec;
240     ts.tv_nsec = reltime._nsec;
241     for (;;) {
242         struct timespec rem;
243         int ret = clock_nanosleep(getClockId(), 0, &ts, &rem);
244         if (-1 == ret && errno != EINTR)
245             return false;
246         if (ret == 0)
247             break;
248         // Use the remainder for the next cycle.
249         ts = rem;
250     }
251     return true;
252 #elif defined _WIN32
253     if (reltime < SGTimeStamp::fromSecMSec(0, 1))
254         return true;
255     // Don't know, but may be win32 has something better today??
256     Sleep(static_cast<DWORD>(reltime.toMSecs()));
257     return true;
258 #else
259     SGTimeStamp abstime;
260     abstime.stamp();
261     abstime += reltime;
262
263     SGTimeStamp currentTime;
264     currentTime.stamp();
265     for (;abstime < currentTime;) {
266         SGTimeStamp timeDiff = abstime - currentTime;
267         if (timeDiff < SGTimeStamp::fromSecUSec(0, 1))
268             break;
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);
275             truncated = true;
276         }
277         int ret = usleep(static_cast<int>(timeDiff.toUSecs()));
278         if (-1 == ret && errno != EINTR)
279             return false;
280         if (ret == 0 && !truncated)
281             break;
282
283         currentTime.stamp();
284     }
285
286     return true;
287 #endif
288 }
289
290 int SGTimeStamp::elapsedMSec() const
291 {
292     SGTimeStamp now;
293     now.stamp();
294     
295     return static_cast<int>((now - *this).toMSecs());
296 }