fsfw/osal/FreeRTOS/Mutex.cpp

53 lines
1.2 KiB
C++
Raw Normal View History

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>
2020-06-04 20:12:37 +02:00
const uint32_t MutexIF::POLLING = 0;
const uint32_t MutexIF::BLOCKING = portMAX_DELAY;
2018-07-13 15:56:37 +02:00
Mutex::Mutex() {
handle = xSemaphoreCreateMutex();
2020-05-29 14:16:44 +02:00
if(handle == nullptr) {
sif::error << "Mutex: Creation failure" << std::endl;
2020-05-29 14:02:14 +02:00
}
2018-07-13 15:56:37 +02:00
}
Mutex::~Mutex() {
2020-05-29 14:16:44 +02:00
if (handle != nullptr) {
2018-07-13 15:56:37 +02:00
vSemaphoreDelete(handle);
}
}
ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
2020-05-29 14:16:44 +02:00
if (handle == nullptr) {
2020-05-29 14:02:14 +02:00
return MutexIF::MUTEX_NOT_FOUND;
2018-07-13 15:56:37 +02:00
}
2020-06-04 20:12:37 +02:00
TickType_t timeout = MutexIF::POLLING;
if(timeoutMs == MutexIF::BLOCKING) {
timeout = MutexIF::BLOCKING;
2020-05-29 14:16:44 +02:00
}
2020-06-04 20:12:37 +02:00
else if(timeoutMs > MutexIF::POLLING){
2018-07-13 15:56:37 +02:00
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() {
2020-05-29 14:16:44 +02:00
if (handle == nullptr) {
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
}
}