timeval: use CLOCK_MONOTONIC_RAW if available

Reported-by: Harry Sintonen
Ref: #11288
Closes #11291
This commit is contained in:
Daniel Stenberg 2023-06-09 20:42:51 +02:00
parent f06cc4f85e
commit c92b7228c5
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
3 changed files with 50 additions and 1 deletions

View 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 -------------------------------------------------

View 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

View 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)