From bd06466d3af4f6a645ff264720eda5137090efbe Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 12 Dec 2023 13:28:07 +0100 Subject: [PATCH] Improve clock_gettime usage - correctly use value of _POSIX_MONOTONIC_CLOCK - drop special path for macOS: it supports clock_gettime since macOS 10.12 --- src/porting.h | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/porting.h b/src/porting.h index 6657d7bba..bf01c3e4d 100644 --- a/src/porting.h +++ b/src/porting.h @@ -89,11 +89,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef _WIN32 // POSIX #include #include - - #if defined(__MACH__) && defined(__APPLE__) - #include - #include - #endif #endif namespace porting @@ -183,21 +178,16 @@ inline u64 getTimeNs() { return os_get_time(1000*1000*1000); } inline void os_get_clock(struct timespec *ts) { -#if defined(__MACH__) && defined(__APPLE__) -// From http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x -// OS X does not have clock_gettime, use clock_get_time - clock_serv_t cclock; - mach_timespec_t mts; - host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); - clock_get_time(cclock, &mts); - mach_port_deallocate(mach_task_self(), cclock); - ts->tv_sec = mts.tv_sec; - ts->tv_nsec = mts.tv_nsec; -#elif defined(CLOCK_MONOTONIC_RAW) +#if defined(CLOCK_MONOTONIC_RAW) clock_gettime(CLOCK_MONOTONIC_RAW, ts); -#elif defined(_POSIX_MONOTONIC_CLOCK) +#elif defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK > 0 clock_gettime(CLOCK_MONOTONIC, ts); #else +# if defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK == 0 + // zero means it might be supported at runtime + if (clock_gettime(CLOCK_MONOTONIC, ts) == 0) + return; +# endif struct timeval tv; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, ts);