mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-28 15:53:34 +08:00
Device:: Move to POSIX semaphore abstraction
This commit is contained in:
@@ -306,7 +306,7 @@ CDev::poll(file_t *filp, struct pollfd *fds, bool setup)
|
|||||||
|
|
||||||
/* yes? post the notification */
|
/* yes? post the notification */
|
||||||
if (fds->revents != 0)
|
if (fds->revents != 0)
|
||||||
sem_post(fds->sem);
|
px4_sem_post(fds->sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} 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 */
|
/* 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 */
|
/* XXX semcount check here is a vile hack; counting semphores should not be abused as cvars */
|
||||||
if ((fds->revents != 0) && (fds->sem->semcount <= 0))
|
if ((fds->revents != 0) && (fds->sem->semcount <= 0))
|
||||||
sem_post(fds->sem);
|
px4_sem_post(fds->sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
pollevent_t
|
pollevent_t
|
||||||
|
|||||||
@@ -55,24 +55,11 @@ Device::Device(const char *name) :
|
|||||||
_name(name),
|
_name(name),
|
||||||
_debug_enabled(false)
|
_debug_enabled(false)
|
||||||
{
|
{
|
||||||
#ifndef __PX4_DARWIN
|
int ret = px4_sem_init(&_lock, 0, 1);
|
||||||
_lock = new sem_t;
|
|
||||||
int ret = sem_init(_lock, 0, 1);
|
|
||||||
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
PX4_WARN("SEM INIT FAIL: ret %d, %s", ret, strerror(errno));
|
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
|
|
||||||
|
|
||||||
/* setup a default device ID. When bus_type is UNKNOWN the
|
/* setup a default device ID. When bus_type is UNKNOWN the
|
||||||
other fields are invalid */
|
other fields are invalid */
|
||||||
@@ -85,11 +72,7 @@ Device::Device(const char *name) :
|
|||||||
|
|
||||||
Device::~Device()
|
Device::~Device()
|
||||||
{
|
{
|
||||||
#ifdef __PX4_DARWIN
|
px4_sem_destroy(&_lock);
|
||||||
sem_unlink(_name);
|
|
||||||
#else
|
|
||||||
sem_destroy(_lock);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|||||||
@@ -366,7 +366,7 @@ VDev::poll(file_t *filep, px4_pollfd_struct_t *fds, bool setup)
|
|||||||
|
|
||||||
/* yes? post the notification */
|
/* yes? post the notification */
|
||||||
if (fds->revents != 0)
|
if (fds->revents != 0)
|
||||||
sem_post(fds->sem);
|
px4_sem_post(fds->sem);
|
||||||
} else {
|
} else {
|
||||||
PX4_WARN("Store Poll Waiter error.");
|
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");
|
PX4_DEBUG("VDev::poll_notify_one");
|
||||||
int value;
|
int value;
|
||||||
sem_getvalue(fds->sem, &value);
|
px4_sem_getvalue(fds->sem, &value);
|
||||||
|
|
||||||
/* update the reported event set */
|
/* update the reported event set */
|
||||||
fds->revents |= fds->events & events;
|
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 */
|
/* 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 */
|
/* XXX semcount check here is a vile hack; counting semphores should not be abused as cvars */
|
||||||
if ((fds->revents != 0) && (value <= 0))
|
if ((fds->revents != 0) && (value <= 0)) {
|
||||||
sem_post(fds->sem);
|
px4_sem_post(fds->sem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pollevent_t
|
pollevent_t
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ protected:
|
|||||||
*/
|
*/
|
||||||
void lock() {
|
void lock() {
|
||||||
DEVICE_DEBUG("lock");
|
DEVICE_DEBUG("lock");
|
||||||
do {} while (sem_wait(_lock) != 0);
|
do {} while (px4_sem_wait(&_lock) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -189,11 +189,11 @@ protected:
|
|||||||
*/
|
*/
|
||||||
void unlock() {
|
void unlock() {
|
||||||
DEVICE_DEBUG("unlock");
|
DEVICE_DEBUG("unlock");
|
||||||
sem_post(_lock);
|
px4_sem_post(&_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sem_t * _lock;
|
px4_sem_t _lock;
|
||||||
|
|
||||||
/** disable copy construction for this and all subclasses */
|
/** disable copy construction for this and all subclasses */
|
||||||
Device(const Device &);
|
Device(const Device &);
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ extern "C" {
|
|||||||
|
|
||||||
static void timer_cb(void *data)
|
static void timer_cb(void *data)
|
||||||
{
|
{
|
||||||
sem_t *p_sem = (sem_t *)data;
|
px4_sem_t *p_sem = (px4_sem_t *)data;
|
||||||
sem_post(p_sem);
|
px4_sem_post(p_sem);
|
||||||
PX4_DEBUG("timer_handler: Timer expired");
|
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)
|
int px4_poll(px4_pollfd_struct_t *fds, nfds_t nfds, int timeout)
|
||||||
{
|
{
|
||||||
sem_t sem;
|
px4_sem_t sem;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int ret;
|
int ret;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
PX4_DEBUG("Called px4_poll timeout = %d", timeout);
|
PX4_DEBUG("Called px4_poll timeout = %d", timeout);
|
||||||
sem_init(&sem, 0, 0);
|
px4_sem_init(&sem, 0, 0);
|
||||||
|
|
||||||
// For each fd
|
// For each fd
|
||||||
for (i=0; i<nfds; ++i)
|
for (i=0; i<nfds; ++i)
|
||||||
@@ -228,7 +228,7 @@ int px4_poll(px4_pollfd_struct_t *fds, nfds_t nfds, int timeout)
|
|||||||
work_s _hpwork;
|
work_s _hpwork;
|
||||||
|
|
||||||
hrt_work_queue(&_hpwork, (worker_t)&timer_cb, (void *)&sem, 1000*timeout);
|
hrt_work_queue(&_hpwork, (worker_t)&timer_cb, (void *)&sem, 1000*timeout);
|
||||||
sem_wait(&sem);
|
px4_sem_wait(&sem);
|
||||||
|
|
||||||
// Make sure timer thread is killed before sem goes
|
// Make sure timer thread is killed before sem goes
|
||||||
// out of scope
|
// out of scope
|
||||||
@@ -236,7 +236,7 @@ int px4_poll(px4_pollfd_struct_t *fds, nfds_t nfds, int timeout)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
sem_wait(&sem);
|
px4_sem_wait(&sem);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For each fd
|
// For each fd
|
||||||
@@ -258,7 +258,7 @@ int px4_poll(px4_pollfd_struct_t *fds, nfds_t nfds, int timeout)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sem_destroy(&sem);
|
px4_sem_destroy(&sem);
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user