DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
RobustMutex.cpp
Go to the documentation of this file.
2
3#include <cstring>
4#include "uhal/log/log.hpp"
5
6
7namespace uhallibs {
8namespace ipc {
9
10RobustMutex::RobustMutex() : mCount(0), mSessionActive(false) {
11 pthread_mutexattr_t lAttr;
12
13 int s = pthread_mutexattr_init(&lAttr);
14 if (s != 0) {
15 exception::MutexError lExc;
16 log(lExc, "Error code ", uhal::Integer(s), " (", strerror(s),
17 ") returned in mutex attr initialisation");
18 throw lExc;
19 }
20
21 s = pthread_mutexattr_setpshared(&lAttr, PTHREAD_PROCESS_SHARED);
22 if (s != 0) {
23 exception::MutexError lExc;
24 log(lExc, "Error code ", uhal::Integer(s), " (", strerror(s),
25 ") returned by pthread_mutexattr_setpshared");
26 throw lExc;
27 }
28
29 s = pthread_mutexattr_setrobust(&lAttr, PTHREAD_MUTEX_ROBUST);
30 if (s != 0) {
31 exception::MutexError lExc;
32 log(lExc, "Error code ", uhal::Integer(s), " (", strerror(s),
33 ") returned by pthread_mutexattr_setrobust");
34 throw lExc;
35 }
36
37 s = pthread_mutex_init(&mMutex, &lAttr);
38 if (s != 0) {
39 exception::MutexError lExc;
40 log(lExc, "Error code ", uhal::Integer(s), " (", strerror(s),
41 ") returned in mutex initialisation");
42 throw lExc;
43 }
44}
45
47
49 int s = pthread_mutex_lock(&mMutex);
50 bool lLastOwnerDied = (s == EOWNERDEAD);
51 if (lLastOwnerDied) s = pthread_mutex_consistent(&mMutex);
52
53 if (s != 0) {
54 exception::MutexError lExc;
55 log(lExc, "Error code ", uhal::Integer(s), " (", strerror(s), ") returned when ",
56 lLastOwnerDied ? "making mutex state consistent" : "locking mutex");
57 throw lExc;
58 }
59}
60
62 int s = pthread_mutex_unlock(&mMutex);
63 if (s != 0)
64 log(uhal::Error(), "Error code ", uhal::Integer(s), " (", strerror(s),
65 ") returned when unlocking mutex");
66}
67
68uint64_t RobustMutex::getCounter() const { return mCount; }
69
70bool RobustMutex::isActive() const { return mSessionActive; }
71
73 mCount++;
74 mSessionActive = true;
75}
76
78
79} // namespace ipc
80} // namespace uhal
uint64_t getCounter() const