fsfw/src/fsfw/osal/freertos/Mutex.cpp

51 lines
1.2 KiB
C++
Raw Normal View History

2021-07-14 00:54:39 +02:00
#include "fsfw/osal/freertos/Mutex.h"
2018-07-13 15:56:37 +02:00
2022-05-18 10:51:38 +02:00
#include "fsfw/serviceinterface.h"
2018-07-13 15:56:37 +02:00
Mutex::Mutex() {
2022-02-02 10:29:30 +01:00
handle = xSemaphoreCreateMutex();
if (handle == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "Mutex::Mutex(FreeRTOS): Creation failure" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
}
2018-07-13 15:56:37 +02:00
}
Mutex::~Mutex() {
2022-02-02 10:29:30 +01:00
if (handle != nullptr) {
vSemaphoreDelete(handle);
}
2018-07-13 15:56:37 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType, uint32_t timeoutMs) {
if (handle == nullptr) {
return MutexIF::MUTEX_NOT_FOUND;
}
// 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 (timeoutType == TimeoutType::WAITING) {
timeout = pdMS_TO_TICKS(timeoutMs);
}
2018-07-13 15:56:37 +02:00
2022-02-02 10:29:30 +01:00
BaseType_t returncode = xSemaphoreTake(handle, timeout);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return MutexIF::MUTEX_TIMEOUT;
}
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Mutex::unlockMutex() {
2022-02-02 10:29:30 +01:00
if (handle == nullptr) {
return MutexIF::MUTEX_NOT_FOUND;
}
BaseType_t returncode = xSemaphoreGive(handle);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX;
}
2018-07-13 15:56:37 +02:00
}