This is an automated email from Gerrit. "Grant Ramsay <[email protected]>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9743
-- gerrit commit c0f1290020261c8a3e8bb1ddeb033ad05017199f Author: Grant Ramsay <[email protected]> Date: Mon Jun 15 09:55:40 2026 +1200 time_support: Use monotonic time for timeval_ms gettimeofday is susceptible to NTP adjustments and marked as obsolete. Replace timeval_ms implementation with suitable monotonic time source and fall back to gettimeofday if needed Change-Id: Iba21bfb023a9df1e668175bea327467bbd5b45b4 Signed-off-by: Grant Ramsay <[email protected]> diff --git a/configure.ac b/configure.ac index 335eb2bc3d..9a6917b150 100644 --- a/configure.ac +++ b/configure.ac @@ -48,6 +48,8 @@ AC_TYPE_LONG_LONG_INT AC_SEARCH_LIBS([ioperm], [ioperm]) AC_SEARCH_LIBS([dlopen], [dl]) AC_SEARCH_LIBS([openpty], [util]) +# Older versions of Linux put clock_gettime in librt +AC_SEARCH_LIBS([clock_gettime], [rt]) AC_CHECK_HEADERS([sys/socket.h]) AC_CHECK_HEADERS([elf.h]) @@ -94,6 +96,8 @@ AC_C_BIGENDIAN AC_CHECK_FUNCS([strndup]) AC_CHECK_FUNCS([strnlen]) AC_CHECK_FUNCS([gettimeofday]) +AC_CHECK_FUNCS([clock_gettime]) + AC_CHECK_FUNCS([usleep]) AC_CHECK_FUNCS([realpath]) AC_CHECK_FUNCS([mallinfo]) diff --git a/src/helper/time_support_common.c b/src/helper/time_support_common.c index 9d17a315bf..93bc88fdbf 100644 --- a/src/helper/time_support_common.c +++ b/src/helper/time_support_common.c @@ -9,6 +9,9 @@ * * * Copyright (C) 2008 by Spencer Oliver * * [email protected] * + * * + * Copyright (C) 2026 by Grant Ramsay * + * [email protected] * ***************************************************************************/ #ifdef HAVE_CONFIG_H @@ -17,14 +20,43 @@ #include "time_support.h" +#ifdef _WIN32 +#include <windows.h> +#endif + /* simple and low overhead fetching of ms counter. Use only * the difference between ms counters returned from this fn. + * Use QueryPerformanceCounter for Windows, else + * clock_gettime with CLOCK_MONOTONIC_RAW if available, else + * clock_gettime with CLOCK_MONOTONIC if available, else + * fallback to gettimeofday (susceptible to NTP adjustments) */ int64_t timeval_ms(void) { +#ifdef _WIN32 + static LARGE_INTEGER frequency; + LARGE_INTEGER now; + if (frequency.QuadPart == 0 && !QueryPerformanceFrequency(&frequency)) + return -1; + if (!QueryPerformanceCounter(&now)) + return -1; + return (now.QuadPart * 1000) / frequency.QuadPart; +#elif defined(HAVE_CLOCK_GETTIME) +#ifdef CLOCK_MONOTONIC_RAW + clockid_t clk_id = CLOCK_MONOTONIC_RAW; +#else + clockid_t clk_id = CLOCK_MONOTONIC; +#endif + struct timespec now; + int retval = clock_gettime(clk_id, &now); + if (retval < 0) + return retval; + return (int64_t)now.tv_sec * 1000 + now.tv_nsec / 1000000; +#else struct timeval now; int retval = gettimeofday(&now, NULL); if (retval < 0) return retval; return (int64_t)now.tv_sec * 1000 + now.tv_usec / 1000; +#endif } diff --git a/tools/scripts/camelcase.txt b/tools/scripts/camelcase.txt index 95ef4af551..e566eafad5 100644 --- a/tools/scripts/camelcase.txt +++ b/tools/scripts/camelcase.txt @@ -170,6 +170,8 @@ MsgWaitForMultipleObjects PeekMessage PeekNamedPipe QuadPart +QueryPerformanceCounter +QueryPerformanceFrequency SetConsoleCtrlHandler Sleep WaitForSingleObject --
