2020-05-29 14:02:14 +02:00
|
|
|
#include <framework/osal/FreeRTOS/Mutex.h>
|
2018-07-13 15:56:37 +02:00
|
|
|
|
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
|
|
|
|
|
|
|
const uint32_t MutexIF::NO_TIMEOUT = 0;
|
|
|
|
|
|
|
|
Mutex::Mutex() {
|
|
|
|
handle = xSemaphoreCreateMutex();
|
2020-05-29 14:02:14 +02:00
|
|
|
if(handle == NULL) {
|
|
|
|
sif::error << "Mutex creation failure" << std::endl;
|
|
|
|
}
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::~Mutex() {
|
|
|
|
if (handle != 0) {
|
|
|
|
vSemaphoreDelete(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
|
|
|
|
if (handle == 0) {
|
2020-05-29 14:02:14 +02:00
|
|
|
return MutexIF::MUTEX_NOT_FOUND;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
TickType_t timeout = portMAX_DELAY;
|
|
|
|
if (timeoutMs != NO_TIMEOUT) {
|
|
|
|
timeout = pdMS_TO_TICKS(timeoutMs);
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseType_t returncode = xSemaphoreTake(handle, timeout);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
2020-05-29 14:02:14 +02:00
|
|
|
return MutexIF::MUTEX_TIMEOUT;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t Mutex::unlockMutex() {
|
|
|
|
if (handle == 0) {
|
2020-05-29 14:02:14 +02:00
|
|
|
return MutexIF::MUTEX_NOT_FOUND;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
BaseType_t returncode = xSemaphoreGive(handle);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
2020-05-29 14:02:14 +02:00
|
|
|
return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|