implemented mutex if changes

This commit is contained in:
Robin Müller 2020-08-07 22:10:58 +02:00
parent 9d90348175
commit 7b3fddfd42
4 changed files with 34 additions and 30 deletions

View File

@ -8,9 +8,10 @@ class MutexHelper {
public: public:
MutexHelper(MutexIF* mutex, uint32_t timeoutMs) : MutexHelper(MutexIF* mutex, uint32_t timeoutMs) :
internalMutex(mutex) { internalMutex(mutex) {
ReturnValue_t status = mutex->lockMutex(timeoutMs); ReturnValue_t status = mutex->lockMutex(MutexIF::TimeoutType::WAITING,
timeoutMs);
if(status == MutexIF::MUTEX_TIMEOUT) { if(status == MutexIF::MUTEX_TIMEOUT) {
sif::error << "MutexHelper: Lock of mutex failed with timeout of" sif::error << "MutexHelper: Lock of mutex failed with timeout of "
<< timeoutMs << " milliseconds!" << std::endl; << timeoutMs << " milliseconds!" << std::endl;
} }
else if(status != HasReturnvaluesIF::RETURN_OK){ else if(status != HasReturnvaluesIF::RETURN_OK){

View File

@ -5,7 +5,6 @@
/** /**
* @brief Common interface for OS Mutex objects which provide MUTual EXclusion. * @brief Common interface for OS Mutex objects which provide MUTual EXclusion.
*
* @details https://en.wikipedia.org/wiki/Lock_(computer_science) * @details https://en.wikipedia.org/wiki/Lock_(computer_science)
* @ingroup osal * @ingroup osal
* @ingroup interface * @ingroup interface
@ -13,19 +12,24 @@
class MutexIF { class MutexIF {
public: public:
/** /**
* @brief Timeout value used for polling lock attempt. * Different types of timeout for the mutex lock.
* @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; 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.
};
/** /**
* @brief Timeout value used for permanent blocking lock attempt. * Lock the mutex. The timeout value will only be used for
* @details * TimeoutType::WAITING
* The task will be blocked (indefinitely) until the mutex is unlocked. * @param timeoutType
* Value needs to be defined in implementation. * @param timeoutMs
* @return
*/ */
static const uint32_t BLOCKING; virtual ReturnValue_t lockMutex(TimeoutType timeoutType =
TimeoutType::BLOCKING, uint32_t timeoutMs = 0) = 0;
virtual ReturnValue_t unlockMutex() = 0;
static const uint8_t INTERFACE_ID = CLASS_ID::MUTEX_IF; static const uint8_t INTERFACE_ID = CLASS_ID::MUTEX_IF;
/** /**
@ -78,8 +82,6 @@ public:
static const ReturnValue_t MUTEX_DESTROYED_WHILE_WAITING = MAKE_RETURN_CODE(12); static const ReturnValue_t MUTEX_DESTROYED_WHILE_WAITING = MAKE_RETURN_CODE(12);
virtual ~MutexIF() {} virtual ~MutexIF() {}
virtual ReturnValue_t lockMutex(uint32_t timeoutMs) = 0;
virtual ReturnValue_t unlockMutex() = 0;
}; };

View File

@ -2,13 +2,10 @@
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
const uint32_t MutexIF::POLLING = 0;
const uint32_t MutexIF::BLOCKING = portMAX_DELAY;
Mutex::Mutex() { Mutex::Mutex() {
handle = xSemaphoreCreateMutex(); handle = xSemaphoreCreateMutex();
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "Mutex: Creation failure" << std::endl; sif::error << "Mutex::Mutex(FreeRTOS): Creation failure" << std::endl;
} }
} }
@ -19,15 +16,17 @@ Mutex::~Mutex() {
} }
ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) { ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType,
uint32_t timeoutMs) {
if (handle == nullptr) { if (handle == nullptr) {
return MutexIF::MUTEX_NOT_FOUND; return MutexIF::MUTEX_NOT_FOUND;
} }
TickType_t timeout = MutexIF::POLLING; // If the timeout type is BLOCKING, this will be the correct value.
if(timeoutMs == MutexIF::BLOCKING) { uint32_t timeout = portMAX_DELAY;
timeout = MutexIF::BLOCKING; if(timeoutType == TimeoutType::POLLING) {
timeout = 0;
} }
else if(timeoutMs > MutexIF::POLLING){ else if(timeoutType == TimeoutType::WAITING){
timeout = pdMS_TO_TICKS(timeoutMs); timeout = pdMS_TO_TICKS(timeoutMs);
} }

View File

@ -18,8 +18,10 @@ class Mutex : public MutexIF {
public: public:
Mutex(); Mutex();
~Mutex(); ~Mutex();
ReturnValue_t lockMutex(uint32_t timeoutMs = MutexIF::BLOCKING) override; ReturnValue_t lockMutex(TimeoutType timeoutType,
uint32_t timeoutMs) override;
ReturnValue_t unlockMutex() override; ReturnValue_t unlockMutex() override;
private: private:
SemaphoreHandle_t handle; SemaphoreHandle_t handle;
}; };