From 35fcc013afc8fbc1aaf6e865e6068f14c0404012 Mon Sep 17 00:00:00 2001 From: Erik Boasson Date: Sun, 12 May 2019 09:54:07 +0200 Subject: [PATCH] use clock_gettime on macOS >= 10.12 Since macOS 10.12, the POSIX clock_gettime interface with various different clocks is supported on macOS, so use those when building for 10.12 or newer. Signed-off-by: Erik Boasson --- src/ddsrt/src/time/darwin/time.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ddsrt/src/time/darwin/time.c b/src/ddsrt/src/time/darwin/time.c index a8a35c4..71e2ccb 100644 --- a/src/ddsrt/src/time/darwin/time.c +++ b/src/ddsrt/src/time/darwin/time.c @@ -13,21 +13,29 @@ #include #include #include + +#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12 #include +#endif #include "dds/ddsrt/time.h" dds_time_t dds_time(void) { +#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 + return (int64_t) clock_gettime_nsec_np (CLOCK_REALTIME); +#else struct timeval tv; - (void)gettimeofday(&tv, NULL); - return ((tv.tv_sec * DDS_NSECS_IN_SEC) + (tv.tv_usec * DDS_NSECS_IN_USEC)); +#endif } dds_time_t ddsrt_time_monotonic(void) { +#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 + return (int64_t) clock_gettime_nsec_np (CLOCK_UPTIME_RAW); +#else static mach_timebase_info_data_t timeInfo; uint64_t mt; @@ -49,10 +57,15 @@ dds_time_t ddsrt_time_monotonic(void) } return (dds_time_t)(mt * timeInfo.numer / timeInfo.denom); +#endif } dds_time_t ddsrt_time_elapsed(void) { +#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 + return (int64_t) clock_gettime_nsec_np (CLOCK_MONOTONIC_RAW); +#else /* Elapsed time clock not (yet) supported on this platform. */ return ddsrt_time_monotonic(); +#endif }