aboutsummaryrefslogtreecommitdiffstats
path: root/openbsd-compat
diff options
context:
space:
mode:
authorCharles Longeau <github@chl.be>2012-10-14 17:12:36 +0200
committerCharles Longeau <github@chl.be>2012-10-14 17:12:36 +0200
commit2421bbcc7466361d5b827f841749d807e6962204 (patch)
tree508e8fde15d14818e51a41277d7e00866d452068 /openbsd-compat
parents/clockid_t/int/ to make it build on Mac OS X (diff)
downloadOpenSMTPD-2421bbcc7466361d5b827f841749d807e6962204.tar.xz
OpenSMTPD-2421bbcc7466361d5b827f841749d807e6962204.zip
for Mac OS X: make clock_gettime(CLOCK_MONOTONIC, ...) be more accurate, by using internal Mach calls, instead of gettimeofday()
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/clock_gettime.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/openbsd-compat/clock_gettime.c b/openbsd-compat/clock_gettime.c
index 09305bfc..6c1ef0d4 100644
--- a/openbsd-compat/clock_gettime.c
+++ b/openbsd-compat/clock_gettime.c
@@ -16,6 +16,9 @@
#include "includes.h"
+#ifdef HAVE_MACH_MACH_TIME_H
+#include <mach/mach_time.h>
+#endif
#include <sys/time.h>
#include <time.h>
@@ -23,11 +26,34 @@
int
clock_gettime(int clock_id, struct timespec *tp)
{
- int ret;
- struct timeval tv;
+ int ret = 0;
+ uint64_t time;
+ mach_timebase_info_data_t info;
+ static double scaling_factor = 0;
+
+#if 0
+ struct timeval tv;
ret = gettimeofday(&tv, NULL);
TIMEVAL_TO_TIMESPEC(&tv, tp);
+#endif
+
+/* based on http://code-factor.blogspot.fr/2009/11/monotonic-timers.html */
+
+ time = mach_absolute_time();
+
+ if (scaling_factor == 0) {
+ ret = (int) mach_timebase_info(&info);
+ if (ret != 0)
+ fatal("mach_timebase_info failed");
+ scaling_factor = info.numer/info.denom;
+ }
+
+ time *= scaling_factor;
+
+ tp->tv_sec = time / 1000000000;
+ tp->tv_nsec = time % 1000000000;
+
return (ret);
}
#endif