summaryrefslogtreecommitdiffstatshomepage
path: root/ports/unix/unix_mphal.c
diff options
context:
space:
mode:
authorMikhail Zakharov <mikhail.zakharov@cognitivesystems.com>2019-04-23 11:06:11 -0400
committerDamien George <damien.p.george@gmail.com>2019-06-26 11:07:45 +1000
commitced340d739e84737dd5c8e6b4ab9af2ea44e29e7 (patch)
tree2365f75f4ebe24395716d60d5897027f266e41ca /ports/unix/unix_mphal.c
parentd889def06b8cce97c8ef31c986e051f28cc6fbd7 (diff)
downloadmicropython-ced340d739e84737dd5c8e6b4ab9af2ea44e29e7.tar.gz
micropython-ced340d739e84737dd5c8e6b4ab9af2ea44e29e7.zip
unix/unix_mphal: Use CLOCK_MONOTONIC for ticks_ms/us when available.
Diffstat (limited to 'ports/unix/unix_mphal.c')
-rw-r--r--ports/unix/unix_mphal.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c
index f27c62fd1d..71edaa57ac 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
}