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 <eb@ilities.com>
This commit is contained in:
parent
ed9406f642
commit
35fcc013af
1 changed files with 15 additions and 2 deletions
|
@ -13,21 +13,29 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12
|
||||||
#include <mach/mach_time.h>
|
#include <mach/mach_time.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "dds/ddsrt/time.h"
|
#include "dds/ddsrt/time.h"
|
||||||
|
|
||||||
dds_time_t dds_time(void)
|
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;
|
struct timeval tv;
|
||||||
|
|
||||||
(void)gettimeofday(&tv, NULL);
|
(void)gettimeofday(&tv, NULL);
|
||||||
|
|
||||||
return ((tv.tv_sec * DDS_NSECS_IN_SEC) + (tv.tv_usec * DDS_NSECS_IN_USEC));
|
return ((tv.tv_sec * DDS_NSECS_IN_SEC) + (tv.tv_usec * DDS_NSECS_IN_USEC));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
dds_time_t ddsrt_time_monotonic(void)
|
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;
|
static mach_timebase_info_data_t timeInfo;
|
||||||
uint64_t mt;
|
uint64_t mt;
|
||||||
|
|
||||||
|
@ -49,10 +57,15 @@ dds_time_t ddsrt_time_monotonic(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (dds_time_t)(mt * timeInfo.numer / timeInfo.denom);
|
return (dds_time_t)(mt * timeInfo.numer / timeInfo.denom);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
dds_time_t ddsrt_time_elapsed(void)
|
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. */
|
/* Elapsed time clock not (yet) supported on this platform. */
|
||||||
return ddsrt_time_monotonic();
|
return ddsrt_time_monotonic();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue