Skip to content

Commit

Permalink
unix/mphal: use CLOCK_MONOTONIC for ticks_ms/us
Browse files Browse the repository at this point in the history
  • Loading branch information
mzakharocsc committed Jun 25, 2019
1 parent d889def commit 115d3b1
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ports/unix/unix_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 115d3b1

Please sign in to comment.