From fc3982fa1e3e8972f8f49e2e35eef82e02f1deaf Mon Sep 17 00:00:00 2001 From: Aaron Burghardt Date: Thu, 30 Jul 2015 15:31:50 -0400 Subject: Add a static clock_gettime() substitute for OS X. --- src/utils.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src') diff --git a/src/utils.c b/src/utils.c index ceb65e1..67860b3 100644 --- a/src/utils.c +++ b/src/utils.c @@ -30,6 +30,9 @@ #include #include #include +#ifdef __APPLE__ +#include +#endif #include "utils.h" @@ -299,6 +302,36 @@ int plist_write_to_filename(plist_t plist, const char *filename, enum plist_form return 1; } +#ifdef __APPLE__ +typedef int clockid_t; +#define CLOCK_MONOTONIC 1 + +static int clock_gettime(clockid_t clk_id, struct timespec *ts) +{ + // See http://developer.apple.com/library/mac/qa/qa1398 + + uint64_t mach_time, nano_sec; + + static mach_timebase_info_data_t base_info; + + mach_time = mach_absolute_time(); + + if (base_info.denom == 0) { + (void) mach_timebase_info(&base_info); + } + + if (base_info.numer == 1 && base_info.denom == 1) + nano_sec = mach_time; + else + nano_sec = mach_time * base_info.numer / base_info.denom; + + ts->tv_sec = nano_sec / 1000000000; + ts->tv_nsec = nano_sec % 1000000000; + + return 0; +} +#endif + void get_tick_count(struct timeval * tv) { struct timespec ts; -- cgit v1.1-32-gdbae