From fa8913a7598f3ec99266e4efb02ca12eac1b19cb Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Fri, 14 Aug 2026 10:40:54 +0200 Subject: [PATCH] Use monotonic clock in os_tick_ms and os_tick_sec on POSIX systems --- os/unix.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/os/unix.c b/os/unix.c index 23f6cc4b..d1309f46 100644 --- a/os/unix.c +++ b/os/unix.c @@ -1674,6 +1674,17 @@ uint64_t os_tick(void) uint64_t os_tick_ms(void) { +#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC) + struct timespec ts; + + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { + /* LCOV_EXCL_START */ + return 0; + /* LCOV_EXCL_STOP */ + } + + return ts.tv_sec * 1000ULL + ts.tv_nsec / 1000000; +#else struct timeval tv; if (gettimeofday(&tv, 0) != 0) { @@ -1683,15 +1694,32 @@ uint64_t os_tick_ms(void) } return tv.tv_sec * 1000ULL + tv.tv_usec / 1000; +#endif } uint64_t os_tick_sec(void) { +#if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC) struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { + /* LCOV_EXCL_START */ + return 0; + /* LCOV_EXCL_STOP */ + } return ts.tv_sec; +#else + struct timeval tv; + + if (gettimeofday(&tv, 0) != 0) { + /* LCOV_EXCL_START */ + return 0; + /* LCOV_EXCL_STOP */ + } + + return tv.tv_sec; +#endif } int os_usleep(uint64_t usec)