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
2021-07-14 00:54:39 +02:00
#include "fsfw/serviceinterface/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.
TickType_t timeout = portMAX_DELAY;
2022-02-02 10:29:30 +01:00
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) {
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
} 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) {
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
} else {
return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX;
}
2018-07-13 15:56:37 +02:00
}