diff options
author | Hasso Tepper <hasso.tepper@gmail.com> | 2012-10-11 11:31:54 +0000 |
---|---|---|
committer | David Lamparter <equinox@opensourcerouting.org> | 2012-11-05 15:16:53 -0500 |
commit | 3b96b78136d04ddb7e39d86577cad75acb25237a (patch) | |
tree | a056f89d771aa6706816b0b8507fc4b44028d865 | |
parent | 43057bf22a4240b5e163c30a1f66deb702ce746e (diff) |
lib: Implement monotonically increasing clock for Darwin.
There is no Posix CLOCK_MONOTONIC in Darwin, but monotonically
increasing clock can be implemented using mach_absolute_time().
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
-rw-r--r-- | lib/thread.c | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/lib/thread.c b/lib/thread.c index 6341dfd7..16c92c24 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -39,6 +39,11 @@ extern int agentx_enabled; #endif +#if defined(__APPLE__) +#include <mach/mach.h> +#include <mach/mach_time.h> +#endif + /* Recent absolute time of day */ struct timeval recent_time; @@ -103,7 +108,7 @@ timeval_elapsed (struct timeval a, struct timeval b) + (a.tv_usec - b.tv_usec)); } -#ifndef HAVE_CLOCK_MONOTONIC +#if !defined(HAVE_CLOCK_MONOTONIC) && !defined(__APPLE__) static void quagga_gettimeofday_relative_adjust (void) { @@ -122,7 +127,7 @@ quagga_gettimeofday_relative_adjust (void) } last_recent_time = recent_time; } -#endif /* !HAVE_CLOCK_MONOTONIC */ +#endif /* !HAVE_CLOCK_MONOTONIC && !__APPLE__ */ /* gettimeofday wrapper, to keep recent_time updated */ static int @@ -162,7 +167,23 @@ quagga_get_relative (struct timeval *tv) relative_time.tv_usec = tp.tv_nsec / 1000; } } -#else /* !HAVE_CLOCK_MONOTONIC */ +#elif defined(__APPLE__) + { + uint64_t ticks; + uint64_t useconds; + static mach_timebase_info_data_t timebase_info; + + ticks = mach_absolute_time(); + if (timebase_info.denom == 0) + mach_timebase_info(&timebase_info); + + useconds = ticks * timebase_info.numer / timebase_info.denom / 1000; + relative_time.tv_sec = useconds / 1000000; + relative_time.tv_usec = useconds % 1000000; + + return 0; + } +#else /* !HAVE_CLOCK_MONOTONIC && !__APPLE__ */ if (!(ret = quagga_gettimeofday (&recent_time))) quagga_gettimeofday_relative_adjust(); #endif /* HAVE_CLOCK_MONOTONIC */ |