Skip to content

Commit

Permalink
darwin: simplify uv_hrtime
Browse files Browse the repository at this point in the history
mach_continuous_time is available since macOS 10.12, but our minimum
version is 11, so no need for a workaround.

Also, prefer that to `clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW)` which
the documentation suggests
(https://developer.apple.com/documentation/driverkit/3438077-mach_continuous_time)
since the latter calls mach_timebase_info every time, unnecessarify: https://github.com/apple-open-source/macos/blob/49dcc07a40d19fa97384033a8398dae5d00d11a1/Libc/gen/clock_gettime.c#L107
  • Loading branch information
saghul committed Apr 15, 2024
1 parent f50ae53 commit c0a61c3
Showing 1 changed file with 1 addition and 7 deletions.
8 changes: 1 addition & 7 deletions src/unix/darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <stdint.h>
#include <errno.h>

#include <dlfcn.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <mach-o/dyld.h> /* _NSGetExecutablePath */
Expand All @@ -34,7 +33,6 @@
#include <unistd.h> /* sysconf */

static uv_once_t once = UV_ONCE_INIT;
static uint64_t (*time_func)(void);
static mach_timebase_info_data_t timebase;


Expand All @@ -56,16 +54,12 @@ void uv__platform_loop_delete(uv_loop_t* loop) {
static void uv__hrtime_init_once(void) {
if (KERN_SUCCESS != mach_timebase_info(&timebase))
abort();

time_func = (uint64_t (*)(void)) dlsym(RTLD_DEFAULT, "mach_continuous_time");
if (time_func == NULL)
time_func = mach_absolute_time;
}


uint64_t uv__hrtime(uv_clocktype_t type) {
uv_once(&once, uv__hrtime_init_once);
return time_func() * timebase.numer / timebase.denom;
return mach_continuous_time() * timebase.numer / timebase.denom;
}


Expand Down

0 comments on commit c0a61c3

Please sign in to comment.