summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2014-11-27 10:42:21 (GMT)
committer Vincent Sanders <vince@kyllikki.org>2014-11-27 10:42:21 (GMT)
commit93eef32ee77e15527f1401d541e8e7f3fc3ff765 (patch)
tree8111c60a36cf0f74b5e879b2172f085ccc0663bd
parent223480e55ed098cb5f6001f9f49e73b191586e18 (diff)
downloadlibnsutils-93eef32ee77e15527f1401d541e8e7f3fc3ff765.tar.gz
libnsutils-93eef32ee77e15527f1401d541e8e7f3fc3ff765.tar.bz2
Implement monotonic time fetch for MACH systems
-rw-r--r--src/time.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/time.c b/src/time.c
index 7beef48..ecd4f42 100644
--- a/src/time.c
+++ b/src/time.c
@@ -16,10 +16,12 @@
#include <stdlib.h>
#include <unistd.h>
-#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)
+#if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)) || defined(__OpenBSD__)
#include <time.h>
#elif defined(__riscos)
#include <oslib/os.h>
+#elif defined(__MACH__)
+#include <mach/mach_time.h>
#else
#include <sys/time.h>
#endif
@@ -31,7 +33,7 @@ nsuerror nsu_getmonotonic_ms(uint64_t *current_out)
uint64_t current;
static uint64_t prev = 0; /* previous time so we never go backwards */
-#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)
+#if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && (defined _POSIX_MONOTONIC_CLOCK)) || defined(__OpenBSD__)
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
@@ -41,6 +43,15 @@ nsuerror nsu_getmonotonic_ms(uint64_t *current_out)
time = os_read_monotonic_time();
current = time * 10;
+#elif defined(__MACH__)
+ clock_serv_t cclock;
+ mach_timespec_t mts;
+
+ host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
+ clock_get_time(cclock, &mts);
+ mach_port_deallocate(mach_task_self(), cclock);
+
+ current = (mts.tv_sec * 1000) + (mts.tv_nsec / 1000000);
#else
#warning "Using dodgy gettimeofday() fallback"
/** \todo Implement this properly! */