fsfw/src/fsfw_tests/internal/osal/testMutex.cpp

49 lines
1.4 KiB
C++
Raw Normal View History

#include "testMutex.h"
2020-10-29 17:22:41 +01:00
2020-12-27 15:15:51 +01:00
#include <fsfw/ipc/MutexFactory.h>
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
#include "fsfw/platform.h"
#include "fsfw_tests/internal/UnittDefinitions.h"
2021-08-16 11:27:46 +02:00
2022-02-02 10:29:30 +01:00
#if defined PLATFORM_WIN || defined PLATFORM_UNIX
2020-10-20 17:11:23 +02:00
#include <future>
2022-02-02 10:29:30 +01:00
#include <thread>
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
#include "fsfw/osal/host/Mutex.h"
#endif
2020-10-20 17:11:23 +02:00
void testmutex::testMutex() {
2022-02-02 10:29:30 +01:00
std::string id = "[testMutex]";
MutexIF* mutex = MutexFactory::instance()->createMutex();
auto result = mutex->lockMutex(MutexIF::TimeoutType::POLLING);
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
unitt::put_error(id);
}
// timed_mutex from the C++ library specifies undefined behaviour if
// the timed mutex is locked twice from the same thread.
// TODO: we should pass a define like FSFW_OSAL_HOST to the build.
2021-08-16 11:27:46 +02:00
#if defined PLATFORM_WIN || defined PLATFORM_UNIX
2022-02-02 10:29:30 +01:00
// This calls the function from
// another thread and stores the returnvalue in a future.
auto future = std::async(&MutexIF::lockMutex, mutex, MutexIF::TimeoutType::WAITING, 1);
result = future.get();
2020-10-20 17:11:23 +02:00
#else
2022-02-02 10:29:30 +01:00
result = mutex->lockMutex(MutexIF::TimeoutType::WAITING, 1);
2020-10-20 17:11:23 +02:00
#endif
2022-02-02 10:29:30 +01:00
if (result != MutexIF::MUTEX_TIMEOUT) {
unitt::put_error(id);
}
2020-10-20 17:11:23 +02:00
2022-02-02 10:29:30 +01:00
result = mutex->unlockMutex();
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
unitt::put_error(id);
}
2021-01-15 17:25:27 +01:00
2021-08-16 11:27:46 +02:00
#if !defined PLATFORM_WIN && !defined PLATFORM_UNIX
2022-02-02 10:29:30 +01:00
result = mutex->unlockMutex();
if (result != MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX) {
unitt::put_error(id);
}
2021-01-15 17:25:27 +01:00
#endif
2020-10-20 17:11:23 +02:00
}