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); xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
} }
ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) { ReturnValue_t BinarySemaphoreUsingTask::acquire(TimeoutType timeoutType,
TickType_t timeout = SemaphoreIF::POLLING; uint32_t timeoutMs) {
if(timeoutMs == SemaphoreIF::BLOCKING) { TickType_t timeout = 0;
timeout = SemaphoreIF::BLOCKING; if(timeoutType == TimeoutType::POLLING) {
} timeout = 0;
else if(timeoutMs > SemaphoreIF::POLLING){ }
timeout = pdMS_TO_TICKS(timeoutMs); else if(timeoutType == TimeoutType::WAITING){
} timeout = pdMS_TO_TICKS(timeoutMs);
return acquireWithTickTimeout(timeout); }
else {
timeout = portMAX_DELAY;
}
return acquireWithTickTimeout(timeoutType, timeout);
} }
ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout( ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout(
TickType_t timeoutTicks) { TimeoutType timeoutType, TickType_t timeoutTicks) {
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks); BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
if (returncode == pdPASS) { if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;

View File

@ -25,8 +25,8 @@ public:
//! @brief Default dtor //! @brief Default dtor
virtual~ BinarySemaphoreUsingTask(); virtual~ BinarySemaphoreUsingTask();
ReturnValue_t acquire(uint32_t timeoutMs = ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING,
SemaphoreIF::BLOCKING) override; uint32_t timeoutMs = portMAX_DELAY) override;
ReturnValue_t release() override; ReturnValue_t release() override;
uint8_t getSemaphoreCounter() const override; uint8_t getSemaphoreCounter() const override;
static uint8_t getSemaphoreCounter(TaskHandle_t taskHandle); static uint8_t getSemaphoreCounter(TaskHandle_t taskHandle);
@ -39,8 +39,9 @@ public:
* @return - @c RETURN_OK on success * @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure * - @c RETURN_FAILED on failure
*/ */
ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks = ReturnValue_t acquireWithTickTimeout(
SemaphoreIF::BLOCKING); TimeoutType timeoutType = TimeoutType::BLOCKING,
TickType_t timeoutTicks = portMAX_DELAY);
/** /**
* Get handle to the task related to the semaphore. * Get handle to the task related to the semaphore.

View File

@ -35,18 +35,23 @@ BinarySemaphore& BinarySemaphore::operator =(
return *this; return *this;
} }
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) { ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType,
TickType_t timeout = SemaphoreIF::POLLING; uint32_t timeoutMs) {
if(timeoutMs == SemaphoreIF::BLOCKING) { TickType_t timeout = 0;
timeout = SemaphoreIF::BLOCKING; if(timeoutType == TimeoutType::POLLING) {
timeout = 0;
} }
else if(timeoutMs > SemaphoreIF::POLLING){ else if(timeoutType == TimeoutType::WAITING){
timeout = pdMS_TO_TICKS(timeoutMs); 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) { if(handle == nullptr) {
return SemaphoreIF::SEMAPHORE_INVALID; return SemaphoreIF::SEMAPHORE_INVALID;
} }

View File

@ -52,8 +52,8 @@ public:
* @return -@c RETURN_OK on success * @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/ */
ReturnValue_t acquire(uint32_t timeoutMs = ReturnValue_t acquire(TimeoutType timeoutType =
SemaphoreIF::BLOCKING) override; TimeoutType::BLOCKING, uint32_t timeoutMs = portMAX_DELAY) override;
/** /**
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks. * Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
@ -61,8 +61,8 @@ public:
* @return -@c RETURN_OK on success * @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/ */
ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks = ReturnValue_t acquireWithTickTimeout(TimeoutType timeoutType =
SemaphoreIF::BLOCKING); TimeoutType::BLOCKING, TickType_t timeoutTicks = portMAX_DELAY);
/** /**
* Release the binary semaphore. * Release the binary semaphore.

View File

@ -37,20 +37,24 @@ CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() {
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr); xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
} }
ReturnValue_t CountingSemaphoreUsingTask::acquire(uint32_t timeoutMs) { ReturnValue_t CountingSemaphoreUsingTask::acquire(TimeoutType timeoutType,
TickType_t timeout = SemaphoreIF::POLLING; uint32_t timeoutMs) {
if(timeoutMs == SemaphoreIF::BLOCKING) { TickType_t timeout = 0;
timeout = SemaphoreIF::BLOCKING; if(timeoutType == TimeoutType::POLLING) {
} timeout = 0;
else if(timeoutMs > SemaphoreIF::POLLING){ }
timeout = pdMS_TO_TICKS(timeoutMs); else if(timeoutType == TimeoutType::WAITING){
} timeout = pdMS_TO_TICKS(timeoutMs);
return acquireWithTickTimeout(timeout); }
else {
timeout = portMAX_DELAY;
}
return acquireWithTickTimeout(timeoutType, timeout);
} }
ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout( ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout(
TickType_t timeoutTicks) { TimeoutType timeoutType, TickType_t timeoutTicks) {
// Decrement notfication value without resetting it. // Decrement notfication value without resetting it.
BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks); BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks);
if (getSemaphoreCounter() == oldCount - 1) { if (getSemaphoreCounter() == oldCount - 1) {

View File

@ -31,7 +31,8 @@ public:
* @return -@c RETURN_OK on success * @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout * -@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 * Release a semaphore, increasing the number of available counting
@ -61,7 +62,8 @@ public:
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/ */
ReturnValue_t acquireWithTickTimeout( 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. * Get handle to the task related to the semaphore.

View File

@ -6,8 +6,6 @@
#include "../../serviceinterface/ServiceInterfaceStream.h" #include "../../serviceinterface/ServiceInterfaceStream.h"
SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr; 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_REGULAR_SEMAPHORES = 0;
static const uint32_t USE_TASK_NOTIFICATIONS = 1; static const uint32_t USE_TASK_NOTIFICATIONS = 1;

View File

@ -20,21 +20,16 @@
*/ */
class SemaphoreIF { class SemaphoreIF {
public: 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() {}; 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; static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
//! Semaphore timeout //! Semaphore timeout
@ -52,7 +47,8 @@ public:
* @param timeoutMs * @param timeoutMs
* @return - c RETURN_OK for successfull acquisition * @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. * Corrensponding call to release a semaphore.