some safety updates and fixes

This commit is contained in:
Robin Müller 2020-05-27 23:29:06 +02:00
parent 63dbf99592
commit 7ce505fdf9
4 changed files with 52 additions and 13 deletions

View File

@ -1,12 +1,22 @@
#include <framework/osal/FreeRTOS/BinSemaphUsingTask.h> #include <framework/osal/FreeRTOS/BinSemaphUsingTask.h>
#include <framework/osal/FreeRTOS/TaskManagement.h> #include <framework/osal/FreeRTOS/TaskManagement.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() { BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() {
handle = TaskManagement::getCurrentTaskHandle(); handle = TaskManagement::getCurrentTaskHandle();
if(handle == nullptr) {
sif::error << "Could not retrieve task handle. Please ensure the"
"constructor was called inside a task." << std::endl;
}
xTaskNotifyGive(handle); xTaskNotifyGive(handle);
locked = false; locked = false;
} }
BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() {
// Clear notification value on destruction.
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
}
ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) { ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) {
TickType_t timeout = SemaphoreIF::NO_TIMEOUT; TickType_t timeout = SemaphoreIF::NO_TIMEOUT;
if(timeoutMs == SemaphoreIF::MAX_TIMEOUT) { if(timeoutMs == SemaphoreIF::MAX_TIMEOUT) {

View File

@ -10,7 +10,9 @@ extern "C" {
} }
/** /**
* @brief Binary Semaphore implementation using Task Notifications * @brief Binary Semaphore implementation using the task notification value.
* The notification value should therefore not be used
* for other purposes.
* @details * @details
* Additional information: https://www.freertos.org/RTOS-task-notifications.html * Additional information: https://www.freertos.org/RTOS-task-notifications.html
* and general semaphore documentation. * and general semaphore documentation.
@ -22,6 +24,8 @@ public:
//! @brief Default ctor //! @brief Default ctor
BinarySemaphoreUsingTask(); BinarySemaphoreUsingTask();
//! @brief Default dtor
virtual~ BinarySemaphoreUsingTask();
ReturnValue_t acquire(uint32_t timeoutMs = ReturnValue_t acquire(uint32_t timeoutMs =
SemaphoreIF::NO_TIMEOUT) override; SemaphoreIF::NO_TIMEOUT) override;

View File

@ -9,13 +9,35 @@ CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(const uint8_t maxCount,
"intial cout. Setting initial count to max count." << std::endl; "intial cout. Setting initial count to max count." << std::endl;
initCount = maxCount; initCount = maxCount;
} }
handle = TaskManagement::getCurrentTaskHandle(); handle = TaskManagement::getCurrentTaskHandle();
if(handle == nullptr) {
sif::error << "Could not retrieve task handle. Please ensure the"
"constructor was called inside a task." << std::endl;
}
uint32_t oldNotificationValue;
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite,
&oldNotificationValue);
if(oldNotificationValue != 0) {
sif::warning << "Semaphore initiated but current notification value"
" is not 0. Please ensure the notification value is not used"
"for other purposes!" << std::endl;
}
while(currentCount != initCount) { while(currentCount != initCount) {
xTaskNotifyGive(handle); xTaskNotifyGive(handle);
currentCount++; currentCount++;
} }
} }
CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() {
// Clear notification value on destruction.
// If this is not desired, don't call the destructor
// (or implement a boolea which disables the reset)
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
}
ReturnValue_t CountingSemaphoreUsingTask::acquire(uint32_t timeoutMs) { ReturnValue_t CountingSemaphoreUsingTask::acquire(uint32_t timeoutMs) {
TickType_t timeout = SemaphoreIF::NO_TIMEOUT; TickType_t timeout = SemaphoreIF::NO_TIMEOUT;
if(timeoutMs == SemaphoreIF::MAX_TIMEOUT) { if(timeoutMs == SemaphoreIF::MAX_TIMEOUT) {
@ -24,22 +46,16 @@ ReturnValue_t CountingSemaphoreUsingTask::acquire(uint32_t timeoutMs) {
else if(timeoutMs > SemaphoreIF::NO_TIMEOUT){ else if(timeoutMs > SemaphoreIF::NO_TIMEOUT){
timeout = pdMS_TO_TICKS(timeoutMs); timeout = pdMS_TO_TICKS(timeoutMs);
} }
return acquireWithTickTimeout(timeout);
BaseType_t returncode = ulTaskNotifyTake(pdFALSE, timeout);
if (returncode == pdPASS) {
currentCount--;
return HasReturnvaluesIF::RETURN_OK;
}
else {
return SemaphoreIF::SEMAPHORE_TIMEOUT;
}
} }
ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout( ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout(
TickType_t timeoutTicks) { TickType_t timeoutTicks) {
BaseType_t returncode = ulTaskNotifyTake(pdFALSE, timeoutTicks); // Decrement notfication value without resetting it.
if (returncode == pdPASS) { BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks);
currentCount--; if (getSemaphoreCounter() == oldCount - 1) {
currentCount --;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
else { else {
@ -63,7 +79,7 @@ ReturnValue_t CountingSemaphoreUsingTask::release() {
} }
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounter() const { uint8_t CountingSemaphoreUsingTask::getSemaphoreCounter() const {
uint32_t notificationValue; uint32_t notificationValue = 0;
xTaskNotifyAndQuery(handle, 0, eNoAction, &notificationValue); xTaskNotifyAndQuery(handle, 0, eNoAction, &notificationValue);
return notificationValue; return notificationValue;
} }

View File

@ -9,9 +9,18 @@ extern "C" {
#include <freertos/task.h> #include <freertos/task.h>
} }
/**
* @brief Couting Semaphore implementation which uses the notification value
* of the task. The notification value should therefore not be used
* for other purposes
* @details
* Additional information: https://www.freertos.org/RTOS-task-notifications.html
* and general semaphore documentation.
*/
class CountingSemaphoreUsingTask: public SemaphoreIF { class CountingSemaphoreUsingTask: public SemaphoreIF {
public: public:
CountingSemaphoreUsingTask(const uint8_t maxCount, uint8_t initCount); CountingSemaphoreUsingTask(const uint8_t maxCount, uint8_t initCount);
virtual ~CountingSemaphoreUsingTask();
ReturnValue_t acquire(uint32_t timeoutMs = SemaphoreIF::NO_TIMEOUT) override; ReturnValue_t acquire(uint32_t timeoutMs = SemaphoreIF::NO_TIMEOUT) override;
ReturnValue_t release() override; ReturnValue_t release() override;