fsfw/osal/FreeRTOS/Mutex.cpp

52 lines
1.1 KiB
C++
Raw Normal View History

2020-08-25 18:15:02 +02:00
#include "Mutex.h"
2018-07-13 15:56:37 +02:00
2020-08-13 20:53:35 +02:00
#include "../../serviceinterface/ServiceInterfaceStream.h"
2018-07-13 15:56:37 +02:00
Mutex::Mutex() {
handle = xSemaphoreCreateMutex();
2020-05-29 14:16:44 +02:00
if(handle == nullptr) {
2020-08-07 22:10:58 +02:00
sif::error << "Mutex::Mutex(FreeRTOS): 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);
}
}
2020-08-07 22:10:58 +02:00
ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType,
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-08-07 22:10:58 +02:00
// If the timeout type is BLOCKING, this will be the correct value.
uint32_t timeout = portMAX_DELAY;
if(timeoutType == TimeoutType::POLLING) {
timeout = 0;
2020-05-29 14:16:44 +02:00
}
2020-08-07 22:10:58 +02:00
else if(timeoutType == TimeoutType::WAITING){
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
}
}