From 06c5037025e59bd56a8741b6285f340b192f2b15 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Sun, 9 Dec 2018 12:39:06 +0100 Subject: [PATCH] platforms: use CLOCK_MONOTONIC For the non-lockstep case we want to use CLOCK_MONOTONIC if possible. --- platforms/posix/src/px4_layer/px4_sem.cpp | 6 ++++++ src/lib/cdev/posix/cdev_platform.cpp | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/platforms/posix/src/px4_layer/px4_sem.cpp b/platforms/posix/src/px4_layer/px4_sem.cpp index bfac33ccba..c0199b1e0d 100644 --- a/platforms/posix/src/px4_layer/px4_sem.cpp +++ b/platforms/posix/src/px4_layer/px4_sem.cpp @@ -58,6 +58,12 @@ int px4_sem_init(px4_sem_t *s, int pshared, unsigned value) pthread_cond_init(&(s->wait), nullptr); pthread_mutex_init(&(s->lock), nullptr); + // We want to use CLOCK_MONOTONIC if possible. + pthread_condattr_t attr; + pthread_condattr_init(&attr); + pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); + pthread_cond_init(&(s->wait), &attr); + return 0; } diff --git a/src/lib/cdev/posix/cdev_platform.cpp b/src/lib/cdev/posix/cdev_platform.cpp index 4fa9fc78ee..a4da964f83 100644 --- a/src/lib/cdev/posix/cdev_platform.cpp +++ b/src/lib/cdev/posix/cdev_platform.cpp @@ -379,7 +379,8 @@ extern "C" { // Get the current time struct timespec ts; - // px4_sem_timedwait is implemented using CLOCK_MONOTONIC. + // px4_sem_timedwait is implemented using CLOCK_MONOTONIC, + // at least for lockstep, on Qurt and on Linux. px4_clock_gettime(CLOCK_MONOTONIC, &ts); // Calculate an absolute time in the future