about summary refs log tree commit diff
path: root/evloop
diff options
context:
space:
mode:
Diffstat (limited to 'evloop')
-rw-r--r--evloop/evloop.h1
-rw-r--r--evloop/timer.c39
2 files changed, 34 insertions, 6 deletions
diff --git a/evloop/evloop.h b/evloop/evloop.h
index 55589aa..44f8803 100644
--- a/evloop/evloop.h
+++ b/evloop/evloop.h
@@ -53,5 +53,6 @@ void evtimer_del(struct timeout *);
 
 void evtimers_run(void);
 struct timespec evtimer_delay(void);
+int evtimer_gettime(struct timespec *);
 
 #endif
diff --git a/evloop/timer.c b/evloop/timer.c
index 3174c08..d115959 100644
--- a/evloop/timer.c
+++ b/evloop/timer.c
@@ -3,12 +3,39 @@
 #include "evloop.h"
 #include "timeheap.h"
 
-#if defined(CLOCK_MONOTONIC_FAST)
+#if defined(HAVE_CLOCK_MONOTONIC)
+
+#ifdef CLOCK_MONOTONIC_FAST
 #define TIMER_CLOCK CLOCK_MONOTONIC_FAST
-#elif defined(CLOCK_MONOTONIC)
+#else
 #define TIMER_CLOCK CLOCK_MONOTONIC
+#endif
+
+int
+evtimer_gettime(struct timespec *ts)
+{
+    return clock_gettime(TIMER_CLOCK, ts);
+}
+
+#elif defined(HAVE_MACH_ABSOLUTE_TIME)
+
+#include <mach/mach_time.h>
+
+int
+evtimer_gettime(struct timespec *ts)
+{
+    uint64_t nsecs;
+    static mach_timebase_info_data_t nsratio = { 0, 0 };
+    if (nsratio.denom == 0)
+        mach_timebase_info(&nsratio);
+    nsecs = mach_absolute_time() * nsratio.numer / nsratio.denom;
+    ts->tv_sec = nsecs / 1000000000;
+    ts->tv_nsec = nsecs - ts->tv_sec * 1000000000;
+    return 0;
+}
+
 #else
-#error CLOCK_MONOTONIC needed!
+#error No supported time mechanism
 #endif
 
 static struct timespec
@@ -50,7 +77,7 @@ int
 evtimer_add(struct timeout *h, struct timespec *t)
 {
     struct timespec now, sum;
-    clock_gettime(TIMER_CLOCK, &now);
+    evtimer_gettime(&now);
     sum = addtime(now, *t);
     if (h->th.i == -1)
         return timeheap_insert(&h->th, &sum);
@@ -73,7 +100,7 @@ void
 evtimers_run(void)
 {
     struct timespec now;
-    clock_gettime(TIMER_CLOCK, &now);
+    evtimer_gettime(&now);
     while (timeheap_size() > 0) {
         struct timespec diff = subtime(timeheap_top(), now);
         if (diff.tv_sec < 0) {
@@ -93,7 +120,7 @@ evtimer_delay(void)
         diff.tv_sec = -1;
         diff.tv_nsec = 0;
     } else {
-        clock_gettime(TIMER_CLOCK, &now);
+        evtimer_gettime(&now);
         diff = subtime(timeheap_top(), now);
         if (diff.tv_sec < 0) {
             diff.tv_sec = 0;