From 604644d82379668eca81c7de7a174a29f79aa248 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Sun, 20 Sep 2015 00:26:56 +0200 Subject: [PATCH] POSIX: Improvements on semaphore abstraction --- src/platforms/posix/px4_layer/drv_hrt.c | 23 ++++--------------- .../posix/px4_layer/px4_posix_impl.cpp | 3 ++- .../posix/px4_layer/px4_posix_tasks.cpp | 2 +- src/platforms/posix/px4_layer/px4_sem.cpp | 7 +++--- 4 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/platforms/posix/px4_layer/drv_hrt.c b/src/platforms/posix/px4_layer/drv_hrt.c index 3afaa7f943..74bfb9ee41 100644 --- a/src/platforms/posix/px4_layer/drv_hrt.c +++ b/src/platforms/posix/px4_layer/drv_hrt.c @@ -60,7 +60,7 @@ static void hrt_call_reschedule(void); #define HRT_INTERVAL_MIN 50 #define HRT_INTERVAL_MAX 50000000 -static sem_t *_hrt_lock; +static px4_sem_t _hrt_lock; static struct work_s _hrt_work; static hrt_abstime px4_timestart = 0; @@ -71,12 +71,12 @@ __EXPORT hrt_abstime hrt_reset(void); static void hrt_lock(void) { - sem_wait(_hrt_lock); + px4_sem_wait(&_hrt_lock); } static void hrt_unlock(void) { - sem_post(_hrt_lock); + px4_sem_post(&_hrt_lock); } #ifdef __PX4_DARWIN @@ -99,13 +99,11 @@ int clock_gettime(clockid_t clk_id, struct timespec *t) } if (!px4_timestart) { - hrt_lock(); mach_timebase_info_data_t tb = {}; mach_timebase_info(&tb); px4_timebase = tb.numer; px4_timebase /= tb.denom; px4_timestart = mach_absolute_time(); - hrt_unlock(); } memset(t, 0, sizeof(*t)); @@ -230,23 +228,12 @@ void hrt_call_delay(struct hrt_call *entry, hrt_abstime delay) */ void hrt_init(void) { - //printf("hrt_init\n"); sq_init(&callout_queue); - #ifdef __PX4_DARWIN - /* not using O_EXCL as the device handles are unique */ - _hrt_lock = sem_open(HRT_LOCK_NAME, O_CREAT, 0777, 1); - - if (_hrt_lock == SEM_FAILED) { - PX4_WARN("SEM INIT FAIL: %s", strerror(errno)); - } - #else - _hrt_lock = malloc(sizeof(sem_t)); - int sem_ret = sem_init(_hrt_lock, 0, 1); + int sem_ret = px4_sem_init(&_hrt_lock, 0, 1); if (sem_ret) { - PX4_WARN("SEM INIT FAIL: %s", strerror(errno)); + PX4_ERR("SEM INIT FAIL: %s", strerror(errno)); } - #endif memset(&_hrt_work, 0, sizeof(_hrt_work)); } diff --git a/src/platforms/posix/px4_layer/px4_posix_impl.cpp b/src/platforms/posix/px4_layer/px4_posix_impl.cpp index faf3b81990..5311ead749 100644 --- a/src/platforms/posix/px4_layer/px4_posix_impl.cpp +++ b/src/platforms/posix/px4_layer/px4_posix_impl.cpp @@ -72,6 +72,7 @@ int px4_clock_gettime(clockid_t clk_id, struct timespec *tp) int px4_clock_settime(clockid_t clk_id, struct timespec *tp) { /* do nothing right now */ + return 0; } #endif @@ -86,7 +87,7 @@ void init_once(void); void init_once(void) { _shell_task_id = pthread_self(); - printf("[init] shell id: %lu\n", _shell_task_id); + printf("[init] shell id: %lu\n", (unsigned long)_shell_task_id); work_queues_init(); hrt_work_queue_init(); hrt_init(); diff --git a/src/platforms/posix/px4_layer/px4_posix_tasks.cpp b/src/platforms/posix/px4_layer/px4_posix_tasks.cpp index de929e3a70..f112290cb3 100644 --- a/src/platforms/posix/px4_layer/px4_posix_tasks.cpp +++ b/src/platforms/posix/px4_layer/px4_posix_tasks.cpp @@ -271,7 +271,7 @@ void px4_show_tasks() for (idx=0; idx < PX4_MAX_TASKS; idx++) { if (taskmap[idx].isused) { - PX4_INFO(" %-10s %lu", taskmap[idx].name.c_str(), taskmap[idx].pid); + PX4_INFO(" %-10s %lu", taskmap[idx].name.c_str(), (unsigned long)taskmap[idx].pid); count++; } } diff --git a/src/platforms/posix/px4_layer/px4_sem.cpp b/src/platforms/posix/px4_layer/px4_sem.cpp index ebf0e9cd51..70d9431c05 100644 --- a/src/platforms/posix/px4_layer/px4_sem.cpp +++ b/src/platforms/posix/px4_layer/px4_sem.cpp @@ -85,14 +85,13 @@ int px4_sem_post(px4_sem_t *s) return 0; } -int px4_sem_getvalue(px4_sem_t *s) +int px4_sem_getvalue(px4_sem_t *s, int *sval) { - int val; pthread_mutex_lock(&(s->lock)); - val = s->value; + *sval = s->value; pthread_mutex_unlock(&(s->lock)); - return val; + return 0; } int px4_sem_destroy(px4_sem_t *s)