From be19311256e9e5dd69f0146accde0df754f96c6d Mon Sep 17 00:00:00 2001 From: Andy Grundman Date: Sun, 20 Oct 2024 19:19:11 -0400 Subject: [PATCH] macOS/iOS: use clock_gettime_nsec_np(CLOCK_UPTIME_RAW) instead of mach_absolute_time() per Apple's recommendation. Move has_monotonic_time variable into the final variant of platform-specific time code (used by Linux/Unix). --- src/Platform.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Platform.c b/src/Platform.c index e409189..9745b23 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -468,7 +468,6 @@ void PltWaitForConditionVariable(PLT_COND* cond, PLT_MUTEX* mutex) { // These functions return a number of microseconds or milliseconds since an opaque start time. -static bool has_monotonic_time = false; static bool ticks_started = false; #if defined(LC_WINDOWS) @@ -496,25 +495,22 @@ uint64_t PltGetMicroseconds(void) { #elif defined(LC_DARWIN) -static mach_timebase_info_data_t mach_base_info; -static uint64_t start; +static uint64_t start_ns; void PltTicksInit(void) { if (ticks_started) { return; } ticks_started = true; - mach_timebase_info(&mach_base_info); - has_monotonic_time = true; - start = mach_absolute_time(); + start_ns = clock_gettime_nsec_np(CLOCK_UPTIME_RAW); } uint64_t PltGetMicroseconds(void) { if (!ticks_started) { PltTicksInit(); } - const uint64_t now = mach_absolute_time(); - return (((now - start) * mach_base_info.numer) / mach_base_info.denom) / 1000; + const uint64_t now_ns = clock_gettime_nsec_np(CLOCK_UPTIME_RAW); + return (now_ns - start_ns) / 1000; } #elif defined(__vita__) @@ -569,6 +565,7 @@ static struct timespec start_ts; # endif #endif +static bool has_monotonic_time = false; static struct timeval start_tv; void PltTicksInit(void) {