fsfw/unittest/internal/osal/IntTestMutex.cpp

43 lines
1.1 KiB
C++
Raw Normal View History

2020-10-29 17:22:41 +01:00
#include "IntTestMutex.h"
2020-12-14 10:48:39 +01:00
#include "../../../ipc/MutexFactory.h"
2020-12-01 16:22:59 +01:00
#include "../UnittDefinitions.h"
2020-10-20 17:11:23 +02:00
#if defined(hosted)
2020-12-01 16:50:02 +01:00
#include "../../osal/hosted/Mutex.h"
2020-10-20 17:11:23 +02:00
#include <thread>
#include <future>
#endif
void testmutex::testMutex() {
std::string id = "[testMutex]";
MutexIF* mutex = MutexFactory::instance()->createMutex();
auto result = mutex->lockMutex(MutexIF::POLLING);
if(result != HasReturnvaluesIF::RETURN_OK) {
unitt::put_error(id);
}
// timed_mutex from the C++ library specifies undefined behaviour if
// the timed mutex is locked twice from the same thread.
#if defined(hosted)
2020-12-01 16:50:02 +01:00
// This calls the function from
2020-10-20 17:11:23 +02:00
// another thread and stores the returnvalue in a future.
auto future = std::async(&MutexIF::lockMutex, mutex, 1);
result = future.get();
#else
result = mutex->lockMutex(MutexIF::TimeoutType::WAITING, 1);
#endif
if(result != MutexIF::MUTEX_TIMEOUT) {
unitt::put_error(id);
}
result = mutex->unlockMutex();
if(result != HasReturnvaluesIF::RETURN_OK) {
unitt::put_error(id);
}
result = mutex->unlockMutex();
if(result != MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX) {
unitt::put_error(id);
}
}