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