[ovs-dev] [PATCH 2/5] timeval: Add clock_gettime() for OS X

Lance Richardson lrichard at redhat.com
Mon Mar 14 14:30:16 UTC 2016


OS X does not implement clock_gettime().

Signed-off-by: Lance Richardson <lrichard at redhat.com>
---
 lib/timeval.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/lib/timeval.c b/lib/timeval.c
index d390df1..8190b87 100644
--- a/lib/timeval.c
+++ b/lib/timeval.c
@@ -41,8 +41,9 @@
 
 VLOG_DEFINE_THIS_MODULE(timeval);
 
-#ifdef _WIN32
+#if defined(_WIN32) || defined(__MACH__)
 typedef unsigned int clockid_t;
+static int clock_gettime(clock_t id, struct timespec *ts);
 
 #ifndef CLOCK_MONOTONIC
 #define CLOCK_MONOTONIC 1
@@ -51,7 +52,9 @@ typedef unsigned int clockid_t;
 #ifndef CLOCK_REALTIME
 #define CLOCK_REALTIME 2
 #endif
+#endif /* defined(_WIN32) || defined(__MACH__) */
 
+#ifdef _WIN32
 /* Number of 100 ns intervals from January 1, 1601 till January 1, 1970. */
 const static unsigned long long unix_epoch = 116444736000000000;
 #endif /* _WIN32 */
@@ -417,6 +420,34 @@ clock_gettime(clock_t id, struct timespec *ts)
 }
 #endif /* _WIN32 */
 
+#ifdef __MACH__
+#include <mach/clock.h>
+#include <mach/mach.h>
+static int
+clock_gettime(clock_t id, struct timespec *ts)
+{
+    mach_timespec_t mts;
+    clock_serv_t clk;
+    clock_id_t cid;
+
+    if (id == CLOCK_MONOTONIC) {
+        cid = SYSTEM_CLOCK;
+    } else if (id == CLOCK_REALTIME) {
+        cid = CALENDAR_CLOCK;
+    } else {
+        return -1;
+    }
+
+    host_get_clock_service(mach_host_self(), cid, &clk);
+    clock_get_time(clk, &mts);
+    mach_port_deallocate(mach_task_self(), clk);
+    ts->tv_sec = mts.tv_sec;
+    ts->tv_nsec = mts.tv_nsec;
+
+    return 0;
+}
+#endif
+
 void
 xgettimeofday(struct timeval *tv)
 {
-- 
2.5.0




More information about the dev mailing list