From c24343f107d1f3b77bd402442d2f363a6612d0b3 Mon Sep 17 00:00:00 2001 From: James Hilliard Date: Mon, 4 Jul 2022 03:50:32 -0600 Subject: [PATCH] util: use correct clock on OSX CLOCK_MONOTONIC_RAW on OSX is not the same as CLOCK_MONOTONIC_RAW on linux --- mongoose.c | 4 ++++ src/util.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/mongoose.c b/mongoose.c index dadab9ab..15b1102c 100644 --- a/mongoose.c +++ b/mongoose.c @@ -5488,6 +5488,10 @@ uint64_t mg_millis(void) { return xTaskGetTickCount() * portTICK_PERIOD_MS; #elif MG_ARCH == MG_ARCH_AZURERTOS return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND); +#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__) + // Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux + // Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux + return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000; #elif MG_ARCH == MG_ARCH_UNIX struct timespec ts = {0, 0}; // See #1615 - prefer monotonic clock diff --git a/src/util.c b/src/util.c index 06b3fd7c..af741fde 100644 --- a/src/util.c +++ b/src/util.c @@ -102,6 +102,10 @@ uint64_t mg_millis(void) { return xTaskGetTickCount() * portTICK_PERIOD_MS; #elif MG_ARCH == MG_ARCH_AZURERTOS return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND); +#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__) + // Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux + // Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux + return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000; #elif MG_ARCH == MG_ARCH_UNIX struct timespec ts = {0, 0}; // See #1615 - prefer monotonic clock