Merge pull request 'Semaphore Initialization - Linux and FreeRTOS' (#89) from KSat/fsfw:mueller_binSemaphoreInit into master
Reviewed-on: fsfw/fsfw#89
This commit is contained in:
commit
ce4d010b5c
95
osal/FreeRTOS/BinSemaphUsingTask.cpp
Normal file
95
osal/FreeRTOS/BinSemaphUsingTask.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
#include "../../osal/FreeRTOS/BinSemaphUsingTask.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() {
|
||||
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);
|
||||
}
|
||||
|
||||
BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() {
|
||||
// Clear notification value on destruction.
|
||||
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::acquire(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) {
|
||||
TickType_t timeout = 0;
|
||||
if(timeoutType == TimeoutType::POLLING) {
|
||||
timeout = 0;
|
||||
}
|
||||
else if(timeoutType == TimeoutType::WAITING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
else {
|
||||
timeout = portMAX_DELAY;
|
||||
}
|
||||
return acquireWithTickTimeout(timeoutType, timeout);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout(
|
||||
TimeoutType timeoutType, TickType_t timeoutTicks) {
|
||||
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::release() {
|
||||
return release(this->handle);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::release(
|
||||
TaskHandle_t taskHandle) {
|
||||
if(getSemaphoreCounter(taskHandle) == 1) {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
BaseType_t returncode = xTaskNotifyGive(taskHandle);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
// This should never happen.
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
TaskHandle_t BinarySemaphoreUsingTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter() const {
|
||||
return getSemaphoreCounter(this->handle);
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter(
|
||||
TaskHandle_t taskHandle) {
|
||||
uint32_t notificationValue;
|
||||
xTaskNotifyAndQuery(taskHandle, 0, eNoAction, ¬ificationValue);
|
||||
return notificationValue;
|
||||
}
|
||||
|
||||
// Be careful with the stack size here. This is called from an ISR!
|
||||
ReturnValue_t BinarySemaphoreUsingTask::releaseFromISR(
|
||||
TaskHandle_t taskHandle, BaseType_t * higherPriorityTaskWoken) {
|
||||
if(getSemaphoreCounterFromISR(taskHandle, higherPriorityTaskWoken) == 1) {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
vTaskNotifyGiveFromISR(taskHandle, higherPriorityTaskWoken);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounterFromISR(
|
||||
TaskHandle_t taskHandle, BaseType_t* higherPriorityTaskWoken) {
|
||||
uint32_t notificationValue = 0;
|
||||
xTaskNotifyAndQueryFromISR(taskHandle, 0, eNoAction, ¬ificationValue,
|
||||
higherPriorityTaskWoken);
|
||||
return notificationValue;
|
||||
}
|
76
osal/FreeRTOS/BinSemaphUsingTask.h
Normal file
76
osal/FreeRTOS/BinSemaphUsingTask.h
Normal file
@ -0,0 +1,76 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
|
||||
|
||||
#include "../../returnvalues/HasReturnvaluesIF.h"
|
||||
#include "../../tasks/SemaphoreIF.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
/**
|
||||
* @brief Binary Semaphore implementation using the task notification value.
|
||||
* 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 BinarySemaphoreUsingTask: public SemaphoreIF,
|
||||
public HasReturnvaluesIF {
|
||||
public:
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
|
||||
|
||||
//! @brief Default ctor
|
||||
BinarySemaphoreUsingTask();
|
||||
//! @brief Default dtor
|
||||
virtual~ BinarySemaphoreUsingTask();
|
||||
|
||||
ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING,
|
||||
uint32_t timeoutMs = portMAX_DELAY) override;
|
||||
ReturnValue_t release() override;
|
||||
uint8_t getSemaphoreCounter() const override;
|
||||
static uint8_t getSemaphoreCounter(TaskHandle_t taskHandle);
|
||||
static uint8_t getSemaphoreCounterFromISR(TaskHandle_t taskHandle,
|
||||
BaseType_t* higherPriorityTaskWoken);
|
||||
|
||||
/**
|
||||
* Same as acquire() with timeout in FreeRTOS ticks.
|
||||
* @param timeoutTicks
|
||||
* @return - @c RETURN_OK on success
|
||||
* - @c RETURN_FAILED on failure
|
||||
*/
|
||||
ReturnValue_t acquireWithTickTimeout(
|
||||
TimeoutType timeoutType = TimeoutType::BLOCKING,
|
||||
TickType_t timeoutTicks = portMAX_DELAY);
|
||||
|
||||
/**
|
||||
* Get handle to the task related to the semaphore.
|
||||
* @return
|
||||
*/
|
||||
TaskHandle_t getTaskHandle();
|
||||
|
||||
/**
|
||||
* Wrapper function to give back semaphore from handle
|
||||
* @param semaphore
|
||||
* @return - @c RETURN_OK on success
|
||||
* - @c RETURN_FAILED on failure
|
||||
*/
|
||||
static ReturnValue_t release(TaskHandle_t taskToNotify);
|
||||
|
||||
/**
|
||||
* Wrapper function to give back semaphore from handle when called from an ISR
|
||||
* @param semaphore
|
||||
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with
|
||||
* a higher priority was unblocked. A context switch should be requested
|
||||
* from an ISR if this is the case (see TaskManagement functions)
|
||||
* @return - @c RETURN_OK on success
|
||||
* - @c RETURN_FAILED on failure
|
||||
*/
|
||||
static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify,
|
||||
BaseType_t * higherPriorityTaskWoken);
|
||||
|
||||
protected:
|
||||
TaskHandle_t handle;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ */
|
108
osal/FreeRTOS/BinarySemaphore.cpp
Normal file
108
osal/FreeRTOS/BinarySemaphore.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
#include "../../osal/FreeRTOS/BinarySemaphore.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
BinarySemaphore::BinarySemaphore() {
|
||||
handle = xSemaphoreCreateBinary();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
|
||||
}
|
||||
// Initiated semaphore must be given before it can be taken.
|
||||
xSemaphoreGive(handle);
|
||||
}
|
||||
|
||||
BinarySemaphore::~BinarySemaphore() {
|
||||
vSemaphoreDelete(handle);
|
||||
}
|
||||
|
||||
BinarySemaphore::BinarySemaphore(BinarySemaphore&& s) {
|
||||
handle = xSemaphoreCreateBinary();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "Binary semaphore creation failure" << std::endl;
|
||||
}
|
||||
xSemaphoreGive(handle);
|
||||
}
|
||||
|
||||
BinarySemaphore& BinarySemaphore::operator =(
|
||||
BinarySemaphore&& s) {
|
||||
if(&s != this) {
|
||||
handle = xSemaphoreCreateBinary();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "Binary semaphore creation failure" << std::endl;
|
||||
}
|
||||
xSemaphoreGive(handle);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) {
|
||||
TickType_t timeout = 0;
|
||||
if(timeoutType == TimeoutType::POLLING) {
|
||||
timeout = 0;
|
||||
}
|
||||
else if(timeoutType == TimeoutType::WAITING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
else {
|
||||
timeout = portMAX_DELAY;
|
||||
}
|
||||
return acquireWithTickTimeout(timeoutType, timeout);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TimeoutType timeoutType,
|
||||
TickType_t timeoutTicks) {
|
||||
if(handle == nullptr) {
|
||||
return SemaphoreIF::SEMAPHORE_INVALID;
|
||||
}
|
||||
|
||||
BaseType_t returncode = xSemaphoreTake(handle, timeoutTicks);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphore::release() {
|
||||
return release(handle);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphore::release(SemaphoreHandle_t semaphore) {
|
||||
if (semaphore == nullptr) {
|
||||
return SemaphoreIF::SEMAPHORE_INVALID;
|
||||
}
|
||||
BaseType_t returncode = xSemaphoreGive(semaphore);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphore::getSemaphoreCounter() const {
|
||||
return uxSemaphoreGetCount(handle);
|
||||
}
|
||||
|
||||
SemaphoreHandle_t BinarySemaphore::getSemaphore() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
|
||||
// Be careful with the stack size here. This is called from an ISR!
|
||||
ReturnValue_t BinarySemaphore::releaseFromISR(
|
||||
SemaphoreHandle_t semaphore, BaseType_t * higherPriorityTaskWoken) {
|
||||
if (semaphore == nullptr) {
|
||||
return SemaphoreIF::SEMAPHORE_INVALID;
|
||||
}
|
||||
BaseType_t returncode = xSemaphoreGiveFromISR(semaphore,
|
||||
higherPriorityTaskWoken);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
}
|
107
osal/FreeRTOS/BinarySemaphore.h
Normal file
107
osal/FreeRTOS/BinarySemaphore.h
Normal file
@ -0,0 +1,107 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
|
||||
|
||||
#include "../../returnvalues/HasReturnvaluesIF.h"
|
||||
#include "../../tasks/SemaphoreIF.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
/**
|
||||
* @brief OS Tool to achieve synchronization of between tasks or between
|
||||
* task and ISR. The default semaphore implementation creates a
|
||||
* binary semaphore, which can only be taken once.
|
||||
* @details
|
||||
* Documentation: https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html
|
||||
*
|
||||
* Please note that if the semaphore implementation is only related to
|
||||
* the synchronization of one task, the new task notifications can be used,
|
||||
* also see the BinSemaphUsingTask and CountingSemaphUsingTask classes.
|
||||
* These use the task notification value instead of a queue and are
|
||||
* faster and more efficient.
|
||||
*
|
||||
* @author R. Mueller
|
||||
* @ingroup osal
|
||||
*/
|
||||
class BinarySemaphore: public SemaphoreIF,
|
||||
public HasReturnvaluesIF {
|
||||
public:
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
|
||||
|
||||
//! @brief Default ctor
|
||||
BinarySemaphore();
|
||||
//! @brief Copy ctor, deleted explicitely.
|
||||
BinarySemaphore(const BinarySemaphore&) = delete;
|
||||
//! @brief Copy assignment, deleted explicitely.
|
||||
BinarySemaphore& operator=(const BinarySemaphore&) = delete;
|
||||
//! @brief Move ctor
|
||||
BinarySemaphore (BinarySemaphore &&);
|
||||
//! @brief Move assignment
|
||||
BinarySemaphore & operator=(BinarySemaphore &&);
|
||||
//! @brief Destructor
|
||||
virtual ~BinarySemaphore();
|
||||
|
||||
uint8_t getSemaphoreCounter() const override;
|
||||
|
||||
/**
|
||||
* Take the binary semaphore.
|
||||
* If the semaphore has already been taken, the task will be blocked
|
||||
* for a maximum of #timeoutMs or until the semaphore is given back,
|
||||
* for example by an ISR or another task.
|
||||
* @param timeoutMs
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
|
||||
*/
|
||||
ReturnValue_t acquire(TimeoutType timeoutType =
|
||||
TimeoutType::BLOCKING, uint32_t timeoutMs = portMAX_DELAY) override;
|
||||
|
||||
/**
|
||||
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
|
||||
* @param timeoutTicks
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
|
||||
*/
|
||||
ReturnValue_t acquireWithTickTimeout(TimeoutType timeoutType =
|
||||
TimeoutType::BLOCKING, TickType_t timeoutTicks = portMAX_DELAY);
|
||||
|
||||
/**
|
||||
* Release the binary semaphore.
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
|
||||
* already available.
|
||||
*/
|
||||
ReturnValue_t release() override;
|
||||
|
||||
/**
|
||||
* Get Handle to the semaphore.
|
||||
* @return
|
||||
*/
|
||||
SemaphoreHandle_t getSemaphore();
|
||||
|
||||
/**
|
||||
* Wrapper function to give back semaphore from handle
|
||||
* @param semaphore
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
|
||||
* already available.
|
||||
*/
|
||||
static ReturnValue_t release(SemaphoreHandle_t semaphore);
|
||||
|
||||
/**
|
||||
* Wrapper function to give back semaphore from handle when called from an ISR
|
||||
* @param semaphore
|
||||
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with
|
||||
* a higher priority was unblocked. A context switch from an ISR should
|
||||
* then be requested (see TaskManagement functions)
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
|
||||
* already available.
|
||||
*/
|
||||
static ReturnValue_t releaseFromISR(SemaphoreHandle_t semaphore,
|
||||
BaseType_t * higherPriorityTaskWoken);
|
||||
|
||||
protected:
|
||||
SemaphoreHandle_t handle;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */
|
114
osal/FreeRTOS/CountingSemaphUsingTask.cpp
Normal file
114
osal/FreeRTOS/CountingSemaphUsingTask.cpp
Normal file
@ -0,0 +1,114 @@
|
||||
#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(const uint8_t maxCount,
|
||||
uint8_t initCount): maxCount(maxCount) {
|
||||
if(initCount > maxCount) {
|
||||
sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
|
||||
"intial cout. Setting initial count to max count." << std::endl;
|
||||
initCount = maxCount;
|
||||
}
|
||||
|
||||
handle = TaskManagement::getCurrentTaskHandle();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphoreUsingTask: 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 << "CountinSemaphoreUsingTask: Semaphore initiated but "
|
||||
"current notification value is not 0. Please ensure the "
|
||||
"notification value is not used for other purposes!" << std::endl;
|
||||
}
|
||||
for(int i = 0; i < initCount; i++) {
|
||||
xTaskNotifyGive(handle);
|
||||
}
|
||||
}
|
||||
|
||||
CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() {
|
||||
// Clear notification value on destruction.
|
||||
// If this is not desired, don't call the destructor
|
||||
// (or implement a boolean which disables the reset)
|
||||
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::acquire(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) {
|
||||
TickType_t timeout = 0;
|
||||
if(timeoutType == TimeoutType::POLLING) {
|
||||
timeout = 0;
|
||||
}
|
||||
else if(timeoutType == TimeoutType::WAITING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
else {
|
||||
timeout = portMAX_DELAY;
|
||||
}
|
||||
return acquireWithTickTimeout(timeoutType, timeout);
|
||||
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout(
|
||||
TimeoutType timeoutType, TickType_t timeoutTicks) {
|
||||
// Decrement notfication value without resetting it.
|
||||
BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks);
|
||||
if (getSemaphoreCounter() == oldCount - 1) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::release() {
|
||||
if(getSemaphoreCounter() == maxCount) {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
return release(handle);
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::release(
|
||||
TaskHandle_t taskToNotify) {
|
||||
BaseType_t returncode = xTaskNotifyGive(taskToNotify);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
// This should never happen.
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounter() const {
|
||||
uint32_t notificationValue = 0;
|
||||
xTaskNotifyAndQuery(handle, 0, eNoAction, ¬ificationValue);
|
||||
return notificationValue;
|
||||
}
|
||||
|
||||
TaskHandle_t CountingSemaphoreUsingTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::releaseFromISR(
|
||||
TaskHandle_t taskToNotify, BaseType_t* higherPriorityTaskWoken) {
|
||||
vTaskNotifyGiveFromISR(taskToNotify, higherPriorityTaskWoken);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounterFromISR(
|
||||
TaskHandle_t task, BaseType_t* higherPriorityTaskWoken) {
|
||||
uint32_t notificationValue;
|
||||
xTaskNotifyAndQueryFromISR(task, 0, eNoAction, ¬ificationValue,
|
||||
higherPriorityTaskWoken);
|
||||
return notificationValue;
|
||||
}
|
||||
|
||||
uint8_t CountingSemaphoreUsingTask::getMaxCount() const {
|
||||
return maxCount;
|
||||
}
|
102
osal/FreeRTOS/CountingSemaphUsingTask.h
Normal file
102
osal/FreeRTOS/CountingSemaphUsingTask.h
Normal file
@ -0,0 +1,102 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
|
||||
|
||||
#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
|
||||
#include "../../tasks/SemaphoreIF.h"
|
||||
|
||||
extern "C" {
|
||||
#include <freertos/FreeRTOS.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 {
|
||||
public:
|
||||
CountingSemaphoreUsingTask(const uint8_t maxCount, uint8_t initCount);
|
||||
virtual ~CountingSemaphoreUsingTask();
|
||||
|
||||
/**
|
||||
* Acquire the counting semaphore.
|
||||
* If no semaphores are available, the task will be blocked
|
||||
* for a maximum of #timeoutMs or until one is given back,
|
||||
* for example by an ISR or another task.
|
||||
* @param timeoutMs
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
|
||||
*/
|
||||
ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING,
|
||||
uint32_t timeoutMs = portMAX_DELAY) override;
|
||||
|
||||
/**
|
||||
* Release a semaphore, increasing the number of available counting
|
||||
* semaphores up to the #maxCount value.
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
|
||||
* already available.
|
||||
*/
|
||||
ReturnValue_t release() override;
|
||||
|
||||
uint8_t getSemaphoreCounter() const override;
|
||||
/**
|
||||
* Get the semaphore counter from an ISR.
|
||||
* @param task
|
||||
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with
|
||||
* a higher priority was unblocked. A context switch should be requested
|
||||
* from an ISR if this is the case (see TaskManagement functions)
|
||||
* @return
|
||||
*/
|
||||
static uint8_t getSemaphoreCounterFromISR(TaskHandle_t task,
|
||||
BaseType_t* higherPriorityTaskWoken);
|
||||
|
||||
/**
|
||||
* Acquire with a timeout value in ticks
|
||||
* @param timeoutTicks
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
|
||||
*/
|
||||
ReturnValue_t acquireWithTickTimeout(
|
||||
TimeoutType timeoutType = TimeoutType::BLOCKING,
|
||||
TickType_t timeoutTicks = portMAX_DELAY);
|
||||
|
||||
/**
|
||||
* Get handle to the task related to the semaphore.
|
||||
* @return
|
||||
*/
|
||||
TaskHandle_t getTaskHandle();
|
||||
|
||||
/**
|
||||
* Release semaphore of task by supplying task handle
|
||||
* @param taskToNotify
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
|
||||
* already available.
|
||||
*/
|
||||
static ReturnValue_t release(TaskHandle_t taskToNotify);
|
||||
/**
|
||||
* Release seamphore of a task from an ISR.
|
||||
* @param taskToNotify
|
||||
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with
|
||||
* a higher priority was unblocked. A context switch should be requested
|
||||
* from an ISR if this is the case (see TaskManagement functions)
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
|
||||
* already available.
|
||||
*/
|
||||
static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify,
|
||||
BaseType_t* higherPriorityTaskWoken);
|
||||
|
||||
uint8_t getMaxCount() const;
|
||||
|
||||
private:
|
||||
TaskHandle_t handle;
|
||||
const uint8_t maxCount;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ */
|
43
osal/FreeRTOS/CountingSemaphore.cpp
Normal file
43
osal/FreeRTOS/CountingSemaphore.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include "../../osal/FreeRTOS/CountingSemaphore.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
// Make sure #define configUSE_COUNTING_SEMAPHORES 1 is set in
|
||||
// free FreeRTOSConfig.h file.
|
||||
CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount):
|
||||
maxCount(maxCount), initCount(initCount) {
|
||||
if(initCount > maxCount) {
|
||||
sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
|
||||
"intial cout. Setting initial count to max count." << std::endl;
|
||||
initCount = maxCount;
|
||||
}
|
||||
|
||||
handle = xSemaphoreCreateCounting(maxCount, initCount);
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphore: Creation failure" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
CountingSemaphore::CountingSemaphore(CountingSemaphore&& other):
|
||||
maxCount(other.maxCount), initCount(other.initCount) {
|
||||
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphore: Creation failure" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
CountingSemaphore& CountingSemaphore::operator =(
|
||||
CountingSemaphore&& other) {
|
||||
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphore: Creation failure" << std::endl;
|
||||
}
|
||||
return * this;
|
||||
}
|
||||
|
||||
|
||||
uint8_t CountingSemaphore::getMaxCount() const {
|
||||
return maxCount;
|
||||
}
|
34
osal/FreeRTOS/CountingSemaphore.h
Normal file
34
osal/FreeRTOS/CountingSemaphore.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
|
||||
#include "../../osal/FreeRTOS/BinarySemaphore.h"
|
||||
|
||||
/**
|
||||
* @brief Counting semaphores, which can be acquire more than once.
|
||||
* @details
|
||||
* See: https://www.freertos.org/CreateCounting.html
|
||||
* API of counting semaphores is almost identical to binary semaphores,
|
||||
* so we just inherit from binary semaphore and provide the respective
|
||||
* constructors.
|
||||
*/
|
||||
class CountingSemaphore: public BinarySemaphore {
|
||||
public:
|
||||
CountingSemaphore(const uint8_t maxCount, uint8_t initCount);
|
||||
//! @brief Copy ctor, disabled
|
||||
CountingSemaphore(const CountingSemaphore&) = delete;
|
||||
//! @brief Copy assignment, disabled
|
||||
CountingSemaphore& operator=(const CountingSemaphore&) = delete;
|
||||
//! @brief Move ctor
|
||||
CountingSemaphore (CountingSemaphore &&);
|
||||
//! @brief Move assignment
|
||||
CountingSemaphore & operator=(CountingSemaphore &&);
|
||||
|
||||
/* Same API as binary semaphore otherwise. acquire() can be called
|
||||
* until there are not semaphores left and release() can be called
|
||||
* until maxCount is reached. */
|
||||
uint8_t getMaxCount() const;
|
||||
private:
|
||||
const uint8_t maxCount;
|
||||
uint8_t initCount = 0;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */
|
59
osal/FreeRTOS/SemaphoreFactory.cpp
Normal file
59
osal/FreeRTOS/SemaphoreFactory.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include "../../osal/FreeRTOS/BinarySemaphore.h"
|
||||
#include "../../osal/FreeRTOS/BinSemaphUsingTask.h"
|
||||
#include "../../osal/FreeRTOS/CountingSemaphore.h"
|
||||
#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
|
||||
#include "../../tasks/SemaphoreFactory.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr;
|
||||
|
||||
static const uint32_t USE_REGULAR_SEMAPHORES = 0;
|
||||
static const uint32_t USE_TASK_NOTIFICATIONS = 1;
|
||||
|
||||
SemaphoreFactory::SemaphoreFactory() {
|
||||
}
|
||||
|
||||
SemaphoreFactory::~SemaphoreFactory() {
|
||||
delete factoryInstance;
|
||||
}
|
||||
|
||||
SemaphoreFactory* SemaphoreFactory::instance() {
|
||||
if (factoryInstance == nullptr){
|
||||
factoryInstance = new SemaphoreFactory();
|
||||
}
|
||||
return SemaphoreFactory::factoryInstance;
|
||||
}
|
||||
|
||||
SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t argument) {
|
||||
if(argument == USE_REGULAR_SEMAPHORES) {
|
||||
return new BinarySemaphore();
|
||||
}
|
||||
else if(argument == USE_TASK_NOTIFICATIONS) {
|
||||
return new BinarySemaphoreUsingTask();
|
||||
}
|
||||
else {
|
||||
sif::warning << "SemaphoreFactory: Invalid argument, return regular"
|
||||
"binary semaphore" << std::endl;
|
||||
return new BinarySemaphore();
|
||||
}
|
||||
}
|
||||
|
||||
SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t maxCount,
|
||||
uint8_t initCount, uint32_t argument) {
|
||||
if(argument == USE_REGULAR_SEMAPHORES) {
|
||||
return new CountingSemaphore(maxCount, initCount);
|
||||
}
|
||||
else if(argument == USE_TASK_NOTIFICATIONS) {
|
||||
return new CountingSemaphoreUsingTask(maxCount, initCount);
|
||||
}
|
||||
else {
|
||||
sif::warning << "SemaphoreFactory: Invalid argument, return regular"
|
||||
"binary semaphore" << std::endl;
|
||||
return new CountingSemaphore(maxCount, initCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) {
|
||||
delete semaphore;
|
||||
}
|
149
osal/linux/BinarySemaphore.cpp
Normal file
149
osal/linux/BinarySemaphore.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
#include "../../osal/linux/BinarySemaphore.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
extern "C" {
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
}
|
||||
|
||||
BinarySemaphore::BinarySemaphore() {
|
||||
// Using unnamed semaphores for now
|
||||
initSemaphore();
|
||||
}
|
||||
|
||||
BinarySemaphore::~BinarySemaphore() {
|
||||
sem_destroy(&handle);
|
||||
}
|
||||
|
||||
BinarySemaphore::BinarySemaphore(BinarySemaphore&& s) {
|
||||
initSemaphore();
|
||||
}
|
||||
|
||||
BinarySemaphore& BinarySemaphore::operator =(
|
||||
BinarySemaphore&& s) {
|
||||
initSemaphore();
|
||||
return * this;
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) {
|
||||
int result = 0;
|
||||
if(timeoutType == TimeoutType::POLLING) {
|
||||
result = sem_trywait(&handle);
|
||||
}
|
||||
else if(timeoutType == TimeoutType::BLOCKING) {
|
||||
result = sem_wait(&handle);
|
||||
}
|
||||
else if(timeoutType == TimeoutType::WAITING){
|
||||
timespec timeOut;
|
||||
clock_gettime(CLOCK_REALTIME, &timeOut);
|
||||
uint64_t nseconds = timeOut.tv_sec * 1000000000 + timeOut.tv_nsec;
|
||||
nseconds += timeoutMs * 1000000;
|
||||
timeOut.tv_sec = nseconds / 1000000000;
|
||||
timeOut.tv_nsec = nseconds - timeOut.tv_sec * 1000000000;
|
||||
result = sem_timedwait(&handle, &timeOut);
|
||||
if(result != 0 and errno == EINVAL) {
|
||||
sif::debug << "BinarySemaphore::acquire: Invalid time value possible"
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
if(result == 0) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
switch(errno) {
|
||||
case(EAGAIN):
|
||||
// Operation could not be performed without blocking (for sem_trywait)
|
||||
case(ETIMEDOUT):
|
||||
// Semaphore is 0
|
||||
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
||||
case(EINVAL):
|
||||
// Semaphore invalid
|
||||
return SemaphoreIF::SEMAPHORE_INVALID;
|
||||
case(EINTR):
|
||||
// Call was interrupted by signal handler
|
||||
sif::debug << "BinarySemaphore::acquire: Signal handler interrupted."
|
||||
"Code " << strerror(errno) << std::endl;
|
||||
/* No break */
|
||||
default:
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphore::release() {
|
||||
return BinarySemaphore::release(&this->handle);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphore::release(sem_t *handle) {
|
||||
ReturnValue_t countResult = checkCount(handle, 1);
|
||||
if(countResult != HasReturnvaluesIF::RETURN_OK) {
|
||||
return countResult;
|
||||
}
|
||||
|
||||
int result = sem_post(handle);
|
||||
if(result == 0) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
switch(errno) {
|
||||
case(EINVAL):
|
||||
// Semaphore invalid
|
||||
return SemaphoreIF::SEMAPHORE_INVALID;
|
||||
case(EOVERFLOW):
|
||||
// SEM_MAX_VALUE overflow. This should never happen
|
||||
default:
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphore::getSemaphoreCounter() const {
|
||||
// And another ugly cast :-D
|
||||
return getSemaphoreCounter(const_cast<sem_t*>(&this->handle));
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphore::getSemaphoreCounter(sem_t *handle) {
|
||||
int value = 0;
|
||||
int result = sem_getvalue(handle, &value);
|
||||
if (result == 0) {
|
||||
return value;
|
||||
}
|
||||
else if(result != 0 and errno == EINVAL) {
|
||||
// Could be called from interrupt, use lightweight printf
|
||||
printf("BinarySemaphore::getSemaphoreCounter: Invalid semaphore\n");
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
// This should never happen.
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void BinarySemaphore::initSemaphore(uint8_t initCount) {
|
||||
auto result = sem_init(&handle, true, initCount);
|
||||
if(result == -1) {
|
||||
switch(errno) {
|
||||
case(EINVAL):
|
||||
// Value exceeds SEM_VALUE_MAX
|
||||
case(ENOSYS):
|
||||
// System does not support process-shared semaphores
|
||||
sif::error << "BinarySemaphore: Init failed with" << strerror(errno)
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphore::checkCount(sem_t* handle, uint8_t maxCount) {
|
||||
int value = getSemaphoreCounter(handle);
|
||||
if(value >= maxCount) {
|
||||
if(maxCount == 1 and value > 1) {
|
||||
// Binary Semaphore special case.
|
||||
// This is a config error use lightweight printf is this is called
|
||||
// from an interrupt
|
||||
printf("BinarySemaphore::release: Value of binary semaphore greater"
|
||||
" than 1!\n");
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
81
osal/linux/BinarySemaphore.h
Normal file
81
osal/linux/BinarySemaphore.h
Normal file
@ -0,0 +1,81 @@
|
||||
#ifndef FRAMEWORK_OSAL_LINUX_BINARYSEMPAHORE_H_
|
||||
#define FRAMEWORK_OSAL_LINUX_BINARYSEMPAHORE_H_
|
||||
|
||||
#include "../../returnvalues/HasReturnvaluesIF.h"
|
||||
#include "../../tasks/SemaphoreIF.h"
|
||||
|
||||
extern "C" {
|
||||
#include <semaphore.h>
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief OS Tool to achieve synchronization of between tasks or between
|
||||
* task and ISR. The default semaphore implementation creates a
|
||||
* binary semaphore, which can only be taken once.
|
||||
* @details
|
||||
* See: http://www.man7.org/linux/man-pages/man7/sem_overview.7.html
|
||||
* @author R. Mueller
|
||||
* @ingroup osal
|
||||
*/
|
||||
class BinarySemaphore: public SemaphoreIF,
|
||||
public HasReturnvaluesIF {
|
||||
public:
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
|
||||
|
||||
//! @brief Default ctor
|
||||
BinarySemaphore();
|
||||
//! @brief Copy ctor, deleted explicitely.
|
||||
BinarySemaphore(const BinarySemaphore&) = delete;
|
||||
//! @brief Copy assignment, deleted explicitely.
|
||||
BinarySemaphore& operator=(const BinarySemaphore&) = delete;
|
||||
//! @brief Move ctor
|
||||
BinarySemaphore (BinarySemaphore &&);
|
||||
//! @brief Move assignment
|
||||
BinarySemaphore & operator=(BinarySemaphore &&);
|
||||
//! @brief Destructor
|
||||
virtual ~BinarySemaphore();
|
||||
|
||||
void initSemaphore(uint8_t initCount = 1);
|
||||
|
||||
uint8_t getSemaphoreCounter() const override;
|
||||
static uint8_t getSemaphoreCounter(sem_t* handle);
|
||||
|
||||
/**
|
||||
* Take the binary semaphore.
|
||||
* If the semaphore has already been taken, the task will be blocked
|
||||
* for a maximum of #timeoutMs or until the semaphore is given back,
|
||||
* for example by an ISR or another task.
|
||||
* @param timeoutMs
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
|
||||
*/
|
||||
ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING,
|
||||
uint32_t timeoutMs = 0) override;
|
||||
|
||||
/**
|
||||
* Release the binary semaphore.
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
|
||||
* already available.
|
||||
*/
|
||||
virtual ReturnValue_t release() override;
|
||||
/**
|
||||
* This static function can be used to release a semaphore by providing
|
||||
* its handle.
|
||||
* @param handle
|
||||
* @return
|
||||
*/
|
||||
static ReturnValue_t release(sem_t* handle);
|
||||
|
||||
/** Checks the validity of the semaphore count against a specified
|
||||
* known maxCount
|
||||
* @param handle
|
||||
* @param maxCount
|
||||
* @return
|
||||
*/
|
||||
static ReturnValue_t checkCount(sem_t* handle, uint8_t maxCount);
|
||||
protected:
|
||||
sem_t handle;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */
|
54
osal/linux/CountingSemaphore.cpp
Normal file
54
osal/linux/CountingSemaphore.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include "../../osal/linux/CountingSemaphore.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount):
|
||||
maxCount(maxCount), initCount(initCount) {
|
||||
if(initCount > maxCount) {
|
||||
sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
|
||||
"intial cout. Setting initial count to max count." << std::endl;
|
||||
initCount = maxCount;
|
||||
}
|
||||
|
||||
initSemaphore(initCount);
|
||||
}
|
||||
|
||||
CountingSemaphore::CountingSemaphore(CountingSemaphore&& other):
|
||||
maxCount(other.maxCount), initCount(other.initCount) {
|
||||
initSemaphore(initCount);
|
||||
}
|
||||
|
||||
CountingSemaphore& CountingSemaphore::operator =(
|
||||
CountingSemaphore&& other) {
|
||||
initSemaphore(other.initCount);
|
||||
return * this;
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphore::release() {
|
||||
ReturnValue_t result = checkCount(&handle, maxCount);
|
||||
if(result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
return CountingSemaphore::release(&this->handle);
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphore::release(sem_t* handle) {
|
||||
int result = sem_post(handle);
|
||||
if(result == 0) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
switch(errno) {
|
||||
case(EINVAL):
|
||||
// Semaphore invalid
|
||||
return SemaphoreIF::SEMAPHORE_INVALID;
|
||||
case(EOVERFLOW):
|
||||
// SEM_MAX_VALUE overflow. This should never happen
|
||||
default:
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t CountingSemaphore::getMaxCount() const {
|
||||
return maxCount;
|
||||
}
|
||||
|
37
osal/linux/CountingSemaphore.h
Normal file
37
osal/linux/CountingSemaphore.h
Normal file
@ -0,0 +1,37 @@
|
||||
#ifndef FRAMEWORK_OSAL_LINUX_COUNTINGSEMAPHORE_H_
|
||||
#define FRAMEWORK_OSAL_LINUX_COUNTINGSEMAPHORE_H_
|
||||
#include "../../osal/linux/BinarySemaphore.h"
|
||||
|
||||
/**
|
||||
* @brief Counting semaphores, which can be acquired more than once.
|
||||
* @details
|
||||
* See: https://www.freertos.org/CreateCounting.html
|
||||
* API of counting semaphores is almost identical to binary semaphores,
|
||||
* so we just inherit from binary semaphore and provide the respective
|
||||
* constructors.
|
||||
*/
|
||||
class CountingSemaphore: public BinarySemaphore {
|
||||
public:
|
||||
CountingSemaphore(const uint8_t maxCount, uint8_t initCount);
|
||||
//! @brief Copy ctor, disabled
|
||||
CountingSemaphore(const CountingSemaphore&) = delete;
|
||||
//! @brief Copy assignment, disabled
|
||||
CountingSemaphore& operator=(const CountingSemaphore&) = delete;
|
||||
//! @brief Move ctor
|
||||
CountingSemaphore (CountingSemaphore &&);
|
||||
//! @brief Move assignment
|
||||
CountingSemaphore & operator=(CountingSemaphore &&);
|
||||
|
||||
ReturnValue_t release() override;
|
||||
static ReturnValue_t release(sem_t* sem);
|
||||
/* Same API as binary semaphore otherwise. acquire() can be called
|
||||
* until there are not semaphores left and release() can be called
|
||||
* until maxCount is reached. */
|
||||
|
||||
uint8_t getMaxCount() const;
|
||||
private:
|
||||
const uint8_t maxCount;
|
||||
uint8_t initCount = 0;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */
|
33
osal/linux/SemaphoreFactory.cpp
Normal file
33
osal/linux/SemaphoreFactory.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include "../../tasks/SemaphoreFactory.h"
|
||||
#include "BinarySemaphore.h"
|
||||
#include "CountingSemaphore.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr;
|
||||
|
||||
SemaphoreFactory::SemaphoreFactory() {
|
||||
}
|
||||
|
||||
SemaphoreFactory::~SemaphoreFactory() {
|
||||
delete factoryInstance;
|
||||
}
|
||||
|
||||
SemaphoreFactory* SemaphoreFactory::instance() {
|
||||
if (factoryInstance == nullptr){
|
||||
factoryInstance = new SemaphoreFactory();
|
||||
}
|
||||
return SemaphoreFactory::factoryInstance;
|
||||
}
|
||||
|
||||
SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t arguments) {
|
||||
return new BinarySemaphore();
|
||||
}
|
||||
|
||||
SemaphoreIF* SemaphoreFactory::createCountingSemaphore(const uint8_t maxCount,
|
||||
uint8_t initCount, uint32_t arguments) {
|
||||
return new CountingSemaphore(maxCount, initCount);
|
||||
}
|
||||
|
||||
void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) {
|
||||
delete semaphore;
|
||||
}
|
50
tasks/SemaphoreFactory.h
Normal file
50
tasks/SemaphoreFactory.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef FSFW_TASKS_SEMAPHOREFACTORY_H_
|
||||
#define FSFW_TASKS_SEMAPHOREFACTORY_H_
|
||||
|
||||
#include "../tasks/SemaphoreIF.h"
|
||||
|
||||
/**
|
||||
* Creates Semaphore.
|
||||
* This class is a "singleton" interface, i.e. it provides an
|
||||
* interface, but also is the base class for a singleton.
|
||||
*/
|
||||
class SemaphoreFactory {
|
||||
public:
|
||||
virtual ~SemaphoreFactory();
|
||||
/**
|
||||
* Returns the single instance of SemaphoreFactory.
|
||||
* The implementation of #instance is found in its subclasses.
|
||||
* Thus, we choose link-time variability of the instance.
|
||||
*/
|
||||
static SemaphoreFactory* instance();
|
||||
|
||||
/**
|
||||
* Create a binary semaphore.
|
||||
* Creator function for a binary semaphore which may only be acquired once
|
||||
* @param argument Can be used to pass implementation specific information.
|
||||
* @return Pointer to newly created semaphore class instance.
|
||||
*/
|
||||
SemaphoreIF* createBinarySemaphore(uint32_t arguments = 0);
|
||||
/**
|
||||
* Create a counting semaphore.
|
||||
* Creator functons for a counting semaphore which may be acquired multiple
|
||||
* times.
|
||||
* @param count Semaphore can be taken count times.
|
||||
* @param initCount Initial count value.
|
||||
* @param argument Can be used to pass implementation specific information.
|
||||
* @return
|
||||
*/
|
||||
SemaphoreIF* createCountingSemaphore(const uint8_t maxCount,
|
||||
uint8_t initCount, uint32_t arguments = 0);
|
||||
|
||||
void deleteSemaphore(SemaphoreIF* semaphore);
|
||||
|
||||
private:
|
||||
/**
|
||||
* External instantiation is not allowed.
|
||||
*/
|
||||
SemaphoreFactory();
|
||||
static SemaphoreFactory* factoryInstance;
|
||||
};
|
||||
|
||||
#endif /* FSFW_TASKS_SEMAPHOREFACTORY_H_ */
|
68
tasks/SemaphoreIF.h
Normal file
68
tasks/SemaphoreIF.h
Normal file
@ -0,0 +1,68 @@
|
||||
#ifndef FRAMEWORK_TASKS_SEMAPHOREIF_H_
|
||||
#define FRAMEWORK_TASKS_SEMAPHOREIF_H_
|
||||
#include "../returnvalues/FwClassIds.h"
|
||||
#include "../returnvalues/HasReturnvaluesIF.h"
|
||||
#include <cstdint>
|
||||
|
||||
/**
|
||||
* @brief Generic interface for semaphores, which can be used to achieve
|
||||
* task synchronization. This is a generic interface which can be
|
||||
* used for both binary semaphores and counting semaphores.
|
||||
* @details
|
||||
* A semaphore is a synchronization primitive.
|
||||
* See: https://en.wikipedia.org/wiki/Semaphore_(programming)
|
||||
* A semaphore can be used to achieve task synchonization and track the
|
||||
* availability of resources by using either the binary or the counting
|
||||
* semaphore types.
|
||||
*
|
||||
* If mutual exlcusion of a resource is desired, a mutex should be used,
|
||||
* which is a special form of a semaphore and has an own interface.
|
||||
*/
|
||||
class SemaphoreIF {
|
||||
public:
|
||||
/**
|
||||
* Different types of timeout for the mutex lock.
|
||||
*/
|
||||
enum TimeoutType {
|
||||
POLLING, //!< If mutex is not available, return immediately
|
||||
WAITING, //!< Wait a specified time for the mutex to become available
|
||||
BLOCKING //!< Block indefinitely until the mutex becomes available.
|
||||
};
|
||||
|
||||
virtual~ SemaphoreIF() {};
|
||||
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
|
||||
//! Semaphore timeout
|
||||
static constexpr ReturnValue_t SEMAPHORE_TIMEOUT = MAKE_RETURN_CODE(1);
|
||||
//! The current semaphore can not be given, because it is not owned
|
||||
static constexpr ReturnValue_t SEMAPHORE_NOT_OWNED = MAKE_RETURN_CODE(2);
|
||||
static constexpr ReturnValue_t SEMAPHORE_INVALID = MAKE_RETURN_CODE(3);
|
||||
|
||||
/**
|
||||
* Generic call to acquire a semaphore.
|
||||
* If there are no more semaphores to be taken (for a counting semaphore,
|
||||
* a semaphore may be taken more than once), the taks will block
|
||||
* for a maximum of timeoutMs while trying to acquire the semaphore.
|
||||
* This can be used to achieve task synchrnization.
|
||||
* @param timeoutMs
|
||||
* @return - c RETURN_OK for successfull acquisition
|
||||
*/
|
||||
virtual ReturnValue_t acquire(TimeoutType timeoutType =
|
||||
TimeoutType::BLOCKING, uint32_t timeoutMs = 0) = 0;
|
||||
|
||||
/**
|
||||
* Corrensponding call to release a semaphore.
|
||||
* @return -@c RETURN_OK for successfull release
|
||||
*/
|
||||
virtual ReturnValue_t release() = 0;
|
||||
|
||||
/**
|
||||
* If the semaphore is a counting semaphore then the semaphores current
|
||||
* count value is returned. If the semaphore is a binary semaphore then 1
|
||||
* is returned if the semaphore is available, and 0 is returned if the
|
||||
* semaphore is not available.
|
||||
*/
|
||||
virtual uint8_t getSemaphoreCounter() const = 0;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_TASKS_SEMAPHOREIF_H_ */
|
Loading…
Reference in New Issue
Block a user