fsfw/osal/FreeRTOS/BinarySemaphore.cpp

161 lines
4.2 KiB
C++
Raw Normal View History

2020-04-22 19:44:03 +02:00
#include <framework/osal/FreeRTOS/BinarySemaphore.h>
#include <framework/osal/FreeRTOS/TaskManagement.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
2020-05-18 20:35:13 +02:00
Semaphore::Semaphore() {
handle = xSemaphoreCreateBinary();
2020-04-22 19:44:03 +02:00
if(handle == nullptr) {
2020-05-18 20:35:13 +02:00
sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
2020-04-22 19:44:03 +02:00
}
xSemaphoreGive(handle);
2020-04-22 19:44:03 +02:00
}
2020-05-18 20:35:13 +02:00
Semaphore::~Semaphore() {
2020-04-22 19:44:03 +02:00
vSemaphoreDelete(handle);
}
// This copy ctor is important as it prevents the assignment to a ressource
// (other.handle) variable which is later deleted!
2020-05-18 20:35:13 +02:00
Semaphore::Semaphore(const Semaphore& other) {
handle = xSemaphoreCreateBinary();
2020-04-22 19:44:03 +02:00
if(handle == nullptr) {
2020-05-18 17:48:16 +02:00
sif::error << "Binary semaphore creation failure" << std::endl;
2020-04-22 19:44:03 +02:00
}
xSemaphoreGive(handle);
2020-04-22 19:44:03 +02:00
}
2020-05-18 20:35:13 +02:00
Semaphore& Semaphore::operator =(const Semaphore& s) {
2020-04-22 19:44:03 +02:00
if(this != &s) {
handle = xSemaphoreCreateBinary();
2020-04-22 19:44:03 +02:00
if(handle == nullptr) {
2020-05-18 17:48:16 +02:00
sif::error << "Binary semaphore creation failure" << std::endl;
2020-04-22 19:44:03 +02:00
}
xSemaphoreGive(handle);
2020-04-22 19:44:03 +02:00
}
return *this;
}
2020-05-18 20:35:13 +02:00
Semaphore::Semaphore(Semaphore&& s) {
handle = xSemaphoreCreateBinary();
2020-04-22 19:44:03 +02:00
if(handle == nullptr) {
2020-05-18 17:48:16 +02:00
sif::error << "Binary semaphore creation failure" << std::endl;
2020-04-22 19:44:03 +02:00
}
xSemaphoreGive(handle);
2020-04-22 19:44:03 +02:00
}
2020-05-18 20:35:13 +02:00
Semaphore& Semaphore::operator =(
Semaphore&& s) {
2020-04-22 19:44:03 +02:00
if(&s != this) {
handle = xSemaphoreCreateBinary();
2020-04-22 19:44:03 +02:00
if(handle == nullptr) {
2020-05-18 17:48:16 +02:00
sif::error << "Binary semaphore creation failure" << std::endl;
2020-04-22 19:44:03 +02:00
}
xSemaphoreGive(handle);
2020-04-22 19:44:03 +02:00
}
return *this;
}
2020-05-18 20:35:13 +02:00
ReturnValue_t Semaphore::takeBinarySemaphore(uint32_t timeoutMs) {
2020-04-22 19:44:03 +02:00
if(handle == nullptr) {
return SEMAPHORE_NULLPOINTER;
}
2020-05-18 20:35:13 +02:00
TickType_t timeout = Semaphore::NO_BLOCK_TICKS;
if(timeoutMs == Semaphore::BLOCK_TIMEOUT) {
timeout = Semaphore::BLOCK_TIMEOUT_TICKS;
2020-04-22 19:44:03 +02:00
}
2020-05-18 20:35:13 +02:00
else if(timeoutMs > Semaphore::NO_BLOCK_TIMEOUT){
2020-04-22 19:44:03 +02:00
timeout = pdMS_TO_TICKS(timeoutMs);
}
BaseType_t returncode = xSemaphoreTake(handle, timeout);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
}
else {
return SEMAPHORE_TIMEOUT;
}
}
2020-05-18 20:35:13 +02:00
ReturnValue_t Semaphore::takeBinarySemaphoreTickTimeout(
2020-04-22 19:44:03 +02:00
TickType_t timeoutTicks) {
if(handle == nullptr) {
return SEMAPHORE_NULLPOINTER;
}
BaseType_t returncode = xSemaphoreTake(handle, timeoutTicks);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return SEMAPHORE_TIMEOUT;
}
}
2020-05-18 20:35:13 +02:00
ReturnValue_t Semaphore::giveBinarySemaphore() {
2020-04-22 19:44:03 +02:00
if (handle == nullptr) {
return SEMAPHORE_NULLPOINTER;
}
BaseType_t returncode = xSemaphoreGive(handle);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return SEMAPHORE_NOT_OWNED;
}
}
2020-05-18 20:35:13 +02:00
SemaphoreHandle_t Semaphore::getSemaphore() {
2020-04-22 19:44:03 +02:00
return handle;
}
2020-05-18 20:35:13 +02:00
ReturnValue_t Semaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore) {
2020-04-22 19:44:03 +02:00
if (semaphore == nullptr) {
return SEMAPHORE_NULLPOINTER;
}
BaseType_t returncode = xSemaphoreGive(semaphore);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
2020-05-18 20:35:13 +02:00
void Semaphore::resetSemaphore() {
2020-04-22 19:44:03 +02:00
if(handle != nullptr) {
vSemaphoreDelete(handle);
handle = xSemaphoreCreateBinary();
xSemaphoreGive(handle);
2020-04-22 19:44:03 +02:00
}
}
2020-05-18 20:35:13 +02:00
ReturnValue_t Semaphore::acquire(uint32_t timeoutMs) {
return takeBinarySemaphore(timeoutMs);
}
2020-05-18 20:35:13 +02:00
ReturnValue_t Semaphore::release() {
return giveBinarySemaphore();
}
2020-05-18 20:35:13 +02:00
uint8_t Semaphore::getSemaphoreCounter() {
return uxSemaphoreGetCount(handle);
}
2020-04-22 19:44:03 +02:00
// Be careful with the stack size here. This is called from an ISR!
2020-05-18 20:35:13 +02:00
ReturnValue_t Semaphore::giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore,
2020-04-22 19:44:03 +02:00
BaseType_t * higherPriorityTaskWoken) {
if (semaphore == nullptr) {
return SEMAPHORE_NULLPOINTER;
}
BaseType_t returncode = xSemaphoreGiveFromISR(semaphore, higherPriorityTaskWoken);
if (returncode == pdPASS) {
if(*higherPriorityTaskWoken == pdPASS) {
// Request context switch because unblocking the semaphore
// caused a high priority task unblock.
TaskManagement::requestContextSwitch(CallContext::isr);
}
return HasReturnvaluesIF::RETURN_OK;
} else {
return SEMAPHORE_NOT_OWNED;
}
}
2020-05-18 20:35:13 +02:00