libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 5437bfdf9c8b8dd3faa55471eeb655ba908c96c3
parent cd6f8668cdfc254a9e3dad320f79200c40929e20
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Fri, 27 Aug 2021 12:53:09 +0300

MHD_monotonic_msec_counter(): added new internal function

Diffstat:
Msrc/microhttpd/mhd_mono_clock.c | 58+++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/microhttpd/mhd_mono_clock.h | 14+++++++++++++-
2 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/src/microhttpd/mhd_mono_clock.c b/src/microhttpd/mhd_mono_clock.c @@ -354,7 +354,7 @@ MHD_monotonic_sec_counter_finish (void) /** - * Monotonic seconds counter, useful for timeout calculation. + * Monotonic seconds counter. * Tries to be not affected by manually setting the system real time * clock or adjustments by NTP synchronization. * @@ -403,3 +403,59 @@ MHD_monotonic_sec_counter (void) return time (NULL) - sys_clock_start; } + + +/** + * Monotonic milliseconds counter, useful for timeout calculation. + * Tries to be not affected by manually setting the system real time + * clock or adjustments by NTP synchronization. + * + * @return number of microseconds from some fixed moment + */ +uint64_t +MHD_monotonic_msec_counter (void) +{ +#ifdef HAVE_CLOCK_GETTIME + struct timespec ts; + + if ( (_MHD_UNWANTED_CLOCK != mono_clock_id) && + (0 == clock_gettime (mono_clock_id, + &ts)) ) + return (uint64_t) (((uint64_t) (ts.tv_sec - mono_clock_start)) * 1000 + + (ts.tv_nsec / 1000000)); +#endif /* HAVE_CLOCK_GETTIME */ +#ifdef HAVE_CLOCK_GET_TIME + if (_MHD_INVALID_CLOCK_SERV != mono_clock_service) + { + mach_timespec_t cur_time; + + if (KERN_SUCCESS == clock_get_time (mono_clock_service, + &cur_time)) + return (uint64_t) (((uint64_t) (cur_time.tv_sec - mono_clock_start)) + * 1000 + (cur_time.tv_nsec / 1000000)); + } +#endif /* HAVE_CLOCK_GET_TIME */ +#if defined(_WIN32) +#if _WIN32_WINNT >= 0x0600 + if (1) + return (uint64_t) (GetTickCount64 () - tick_start); +#else /* _WIN32_WINNT < 0x0600 */ + if (0 != perf_freq) + { + LARGE_INTEGER perf_counter; + uint64_t num_ticks; + + QueryPerformanceCounter (&perf_counter); /* never fail on XP and later */ + num_ticks = (uint64_t) (perf_counter.QuadPart - perf_start); + return ((num_ticks / perf_freq) * 1000) + + ((num_ticks % perf_freq) / (perf_freq / 1000)); + } +#endif /* _WIN32_WINNT < 0x0600 */ +#endif /* _WIN32 */ +#ifdef HAVE_GETHRTIME + if (1) + return ((uint64_t) (gethrtime () - hrtime_start)) / 1000000; +#endif /* HAVE_GETHRTIME */ + + return time (NULL) - sys_clock_start; +} diff --git a/src/microhttpd/mhd_mono_clock.h b/src/microhttpd/mhd_mono_clock.h @@ -32,6 +32,7 @@ #elif defined(HAVE_SYS_TYPES_H) #include <sys/types.h> #endif +#include <stdint.h> /** * Initialise monotonic seconds counter. @@ -48,7 +49,7 @@ MHD_monotonic_sec_counter_finish (void); /** - * Monotonic seconds counter, useful for timeout calculation. + * Monotonic seconds counter. * Tries to be not affected by manually setting the system real time * clock or adjustments by NTP synchronization. * @@ -57,4 +58,15 @@ MHD_monotonic_sec_counter_finish (void); time_t MHD_monotonic_sec_counter (void); + +/** + * Monotonic milliseconds counter, useful for timeout calculation. + * Tries to be not affected by manually setting the system real time + * clock or adjustments by NTP synchronization. + * + * @return number of microseconds from some fixed moment + */ +uint64_t +MHD_monotonic_msec_counter (void); + #endif /* MHD_MONO_CLOCK_H */