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 10 of file RobustMutex.cpp.

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

◆ ~RobustMutex()

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

Definition at line 46 of file RobustMutex.cpp.

46{}

◆ RobustMutex() [2/2]

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

Member Function Documentation

◆ endSession()

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

Definition at line 77 of file RobustMutex.cpp.

77{ mSessionActive = false; }

◆ getCounter()

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

Definition at line 68 of file RobustMutex.cpp.

68{ return mCount; }

◆ isActive()

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

Definition at line 70 of file RobustMutex.cpp.

70{ return mSessionActive; }

◆ lock()

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

Definition at line 48 of file RobustMutex.cpp.

48 {
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}

◆ startSession()

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

Definition at line 72 of file RobustMutex.cpp.

72 {
73 mCount++;
74 mSessionActive = true;
75}

◆ unlock()

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

Definition at line 61 of file RobustMutex.cpp.

61 {
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}

Member Data Documentation

◆ mCount

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

Definition at line 41 of file RobustMutex.hpp.

◆ mMutex

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

Definition at line 40 of file RobustMutex.hpp.

◆ mSessionActive

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

Definition at line 42 of file RobustMutex.hpp.


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