implemented new mutex interface
This commit is contained in:
@ -2,9 +2,6 @@
|
||||
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
const uint32_t MutexIF::POLLING = 0;
|
||||
const uint32_t MutexIF::BLOCKING = portMAX_DELAY;
|
||||
|
||||
Mutex::Mutex() {
|
||||
handle = xSemaphoreCreateMutex();
|
||||
if(handle == nullptr) {
|
||||
@ -19,15 +16,17 @@ Mutex::~Mutex() {
|
||||
|
||||
}
|
||||
|
||||
ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
|
||||
ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) {
|
||||
if (handle == nullptr) {
|
||||
return MutexIF::MUTEX_NOT_FOUND;
|
||||
}
|
||||
TickType_t timeout = MutexIF::POLLING;
|
||||
if(timeoutMs == MutexIF::BLOCKING) {
|
||||
timeout = MutexIF::BLOCKING;
|
||||
// If the timeout type is BLOCKING, this will be the correct value.
|
||||
uint32_t timeout = portMAX_DELAY;
|
||||
if(timeoutType == TimeoutType::POLLING) {
|
||||
timeout = 0;
|
||||
}
|
||||
else if(timeoutMs > MutexIF::POLLING){
|
||||
else if(timeoutType == TimeoutType::WAITING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,8 @@ class Mutex : public MutexIF {
|
||||
public:
|
||||
Mutex();
|
||||
~Mutex();
|
||||
ReturnValue_t lockMutex(uint32_t timeoutMs = MutexIF::BLOCKING) override;
|
||||
ReturnValue_t lockMutex(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) override;
|
||||
ReturnValue_t unlockMutex() override;
|
||||
|
||||
private:
|
||||
|
@ -40,9 +40,13 @@ Mutex::~Mutex() {
|
||||
pthread_mutex_destroy(&mutex);
|
||||
}
|
||||
|
||||
ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
|
||||
ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType, uint32_t timeoutMs) {
|
||||
int status = 0;
|
||||
if (timeoutMs != MutexIF::BLOCKING) {
|
||||
|
||||
if(timeoutType == TimeoutType::POLLING) {
|
||||
status = pthread_mutex_trylock(&mutex);
|
||||
}
|
||||
else if (timeoutType == TimeoutType::WAITING) {
|
||||
timespec timeOut;
|
||||
clock_gettime(CLOCK_REALTIME, &timeOut);
|
||||
uint64_t nseconds = timeOut.tv_sec * 1000000000 + timeOut.tv_nsec;
|
||||
@ -50,9 +54,11 @@ ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
|
||||
timeOut.tv_sec = nseconds / 1000000000;
|
||||
timeOut.tv_nsec = nseconds - timeOut.tv_sec * 1000000000;
|
||||
status = pthread_mutex_timedlock(&mutex, &timeOut);
|
||||
} else {
|
||||
}
|
||||
else if(timeoutType == TimeoutType::BLOCKING) {
|
||||
status = pthread_mutex_lock(&mutex);
|
||||
}
|
||||
|
||||
switch (status) {
|
||||
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.
|
||||
|
@ -12,7 +12,7 @@ class Mutex : public MutexIF {
|
||||
public:
|
||||
Mutex();
|
||||
virtual ~Mutex();
|
||||
virtual ReturnValue_t lockMutex(uint32_t timeoutMs);
|
||||
virtual ReturnValue_t lockMutex(TimeoutType timeoutType, uint32_t timeoutMs);
|
||||
virtual ReturnValue_t unlockMutex();
|
||||
private:
|
||||
pthread_mutex_t mutex;
|
||||
|
Reference in New Issue
Block a user