fsfw/src/fsfw/ipc/MutexGuard.h

61 lines
1.9 KiB
C
Raw Normal View History

2021-03-09 21:25:22 +01:00
#ifndef FRAMEWORK_IPC_MUTEXGUARD_H_
#define FRAMEWORK_IPC_MUTEXGUARD_H_
2018-07-13 18:28:26 +02:00
2021-02-23 14:40:08 +01:00
#include "../serviceinterface/ServiceInterface.h"
2022-02-02 10:29:30 +01:00
#include "MutexFactory.h"
2018-07-13 18:28:26 +02:00
2021-03-09 11:25:13 +01:00
class MutexGuard {
2022-02-02 10:29:30 +01:00
public:
MutexGuard(MutexIF* mutex, MutexIF::TimeoutType timeoutType = MutexIF::TimeoutType::BLOCKING,
2023-03-02 15:20:59 +01:00
uint32_t timeoutMs = 0, const char* context = nullptr)
2022-02-02 10:29:30 +01:00
: internalMutex(mutex) {
2023-03-02 15:20:59 +01:00
if (context == nullptr) {
context = "unknown";
}
2022-02-02 10:29:30 +01:00
if (mutex == nullptr) {
2021-02-23 14:40:08 +01:00
#if FSFW_VERBOSE_LEVEL >= 1
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2023-03-02 15:20:59 +01:00
sif::error << "MutexGuard::" << context << ": Passed mutex is invalid!" << std::endl;
2021-02-23 14:40:08 +01:00
#else
2023-03-02 15:20:59 +01:00
sif::printError("MutexGuard::%s: Passed mutex is invalid!\n", context);
2021-02-23 14:40:08 +01:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
2022-02-02 10:29:30 +01:00
return;
}
result = mutex->lockMutex(timeoutType, timeoutMs);
2021-02-23 14:40:08 +01:00
#if FSFW_VERBOSE_LEVEL >= 1
2022-02-02 10:29:30 +01:00
if (result == MutexIF::MUTEX_TIMEOUT) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2023-03-03 14:30:35 +01:00
sif::error << "MutexGuard::" << context << ": Lock of mutex failed with timeout of "
<< timeoutMs << " milliseconds!" << std::endl;
2021-02-23 14:40:08 +01:00
#else
2023-03-03 14:30:35 +01:00
sif::printError("MutexGuard::%s: Lock of mutex failed with timeout of %lu milliseconds\n",
context, timeoutMs);
2021-02-23 14:40:08 +01:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
2018-07-13 18:28:26 +02:00
2022-08-16 01:08:26 +02:00
} else if (result != returnvalue::OK) {
2021-02-23 14:40:08 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "MutexGuard: Lock of Mutex failed with code " << result << std::endl;
2021-02-23 14:40:08 +01:00
#else
2022-02-02 10:29:30 +01:00
sif::printError("MutexGuard: Lock of Mutex failed with code %d\n", result);
2021-02-23 14:40:08 +01:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
2022-02-02 10:29:30 +01:00
}
2021-03-04 20:43:08 +01:00
#else
2021-02-23 14:40:08 +01:00
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
2022-02-02 10:29:30 +01:00
}
2021-02-23 14:40:08 +01:00
2022-02-02 10:29:30 +01:00
ReturnValue_t getLockResult() const { return result; }
2021-03-09 11:25:13 +01:00
2022-02-02 10:29:30 +01:00
~MutexGuard() {
if (internalMutex != nullptr) {
internalMutex->unlockMutex();
2021-02-23 14:40:08 +01:00
}
2022-02-02 10:29:30 +01:00
}
private:
MutexIF* internalMutex;
2022-08-16 01:08:26 +02:00
ReturnValue_t result = returnvalue::FAILED;
2018-07-13 18:28:26 +02:00
};
2021-02-23 14:40:08 +01:00
2021-03-09 21:25:22 +01:00
#endif /* FRAMEWORK_IPC_MUTEXGUARD_H_ */