1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 03:34:53 +08:00

Use macOS clock_gettime in timespec_now if it is available. Reduces CPU usage.

This commit is contained in:
Aaron Jackson 2022-06-15 10:21:46 +12:00
parent 69e0372cf0
commit aca89433a3

View File

@ -82,14 +82,20 @@ timespec
MillisecondTimer::timespec_now ()
{
timespec time;
# ifdef __MACH__ // 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(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time.tv_sec = mts.tv_sec;
time.tv_nsec = mts.tv_nsec;
// OS X does not have clock_gettime until 10.12, use clock_get_time
#if defined(__MACH__) && MAC_OS_X_VERSION_MIN_REQUIRED < 101200
if (__builtin_available(macOS 10.12, *)) {
clock_gettime(CLOCK_MONOTONIC, &time);
}
else {
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time.tv_sec = mts.tv_sec;
time.tv_nsec = mts.tv_nsec;
}
# else
clock_gettime(CLOCK_MONOTONIC, &time);
# endif