diff mbox

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

Message ID 1458057179-27633-3-git-send-email-lrichard@redhat.com
State Accepted
Headers show

Commit Message

Lance Richardson March 15, 2016, 3:52 p.m. UTC
OS X does not implement clock_gettime(), implement replacement.

Signed-off-by: Lance Richardson <lrichard@redhat.com>
---
Changes from v1: None

 lib/timeval.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

Comments

Ben Pfaff March 23, 2016, 1:41 a.m. UTC | #1
On Tue, Mar 15, 2016 at 11:52:56AM -0400, Lance Richardson wrote:
> OS X does not implement clock_gettime(), implement replacement.
> 
> Signed-off-by: Lance Richardson <lrichard@redhat.com>

Applied, thanks!
diff mbox

Patch

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)
 {