Jpp
test_elongated_shower_pde
the software that should make you happy
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
software
JLang
JMutex.hh
Go to the documentation of this file.
1
#ifndef __JLANG__JMUTEX__
2
#define __JLANG__JMUTEX__
3
4
#include <pthread.h>
5
6
/**
7
* \file
8
* Very basic lock facilities to be used in multithreaded programs.
9
* \author cpellegrino
10
*/
11
12
namespace
JSYNCHRONIZATION {}
13
namespace
JPP {
using namespace
JSYNCHRONIZATION; }
14
15
namespace
JSYNCHRONIZATION {
16
17
/**
18
* Scoped lock.
19
*
20
* The main purpose of this class is to provide an exception-safe locking
21
* facility.
22
* The lock on the mutex is acquired during construction, while the
23
* unlock is performed in the destructor.
24
*
25
* Es:
26
* Case 1, without scoped lock:
27
* using namespace JSYNCHRONIZATION;
28
* JMutex mutex;
29
*
30
* {
31
* mutex.lock();
32
* throwing_function(variable_to_protect); // <- this shall throw
33
* mutex.lock(); // <- this call is never
34
* // executed -> deadlock
35
* }
36
*
37
* Case 2, with scoped lock:
38
* using namespace JSYNCHRONIZATION;
39
* JMutex mutex;
40
*
41
* {
42
* JScopedLock(mutex);
43
* throwing_function(variable_to_protect); // <- this shall throw
44
* } // the lock is released here automatically
45
*/
46
template
<
typename
Lockable>
47
class
JBasicScopedLock
48
{
49
private
:
50
Lockable*
m_lock
;
51
52
/**
53
* Neither copy-constructible nor copy-assignable
54
*/
55
JBasicScopedLock
(
const
JBasicScopedLock
&);
56
JBasicScopedLock
(
JBasicScopedLock
&&);
57
JBasicScopedLock
operator =
(
const
JBasicScopedLock
&);
58
JBasicScopedLock
operator =
(
JBasicScopedLock
&&);
59
60
public
:
61
62
/**
63
* Constructor
64
* \param lock a lockable object (e.g.\ JScopedLock)
65
*/
66
explicit
JBasicScopedLock
(Lockable& lock)
67
:
68
m_lock
(&lock)
69
{
70
m_lock
->lock();
71
}
72
73
/**
74
* Destructor
75
*/
76
~JBasicScopedLock
()
77
{
78
unlock
();
79
}
80
81
/**
82
* Unlock the mutex.
83
* \return 0 on success, an error code otherwise.
84
* See man 3p pthread_mutex_lock for further info.
85
*/
86
int
unlock
()
87
{
88
return
m_lock
->unlock();
89
}
90
};
91
92
/**
93
* Mutex.
94
*/
95
class
JMutex
96
{
97
pthread_mutex_t
m_mutex
;
98
99
public
:
100
101
/**
102
* Constructor.
103
*/
104
JMutex
()
105
{
106
pthread_mutex_init(&
m_mutex
, 0);
107
}
108
109
/**
110
* Destructor.
111
*/
112
~JMutex
()
113
{
114
pthread_mutex_destroy(&
m_mutex
);
115
}
116
117
/**
118
* Lock the mutex.
119
* \return 0 on success, an error code otherwise.
120
* See man 3p pthread_mutex_lock for further info.
121
*/
122
int
lock
()
123
{
124
return
pthread_mutex_lock(&
m_mutex
);
125
}
126
127
/**
128
* Try lock the mutex.
129
* \return 0 on success, an error code otherwise.
130
* See man 3p pthread_mutex_lock for further info.
131
*/
132
int
try_lock
()
133
{
134
return
pthread_mutex_trylock(&
m_mutex
);
135
}
136
137
/**
138
* Unlock the mutex.
139
* \return 0 on success, an error code otherwise.
140
* See man 3p pthread_mutex_lock for further info.
141
*/
142
int
unlock
()
143
{
144
return
pthread_mutex_unlock(&
m_mutex
);
145
}
146
147
typedef
JBasicScopedLock<JMutex>
JScopedLock
;
148
};
149
150
}
// ns JSYNCHRONIZATION
151
152
#endif // __JLANG__JMUTEX__
JSYNCHRONIZATION::JMutex::~JMutex
~JMutex()
Destructor.
Definition:
JMutex.hh:112
JSYNCHRONIZATION::JMutex::JScopedLock
JBasicScopedLock< JMutex > JScopedLock
Definition:
JMutex.hh:147
JSYNCHRONIZATION::JBasicScopedLock::operator=
JBasicScopedLock operator=(const JBasicScopedLock &)
JSYNCHRONIZATION::JMutex::lock
int lock()
Lock the mutex.
Definition:
JMutex.hh:122
JSYNCHRONIZATION::JBasicScopedLock::unlock
int unlock()
Unlock the mutex.
Definition:
JMutex.hh:86
JSYNCHRONIZATION::JBasicScopedLock::JBasicScopedLock
JBasicScopedLock(const JBasicScopedLock &)
Neither copy-constructible nor copy-assignable.
JSYNCHRONIZATION::JBasicScopedLock
Scoped lock.
Definition:
JMutex.hh:47
JSYNCHRONIZATION::JMutex::m_mutex
pthread_mutex_t m_mutex
Definition:
JMutex.hh:97
JSYNCHRONIZATION::JMutex::unlock
int unlock()
Unlock the mutex.
Definition:
JMutex.hh:142
JSYNCHRONIZATION::JBasicScopedLock::JBasicScopedLock
JBasicScopedLock(Lockable &lock)
Constructor.
Definition:
JMutex.hh:66
JSYNCHRONIZATION::JMutex::JMutex
JMutex()
Constructor.
Definition:
JMutex.hh:104
JSYNCHRONIZATION::JBasicScopedLock::~JBasicScopedLock
~JBasicScopedLock()
Destructor.
Definition:
JMutex.hh:76
JSYNCHRONIZATION::JMutex
Mutex.
Definition:
JMutex.hh:95
JSYNCHRONIZATION::JBasicScopedLock::m_lock
Lockable * m_lock
Definition:
JMutex.hh:50
JSYNCHRONIZATION::JMutex::try_lock
int try_lock()
Try lock the mutex.
Definition:
JMutex.hh:132
Generated by
1.8.5