added retvals for mutex

This commit is contained in:
Robin Müller 2020-05-29 14:02:14 +02:00
parent cb14ec15b5
commit 8f563b7b21
2 changed files with 23 additions and 20 deletions

View File

@ -1,4 +1,4 @@
#include "Mutex.h" #include <framework/osal/FreeRTOS/Mutex.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
@ -6,7 +6,9 @@ const uint32_t MutexIF::NO_TIMEOUT = 0;
Mutex::Mutex() { Mutex::Mutex() {
handle = xSemaphoreCreateMutex(); handle = xSemaphoreCreateMutex();
//TODO print error if(handle == NULL) {
sif::error << "Mutex creation failure" << std::endl;
}
} }
Mutex::~Mutex() { Mutex::~Mutex() {
@ -18,8 +20,7 @@ Mutex::~Mutex() {
ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) { ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
if (handle == 0) { if (handle == 0) {
//TODO Does not exist return MutexIF::MUTEX_NOT_FOUND;
return HasReturnvaluesIF::RETURN_FAILED;
} }
TickType_t timeout = portMAX_DELAY; TickType_t timeout = portMAX_DELAY;
if (timeoutMs != NO_TIMEOUT) { if (timeoutMs != NO_TIMEOUT) {
@ -30,21 +31,18 @@ ReturnValue_t Mutex::lockMutex(uint32_t timeoutMs) {
if (returncode == pdPASS) { if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { } else {
//TODO could not be acquired/timeout return MutexIF::MUTEX_TIMEOUT;
return HasReturnvaluesIF::RETURN_FAILED;
} }
} }
ReturnValue_t Mutex::unlockMutex() { ReturnValue_t Mutex::unlockMutex() {
if (handle == 0) { if (handle == 0) {
//TODO Does not exist return MutexIF::MUTEX_NOT_FOUND;
return HasReturnvaluesIF::RETURN_FAILED;
} }
BaseType_t returncode = xSemaphoreGive(handle); BaseType_t returncode = xSemaphoreGive(handle);
if (returncode == pdPASS) { if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} else { } else {
//TODO is not owner return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX;
return HasReturnvaluesIF::RETURN_FAILED;
} }
} }

View File

@ -1,22 +1,27 @@
#ifndef OS_RTEMS_MUTEX_H_ #ifndef FRAMEWORK_FREERTOS_MUTEX_H_
#define OS_RTEMS_MUTEX_H_ #define FRAMEWORK_FREERTOS_MUTEX_H_
#include <framework/ipc/MutexIF.h> #include <framework/ipc/MutexIF.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <FreeRTOS.h> /**
#include "semphr.h" * @brief OS component to implement MUTual EXclusion
*
* @details
* Mutexes are binary semaphores which include a priority inheritance mechanism.
* Documentation: https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html
* @ingroup osal
*/
class Mutex : public MutexIF { class Mutex : public MutexIF {
public: public:
Mutex(); Mutex();
~Mutex(); ~Mutex();
ReturnValue_t lockMutex(uint32_t timeoutMs); ReturnValue_t lockMutex(uint32_t timeoutMs) override;
ReturnValue_t unlockMutex(); ReturnValue_t unlockMutex() override;
private: private:
SemaphoreHandle_t handle; SemaphoreHandle_t handle;
}; };
#endif /* OS_RTEMS_MUTEX_H_ */ #endif /* FRAMEWORK_FREERTOS_MUTEX_H_ */