replaced old semaph api

This commit is contained in:
Robin Müller 2020-05-27 21:27:31 +02:00
parent 7145982b4a
commit 88e3dc15b2
8 changed files with 48 additions and 35 deletions

View File

@ -4,6 +4,8 @@
BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() { BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() {
handle = TaskManagement::getCurrentTaskHandle(); handle = TaskManagement::getCurrentTaskHandle();
xTaskNotifyGive(handle);
locked = false;
} }
ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) { ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) {
@ -25,6 +27,7 @@ ReturnValue_t BinarySemaphoreUsingTask::takeBinarySemaphore(uint32_t timeoutMs)
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeout); BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeout);
if (returncode == pdPASS) { if (returncode == pdPASS) {
locked = true;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
else { else {
@ -36,6 +39,7 @@ ReturnValue_t BinarySemaphoreUsingTask::takeBinarySemaphoreTickTimeout(
TickType_t timeoutTicks) { TickType_t timeoutTicks) {
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks); BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
if (returncode == pdPASS) { if (returncode == pdPASS) {
locked = true;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { } else {
return SEMAPHORE_TIMEOUT; return SEMAPHORE_TIMEOUT;
@ -43,11 +47,15 @@ ReturnValue_t BinarySemaphoreUsingTask::takeBinarySemaphoreTickTimeout(
} }
ReturnValue_t BinarySemaphoreUsingTask::giveBinarySemaphore() { ReturnValue_t BinarySemaphoreUsingTask::giveBinarySemaphore() {
if(not locked) {
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
}
BaseType_t returncode = xTaskNotifyGive(handle); BaseType_t returncode = xTaskNotifyGive(handle);
if (returncode == pdPASS) { if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { } else {
return SEMAPHORE_NOT_OWNED; // This should never happen
return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
@ -77,8 +85,10 @@ ReturnValue_t BinarySemaphoreUsingTask::giveBinarySemaphore(TaskHandle_t taskHan
BaseType_t returncode = xTaskNotifyGive(taskHandle); BaseType_t returncode = xTaskNotifyGive(taskHandle);
if (returncode == pdPASS) { if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { }
return SEMAPHORE_NOT_OWNED; else {
// This should never happen.
return HasReturnvaluesIF::RETURN_FAILED;
} }
} }

View File

@ -85,6 +85,9 @@ public:
protected: protected:
TaskHandle_t handle; TaskHandle_t handle;
// This boolean is required to track whether the semaphore is locked
// or unlocked.
bool locked;
}; };

View File

@ -7,6 +7,7 @@ BinarySemaphore::BinarySemaphore() {
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "Semaphore: Binary semaph creation failure" << std::endl; sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
} }
// Initiated semaphore must be given before it can be taken.
xSemaphoreGive(handle); xSemaphoreGive(handle);
} }
@ -34,7 +35,7 @@ BinarySemaphore& BinarySemaphore::operator =(
return *this; return *this;
} }
ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) { ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
if(handle == nullptr) { if(handle == nullptr) {
return SEMAPHORE_NULLPOINTER; return SEMAPHORE_NULLPOINTER;
} }
@ -55,7 +56,7 @@ ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) {
} }
} }
ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout( ReturnValue_t BinarySemaphore::acquireWithTickTimeout(
TickType_t timeoutTicks) { TickType_t timeoutTicks) {
if(handle == nullptr) { if(handle == nullptr) {
return SEMAPHORE_NULLPOINTER; return SEMAPHORE_NULLPOINTER;
@ -69,7 +70,7 @@ ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout(
} }
} }
ReturnValue_t BinarySemaphore::giveBinarySemaphore() { ReturnValue_t BinarySemaphore::release() {
if (handle == nullptr) { if (handle == nullptr) {
return SEMAPHORE_NULLPOINTER; return SEMAPHORE_NULLPOINTER;
} }
@ -97,25 +98,18 @@ ReturnValue_t BinarySemaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore)
} }
} }
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
return takeBinarySemaphore(timeoutMs);
}
ReturnValue_t BinarySemaphore::release() {
return giveBinarySemaphore();
}
uint8_t BinarySemaphore::getSemaphoreCounter() { uint8_t BinarySemaphore::getSemaphoreCounter() {
return uxSemaphoreGetCount(handle); return uxSemaphoreGetCount(handle);
} }
// Be careful with the stack size here. This is called from an ISR! // Be careful with the stack size here. This is called from an ISR!
ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore, ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR(
BaseType_t * higherPriorityTaskWoken) { SemaphoreHandle_t semaphore, BaseType_t * higherPriorityTaskWoken) {
if (semaphore == nullptr) { if (semaphore == nullptr) {
return SEMAPHORE_NULLPOINTER; return SEMAPHORE_NULLPOINTER;
} }
BaseType_t returncode = xSemaphoreGiveFromISR(semaphore, higherPriorityTaskWoken); BaseType_t returncode = xSemaphoreGiveFromISR(semaphore,
higherPriorityTaskWoken);
if (returncode == pdPASS) { if (returncode == pdPASS) {
if(*higherPriorityTaskWoken == pdPASS) { if(*higherPriorityTaskWoken == pdPASS) {
// Request context switch because unblocking the semaphore // Request context switch because unblocking the semaphore

View File

@ -43,9 +43,6 @@ public:
//! @brief Destructor //! @brief Destructor
virtual ~BinarySemaphore(); virtual ~BinarySemaphore();
ReturnValue_t acquire(uint32_t timeoutMs =
SemaphoreIF::NO_TIMEOUT) override;
ReturnValue_t release() override;
uint8_t getSemaphoreCounter() override; uint8_t getSemaphoreCounter() override;
/** /**
@ -57,8 +54,8 @@ 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 takeBinarySemaphore(uint32_t timeoutMs = ReturnValue_t acquire(uint32_t timeoutMs =
SemaphoreIF::NO_TIMEOUT); SemaphoreIF::NO_TIMEOUT) override;
/** /**
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks. * Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
@ -66,7 +63,7 @@ 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 takeBinarySemaphoreTickTimeout(TickType_t timeoutTicks = ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks =
BinarySemaphore::NO_TIMEOUT); BinarySemaphore::NO_TIMEOUT);
/** /**
@ -74,7 +71,7 @@ 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 giveBinarySemaphore(); ReturnValue_t release() override;
/** /**
* Get Handle to the semaphore. * Get Handle to the semaphore.

View File

@ -1,10 +1,19 @@
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h> #include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h>
#include <framework/osal/FreeRTOS/TaskManagement.h> #include <framework/osal/FreeRTOS/TaskManagement.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h>
CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(uint8_t count, CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(uint8_t maxCount,
uint8_t initCount): uint8_t initCount): maxCount(maxCount) {
count(count), 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 = TaskManagement::getCurrentTaskHandle(); handle = TaskManagement::getCurrentTaskHandle();
while(currentCount != initCount) {
xTaskNotifyGive(handle);
currentCount++;
}
} }
ReturnValue_t CountingSemaphoreUsingTask::acquire( ReturnValue_t CountingSemaphoreUsingTask::acquire(

View File

@ -11,7 +11,7 @@ extern "C" {
class CountingSemaphoreUsingTask: public SemaphoreIF { class CountingSemaphoreUsingTask: public SemaphoreIF {
public: public:
CountingSemaphoreUsingTask(uint8_t count, uint8_t initCount); CountingSemaphoreUsingTask(uint8_t maxCount, uint8_t initCount);
ReturnValue_t acquire(uint32_t timeoutMs) override; ReturnValue_t acquire(uint32_t timeoutMs) override;
ReturnValue_t release() override; ReturnValue_t release() override;
@ -19,8 +19,8 @@ public:
private: private:
TaskHandle_t handle; TaskHandle_t handle;
uint8_t count = 0; const uint8_t maxCount;
uint8_t initCount = 0; uint8_t currentCount = 0;
}; };
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ */ #endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ */

View File

@ -40,18 +40,18 @@ SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t argument) {
} }
} }
SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t count, SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t maxCount,
uint8_t initCount, uint32_t argument) { uint8_t initCount, uint32_t argument) {
if(argument == USE_REGULAR_SEMAPHORES) { if(argument == USE_REGULAR_SEMAPHORES) {
return new CountingSemaphore(count, initCount); return new CountingSemaphore(maxCount, initCount);
} }
else if(argument == USE_TASK_NOTIFICATIONS) { else if(argument == USE_TASK_NOTIFICATIONS) {
return new CountingSemaphoreUsingTask(count, initCount); return new CountingSemaphoreUsingTask(maxCount, initCount);
} }
else { else {
sif::warning << "SemaphoreFactory: Invalid argument, return regular" sif::warning << "SemaphoreFactory: Invalid argument, return regular"
"binary semaphore" << std::endl; "binary semaphore" << std::endl;
return new CountingSemaphore(count, initCount); return new CountingSemaphore(maxCount, initCount);
} }
} }

View File

@ -33,7 +33,7 @@ public:
* @param argument Can be used to pass implementation specific information. * @param argument Can be used to pass implementation specific information.
* @return * @return
*/ */
SemaphoreIF* createCountingSemaphore(uint8_t count, uint8_t initCount, SemaphoreIF* createCountingSemaphore(uint8_t maxCount, uint8_t initCount,
uint32_t argument = 0); uint32_t argument = 0);
void deleteSemaphore(SemaphoreIF* mutex); void deleteSemaphore(SemaphoreIF* mutex);