mutex api changes
This commit is contained in:
parent
7b3fddfd42
commit
caeb2f9dd6
@ -6,9 +6,10 @@
|
|||||||
|
|
||||||
class MutexHelper {
|
class MutexHelper {
|
||||||
public:
|
public:
|
||||||
MutexHelper(MutexIF* mutex, uint32_t timeoutMs) :
|
MutexHelper(MutexIF* mutex, MutexIF::TimeoutType timeoutType =
|
||||||
|
MutexIF::TimeoutType::BLOCKING, uint32_t timeoutMs = 0) :
|
||||||
internalMutex(mutex) {
|
internalMutex(mutex) {
|
||||||
ReturnValue_t status = mutex->lockMutex(MutexIF::TimeoutType::WAITING,
|
ReturnValue_t status = mutex->lockMutex(timeoutType,
|
||||||
timeoutMs);
|
timeoutMs);
|
||||||
if(status == MutexIF::MUTEX_TIMEOUT) {
|
if(status == MutexIF::MUTEX_TIMEOUT) {
|
||||||
sif::error << "MutexHelper: Lock of mutex failed with timeout of "
|
sif::error << "MutexHelper: Lock of mutex failed with timeout of "
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||||
#include <framework/timemanager/Clock.h>
|
#include <framework/timemanager/Clock.h>
|
||||||
|
|
||||||
const uint32_t MutexIF::POLLING = 0;
|
|
||||||
const uint32_t MutexIF::BLOCKING = 0xffffffff;
|
|
||||||
uint8_t Mutex::count = 0;
|
uint8_t Mutex::count = 0;
|
||||||
|
|
||||||
|
|
||||||
@ -26,7 +24,9 @@ Mutex::Mutex() {
|
|||||||
sif::error << "Mutex: creation with name, id " << mutex.__data.__count
|
sif::error << "Mutex: creation with name, id " << mutex.__data.__count
|
||||||
<< ", " << " failed with " << strerror(status) << std::endl;
|
<< ", " << " failed with " << strerror(status) << std::endl;
|
||||||
}
|
}
|
||||||
//After a mutex attributes object has been used to initialize one or more mutexes, any function affecting the attributes object (including destruction) shall not affect any previously initialized mutexes.
|
// After a mutex attributes object has been used to initialize one or more
|
||||||
|
// mutexes, any function affecting the attributes object
|
||||||
|
// (including destruction) shall not affect any previously initialized mutexes.
|
||||||
status = pthread_mutexattr_destroy(&mutexAttr);
|
status = pthread_mutexattr_destroy(&mutexAttr);
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
sif::error << "Mutex: Attribute destroy failed with " << strerror(status) << std::endl;
|
sif::error << "Mutex: Attribute destroy failed with " << strerror(status) << std::endl;
|
||||||
@ -38,15 +38,13 @@ Mutex::~Mutex() {
|
|||||||
pthread_mutex_destroy(&mutex);
|
pthread_mutex_destroy(&mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
|
ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType, uint32_t timeoutMs) {
|
||||||
int status = 0;
|
int status = 0;
|
||||||
if(timeoutMs == MutexIF::POLLING) {
|
|
||||||
status = pthread_mutex_trylock(&mutex);
|
if(timeoutType == TimeoutType::POLLING) {
|
||||||
|
status = pthread_mutex_trylock(&mutex);
|
||||||
}
|
}
|
||||||
else if (timeoutMs == MutexIF::BLOCKING) {
|
else if (timeoutType == TimeoutType::WAITING) {
|
||||||
status = pthread_mutex_lock(&mutex);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
timespec timeOut;
|
timespec timeOut;
|
||||||
clock_gettime(CLOCK_REALTIME, &timeOut);
|
clock_gettime(CLOCK_REALTIME, &timeOut);
|
||||||
uint64_t nseconds = timeOut.tv_sec * 1000000000 + timeOut.tv_nsec;
|
uint64_t nseconds = timeOut.tv_sec * 1000000000 + timeOut.tv_nsec;
|
||||||
@ -55,25 +53,34 @@ ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
|
|||||||
timeOut.tv_nsec = nseconds - timeOut.tv_sec * 1000000000;
|
timeOut.tv_nsec = nseconds - timeOut.tv_sec * 1000000000;
|
||||||
status = pthread_mutex_timedlock(&mutex, &timeOut);
|
status = pthread_mutex_timedlock(&mutex, &timeOut);
|
||||||
}
|
}
|
||||||
|
else if(timeoutType == TimeoutType::BLOCKING) {
|
||||||
|
status = pthread_mutex_lock(&mutex);
|
||||||
|
}
|
||||||
|
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case EINVAL:
|
case EINVAL:
|
||||||
//The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's priority is higher than the mutex's current priority ceiling.
|
// The mutex was created with the protocol attribute having the value
|
||||||
|
// PTHREAD_PRIO_PROTECT and the calling thread's priority is higher
|
||||||
|
// than the mutex's current priority ceiling.
|
||||||
return WRONG_ATTRIBUTE_SETTING;
|
return WRONG_ATTRIBUTE_SETTING;
|
||||||
//The process or thread would have blocked, and the abs_timeout parameter specified a nanoseconds field value less than zero or greater than or equal to 1000 million.
|
// The process or thread would have blocked, and the abs_timeout
|
||||||
//The value specified by mutex does not refer to an initialized mutex object.
|
// parameter specified a nanoseconds field value less than zero or
|
||||||
|
// greater than or equal to 1000 million.
|
||||||
|
// The value specified by mutex does not refer to an initialized mutex object.
|
||||||
//return MUTEX_NOT_FOUND;
|
//return MUTEX_NOT_FOUND;
|
||||||
case EBUSY:
|
case EBUSY:
|
||||||
//The mutex could not be acquired because it was already locked.
|
// The mutex could not be acquired because it was already locked.
|
||||||
return MUTEX_ALREADY_LOCKED;
|
return MUTEX_ALREADY_LOCKED;
|
||||||
case ETIMEDOUT:
|
case ETIMEDOUT:
|
||||||
//The mutex could not be locked before the specified timeout expired.
|
// The mutex could not be locked before the specified timeout expired.
|
||||||
return MUTEX_TIMEOUT;
|
return MUTEX_TIMEOUT;
|
||||||
case EAGAIN:
|
case EAGAIN:
|
||||||
//The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.
|
// The mutex could not be acquired because the maximum number of
|
||||||
|
// recursive locks for mutex has been exceeded.
|
||||||
return MUTEX_MAX_LOCKS;
|
return MUTEX_MAX_LOCKS;
|
||||||
case EDEADLK:
|
case EDEADLK:
|
||||||
//A deadlock condition was detected or the current thread already owns the mutex.
|
// A deadlock condition was detected or the current thread
|
||||||
|
// already owns the mutex.
|
||||||
return CURR_THREAD_ALREADY_OWNS_MUTEX;
|
return CURR_THREAD_ALREADY_OWNS_MUTEX;
|
||||||
case 0:
|
case 0:
|
||||||
//Success
|
//Success
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
#ifndef OS_RTEMS_MUTEX_H_
|
#ifndef OS_LINUX_MUTEX_H_
|
||||||
#define OS_RTEMS_MUTEX_H_
|
#define OS_LINUX_MUTEX_H_
|
||||||
|
|
||||||
#include <framework/ipc/MutexIF.h>
|
#include <framework/ipc/MutexIF.h>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Mutex : public MutexIF {
|
class Mutex : public MutexIF {
|
||||||
public:
|
public:
|
||||||
Mutex();
|
Mutex();
|
||||||
virtual ~Mutex();
|
virtual ~Mutex();
|
||||||
virtual ReturnValue_t lockMutex(uint32_t timeoutMs);
|
virtual ReturnValue_t lockMutex(TimeoutType timeoutType, uint32_t timeoutMs);
|
||||||
virtual ReturnValue_t unlockMutex();
|
virtual ReturnValue_t unlockMutex();
|
||||||
private:
|
private:
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
|
Loading…
Reference in New Issue
Block a user