Skip to content

Commit

Permalink
macOS/iOS: use clock_gettime_nsec_np(CLOCK_UPTIME_RAW) instead of mac…
Browse files Browse the repository at this point in the history
…h_absolute_time() per Apple's recommendation.

Move has_monotonic_time variable into the final variant of platform-specific time code (used by Linux/Unix).
  • Loading branch information
andygrundman committed Oct 20, 2024
1 parent c1a2873 commit 583754f
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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__)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 583754f

Please sign in to comment.