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() {
MutexIF* mutex = MutexFactory::instance()->createMutex();
MutexIF* mutex2 = MutexFactory::instance()->createMutex();
2021-06-08 13:35:49 +02:00
2022-05-05 20:55:28 +02:00
ReturnValue_t result = mutex->lockMutex(MutexIF::TimeoutType::WAITING, 2 * 60 * 1000);
if (result != HasReturnvaluesIF::RETURN_OK) {
2021-06-08 13:35:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-05 20:55:28 +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);
if (result != HasReturnvaluesIF::RETURN_OK) {
2021-06-08 13:35:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-05 20:55:28 +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();
if (result != HasReturnvaluesIF::RETURN_OK) {
2021-06-08 13:35:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-05 20:55:28 +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();
if (result != HasReturnvaluesIF::RETURN_OK) {
2021-06-08 13:35:49 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-05-05 20:55:28 +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
}