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:
MutexHelper(MutexIF* mutex, uint32_t timeoutMs) :
internalMutex(mutex) {
ReturnValue_t status = mutex->lockMutex(timeoutMs);
ReturnValue_t status = mutex->lockMutex(MutexIF::TimeoutType::WAITING,
timeoutMs);
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;
}
else if(status != HasReturnvaluesIF::RETURN_OK){

View File

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

View File

@ -2,13 +2,10 @@
#include <framework/serviceinterface/ServiceInterfaceStream.h>
const uint32_t MutexIF::POLLING = 0;
const uint32_t MutexIF::BLOCKING = portMAX_DELAY;
Mutex::Mutex() {
handle = xSemaphoreCreateMutex();
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) {
return MutexIF::MUTEX_NOT_FOUND;
}
TickType_t timeout = MutexIF::POLLING;
if(timeoutMs == MutexIF::BLOCKING) {
timeout = MutexIF::BLOCKING;
// If the timeout type is BLOCKING, this will be the correct value.
uint32_t timeout = portMAX_DELAY;
if(timeoutType == TimeoutType::POLLING) {
timeout = 0;
}
else if(timeoutMs > MutexIF::POLLING){
else if(timeoutType == TimeoutType::WAITING){
timeout = pdMS_TO_TICKS(timeoutMs);
}

View File

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