]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
timeval: use CLOCK_MONOTONIC_RAW if available
authorDaniel Stenberg <daniel@haxx.se>
Fri, 9 Jun 2023 18:42:51 +0000 (20:42 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Mon, 12 Jun 2023 22:02:32 +0000 (00:02 +0200)
Reported-by: Harry Sintonen
Ref: #11288
Closes #11291

acinclude.m4
configure.ac
lib/timeval.c

index 5dbdd93fef0770b5f9c308e35416185c83d2c164..df2004937ce99e2892d174fcf71857b17550d8e7 100644 (file)
@@ -1072,6 +1072,38 @@ AC_DEFUN([CURL_CHECK_FUNC_CLOCK_GETTIME_MONOTONIC], [
   dnl until library linking and run-time checks for clock_gettime succeed.
 ])
 
+dnl CURL_CHECK_FUNC_CLOCK_GETTIME_MONOTONIC_RAW
+dnl -------------------------------------------------
+dnl Check if monotonic clock_gettime is available.
+
+AC_DEFUN([CURL_CHECK_FUNC_CLOCK_GETTIME_MONOTONIC_RAW], [
+  AC_CHECK_HEADERS(sys/types.h sys/time.h)
+  AC_MSG_CHECKING([for raw monotonic clock_gettime])
+  #
+  if test "x$dontwant_rt" = "xno" ; then
+    AC_COMPILE_IFELSE([
+      AC_LANG_PROGRAM([[
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#include <time.h>
+      ]],[[
+        struct timespec ts;
+        (void)clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
+      ]])
+    ],[
+      AC_MSG_RESULT([yes])
+      AC_DEFINE_UNQUOTED(HAVE_CLOCK_GETTIME_MONOTONIC_RAW, 1,
+        [Define to 1 if you have the clock_gettime function and raw monotonic timer.])
+    ],[
+      AC_MSG_RESULT([no])
+    ])
+  fi
+])
+
 
 dnl CURL_CHECK_LIBS_CLOCK_GETTIME_MONOTONIC
 dnl -------------------------------------------------
index 87fded66e733b436f088976022e92e8062bda1b6..373e2e0cef6862527d323d625347e4cc7c1d9617 100644 (file)
@@ -1339,6 +1339,9 @@ dnl check for additional required libraries.
 dnl **********************************************************************
 CURL_CHECK_LIBS_CLOCK_GETTIME_MONOTONIC
 
+dnl Check for even better option
+CURL_CHECK_FUNC_CLOCK_GETTIME_MONOTONIC_RAW
+
 dnl **********************************************************************
 dnl The preceding library checks are all potentially useful for test
 dnl servers and libtest cases which require networking and clock_gettime
index dca1c6fe50a14ec7ae968d3bc6aed43f885aade7..2de79be46de7a4ee05f3af59cf1f33c1acc3ae13 100644 (file)
@@ -58,7 +58,8 @@ struct curltime Curl_now(void)
   return now;
 }
 
-#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
+#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC) ||  \
+  defined(HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
 
 struct curltime Curl_now(void)
 {
@@ -87,6 +88,19 @@ struct curltime Curl_now(void)
     have_clock_gettime = TRUE;
 #endif
 
+#ifdef HAVE_CLOCK_GETTIME_MONOTONIC_RAW
+  if(
+#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) &&    \
+        (HAVE_BUILTIN_AVAILABLE == 1)
+    have_clock_gettime &&
+#endif
+    (0 == clock_gettime(CLOCK_MONOTONIC_RAW, &tsnow))) {
+    cnow.tv_sec = tsnow.tv_sec;
+    cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000);
+  }
+  else
+#endif
+
   if(
 #if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
         (HAVE_BUILTIN_AVAILABLE == 1)