commit e544cf36300563fc4ed790b80a80b52669529bbb
parent cc650acad6f6777b35045c3387a264a6dcc5ffab
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Sun, 29 Aug 2021 22:07:13 +0300
Added fallback functions for MHD_monotonic_msec_counter() values
Diffstat:
2 files changed, 105 insertions(+), 11 deletions(-)
diff --git a/configure.ac b/configure.ac
@@ -1479,6 +1479,63 @@ AC_LINK_IFELSE(
[AC_MSG_RESULT([[no]])
])
+AS_VAR_IF([ac_cv_header_time_h], ["yes"],
+ [
+ AC_CACHE_CHECK([[for C11 timespec_get()]], [mhd_cv_func_timespec_get],
+ [
+ AC_LINK_IFELSE(
+ [
+ AC_LANG_PROGRAM(
+ [[
+#include <time.h>
+
+#ifndef TIME_UTC
+#error TIME_UTC must be defined to use timespec_get()
+choke me now
+#endif
+ ]],
+ [[
+ struct timespec ts;
+ if (TIME_UTC != timespec_get (&ts, TIME_UTC))
+ return 1;
+ ]]
+ )
+ ], [[mhd_cv_func_timespec_get="yes"]], [[mhd_cv_func_timespec_get="no"]]
+ )
+ ]
+ )
+ AS_VAR_IF([mhd_cv_func_timespec_get], ["yes"],
+ [AC_DEFINE([HAVE_TIMESPEC_GET], [1], [Define to 1 if you have C11 `mhd_cv_func_timespec_get' function and TIME_UTC macro.])]
+ )
+ ]
+)
+
+AS_VAR_SET_IF([ac_cv_func_gettimeofday], [mhd_cv_func_gettimeofday="${ac_cv_func_gettimeofday}"])
+AC_CACHE_CHECK([[for gettimeofday(2)]], [mhd_cv_func_gettimeofday], [
+ AC_LINK_IFELSE(
+ [
+ AC_LANG_PROGRAM(
+ [[
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif /* HAVE_SYS_TIME_H */
+#ifdef HAVE_TIME_H
+#include <time.h>
+#endif /* HAVE_TIME_H */
+ ]],
+ [[
+ struct timeval tv;
+ if (0 != gettimeofday (&tv, (void*) 0))
+ return 1;
+ ]]
+ )
+ ], [[mhd_cv_func_gettimeofday="yes"]], [[mhd_cv_func_gettimeofday="no"]]
+ )
+])
+AS_VAR_IF([mhd_cv_func_gettimeofday], ["yes"],
+ [AC_DEFINE([HAVE_GETTIMEOFDAY], [1], [Define to 1 if you have `gettimeofday' function.])]
+)
+
# IPv6
AC_MSG_CHECKING(for IPv6)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
diff --git a/src/microhttpd/mhd_mono_clock.c b/src/microhttpd/mhd_mono_clock.c
@@ -30,20 +30,12 @@
#undef HAVE_CLOCK_GETTIME
#endif /* _WIN32 && ! __CYGWIN__ && HAVE_CLOCK_GETTIME */
-#ifdef HAVE_CLOCK_GETTIME
+#ifdef HAVE_TIME_H
#include <time.h>
-#endif /* HAVE_CLOCK_GETTIME */
-
-#ifdef HAVE_GETHRTIME
+#endif /* HAVE_TIME_H */
#ifdef HAVE_SYS_TIME_H
-/* Solaris defines gethrtime() in sys/time.h */
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
-#ifdef HAVE_TIME_H
-/* HP-UX defines gethrtime() in time.h */
-#include <time.h>
-#endif /* HAVE_TIME_H */
-#endif /* HAVE_GETHRTIME */
#ifdef HAVE_CLOCK_GET_TIME
#include <mach/mach.h>
@@ -65,6 +57,10 @@ static clock_serv_t mono_clock_service = _MHD_INVALID_CLOCK_SERV;
#include <stdint.h>
#endif /* _WIN32 */
+#ifndef NULL
+#define NULL ((void*)0)
+#endif /* ! NULL */
+
#ifdef HAVE_CLOCK_GETTIME
#ifdef CLOCK_REALTIME
#define _MHD_UNWANTED_CLOCK CLOCK_REALTIME
@@ -80,6 +76,10 @@ static clockid_t mono_clock_id = _MHD_UNWANTED_CLOCK;
defined(HAVE_GETHRTIME)
static time_t mono_clock_start;
#endif /* HAVE_CLOCK_GETTIME || HAVE_CLOCK_GET_TIME || HAVE_GETHRTIME */
+#if defined(HAVE_TIMESPEC_GET) || defined(HAVE_GETTIMEOFDAY)
+/* The start value shared for timespec_get() and gettimeofday () */
+static time_t gettime_start;
+#endif /* HAVE_TIMESPEC_GET || HAVE_GETTIMEOFDAY */
static time_t sys_clock_start;
#ifdef HAVE_GETHRTIME
static hrtime_t hrtime_start;
@@ -332,6 +332,25 @@ MHD_monotonic_sec_counter_init (void)
(void) mono_clock_source; /* avoid compiler warning */
#endif /* HAVE_CLOCK_GET_TIME */
+#ifdef HAVE_TIMESPEC_GET
+ if (1)
+ {
+ struct timespec tsg;
+ if (TIME_UTC == timespec_get (&tsg, TIME_UTC))
+ gettime_start = tsg.tv_sec;
+ else
+ gettime_start = 0;
+ }
+#elif defined (HAVE_GETTIMEOFDAY)
+ if (1)
+ {
+ struct timeval tv;
+ if (0 == gettimeofday (&tv, NULL))
+ gettime_start = tv.tv_sec;
+ else
+ gettime_start = 0;
+ }
+#endif /* HAVE_GETTIMEOFDAY */
sys_clock_start = time (NULL);
}
@@ -415,9 +434,11 @@ MHD_monotonic_sec_counter (void)
uint64_t
MHD_monotonic_msec_counter (void)
{
-#ifdef HAVE_CLOCK_GETTIME
+#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_TIMESPEC_GET)
struct timespec ts;
+#endif /* HAVE_CLOCK_GETTIME || HAVE_TIMESPEC_GET */
+#ifdef HAVE_CLOCK_GETTIME
if ( (_MHD_UNWANTED_CLOCK != mono_clock_id) &&
(0 == clock_gettime (mono_clock_id,
&ts)) )
@@ -457,5 +478,21 @@ MHD_monotonic_msec_counter (void)
return ((uint64_t) (gethrtime () - hrtime_start)) / 1000000;
#endif /* HAVE_GETHRTIME */
+ /* Fallbacks, affected by system time change */
+#ifdef HAVE_TIMESPEC_GET
+ if (TIME_UTC == timespec_get (&ts, TIME_UTC))
+ return (uint64_t) (((uint64_t) (ts.tv_sec - gettime_start)) * 1000
+ + (ts.tv_nsec / 1000000));
+#elif defined (HAVE_GETTIMEOFDAY)
+ if (1)
+ {
+ struct timeval tv;
+ if (0 == gettimeofday (&tv, NULL))
+ return (uint64_t) (((uint64_t) (tv.tv_sec - gettime_start)) * 1000
+ + (tv.tv_usec / 1000));
+ }
+#endif /* HAVE_GETTIMEOFDAY */
+
+ /* The last resort fallback with very low resolution */
return (uint64_t) (time (NULL) - sys_clock_start) * 1000;
}