adapted semaphore IF to mutex IF

This commit is contained in:
Robin Müller 2020-08-27 15:52:55 +02:00
parent 1dfdd65662
commit 6d3fdab944
8 changed files with 64 additions and 54 deletions

View File

@ -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

@ -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

@ -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

@ -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

@ -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

@ -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

@ -6,8 +6,6 @@
#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

@ -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;
//! 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.