fsfw/osal/host/Mutex.cpp

40 lines
899 B
C++
Raw Normal View History

2020-09-05 20:39:10 +02:00
#include "Mutex.h"
2020-08-28 18:33:29 +02:00
#include "../../serviceinterface/ServiceInterfaceStream.h"
2020-09-05 20:39:10 +02:00
Mutex::Mutex() {}
2020-08-28 18:33:29 +02:00
2020-09-05 20:39:10 +02:00
ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType, uint32_t timeoutMs) {
2020-08-28 18:33:29 +02:00
if(timeoutMs == MutexIF::BLOCKING) {
mutex.lock();
locked = true;
return HasReturnvaluesIF::RETURN_OK;
}
else if(timeoutMs == MutexIF::POLLING) {
if(mutex.try_lock()) {
locked = true;
return HasReturnvaluesIF::RETURN_OK;
}
}
else if(timeoutMs > MutexIF::POLLING){
auto chronoMs = std::chrono::milliseconds(timeoutMs);
if(mutex.try_lock_for(chronoMs)) {
locked = true;
return HasReturnvaluesIF::RETURN_OK;
}
}
return MutexIF::MUTEX_TIMEOUT;
}
ReturnValue_t Mutex::unlockMutex() {
if(not locked) {
return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX;
}
mutex.unlock();
locked = false;
return HasReturnvaluesIF::RETURN_OK;
}
std::timed_mutex* Mutex::getMutexHandle() {
return &mutex;
}