unix/unix_mphal: Use CLOCK_MONOTONIC for ticks_ms/us when available.

pull/1/head
Mikhail Zakharov 2019-04-23 11:06:11 -04:00 committed by Damien George
parent d889def06b
commit ced340d739
1 changed files with 12 additions and 0 deletions

View File

@ -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
}