]> sigrok.org Git - libserialport.git/commitdiff
Use mach_absolute_time() on OSX without clock_gettime().
authorMartin Ling <redacted>
Sun, 23 Sep 2018 16:43:46 +0000 (17:43 +0100)
committerMartin Ling <redacted>
Sat, 28 Dec 2019 21:02:19 +0000 (22:02 +0100)
This should fix #759 for OSX versions below 10.12.

libserialport_internal.h
serialport.c

index ec15f58fecd359625e11727ce32507009bd2e559..dd69733e9f38a0b3b76764d61890fd7dec3c513f 100644 (file)
@@ -62,6 +62,7 @@
 #include <IOKit/serial/IOSerialKeys.h>
 #include <IOKit/serial/ioss.h>
 #include <sys/syslimits.h>
+#include <mach/mach_time.h>
 #endif
 #ifdef __linux__
 #include <dirent.h>
index 15ca1fda35bc9e85f4404c18109c4cd7c9c10beb..6e681533ed24896a33de59f7e17597a4f0bf5082 100644 (file)
@@ -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