uloop: add a clock_gettime() implementation for mac os x
authorFelix Fietkau <nbd@openwrt.org>
Sun, 6 Jan 2013 01:42:58 +0000 (02:42 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 6 Jan 2013 01:42:58 +0000 (02:42 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
utils.c
utils.h

diff --git a/utils.c b/utils.c
index 4589da856935cf44d7950468fc07811dd6098ee2..078a8a191af27a7ccd862578b1306658d059cb76 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -52,3 +52,43 @@ void *__calloc_a(size_t len, ...)
 
        return ret;
 }
+
+#ifdef __APPLE__
+#include <mach/mach_time.h>
+
+static void clock_gettime_realtime(struct timespec *tv)
+{
+       struct timeval _tv;
+
+       gettimeofday(&_tv, NULL);
+       tv->tv_sec = _tv.tv_sec;
+       tv->tv_nsec = _tv.tv_usec * 1000;
+}
+
+static void clock_gettime_monotonic(struct timespec *tv)
+{
+       mach_timebase_info_data_t info;
+       float sec;
+       uint64_t val;
+
+       mach_timebase_info(&info);
+
+       val = mach_absolute_time();
+       tv->tv_nsec = (val * info.numer / info.denom) % 1000000000;
+
+       sec = val;
+       sec *= info.numer;
+       sec /= info.denom;
+       sec /= 1000000000;
+       tv->tv_sec = sec;
+}
+
+void clock_gettime(int type, struct timespec *tv)
+{
+       if (type == CLOCK_REALTIME)
+               return clock_gettime_realtime(tv);
+       else
+               return clock_gettime_monotonic(tv);
+}
+
+#endif
diff --git a/utils.h b/utils.h
index a86894aa57b1ec25935771dbe828c5a11837a264..41f39ec690f25f408bcb92c8098b55628c22291e 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -20,6 +20,8 @@
 #define __LIBUBOX_UTILS_H
 
 #include <sys/types.h>
+#include <sys/time.h>
+#include <time.h>
 
 /*
  * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
@@ -52,4 +54,13 @@ extern int __BUILD_BUG_ON_CONDITION_FAILED;
 #define BUILD_BUG_ON __BUILD_BUG_ON
 #endif
 
+#ifdef __APPLE__
+
+#define CLOCK_REALTIME 0
+#define CLOCK_MONOTONIC        1
+
+void clock_gettime(int type, struct timespec *tv);
+
+#endif
+
 #endif