diff options
author | Dmitri Tikhonov <dtikhonov@live.com> | 2017-10-30 08:12:41 -0400 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2017-10-30 15:27:46 +0100 |
commit | d531f33ba2210ef11d0849bc73654e03affd0cfa (patch) | |
tree | 70360646ecedccbf1a5579bbe7711152c746fbc6 /lib/timeval.c | |
parent | e240a546a7ac2fa7956adb664b8c40c4dee4f82b (diff) | |
download | curl-d531f33ba2210ef11d0849bc73654e03affd0cfa.tar.gz |
timeval: use mach time on MacOS
If clock_gettime() is not supported, use mach_absolute_time() on MacOS.
closes #2033
Diffstat (limited to 'lib/timeval.c')
-rw-r--r-- | lib/timeval.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/timeval.c b/lib/timeval.c index 4f630bafc..66f923a8e 100644 --- a/lib/timeval.c +++ b/lib/timeval.c @@ -84,6 +84,37 @@ struct curltime Curl_now(void) return cnow; } +#elif defined(HAVE_MACH_ABSOLUTE_TIME) + +#include <stdint.h> +#include <mach/mach_time.h> + +struct curltime Curl_now(void) +{ + /* + ** Monotonic timer on Mac OS is provided by mach_absolute_time(), which + ** returns time in Mach "absolute time units," which are platform-dependent. + ** To convert to nanoseconds, one must use conversion factors specified by + ** mach_timebase_info(). + */ + static mach_timebase_info_data_t timebase; + struct curltime cnow; + uint64_t usecs; + + if(0 == timebase.denom) + (void) mach_timebase_info(&timebase); + + usecs = mach_absolute_time(); + usecs *= timebase.numer; + usecs /= timebase.denom; + usecs /= 1000; + + cnow.tv_sec = usecs / 1000000; + cnow.tv_usec = usecs % 1000000; + + return cnow; +} + #elif defined(HAVE_GETTIMEOFDAY) struct curltime Curl_now(void) |