fsfw-example-common/example/test/MutexExample.cpp

46 lines
1.5 KiB
C++
Raw Normal View History

2021-06-08 13:35:49 +02:00
#include "MutexExample.h"
#include <fsfw/ipc/MutexFactory.h>
#include <fsfw/serviceinterface/ServiceInterface.h>
2022-05-05 20:55:28 +02:00
void MutexExample::example() {
2022-05-22 15:30:08 +02:00
MutexIF *mutex = MutexFactory::instance()->createMutex();
MutexIF *mutex2 = MutexFactory::instance()->createMutex();
2021-06-08 13:35:49 +02:00
2022-08-08 12:32:06 +02:00
ReturnValue_t result = mutex->lockMutex(MutexIF::TimeoutType::WAITING, 2 * 60 * 1000);
2022-09-07 17:40:06 +02:00
if (result != returnvalue::OK) {
2021-06-08 13:35:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-08-08 12:32:06 +02:00
sif::error << "MutexExample::example: Lock Failed with " << result << std::endl;
2021-06-08 13:35:49 +02:00
#else
2022-05-05 20:55:28 +02:00
sif::printError("MutexExample::example: Lock Failed with %hu\n", result);
2021-06-08 13:35:49 +02:00
#endif
2022-05-05 20:55:28 +02:00
}
2021-06-08 13:35:49 +02:00
2022-05-05 20:55:28 +02:00
result = mutex2->lockMutex(MutexIF::TimeoutType::BLOCKING);
2022-09-07 17:40:06 +02:00
if (result != returnvalue::OK) {
2021-06-08 13:35:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-08-08 12:32:06 +02:00
sif::error << "MutexExample::example: Lock Failed with " << result << std::endl;
2021-06-08 13:35:49 +02:00
#else
2022-05-05 20:55:28 +02:00
sif::printError("MutexExample::example: Lock Failed with %hu\n", result);
2021-06-08 13:35:49 +02:00
#endif
2022-05-05 20:55:28 +02:00
}
2021-06-08 13:35:49 +02:00
2022-05-05 20:55:28 +02:00
result = mutex->unlockMutex();
2022-09-07 17:40:06 +02:00
if (result != returnvalue::OK) {
2021-06-08 13:35:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-08-08 12:32:06 +02:00
sif::error << "MutexExample::example: Unlock Failed with " << result << std::endl;
2021-06-08 13:35:49 +02:00
#else
2022-05-05 20:55:28 +02:00
sif::printError("MutexExample::example: Unlock Failed with %hu\n", result);
2021-06-08 13:35:49 +02:00
#endif
2022-05-05 20:55:28 +02:00
}
2021-06-08 13:35:49 +02:00
2022-05-05 20:55:28 +02:00
result = mutex2->unlockMutex();
2022-09-07 17:40:06 +02:00
if (result != returnvalue::OK) {
2021-06-08 13:35:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-08-08 12:32:06 +02:00
sif::error << "MutexExample::example: Unlock Failed with " << result << std::endl;
2021-06-08 13:35:49 +02:00
#else
2022-05-05 20:55:28 +02:00
sif::printError("MutexExample::example: Unlock Failed with %hu\n", result);
2021-06-08 13:35:49 +02:00
#endif
2022-05-05 20:55:28 +02:00
}
2021-06-08 13:35:49 +02:00
}