DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
uhallibs::ipc::RobustMutex Class Reference

#include <RobustMutex.hpp>

Public Member Functions

 RobustMutex ()
 ~RobustMutex ()
void lock ()
void unlock ()
uint64_t getCounter () const
bool isActive () const
void startSession ()
void endSession ()

Private Member Functions

 RobustMutex (const RobustMutex &)

Private Attributes

pthread_mutex_t mMutex
uint64_t mCount
bool mSessionActive

Detailed Description

Definition at line 20 of file RobustMutex.hpp.

Constructor & Destructor Documentation

◆ RobustMutex() [1/2]

uhallibs::ipc::RobustMutex::RobustMutex ( )

Definition at line 9 of file RobustMutex.cpp.

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}
const std::string strerror(int error)
Convert C error number to string.
Definition kernel.cpp:111

◆ ~RobustMutex()

uhallibs::ipc::RobustMutex::~RobustMutex ( )

Definition at line 44 of file RobustMutex.cpp.

44{}

◆ RobustMutex() [2/2]

uhallibs::ipc::RobustMutex::RobustMutex ( const RobustMutex & )
private

Member Function Documentation

◆ endSession()

void uhallibs::ipc::RobustMutex::endSession ( )

Definition at line 95 of file RobustMutex.cpp.

96{
97 mSessionActive = false;
98}

◆ getCounter()

uint64_t uhallibs::ipc::RobustMutex::getCounter ( ) const

Definition at line 76 of file RobustMutex.cpp.

77{
78 return mCount;
79}

◆ isActive()

bool uhallibs::ipc::RobustMutex::isActive ( ) const

Definition at line 82 of file RobustMutex.cpp.

83{
84 return mSessionActive;
85}

◆ lock()

void uhallibs::ipc::RobustMutex::lock ( )

Definition at line 47 of file RobustMutex.cpp.

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}

◆ startSession()

void uhallibs::ipc::RobustMutex::startSession ( )

Definition at line 88 of file RobustMutex.cpp.

89{
90 mCount++;
91 mSessionActive = true;
92}

◆ unlock()

void uhallibs::ipc::RobustMutex::unlock ( )

Definition at line 68 of file RobustMutex.cpp.

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}

Member Data Documentation

◆ mCount

uint64_t uhallibs::ipc::RobustMutex::mCount
private

Definition at line 42 of file RobustMutex.hpp.

◆ mMutex

pthread_mutex_t uhallibs::ipc::RobustMutex::mMutex
private

Definition at line 41 of file RobustMutex.hpp.

◆ mSessionActive

bool uhallibs::ipc::RobustMutex::mSessionActive
private

Definition at line 43 of file RobustMutex.hpp.


The documentation for this class was generated from the following files: