improvements

This commit is contained in:
Robin Müller 2020-05-18 20:39:48 +02:00
parent 7227c3a866
commit 066930b110
4 changed files with 41 additions and 33 deletions

View File

@ -3,7 +3,7 @@
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
Semaphore::Semaphore() { BinarySemaphore::BinarySemaphore() {
handle = xSemaphoreCreateBinary(); handle = xSemaphoreCreateBinary();
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "Semaphore: Binary semaph creation failure" << std::endl; sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
@ -11,13 +11,13 @@ Semaphore::Semaphore() {
xSemaphoreGive(handle); xSemaphoreGive(handle);
} }
Semaphore::~Semaphore() { BinarySemaphore::~BinarySemaphore() {
vSemaphoreDelete(handle); vSemaphoreDelete(handle);
} }
// This copy ctor is important as it prevents the assignment to a ressource // This copy ctor is important as it prevents the assignment to a ressource
// (other.handle) variable which is later deleted! // (other.handle) variable which is later deleted!
Semaphore::Semaphore(const Semaphore& other) { BinarySemaphore::BinarySemaphore(const BinarySemaphore& other) {
handle = xSemaphoreCreateBinary(); handle = xSemaphoreCreateBinary();
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "Binary semaphore creation failure" << std::endl; sif::error << "Binary semaphore creation failure" << std::endl;
@ -25,7 +25,7 @@ Semaphore::Semaphore(const Semaphore& other) {
xSemaphoreGive(handle); xSemaphoreGive(handle);
} }
Semaphore& Semaphore::operator =(const Semaphore& s) { BinarySemaphore& BinarySemaphore::operator =(const BinarySemaphore& s) {
if(this != &s) { if(this != &s) {
handle = xSemaphoreCreateBinary(); handle = xSemaphoreCreateBinary();
if(handle == nullptr) { if(handle == nullptr) {
@ -36,7 +36,7 @@ Semaphore& Semaphore::operator =(const Semaphore& s) {
return *this; return *this;
} }
Semaphore::Semaphore(Semaphore&& s) { BinarySemaphore::BinarySemaphore(BinarySemaphore&& s) {
handle = xSemaphoreCreateBinary(); handle = xSemaphoreCreateBinary();
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "Binary semaphore creation failure" << std::endl; sif::error << "Binary semaphore creation failure" << std::endl;
@ -44,8 +44,8 @@ Semaphore::Semaphore(Semaphore&& s) {
xSemaphoreGive(handle); xSemaphoreGive(handle);
} }
Semaphore& Semaphore::operator =( BinarySemaphore& BinarySemaphore::operator =(
Semaphore&& s) { BinarySemaphore&& s) {
if(&s != this) { if(&s != this) {
handle = xSemaphoreCreateBinary(); handle = xSemaphoreCreateBinary();
if(handle == nullptr) { if(handle == nullptr) {
@ -56,15 +56,15 @@ Semaphore& Semaphore::operator =(
return *this; return *this;
} }
ReturnValue_t Semaphore::takeBinarySemaphore(uint32_t timeoutMs) { ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) {
if(handle == nullptr) { if(handle == nullptr) {
return SEMAPHORE_NULLPOINTER; return SEMAPHORE_NULLPOINTER;
} }
TickType_t timeout = Semaphore::NO_BLOCK_TICKS; TickType_t timeout = BinarySemaphore::NO_BLOCK_TICKS;
if(timeoutMs == Semaphore::BLOCK_TIMEOUT) { if(timeoutMs == BinarySemaphore::BLOCK_TIMEOUT) {
timeout = Semaphore::BLOCK_TIMEOUT_TICKS; timeout = BinarySemaphore::BLOCK_TIMEOUT_TICKS;
} }
else if(timeoutMs > Semaphore::NO_BLOCK_TIMEOUT){ else if(timeoutMs > BinarySemaphore::NO_BLOCK_TIMEOUT){
timeout = pdMS_TO_TICKS(timeoutMs); timeout = pdMS_TO_TICKS(timeoutMs);
} }
@ -77,7 +77,7 @@ ReturnValue_t Semaphore::takeBinarySemaphore(uint32_t timeoutMs) {
} }
} }
ReturnValue_t Semaphore::takeBinarySemaphoreTickTimeout( ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout(
TickType_t timeoutTicks) { TickType_t timeoutTicks) {
if(handle == nullptr) { if(handle == nullptr) {
return SEMAPHORE_NULLPOINTER; return SEMAPHORE_NULLPOINTER;
@ -91,7 +91,7 @@ ReturnValue_t Semaphore::takeBinarySemaphoreTickTimeout(
} }
} }
ReturnValue_t Semaphore::giveBinarySemaphore() { ReturnValue_t BinarySemaphore::giveBinarySemaphore() {
if (handle == nullptr) { if (handle == nullptr) {
return SEMAPHORE_NULLPOINTER; return SEMAPHORE_NULLPOINTER;
} }
@ -103,11 +103,11 @@ ReturnValue_t Semaphore::giveBinarySemaphore() {
} }
} }
SemaphoreHandle_t Semaphore::getSemaphore() { SemaphoreHandle_t BinarySemaphore::getSemaphore() {
return handle; return handle;
} }
ReturnValue_t Semaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore) { ReturnValue_t BinarySemaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore) {
if (semaphore == nullptr) { if (semaphore == nullptr) {
return SEMAPHORE_NULLPOINTER; return SEMAPHORE_NULLPOINTER;
} }
@ -119,7 +119,7 @@ ReturnValue_t Semaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore) {
} }
} }
void Semaphore::resetSemaphore() { void BinarySemaphore::resetSemaphore() {
if(handle != nullptr) { if(handle != nullptr) {
vSemaphoreDelete(handle); vSemaphoreDelete(handle);
handle = xSemaphoreCreateBinary(); handle = xSemaphoreCreateBinary();
@ -127,20 +127,20 @@ void Semaphore::resetSemaphore() {
} }
} }
ReturnValue_t Semaphore::acquire(uint32_t timeoutMs) { ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
return takeBinarySemaphore(timeoutMs); return takeBinarySemaphore(timeoutMs);
} }
ReturnValue_t Semaphore::release() { ReturnValue_t BinarySemaphore::release() {
return giveBinarySemaphore(); return giveBinarySemaphore();
} }
uint8_t Semaphore::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 Semaphore::giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore, ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore,
BaseType_t * higherPriorityTaskWoken) { BaseType_t * higherPriorityTaskWoken) {
if (semaphore == nullptr) { if (semaphore == nullptr) {
return SEMAPHORE_NULLPOINTER; return SEMAPHORE_NULLPOINTER;

View File

@ -23,7 +23,7 @@ extern "C" {
* @author R. Mueller * @author R. Mueller
* @ingroup osal * @ingroup osal
*/ */
class Semaphore: public SemaphoreIF, class BinarySemaphore: public SemaphoreIF,
public HasReturnvaluesIF { public HasReturnvaluesIF {
public: public:
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF; static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
@ -44,20 +44,20 @@ public:
static constexpr ReturnValue_t SEMAPHORE_NULLPOINTER = MAKE_RETURN_CODE(3); static constexpr ReturnValue_t SEMAPHORE_NULLPOINTER = MAKE_RETURN_CODE(3);
//! @brief Default ctor //! @brief Default ctor
Semaphore(); BinarySemaphore();
//! @brief Copy ctor //! @brief Copy ctor
Semaphore(const Semaphore&); BinarySemaphore(const BinarySemaphore&);
//! @brief Copy assignment //! @brief Copy assignment
Semaphore& operator=(const Semaphore&); BinarySemaphore& operator=(const BinarySemaphore&);
//! @brief Move ctor //! @brief Move ctor
Semaphore (Semaphore &&); BinarySemaphore (BinarySemaphore &&);
//! @brief Move assignment //! @brief Move assignment
Semaphore & operator=(Semaphore &&); BinarySemaphore & operator=(BinarySemaphore &&);
//! @brief Destructor //! @brief Destructor
virtual ~Semaphore(); virtual ~BinarySemaphore();
ReturnValue_t acquire(uint32_t timeoutMs = ReturnValue_t acquire(uint32_t timeoutMs =
Semaphore::NO_BLOCK_TIMEOUT) override; BinarySemaphore::NO_BLOCK_TIMEOUT) override;
ReturnValue_t release() override; ReturnValue_t release() override;
uint8_t getSemaphoreCounter() override; uint8_t getSemaphoreCounter() override;
@ -71,7 +71,7 @@ public:
* -@c RETURN_FAILED on failure * -@c RETURN_FAILED on failure
*/ */
ReturnValue_t takeBinarySemaphore(uint32_t timeoutMs = ReturnValue_t takeBinarySemaphore(uint32_t timeoutMs =
Semaphore::NO_BLOCK_TIMEOUT); BinarySemaphore::NO_BLOCK_TIMEOUT);
/** /**
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks. * Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
@ -80,7 +80,7 @@ public:
* - @c RETURN_FAILED on failure * - @c RETURN_FAILED on failure
*/ */
ReturnValue_t takeBinarySemaphoreTickTimeout(TickType_t timeoutTicks = ReturnValue_t takeBinarySemaphoreTickTimeout(TickType_t timeoutTicks =
Semaphore::NO_BLOCK_TICKS); BinarySemaphore::NO_BLOCK_TICKS);
/** /**
* Give back the binary semaphore * Give back the binary semaphore

View File

@ -2,7 +2,15 @@
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ #define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
#include <framework/osal/FreeRTOS/BinarySemaphore.h> #include <framework/osal/FreeRTOS/BinarySemaphore.h>
class CountingSemaphore: public Semaphore { /**
* @brief Counting semaphores, which can be acquire more than once.
* @details
* See: https://www.freertos.org/CreateCounting.html
* API of counting semaphores is almost identical to binary semaphores,
* so we just inherit from binary semaphore and provide the respective
* constructors.
*/
class CountingSemaphore: public BinarySemaphore {
public: public:
CountingSemaphore(uint8_t count, uint8_t initCount); CountingSemaphore(uint8_t count, uint8_t initCount);
//! @brief Copy ctor, disabled //! @brief Copy ctor, disabled

View File

@ -20,7 +20,7 @@ SemaphoreFactory* SemaphoreFactory::instance() {
} }
SemaphoreIF* SemaphoreFactory::createBinarySemaphore() { SemaphoreIF* SemaphoreFactory::createBinarySemaphore() {
return new Semaphore(); return new BinarySemaphore();
} }
SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t count, SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t count,