]> git.mxchange.org Git - simgear.git/blobdiff - simgear/timing/timestamp.cxx
Trying to make old gcc on Jenkins happy.
[simgear.git] / simgear / timing / timestamp.cxx
index a2eae9eef6b2b02af58cee940cd71e072526bc12..24cad841a9995b941de601604e2af122dd249d05 100644 (file)
@@ -31,6 +31,7 @@
 #include <simgear/compiler.h>
 
 #include <ctime>
+#include <cerrno>
 
 #ifdef HAVE_SYS_TIMEB_H
 #  include <sys/timeb.h> // for ftime() and struct timeb
@@ -44,7 +45,6 @@
 
 #if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
 #  include <time.h>
-#  include <errno.h>
 #endif
 
 #ifdef WIN32
@@ -139,7 +139,9 @@ void SGTimeStamp::stamp() {
 // the timer tick) accuracy which is too bad to catch 60Hz...
 bool SGTimeStamp::sleepUntil(const SGTimeStamp& abstime)
 {
-#if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
+    // FreeBSD is missing clock_nanosleep, see
+    // https://wiki.freebsd.org/FreeBSD_and_Standards
+#if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS) && !defined(__FreeBSD__)
     SGTimeStamp abstimeForSleep = abstime;
 
     // Always undersleep by resolution of the clock
@@ -234,7 +236,8 @@ bool SGTimeStamp::sleepUntil(const SGTimeStamp& abstime)
 
 bool SGTimeStamp::sleepFor(const SGTimeStamp& reltime)
 {
-#if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS)
+    // see comment above regarding FreeBSD
+#if defined(_POSIX_TIMERS) && (0 < _POSIX_TIMERS) && !defined(__FreeBSD__)
     struct timespec ts;
     ts.tv_sec = reltime._sec;
     ts.tv_nsec = reltime._nsec;
@@ -255,6 +258,24 @@ bool SGTimeStamp::sleepFor(const SGTimeStamp& reltime)
     // Don't know, but may be win32 has something better today??
     Sleep(static_cast<DWORD>(reltime.toMSecs()));
     return true;
+#elif defined __APPLE__
+    // the generic version below behaves badly on Mac; use nanosleep directly,
+    // similar to the POSIX timers version
+    struct timespec ts;
+    ts.tv_sec = reltime._sec;
+    ts.tv_nsec = reltime._nsec;
+    
+    for (;;) {
+        struct timespec rem;
+        int ret = nanosleep(&ts, &rem);
+        if (-1 == ret && errno != EINTR)
+            return false;
+        if (ret == 0)
+            break;
+        // Use the remainder for the next cycle.
+        ts = rem;
+    }
+    return true;
 #else
     SGTimeStamp abstime;
     abstime.stamp();