From 432452ea9a8119e2bcd8a3ca2d18e0ba80e14f73 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Mon, 4 Jul 2022 10:08:21 +0100 Subject: [PATCH] Fix #1615 - prefer monotonic clock. Add clock for rp2040 --- mongoose.c | 13 +++++++++++++ src/util.c | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/mongoose.c b/mongoose.c index e53b22a4..cca8d0de 100644 --- a/mongoose.c +++ b/mongoose.c @@ -5465,6 +5465,8 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) { uint64_t mg_millis(void) { #if MG_ARCH == MG_ARCH_WIN32 return GetTickCount(); +#elif MG_ARCH == MG_ARCH_RP2040 + return time_us_64() / 1000; #elif MG_ARCH == MG_ARCH_ESP32 return esp_timer_get_time() / 1000; #elif MG_ARCH == MG_ARCH_ESP8266 @@ -5475,7 +5477,18 @@ uint64_t mg_millis(void) { return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND); #elif MG_ARCH == MG_ARCH_UNIX struct timespec ts = {0, 0}; + // See #1615 - prefer monotonic clock +#if defined(CLOCK_MONOTONIC_RAW) + // Raw hardware-based time that is not subject to NTP adjustment + clock_gettime(CLOCK_MONOTONIC_RAW, &ts); +#elif defined(CLOCK_MONOTONIC) + // Affected by the incremental adjustments performed by adjtime and NTP + clock_gettime(CLOCK_MONOTONIC, &ts); +#else + // Affected by discontinuous jumps in the system time and by the incremental + // adjustments performed by adjtime and NTP clock_gettime(CLOCK_REALTIME, &ts); +#endif return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000); #else return (uint64_t) (time(NULL) * 1000); diff --git a/src/util.c b/src/util.c index cafdf862..4dce7ed4 100644 --- a/src/util.c +++ b/src/util.c @@ -79,6 +79,8 @@ int mg_check_ip_acl(struct mg_str acl, uint32_t remote_ip) { uint64_t mg_millis(void) { #if MG_ARCH == MG_ARCH_WIN32 return GetTickCount(); +#elif MG_ARCH == MG_ARCH_RP2040 + return time_us_64() / 1000; #elif MG_ARCH == MG_ARCH_ESP32 return esp_timer_get_time() / 1000; #elif MG_ARCH == MG_ARCH_ESP8266 @@ -89,7 +91,18 @@ uint64_t mg_millis(void) { return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND); #elif MG_ARCH == MG_ARCH_UNIX struct timespec ts = {0, 0}; + // See #1615 - prefer monotonic clock +#if defined(CLOCK_MONOTONIC_RAW) + // Raw hardware-based time that is not subject to NTP adjustment + clock_gettime(CLOCK_MONOTONIC_RAW, &ts); +#elif defined(CLOCK_MONOTONIC) + // Affected by the incremental adjustments performed by adjtime and NTP + clock_gettime(CLOCK_MONOTONIC, &ts); +#else + // Affected by discontinuous jumps in the system time and by the incremental + // adjustments performed by adjtime and NTP clock_gettime(CLOCK_REALTIME, &ts); +#endif return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000); #else return (uint64_t) (time(NULL) * 1000);