fsfw/osal/FreeRTOS/Mutex.cpp
Robin.Mueller bfc7a768ce message queue adaptions for calls from ISR
functions moved to top
binary sempahore file init
mutex return values
2020-02-25 12:54:28 +01:00

50 lines
1.0 KiB
C++

#include "Mutex.h"
#include <framework/serviceinterface/ServiceInterfaceStream.h>
const uint32_t MutexIF::NO_TIMEOUT = 0;
Mutex::Mutex() {
handle = xSemaphoreCreateMutex();
if(handle == NULL) {
error << "Mutex creation failure" << std::endl;
}
}
Mutex::~Mutex() {
if (handle != 0) {
vSemaphoreDelete(handle);
}
}
ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
if (handle == 0) {
return MutexIF::MUTEX_NOT_FOUND;
}
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 {
return MutexIF::MUTEX_TIMEOUT;
}
}
ReturnValue_t Mutex::unlockMutex() {
if (handle == 0) {
//TODO Does not exist
return HasReturnvaluesIF::RETURN_FAILED;
}
BaseType_t returncode = xSemaphoreGive(handle);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX;
}
}