Semaphore Initialization - Linux and FreeRTOS #89

Merged
gaisser merged 16 commits from KSat/fsfw:mueller_binSemaphoreInit into master 2020-08-28 13:25:07 +02:00
10 changed files with 616 additions and 606 deletions
Showing only changes of commit aacda3afc2 - Show all commits

View File

@ -1,6 +1,6 @@
#include <framework/osal/FreeRTOS/BinSemaphUsingTask.h>
#include <framework/osal/FreeRTOS/TaskManagement.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include "../../osal/FreeRTOS/BinSemaphUsingTask.h"
#include "../../osal/FreeRTOS/TaskManagement.h"
#include "../../serviceinterface/ServiceInterfaceStream.h"
BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() {
handle = TaskManagement::getCurrentTaskHandle();
@ -16,19 +16,23 @@ BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() {
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
}
ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) {
TickType_t timeout = SemaphoreIF::POLLING;
if(timeoutMs == SemaphoreIF::BLOCKING) {
timeout = SemaphoreIF::BLOCKING;
}
else if(timeoutMs > SemaphoreIF::POLLING){
timeout = pdMS_TO_TICKS(timeoutMs);
}
return acquireWithTickTimeout(timeout);
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(
TickType_t timeoutTicks) {
TimeoutType timeoutType, TickType_t timeoutTicks) {
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;

View File

@ -1,8 +1,8 @@
#ifndef FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
#define FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <framework/tasks/SemaphoreIF.h>
#include "../../returnvalues/HasReturnvaluesIF.h"
#include "../../tasks/SemaphoreIF.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
@ -25,8 +25,8 @@ public:
//! @brief Default dtor
virtual~ BinarySemaphoreUsingTask();
ReturnValue_t acquire(uint32_t timeoutMs =
SemaphoreIF::BLOCKING) override;
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);
@ -39,8 +39,9 @@ public:
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
*/
ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks =
SemaphoreIF::BLOCKING);
ReturnValue_t acquireWithTickTimeout(
TimeoutType timeoutType = TimeoutType::BLOCKING,
TickType_t timeoutTicks = portMAX_DELAY);
/**
* Get handle to the task related to the semaphore.

View File

@ -1,6 +1,6 @@
#include <framework/osal/FreeRTOS/BinarySemaphore.h>
#include <framework/osal/FreeRTOS/TaskManagement.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include "../../osal/FreeRTOS/BinarySemaphore.h"
#include "../../osal/FreeRTOS/TaskManagement.h"
#include "../../serviceinterface/ServiceInterfaceStream.h"
BinarySemaphore::BinarySemaphore() {
handle = xSemaphoreCreateBinary();
@ -35,18 +35,23 @@ BinarySemaphore& BinarySemaphore::operator =(
return *this;
}
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
TickType_t timeout = SemaphoreIF::POLLING;
if(timeoutMs == SemaphoreIF::BLOCKING) {
timeout = SemaphoreIF::BLOCKING;
ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType,
uint32_t timeoutMs) {
TickType_t timeout = 0;
if(timeoutType == TimeoutType::POLLING) {
timeout = 0;
}
else if(timeoutMs > SemaphoreIF::POLLING){
else if(timeoutType == TimeoutType::WAITING){
timeout = pdMS_TO_TICKS(timeoutMs);
}
return acquireWithTickTimeout(timeout);
else {
timeout = portMAX_DELAY;
}
return acquireWithTickTimeout(timeoutType, timeout);
}
ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TickType_t timeoutTicks) {
ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TimeoutType timeoutType,
TickType_t timeoutTicks) {
if(handle == nullptr) {
return SemaphoreIF::SEMAPHORE_INVALID;
}

View File

@ -1,8 +1,8 @@
#ifndef FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
#define FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <framework/tasks/SemaphoreIF.h>
#include "../../returnvalues/HasReturnvaluesIF.h"
#include "../../tasks/SemaphoreIF.h"
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
@ -52,8 +52,8 @@ public:
* @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/
ReturnValue_t acquire(uint32_t timeoutMs =
SemaphoreIF::BLOCKING) override;
ReturnValue_t acquire(TimeoutType timeoutType =
TimeoutType::BLOCKING, uint32_t timeoutMs = portMAX_DELAY) override;
/**
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
@ -61,8 +61,8 @@ public:
* @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/
ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks =
SemaphoreIF::BLOCKING);
ReturnValue_t acquireWithTickTimeout(TimeoutType timeoutType =
TimeoutType::BLOCKING, TickType_t timeoutTicks = portMAX_DELAY);
/**
* Release the binary semaphore.

View File

@ -1,6 +1,6 @@
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h>
#include <framework/osal/FreeRTOS/TaskManagement.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#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) {
@ -37,20 +37,24 @@ CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() {
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
}
ReturnValue_t CountingSemaphoreUsingTask::acquire(uint32_t timeoutMs) {
TickType_t timeout = SemaphoreIF::POLLING;
if(timeoutMs == SemaphoreIF::BLOCKING) {
timeout = SemaphoreIF::BLOCKING;
}
else if(timeoutMs > SemaphoreIF::POLLING){
timeout = pdMS_TO_TICKS(timeoutMs);
}
return acquireWithTickTimeout(timeout);
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(
TickType_t timeoutTicks) {
TimeoutType timeoutType, TickType_t timeoutTicks) {
// Decrement notfication value without resetting it.
BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks);
if (getSemaphoreCounter() == oldCount - 1) {

View File

@ -1,8 +1,8 @@
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h>
#include <framework/tasks/SemaphoreIF.h>
#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
#include "../../tasks/SemaphoreIF.h"
extern "C" {
#include <freertos/FreeRTOS.h>
@ -31,7 +31,8 @@ public:
* @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/
ReturnValue_t acquire(uint32_t timeoutMs = SemaphoreIF::BLOCKING) override;
ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING,
uint32_t timeoutMs = portMAX_DELAY) override;
/**
* Release a semaphore, increasing the number of available counting
@ -61,7 +62,8 @@ public:
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/
ReturnValue_t acquireWithTickTimeout(
TickType_t timeoutTicks = SemaphoreIF::BLOCKING);
TimeoutType timeoutType = TimeoutType::BLOCKING,
TickType_t timeoutTicks = portMAX_DELAY);
/**
* Get handle to the task related to the semaphore.

View File

@ -1,6 +1,6 @@
#include <framework/osal/FreeRTOS/CountingSemaphore.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#include <framework/osal/FreeRTOS/TaskManagement.h>
#include "../../osal/FreeRTOS/CountingSemaphore.h"
#include "../../serviceinterface/ServiceInterfaceStream.h"
#include "../../osal/FreeRTOS/TaskManagement.h"
#include <freertos/semphr.h>

View File

@ -1,6 +1,6 @@
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
#include <framework/osal/FreeRTOS/BinarySemaphore.h>
#include "../../osal/FreeRTOS/BinarySemaphore.h"
/**
* @brief Counting semaphores, which can be acquire more than once.

View File

@ -1,13 +1,11 @@
#include <framework/osal/FreeRTOS/BinarySemaphore.h>
#include <framework/osal/FreeRTOS/BinSemaphUsingTask.h>
#include <framework/osal/FreeRTOS/CountingSemaphore.h>
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h>
#include <framework/tasks/SemaphoreFactory.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
#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;
const uint32_t SemaphoreIF::POLLING = 0;
const uint32_t SemaphoreIF::BLOCKING = portMAX_DELAY;
static const uint32_t USE_REGULAR_SEMAPHORES = 0;
static const uint32_t USE_TASK_NOTIFICATIONS = 1;

View File

@ -1,7 +1,7 @@
#ifndef FRAMEWORK_TASKS_SEMAPHOREIF_H_
#define FRAMEWORK_TASKS_SEMAPHOREIF_H_
#include <framework/returnvalues/FwClassIds.h>
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include "../returnvalues/FwClassIds.h"
#include "../returnvalues/HasReturnvaluesIF.h"
#include <cstdint>
/**
@ -20,21 +20,16 @@
*/
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() {};
/**
* @brief Timeout value used for polling lock attempt.
* @details
* If the lock is not successfull, MUTEX_TIMEOUT will be returned
* immediately. Value needs to be defined in implementation.
*/
static const uint32_t POLLING;
/**
* @brief Timeout value used for permanent blocking lock attempt.
* @details
* The task will be blocked (indefinitely) until the mutex is unlocked.
* Value needs to be defined in implementation.
*/
static const uint32_t BLOCKING;
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
gaisser marked this conversation as resolved
Review

I don't see the addition of the class id in FwClassIds. Could you add this change as well?

I don't see the addition of the class id in FwClassIds. Could you add this change as well?
Review

It's actually in there, I did a compile check

It's actually in there, I did a compile check
Review

Interesting, I don't find it in the commit view.

Interesting, I don't find it in the commit view.
Review

Oh it's part of the master.

Oh it's part of the master.
//! Semaphore timeout
@ -52,7 +47,8 @@ public:
* @param timeoutMs
* @return - c RETURN_OK for successfull acquisition
*/
virtual ReturnValue_t acquire(uint32_t timeoutMs) = 0;
virtual ReturnValue_t acquire(TimeoutType timeoutType =
TimeoutType::BLOCKING, uint32_t timeoutMs = 0) = 0;
/**
* Corrensponding call to release a semaphore.