updated linux implementation

This commit is contained in:
Robin Müller 2020-08-27 17:06:10 +02:00
parent 7723ee13b3
commit 73db79a3fe
5 changed files with 135 additions and 137 deletions

View File

@ -1,5 +1,5 @@
#include <framework/osal/linux/BinarySemaphore.h> #include "../../osal/linux/BinarySemaphore.h"
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include "../../serviceinterface/ServiceInterfaceStream.h"
extern "C" { extern "C" {
#include <errno.h> #include <errno.h>
@ -25,15 +25,16 @@ BinarySemaphore& BinarySemaphore::operator =(
return * this; return * this;
} }
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) { ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType,
uint32_t timeoutMs) {
int result = 0; int result = 0;
if(timeoutMs == SemaphoreIF::POLLING) { if(timeoutType == TimeoutType::POLLING) {
result = sem_trywait(&handle); result = sem_trywait(&handle);
} }
else if(timeoutMs == SemaphoreIF::BLOCKING) { else if(timeoutType == TimeoutType::BLOCKING) {
result = sem_wait(&handle); result = sem_wait(&handle);
} }
else if(timeoutMs > SemaphoreIF::POLLING){ else if(timeoutType == TimeoutType::WAITING){
timespec timeOut; timespec timeOut;
clock_gettime(CLOCK_REALTIME, &timeOut); clock_gettime(CLOCK_REALTIME, &timeOut);
uint64_t nseconds = timeOut.tv_sec * 1000000000 + timeOut.tv_nsec; uint64_t nseconds = timeOut.tv_sec * 1000000000 + timeOut.tv_nsec;

View File

@ -1,8 +1,8 @@
#ifndef FRAMEWORK_OSAL_LINUX_BINARYSEMPAHORE_H_ #ifndef FRAMEWORK_OSAL_LINUX_BINARYSEMPAHORE_H_
#define FRAMEWORK_OSAL_LINUX_BINARYSEMPAHORE_H_ #define FRAMEWORK_OSAL_LINUX_BINARYSEMPAHORE_H_
#include <framework/returnvalues/HasReturnvaluesIF.h> #include "../../returnvalues/HasReturnvaluesIF.h"
#include <framework/tasks/SemaphoreIF.h> #include "../../tasks/SemaphoreIF.h"
extern "C" { extern "C" {
#include <semaphore.h> #include <semaphore.h>
@ -49,8 +49,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 = TimeoutType::BLOCKING,
SemaphoreIF::BLOCKING) override; uint32_t timeoutMs = 0) override;
/** /**
* Release the binary semaphore. * Release the binary semaphore.

View File

@ -1,54 +1,54 @@
#include <framework/osal/linux/CountingSemaphore.h> #include "../../osal/linux/CountingSemaphore.h"
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include "../../serviceinterface/ServiceInterfaceStream.h"
CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount): CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount):
maxCount(maxCount), initCount(initCount) { maxCount(maxCount), initCount(initCount) {
if(initCount > maxCount) { if(initCount > maxCount) {
sif::error << "CountingSemaphoreUsingTask: Max count bigger than " sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
"intial cout. Setting initial count to max count." << std::endl; "intial cout. Setting initial count to max count." << std::endl;
initCount = maxCount; initCount = maxCount;
} }
initSemaphore(initCount); initSemaphore(initCount);
} }
CountingSemaphore::CountingSemaphore(CountingSemaphore&& other): CountingSemaphore::CountingSemaphore(CountingSemaphore&& other):
maxCount(other.maxCount), initCount(other.initCount) { maxCount(other.maxCount), initCount(other.initCount) {
initSemaphore(initCount); initSemaphore(initCount);
} }
CountingSemaphore& CountingSemaphore::operator =( CountingSemaphore& CountingSemaphore::operator =(
CountingSemaphore&& other) { CountingSemaphore&& other) {
initSemaphore(other.initCount); initSemaphore(other.initCount);
return * this; return * this;
} }
ReturnValue_t CountingSemaphore::release() { ReturnValue_t CountingSemaphore::release() {
ReturnValue_t result = checkCount(&handle, maxCount); ReturnValue_t result = checkCount(&handle, maxCount);
if(result != HasReturnvaluesIF::RETURN_OK) { if(result != HasReturnvaluesIF::RETURN_OK) {
return result; return result;
} }
return CountingSemaphore::release(&this->handle); return CountingSemaphore::release(&this->handle);
} }
ReturnValue_t CountingSemaphore::release(sem_t* handle) { ReturnValue_t CountingSemaphore::release(sem_t* handle) {
int result = sem_post(handle); int result = sem_post(handle);
if(result == 0) { if(result == 0) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
switch(errno) { switch(errno) {
case(EINVAL): case(EINVAL):
// Semaphore invalid // Semaphore invalid
return SemaphoreIF::SEMAPHORE_INVALID; return SemaphoreIF::SEMAPHORE_INVALID;
case(EOVERFLOW): case(EOVERFLOW):
// SEM_MAX_VALUE overflow. This should never happen // SEM_MAX_VALUE overflow. This should never happen
default: default:
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
uint8_t CountingSemaphore::getMaxCount() const { uint8_t CountingSemaphore::getMaxCount() const {
return maxCount; return maxCount;
} }

View File

@ -1,37 +1,37 @@
#ifndef FRAMEWORK_OSAL_LINUX_COUNTINGSEMAPHORE_H_ #ifndef FRAMEWORK_OSAL_LINUX_COUNTINGSEMAPHORE_H_
#define FRAMEWORK_OSAL_LINUX_COUNTINGSEMAPHORE_H_ #define FRAMEWORK_OSAL_LINUX_COUNTINGSEMAPHORE_H_
#include <framework/osal/linux/BinarySemaphore.h> #include "../../osal/linux/BinarySemaphore.h"
/** /**
* @brief Counting semaphores, which can be acquired more than once. * @brief Counting semaphores, which can be acquired more than once.
* @details * @details
* See: https://www.freertos.org/CreateCounting.html * See: https://www.freertos.org/CreateCounting.html
* API of counting semaphores is almost identical to binary semaphores, * API of counting semaphores is almost identical to binary semaphores,
* so we just inherit from binary semaphore and provide the respective * so we just inherit from binary semaphore and provide the respective
* constructors. * constructors.
*/ */
class CountingSemaphore: public BinarySemaphore { class CountingSemaphore: public BinarySemaphore {
public: public:
CountingSemaphore(const uint8_t maxCount, uint8_t initCount); CountingSemaphore(const uint8_t maxCount, uint8_t initCount);
//! @brief Copy ctor, disabled //! @brief Copy ctor, disabled
CountingSemaphore(const CountingSemaphore&) = delete; CountingSemaphore(const CountingSemaphore&) = delete;
//! @brief Copy assignment, disabled //! @brief Copy assignment, disabled
CountingSemaphore& operator=(const CountingSemaphore&) = delete; CountingSemaphore& operator=(const CountingSemaphore&) = delete;
//! @brief Move ctor //! @brief Move ctor
CountingSemaphore (CountingSemaphore &&); CountingSemaphore (CountingSemaphore &&);
//! @brief Move assignment //! @brief Move assignment
CountingSemaphore & operator=(CountingSemaphore &&); CountingSemaphore & operator=(CountingSemaphore &&);
ReturnValue_t release() override; ReturnValue_t release() override;
static ReturnValue_t release(sem_t* sem); static ReturnValue_t release(sem_t* sem);
/* Same API as binary semaphore otherwise. acquire() can be called /* Same API as binary semaphore otherwise. acquire() can be called
* until there are not semaphores left and release() can be called * until there are not semaphores left and release() can be called
* until maxCount is reached. */ * until maxCount is reached. */
uint8_t getMaxCount() const; uint8_t getMaxCount() const;
private: private:
const uint8_t maxCount; const uint8_t maxCount;
uint8_t initCount = 0; uint8_t initCount = 0;
}; };
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */ #endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */

View File

@ -1,36 +1,33 @@
#include <framework/tasks/SemaphoreFactory.h> #include "../../tasks/SemaphoreFactory.h"
#include <framework/osal/linux/BinarySemaphore.h> #include "../../osal/linux/BinarySemaphore.h"
#include <framework/osal/linux/CountingSemaphore.h> #include "../../osal/linux/CountingSemaphore.h"
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include "../../serviceinterface/ServiceInterfaceStream.h"
const uint32_t SemaphoreIF::POLLING = 0; SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr;
const uint32_t SemaphoreIF::BLOCKING = 0xffffffff;
SemaphoreFactory::SemaphoreFactory() {
SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr; }
SemaphoreFactory::SemaphoreFactory() { SemaphoreFactory::~SemaphoreFactory() {
} delete factoryInstance;
}
SemaphoreFactory::~SemaphoreFactory() {
delete factoryInstance; SemaphoreFactory* SemaphoreFactory::instance() {
} if (factoryInstance == nullptr){
factoryInstance = new SemaphoreFactory();
SemaphoreFactory* SemaphoreFactory::instance() { }
if (factoryInstance == nullptr){ return SemaphoreFactory::factoryInstance;
factoryInstance = new SemaphoreFactory(); }
}
return SemaphoreFactory::factoryInstance; SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t arguments) {
} return new BinarySemaphore();
}
SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t arguments) {
return new BinarySemaphore(); SemaphoreIF* SemaphoreFactory::createCountingSemaphore(const uint8_t maxCount,
} uint8_t initCount, uint32_t arguments) {
return new CountingSemaphore(maxCount, initCount);
SemaphoreIF* SemaphoreFactory::createCountingSemaphore(const uint8_t maxCount, }
uint8_t initCount, uint32_t arguments) {
return new CountingSemaphore(maxCount, initCount); void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) {
} delete semaphore;
}
void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) {
delete semaphore;
}