]> granicus.if.org Git - curl/commitdiff
timeval: use mach time on MacOS
authorDmitri Tikhonov <dtikhonov@live.com>
Mon, 30 Oct 2017 12:12:41 +0000 (08:12 -0400)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 30 Oct 2017 14:27:46 +0000 (15:27 +0100)
If clock_gettime() is not supported, use mach_absolute_time() on MacOS.

closes #2033

CMakeLists.txt
configure.ac
lib/curl_config.h.cmake
lib/timeval.c

index c41759aa1e6dd69aa300a2ebd42ebb180d4e0d2f..3aaeb346e702b9f704c7249e778f943ee074a606 100644 (file)
@@ -878,6 +878,7 @@ check_symbol_exists(setrlimit      "${CURL_INCLUDES}" HAVE_SETRLIMIT)
 check_symbol_exists(fcntl          "${CURL_INCLUDES}" HAVE_FCNTL)
 check_symbol_exists(ioctl          "${CURL_INCLUDES}" HAVE_IOCTL)
 check_symbol_exists(setsockopt     "${CURL_INCLUDES}" HAVE_SETSOCKOPT)
+check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME)
 
 # symbol exists in win32, but function does not.
 if(WIN32)
index 68b3a071a5393b3bed1171e8ad72c72f62c0c675..5272feaa079d8dd1a1ca57951f0c5f7ee55e1d6a 100755 (executable)
@@ -3385,6 +3385,7 @@ AC_CHECK_FUNCS([geteuid \
   getrlimit \
   gettimeofday \
   if_nametoindex \
+  mach_absolute_time \
   pipe \
   setlocale \
   setmode \
index c80484f6521c8337f7c389df44d9166d03f24e08..e4d14c784bede6ab360eb16b96516b09664bda43 100644 (file)
 
 /* the signed version of size_t */
 #cmakedefine ssize_t ${ssize_t}
+
+/* Define to 1 if you have the mach_absolute_time function. */
+#cmakedefine HAVE_MACH_ABSOLUTE_TIME 1
index 4f630bafcad92ee7f57b4237f4ba141537993e94..66f923a8eed07546bd0fbdbb059a4e5652d4967a 100644 (file)
@@ -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)