Fix semaphore handling for device on Mac OS

This commit is contained in:
Lorenz Meier
2015-09-16 17:10:24 +02:00
parent 80760cf162
commit 686caf4ca4
4 changed files with 28 additions and 5 deletions
View File
+23 -1
View File
@@ -44,6 +44,7 @@
#include <drivers/drv_device.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
namespace device
{
@@ -54,7 +55,24 @@ Device::Device(const char *name) :
_name(name),
_debug_enabled(false)
{
sem_init(&_lock, 0, 1);
#ifndef __PX4_DARWIN
_lock = new sem_t;
int ret = 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
/* setup a default device ID. When bus_type is UNKNOWN the
other fields are invalid */
@@ -67,7 +85,11 @@ Device::Device(const char *name) :
Device::~Device()
{
#ifdef __PX4_DARWIN
sem_unlink(_name);
#else
sem_destroy(&_lock);
#endif
}
int
+1 -1
View File
@@ -64,7 +64,7 @@ private:
px4_dev_t() {}
};
#define PX4_MAX_DEV 200
#define PX4_MAX_DEV 500
static px4_dev_t *devmap[PX4_MAX_DEV];
/*
+4 -3
View File
@@ -161,6 +161,7 @@ public:
protected:
const char *_name; /**< driver name */
char *_lock_name; /**< name of the semaphore */
bool _debug_enabled; /**< if true, debug messages are printed */
union DeviceId _device_id; /**< device identifier information */
@@ -180,7 +181,7 @@ protected:
*/
void lock() {
DEVICE_DEBUG("lock");
do {} while (sem_wait(&_lock) != 0);
do {} while (sem_wait(_lock) != 0);
}
/**
@@ -188,11 +189,11 @@ protected:
*/
void unlock() {
DEVICE_DEBUG("unlock");
sem_post(&_lock);
sem_post(_lock);
}
private:
sem_t _lock;
sem_t * _lock;
/** disable copy construction for this and all subclasses */
Device(const Device &);