DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
uhallibs
src
RobustMutex.cpp
Go to the documentation of this file.
1
#include "
uhallibs/ipc/RobustMutex.hpp
"
2
3
#include "uhal/log/log.hpp"
4
#include <cstring>
5
6
namespace
uhallibs
{
7
namespace
ipc
{
8
9
RobustMutex::RobustMutex
()
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
44
RobustMutex::~RobustMutex
() {}
45
46
void
47
RobustMutex::lock
()
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
67
void
68
RobustMutex::unlock
()
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
75
uint64_t
76
RobustMutex::getCounter
()
const
77
{
78
return
mCount
;
79
}
80
81
bool
82
RobustMutex::isActive
()
const
83
{
84
return
mSessionActive
;
85
}
86
87
void
88
RobustMutex::startSession
()
89
{
90
mCount
++;
91
mSessionActive
=
true
;
92
}
93
94
void
95
RobustMutex::endSession
()
96
{
97
mSessionActive
=
false
;
98
}
99
100
}
// namespace ipc
101
}
// namespace uhal
RobustMutex.hpp
uhallibs::ipc::RobustMutex::lock
void lock()
Definition
RobustMutex.cpp:47
uhallibs::ipc::RobustMutex::~RobustMutex
~RobustMutex()
Definition
RobustMutex.cpp:44
uhallibs::ipc::RobustMutex::getCounter
uint64_t getCounter() const
Definition
RobustMutex.cpp:76
uhallibs::ipc::RobustMutex::RobustMutex
RobustMutex()
Definition
RobustMutex.cpp:9
uhallibs::ipc::RobustMutex::isActive
bool isActive() const
Definition
RobustMutex.cpp:82
uhallibs::ipc::RobustMutex::startSession
void startSession()
Definition
RobustMutex.cpp:88
uhallibs::ipc::RobustMutex::unlock
void unlock()
Definition
RobustMutex.cpp:68
uhallibs::ipc::RobustMutex::mSessionActive
bool mSessionActive
Definition
RobustMutex.hpp:43
uhallibs::ipc::RobustMutex::mCount
uint64_t mCount
Definition
RobustMutex.hpp:42
uhallibs::ipc::RobustMutex::mMutex
pthread_mutex_t mMutex
Definition
RobustMutex.hpp:41
uhallibs::ipc::RobustMutex::endSession
void endSession()
Definition
RobustMutex.cpp:95
uhallibs::ipc
Definition
RobustMutex.hpp:11
uhallibs
Definition
formatters.hpp:9
Generated on
for DUNE-DAQ by
1.18.0