Use monotonic time function for darwin (#5865)
authorPaolo Capriotti <p.capriotti@gmail.com>
Sat, 24 Mar 2012 02:19:43 +0000 (02:19 +0000)
committerPaolo Capriotti <p.capriotti@gmail.com>
Mon, 16 Apr 2012 14:18:19 +0000 (15:18 +0100)
rts/posix/Clock.h
rts/posix/GetTime.c

index 5062023..2c71d7a 100644 (file)
@@ -27,6 +27,9 @@
 # else
 #  define CLOCK_ID CLOCK_REALTIME
 # endif
+#elif defined(darwin_HOST_OS)
+# include <mach/mach.h>
+# include <mach/mach_time.h>
 #endif
 
 #endif /* POSIX_CLOCK_H */
index 4abc82f..549b3b0 100644 (file)
@@ -71,6 +71,17 @@ Time getProcessElapsedTime(void)
 
     clock_gettime(CLOCK_ID, &ts);
     return SecondsToTime(ts.tv_sec) + NSToTime(ts.tv_nsec);
+#elif defined(darwin_HOST_OS)
+    uint64_t time = mach_absolute_time();
+    static double scaling_factor = 0.0;
+
+    if (scaling_factor == 0.0) {
+        mach_timebase_info_data_t info;
+        (void) mach_timebase_info(&info);
+        scaling_factor = (double)info.numer / (double)info.denom;
+    }
+
+    return (Time)((double)time * scaling_factor);
 #else
     struct timeval tv;