diff options
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | openbsd-compat/clock_gettime.c | 30 |
2 files changed, 29 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac index 14692deb..4253324a 100644 --- a/configure.ac +++ b/configure.ac @@ -130,6 +130,7 @@ AC_CHECK_HEADERS( \ dirent.h \ fcntl.h \ ndir.h \ + mach/mach_time.h \ netdb.h \ pam/pam_appl.h \ security/pam_appl.h \ 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 |