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() {
handle = TaskManagement::getCurrentTaskHandle();
xTaskNotifyGive(handle);
locked = false;
}
ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) {
@ -25,6 +27,7 @@ ReturnValue_t BinarySemaphoreUsingTask::takeBinarySemaphore(uint32_t timeoutMs)
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeout);
if (returncode == pdPASS) {
locked = true;
return HasReturnvaluesIF::RETURN_OK;
}
else {
@ -36,6 +39,7 @@ ReturnValue_t BinarySemaphoreUsingTask::takeBinarySemaphoreTickTimeout(
TickType_t timeoutTicks) {
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
if (returncode == pdPASS) {
locked = true;
return HasReturnvaluesIF::RETURN_OK;
} else {
return SEMAPHORE_TIMEOUT;
@ -43,11 +47,15 @@ ReturnValue_t BinarySemaphoreUsingTask::takeBinarySemaphoreTickTimeout(
}
ReturnValue_t BinarySemaphoreUsingTask::giveBinarySemaphore() {
if(not locked) {
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
}
BaseType_t returncode = xTaskNotifyGive(handle);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} 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);
if (returncode == pdPASS) {
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:
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) {
sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
}
// Initiated semaphore must be given before it can be taken.
xSemaphoreGive(handle);
}
@ -34,7 +35,7 @@ BinarySemaphore& BinarySemaphore::operator =(
return *this;
}
ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) {
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
if(handle == nullptr) {
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) {
if(handle == nullptr) {
return SEMAPHORE_NULLPOINTER;
@ -69,7 +70,7 @@ ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout(
}
}
ReturnValue_t BinarySemaphore::giveBinarySemaphore() {
ReturnValue_t BinarySemaphore::release() {
if (handle == nullptr) {
return SEMAPHORE_NULLPOINTER;
}
@ -96,26 +97,19 @@ ReturnValue_t BinarySemaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore)
return HasReturnvaluesIF::RETURN_FAILED;
}
}
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
return takeBinarySemaphore(timeoutMs);
}
ReturnValue_t BinarySemaphore::release() {
return giveBinarySemaphore();
}
uint8_t BinarySemaphore::getSemaphoreCounter() {
return uxSemaphoreGetCount(handle);
}
// Be careful with the stack size here. This is called from an ISR!
ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore,
BaseType_t * higherPriorityTaskWoken) {
ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR(
SemaphoreHandle_t semaphore, BaseType_t * higherPriorityTaskWoken) {
if (semaphore == nullptr) {
return SEMAPHORE_NULLPOINTER;
}
BaseType_t returncode = xSemaphoreGiveFromISR(semaphore, higherPriorityTaskWoken);
BaseType_t returncode = xSemaphoreGiveFromISR(semaphore,
higherPriorityTaskWoken);
if (returncode == pdPASS) {
if(*higherPriorityTaskWoken == pdPASS) {
// Request context switch because unblocking the semaphore

View File

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

View File

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

View File

@ -11,7 +11,7 @@ extern "C" {
class CountingSemaphoreUsingTask: public SemaphoreIF {
public:
CountingSemaphoreUsingTask(uint8_t count, uint8_t initCount);
CountingSemaphoreUsingTask(uint8_t maxCount, uint8_t initCount);
ReturnValue_t acquire(uint32_t timeoutMs) override;
ReturnValue_t release() override;
@ -19,8 +19,8 @@ public:
private:
TaskHandle_t handle;
uint8_t count = 0;
uint8_t initCount = 0;
const uint8_t maxCount;
uint8_t currentCount = 0;
};
#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) {
if(argument == USE_REGULAR_SEMAPHORES) {
return new CountingSemaphore(count, initCount);
return new CountingSemaphore(maxCount, initCount);
}
else if(argument == USE_TASK_NOTIFICATIONS) {
return new CountingSemaphoreUsingTask(count, initCount);
return new CountingSemaphoreUsingTask(maxCount, initCount);
}
else {
sif::warning << "SemaphoreFactory: Invalid argument, return regular"
"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.
* @return
*/
SemaphoreIF* createCountingSemaphore(uint8_t count, uint8_t initCount,
SemaphoreIF* createCountingSemaphore(uint8_t maxCount, uint8_t initCount,
uint32_t argument = 0);
void deleteSemaphore(SemaphoreIF* mutex);