From 6d3fdab9446d0129823b16aba8cc9e71e2067006 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Thu, 27 Aug 2020 15:52:55 +0200 Subject: [PATCH] adapted semaphore IF to mutex IF --- osal/FreeRTOS/BinSemaphUsingTask.cpp | 24 ++++++++++++--------- osal/FreeRTOS/BinSemaphUsingTask.h | 9 ++++---- osal/FreeRTOS/BinarySemaphore.cpp | 19 +++++++++++------ osal/FreeRTOS/BinarySemaphore.h | 8 +++---- osal/FreeRTOS/CountingSemaphUsingTask.cpp | 24 ++++++++++++--------- osal/FreeRTOS/CountingSemaphUsingTask.h | 6 ++++-- osal/FreeRTOS/SemaphoreFactory.cpp | 2 -- tasks/SemaphoreIF.h | 26 ++++++++++------------- 8 files changed, 64 insertions(+), 54 deletions(-) diff --git a/osal/FreeRTOS/BinSemaphUsingTask.cpp b/osal/FreeRTOS/BinSemaphUsingTask.cpp index 71c0aba9..8c831bbe 100644 --- a/osal/FreeRTOS/BinSemaphUsingTask.cpp +++ b/osal/FreeRTOS/BinSemaphUsingTask.cpp @@ -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; diff --git a/osal/FreeRTOS/BinSemaphUsingTask.h b/osal/FreeRTOS/BinSemaphUsingTask.h index e6989970..2d2cf159 100644 --- a/osal/FreeRTOS/BinSemaphUsingTask.h +++ b/osal/FreeRTOS/BinSemaphUsingTask.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. diff --git a/osal/FreeRTOS/BinarySemaphore.cpp b/osal/FreeRTOS/BinarySemaphore.cpp index 8e15b7a5..f1c78473 100644 --- a/osal/FreeRTOS/BinarySemaphore.cpp +++ b/osal/FreeRTOS/BinarySemaphore.cpp @@ -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; } diff --git a/osal/FreeRTOS/BinarySemaphore.h b/osal/FreeRTOS/BinarySemaphore.h index 4cf270de..5a32088a 100644 --- a/osal/FreeRTOS/BinarySemaphore.h +++ b/osal/FreeRTOS/BinarySemaphore.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. diff --git a/osal/FreeRTOS/CountingSemaphUsingTask.cpp b/osal/FreeRTOS/CountingSemaphUsingTask.cpp index 333865c8..e66671ac 100644 --- a/osal/FreeRTOS/CountingSemaphUsingTask.cpp +++ b/osal/FreeRTOS/CountingSemaphUsingTask.cpp @@ -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) { diff --git a/osal/FreeRTOS/CountingSemaphUsingTask.h b/osal/FreeRTOS/CountingSemaphUsingTask.h index 801e3430..e3733382 100644 --- a/osal/FreeRTOS/CountingSemaphUsingTask.h +++ b/osal/FreeRTOS/CountingSemaphUsingTask.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. diff --git a/osal/FreeRTOS/SemaphoreFactory.cpp b/osal/FreeRTOS/SemaphoreFactory.cpp index 453f2226..8575cf4a 100644 --- a/osal/FreeRTOS/SemaphoreFactory.cpp +++ b/osal/FreeRTOS/SemaphoreFactory.cpp @@ -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; diff --git a/tasks/SemaphoreIF.h b/tasks/SemaphoreIF.h index 3fed4511..30d4ed88 100644 --- a/tasks/SemaphoreIF.h +++ b/tasks/SemaphoreIF.h @@ -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.