]> git.cworth.org Git - apitrace/blobdiff - common/os_time.hpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / common / os_time.hpp
index a4f52093c94c125e7874a8e77f1597f056c0ab7b..3e4960e74554d0179c96b4582202d52390c61378 100644 (file)
 
 
 #if defined(_WIN32)
-#include <windows.h>
-#elif defined(__linux__)
-#include <time.h>
+#  include <windows.h>
 #else
-#include <sys/time.h>
+#  if defined(__linux__)
+#    include <time.h>
+#  elif defined(__APPLE__)
+#    include <mach/mach_time.h>
+#  else
+#    include <sys/time.h>
+#  endif
+#  include <unistd.h>
 #endif
 
 
 namespace os {
 
     // OS dependent time frequency
-#if defined(_WIN32)
-    // runtime variable on Windows
+#if defined(_WIN32) || defined(__APPLE__)
+    // runtime variable on Windows and MacOSX
     extern long long timeFrequency;
 #elif defined(__linux__)
-    // nanoseconds
-    static const long long timeFrequency = 1000000000;
+    // nanoseconds on Linux
+    static const long long timeFrequency = 1000000000LL;
 #else
-    // microseconds on
-    static const long long timeFrequency = 1000000;
+    // microseconds on Unices
+    static const long long timeFrequency = 1000000LL;
 #endif
 
     // Time from an unknown base in a unit determined by timeFrequency
@@ -68,10 +73,17 @@ namespace os {
         return counter.QuadPart;
 #elif defined(__linux__)
         struct timespec tp;
-        if (clock_gettime(CLOCK_REALTIME, &tp) == -1) {
+        if (clock_gettime(CLOCK_MONOTONIC, &tp) == -1) {
             return 0;
         }
         return tp.tv_sec * 1000000000LL + tp.tv_nsec;
+#elif defined(__APPLE__)
+        if (!timeFrequency) {
+            mach_timebase_info_data_t timebaseInfo;
+            mach_timebase_info(&timebaseInfo);
+            timeFrequency = 1000000000LL * timebaseInfo.denom / timebaseInfo.numer;
+        }
+        return mach_absolute_time();
 #else
         struct timeval tv;
         gettimeofday(&tv, NULL);
@@ -79,6 +91,15 @@ namespace os {
 #endif
     }
 
+    // Suspend execution
+    inline void
+    sleep(unsigned long usecs) {
+#if defined(_WIN32)
+        Sleep((usecs + 999) / 1000);
+#else
+        usleep(usecs);
+#endif
+    }
 
 } /* namespace os */