From 7849b8e39130d4ccb7f0aada141dc0deaad852d9 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 19 Jul 2021 15:07:56 +0200 Subject: [PATCH] mutex update --- src/osal/host/Mutex.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/osal/host/Mutex.cpp diff --git a/src/osal/host/Mutex.cpp b/src/osal/host/Mutex.cpp new file mode 100644 index 00000000..1c8b1dd8 --- /dev/null +++ b/src/osal/host/Mutex.cpp @@ -0,0 +1,32 @@ +#include "Mutex.h" +#include "fsfw/serviceinterface/ServiceInterface.h" + +Mutex::Mutex() {} + +ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType, uint32_t timeoutMs) { + if(timeoutType == TimeoutType::BLOCKING) { + mutex.lock(); + return HasReturnvaluesIF::RETURN_OK; + } + else if(timeoutType == TimeoutType::POLLING) { + if(mutex.try_lock()) { + return HasReturnvaluesIF::RETURN_OK; + } + } + else if(timeoutType == TimeoutType::WAITING){ + auto chronoMs = std::chrono::milliseconds(timeoutMs); + if(mutex.try_lock_for(chronoMs)) { + return HasReturnvaluesIF::RETURN_OK; + } + } + return MutexIF::MUTEX_TIMEOUT; +} + +ReturnValue_t Mutex::unlockMutex() { + mutex.unlock(); + return HasReturnvaluesIF::RETURN_OK; +} + +std::timed_mutex* Mutex::getMutexHandle() { + return &mutex; +}