From ced340d739e84737dd5c8e6b4ab9af2ea44e29e7 Mon Sep 17 00:00:00 2001 From: Mikhail Zakharov Date: Tue, 23 Apr 2019 11:06:11 -0400 Subject: [PATCH] unix/unix_mphal: Use CLOCK_MONOTONIC for ticks_ms/us when available. --- ports/unix/unix_mphal.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c index f27c62fd1..71edaa57a 100644 --- a/ports/unix/unix_mphal.c +++ b/ports/unix/unix_mphal.c @@ -187,13 +187,25 @@ void mp_hal_stdout_tx_str(const char *str) { } mp_uint_t mp_hal_ticks_ms(void) { + #if (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK) + struct timespec tv; + clock_gettime(CLOCK_MONOTONIC, &tv); + return tv.tv_sec * 1000 + tv.tv_nsec / 1000000; + #else struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec * 1000 + tv.tv_usec / 1000; + #endif } mp_uint_t mp_hal_ticks_us(void) { + #if (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK) + struct timespec tv; + clock_gettime(CLOCK_MONOTONIC, &tv); + return tv.tv_sec * 1000000 + tv.tv_nsec / 1000; + #else struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec * 1000000 + tv.tv_usec; + #endif }