From 14e5ebbbbc85bbbff5cdabba189f458ab4e34fec Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Sun, 9 Dec 2018 17:11:11 +0100 Subject: [PATCH] platforms: fix clock build for macOS (yet again) --- platforms/posix/src/px4_layer/drv_hrt.cpp | 13 +++++++++++-- platforms/posix/src/px4_layer/px4_sem.cpp | 5 ++++- src/lib/DriverFramework | 2 +- src/lib/cdev/posix/cdev_platform.cpp | 4 ++-- src/platforms/px4_time.h | 4 ++-- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/platforms/posix/src/px4_layer/drv_hrt.cpp b/platforms/posix/src/px4_layer/drv_hrt.cpp index e32e0521be..4f7f98b869 100644 --- a/platforms/posix/src/px4_layer/drv_hrt.cpp +++ b/platforms/posix/src/px4_layer/drv_hrt.cpp @@ -594,20 +594,29 @@ void abstime_to_ts(struct timespec *ts, hrt_abstime abstime) ts->tv_nsec = abstime * 1000; } -#if defined(ENABLE_LOCKSTEP_SCHEDULER) int px4_clock_gettime(clockid_t clk_id, struct timespec *tp) { if (clk_id == CLOCK_MONOTONIC) { - +#if defined(ENABLE_LOCKSTEP_SCHEDULER) const uint64_t abstime = lockstep_scheduler.get_absolute_time(); abstime_to_ts(tp, abstime - px4_timestart_monotonic); return 0; +#else // defined(ENABLE_LOCKSTEP_SCHEDULER) +#if defined(__PX4_DARWIN) + // We don't have CLOCK_MONOTONIC on macOS, so we just have to + // resort back to CLOCK_REALTIME here. + return system_clock_gettime(CLOCK_REALTIME, tp); +#else // defined(__PX4_DARWIN) + return system_clock_gettime(clk_id, tp); +#endif // defined(__PX4_DARWIN) +#endif // defined(ENABLE_LOCKSTEP_SCHEDULER) } else { return system_clock_gettime(clk_id, tp); } } +#if defined(ENABLE_LOCKSTEP_SCHEDULER) int px4_clock_settime(clockid_t clk_id, const struct timespec *ts) { if (clk_id == CLOCK_REALTIME) { diff --git a/platforms/posix/src/px4_layer/px4_sem.cpp b/platforms/posix/src/px4_layer/px4_sem.cpp index c0199b1e0d..355d409feb 100644 --- a/platforms/posix/src/px4_layer/px4_sem.cpp +++ b/platforms/posix/src/px4_layer/px4_sem.cpp @@ -58,11 +58,14 @@ 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. +#if !defined(__PX4_DARWIN) + // We want to use CLOCK_MONOTONIC if possible but we can't on macOS + // because it's not available. pthread_condattr_t attr; pthread_condattr_init(&attr); pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); pthread_cond_init(&(s->wait), &attr); +#endif return 0; } diff --git a/src/lib/DriverFramework b/src/lib/DriverFramework index 701a35b42a..28ccde918b 160000 --- a/src/lib/DriverFramework +++ b/src/lib/DriverFramework @@ -1 +1 @@ -Subproject commit 701a35b42ab2f5e4969630be262df023dbd04670 +Subproject commit 28ccde918b00403d0c6ccb1b818751c3cc573f08 diff --git a/src/lib/cdev/posix/cdev_platform.cpp b/src/lib/cdev/posix/cdev_platform.cpp index a4da964f83..707f3a95a8 100644 --- a/src/lib/cdev/posix/cdev_platform.cpp +++ b/src/lib/cdev/posix/cdev_platform.cpp @@ -379,8 +379,8 @@ extern "C" { // Get the current time struct timespec ts; - // px4_sem_timedwait is implemented using CLOCK_MONOTONIC, - // at least for lockstep, on Qurt and on Linux. + // Note, we can't actually use CLOCK_MONOTONIC on macOS + // but that's hidden and implemented in px4_clock_gettime. px4_clock_gettime(CLOCK_MONOTONIC, &ts); // Calculate an absolute time in the future diff --git a/src/platforms/px4_time.h b/src/platforms/px4_time.h index a2f9f8a656..ea9713b26d 100644 --- a/src/platforms/px4_time.h +++ b/src/platforms/px4_time.h @@ -9,10 +9,11 @@ #define clockid_t int #endif +__EXPORT int px4_clock_gettime(clockid_t clk_id, struct timespec *tp); + #if defined(ENABLE_LOCKSTEP_SCHEDULER) || defined(__PX4_QURT) __BEGIN_DECLS -__EXPORT int px4_clock_gettime(clockid_t clk_id, struct timespec *tp); __EXPORT int px4_clock_settime(clockid_t clk_id, const struct timespec *tp); __EXPORT int px4_usleep(useconds_t usec); @@ -24,7 +25,6 @@ __END_DECLS #else -#define px4_clock_gettime system_clock_gettime #define px4_clock_settime system_clock_settime #define px4_usleep system_usleep #define px4_sleep system_sleep