From: Martin Ling Date: Sun, 23 Sep 2018 16:43:46 +0000 (+0100) Subject: Use mach_absolute_time() on OSX without clock_gettime(). X-Git-Tag: libserialport-0.1.2~82 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=192e77492a2a53a89a6cb9f34f3aed634ffd6956;p=libserialport.git Use mach_absolute_time() on OSX without clock_gettime(). This should fix #759 for OSX versions below 10.12. --- diff --git a/libserialport_internal.h b/libserialport_internal.h index ec15f58..dd69733 100644 --- a/libserialport_internal.h +++ b/libserialport_internal.h @@ -62,6 +62,7 @@ #include #include #include +#include #endif #ifdef __linux__ #include diff --git a/serialport.c b/serialport.c index 15ca1fd..6e68153 100644 --- a/serialport.c +++ b/serialport.c @@ -64,6 +64,13 @@ static void get_time(struct timeval *time) clock_gettime(CLOCK_MONOTONIC, &ts); time->tv_sec = ts.tv_sec; time->tv_usec = ts.tv_nsec / 1000; +#elif defined(__APPLE__) + mach_timebase_info_data_t info; + mach_timebase_info(&info); + uint64_t ticks = mach_absolute_time(); + uint64_t ns = (ticks * info.numer) / info.denom; + time->tv_sec = ns / 1000000000; + time->tv_usec = (ns % 1000000000) / 1000; #else gettimeofday(time, NULL); #endif