μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RobustSessionMutex.cpp
Go to the documentation of this file.
1
3
4
5#include <string.h>
6
7#include "uhal/log/log.hpp"
8
9
10namespace uhal {
11namespace detail {
12
14 mCount(0),
15 mSessionActive(false)
16{
17 pthread_mutexattr_t lAttr;
18
19 int s = pthread_mutexattr_init(&lAttr);
20 if (s != 0) {
21 MutexError lExc;
22 log(lExc, "Error code ", Integer(s), " (", strerror(s), ") returned in mutex attr initialisation");
23 throw lExc;
24 }
25
26 s = pthread_mutexattr_setpshared(&lAttr, PTHREAD_PROCESS_SHARED);
27 if (s != 0) {
28 MutexError lExc;
29 log(lExc, "Error code ", Integer(s), " (", strerror(s), ") returned by pthread_mutexattr_setpshared");
30 throw lExc;
31 }
32
33 s = pthread_mutexattr_setrobust(&lAttr, PTHREAD_MUTEX_ROBUST);
34 if (s != 0) {
35 MutexError lExc;
36 log(lExc, "Error code ", Integer(s), " (", strerror(s), ") returned by pthread_mutexattr_setrobust");
37 throw lExc;
38 }
39
40 s = pthread_mutex_init(&mMutex, &lAttr);
41 if (s != 0) {
42 MutexError lExc;
43 log(lExc, "Error code ", Integer(s), " (", strerror(s), ") returned in mutex initialisation");
44 throw lExc;
45 }
46}
47
48
50{
51}
52
53
55{
56 int s = pthread_mutex_lock(&mMutex);
57 bool lLastOwnerDied = (s == EOWNERDEAD);
58 if (lLastOwnerDied)
59 s = pthread_mutex_consistent(&mMutex);
60
61 if (s != 0) {
62 MutexError lExc;
63 log(lExc, "Error code ", Integer(s), " (", strerror(s), ") returned when ", lLastOwnerDied ? "making mutex state consistent" : "locking mutex");
64 throw lExc;
65 }
66}
67
68
70{
71 int s = pthread_mutex_unlock(&mMutex);
72 if (s != 0)
73 log(Error(), "Error code ", Integer(s), " (", strerror(s), ") returned when unlocking mutex");
74}
75
76
78{
79 return mCount;
80}
81
82
84{
85 return mSessionActive;
86}
87
88
90{
91 mCount++;
92 mSessionActive = true;
93}
94
96{
97 mSessionActive = false;
98}
99
100}
101}
_Integer< T, IntFmt<> > Integer(const T &aT)
Forward declare a function which creates an instance of the ultra-lightweight wrapper from an integer...
ErrorLevel Error
Definition: LogLevels.cpp:61
void log(FatalLevel &aFatal, const T0 &aArg0)
Function to add a log entry at Fatal level.
Definition: log.hxx:18