From 76508ab5b5e5c1c2e70987795d6816a4e69fc770 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Sun, 20 Sep 2015 00:27:41 +0200 Subject: [PATCH] Device:: Move to POSIX semaphore abstraction --- src/drivers/device/cdev.cpp | 4 ++-- src/drivers/device/device_posix.cpp | 27 +++++---------------------- src/drivers/device/vdev.cpp | 9 +++++---- src/drivers/device/vdev.h | 6 +++--- src/drivers/device/vdev_posix.cpp | 14 +++++++------- 5 files changed, 22 insertions(+), 38 deletions(-) diff --git a/src/drivers/device/cdev.cpp b/src/drivers/device/cdev.cpp index 3bc05b0c73..048357ebc8 100644 --- a/src/drivers/device/cdev.cpp +++ b/src/drivers/device/cdev.cpp @@ -306,7 +306,7 @@ CDev::poll(file_t *filp, struct pollfd *fds, bool setup) /* yes? post the notification */ if (fds->revents != 0) - sem_post(fds->sem); + px4_sem_post(fds->sem); } } else { @@ -343,7 +343,7 @@ CDev::poll_notify_one(struct pollfd *fds, pollevent_t events) /* if the state is now interesting, wake the waiter if it's still asleep */ /* XXX semcount check here is a vile hack; counting semphores should not be abused as cvars */ if ((fds->revents != 0) && (fds->sem->semcount <= 0)) - sem_post(fds->sem); + px4_sem_post(fds->sem); } pollevent_t diff --git a/src/drivers/device/device_posix.cpp b/src/drivers/device/device_posix.cpp index 05ae355709..bed97ff56d 100644 --- a/src/drivers/device/device_posix.cpp +++ b/src/drivers/device/device_posix.cpp @@ -55,24 +55,11 @@ Device::Device(const char *name) : _name(name), _debug_enabled(false) { - #ifndef __PX4_DARWIN - _lock = new sem_t; - int ret = sem_init(_lock, 0, 1); + int ret = px4_sem_init(&_lock, 0, 1); - if (ret != 0) { - PX4_WARN("SEM INIT FAIL: ret %d, %s", ret, strerror(errno)); - } - #else - _lock_name = new char[strlen(_name) + 2]; - _lock_name[0] = '/'; - strcpy(&_lock_name[1], _name); - /* not using O_EXCL as the device handles are unique */ - _lock = sem_open(_lock_name, O_CREAT, 0777, 1); - - if (_lock == SEM_FAILED) { - PX4_WARN("SEM INIT FAIL: %s", strerror(errno)); - } - #endif + if (ret != 0) { + PX4_WARN("SEM INIT FAIL: ret %d, %s", ret, strerror(errno)); + } /* setup a default device ID. When bus_type is UNKNOWN the other fields are invalid */ @@ -85,11 +72,7 @@ Device::Device(const char *name) : Device::~Device() { - #ifdef __PX4_DARWIN - sem_unlink(_name); - #else - sem_destroy(_lock); - #endif + px4_sem_destroy(&_lock); } int diff --git a/src/drivers/device/vdev.cpp b/src/drivers/device/vdev.cpp index 430b121c09..1900f19fbe 100644 --- a/src/drivers/device/vdev.cpp +++ b/src/drivers/device/vdev.cpp @@ -366,7 +366,7 @@ VDev::poll(file_t *filep, px4_pollfd_struct_t *fds, bool setup) /* yes? post the notification */ if (fds->revents != 0) - sem_post(fds->sem); + px4_sem_post(fds->sem); } else { PX4_WARN("Store Poll Waiter error."); } @@ -403,7 +403,7 @@ VDev::poll_notify_one(px4_pollfd_struct_t *fds, pollevent_t events) { PX4_DEBUG("VDev::poll_notify_one"); int value; - sem_getvalue(fds->sem, &value); + px4_sem_getvalue(fds->sem, &value); /* update the reported event set */ fds->revents |= fds->events & events; @@ -412,8 +412,9 @@ VDev::poll_notify_one(px4_pollfd_struct_t *fds, pollevent_t events) /* if the state is now interesting, wake the waiter if it's still asleep */ /* XXX semcount check here is a vile hack; counting semphores should not be abused as cvars */ - if ((fds->revents != 0) && (value <= 0)) - sem_post(fds->sem); + if ((fds->revents != 0) && (value <= 0)) { + px4_sem_post(fds->sem); + } } pollevent_t diff --git a/src/drivers/device/vdev.h b/src/drivers/device/vdev.h index 3b4aae01ed..89e06358f5 100644 --- a/src/drivers/device/vdev.h +++ b/src/drivers/device/vdev.h @@ -181,7 +181,7 @@ protected: */ void lock() { DEVICE_DEBUG("lock"); - do {} while (sem_wait(_lock) != 0); + do {} while (px4_sem_wait(&_lock) != 0); } /** @@ -189,11 +189,11 @@ protected: */ void unlock() { DEVICE_DEBUG("unlock"); - sem_post(_lock); + px4_sem_post(&_lock); } private: - sem_t * _lock; + px4_sem_t _lock; /** disable copy construction for this and all subclasses */ Device(const Device &); diff --git a/src/drivers/device/vdev_posix.cpp b/src/drivers/device/vdev_posix.cpp index 44aea614cc..cf6645e106 100644 --- a/src/drivers/device/vdev_posix.cpp +++ b/src/drivers/device/vdev_posix.cpp @@ -56,8 +56,8 @@ extern "C" { static void timer_cb(void *data) { - sem_t *p_sem = (sem_t *)data; - sem_post(p_sem); + px4_sem_t *p_sem = (px4_sem_t *)data; + px4_sem_post(p_sem); PX4_DEBUG("timer_handler: Timer expired"); } @@ -193,13 +193,13 @@ int px4_ioctl(int fd, int cmd, unsigned long arg) int px4_poll(px4_pollfd_struct_t *fds, nfds_t nfds, int timeout) { - sem_t sem; + px4_sem_t sem; int count = 0; int ret; unsigned int i; PX4_DEBUG("Called px4_poll timeout = %d", timeout); - sem_init(&sem, 0, 0); + px4_sem_init(&sem, 0, 0); // For each fd for (i=0; i