Jpp
pmt_effective_area_update_2
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
Lockable*
m_lock
;
50
51
/**
52
* Neither copy-constructible nor copy-assignable
53
*/
54
JBasicScopedLock
(
const
JBasicScopedLock
&);
55
JBasicScopedLock
operator =
(
const
JBasicScopedLock
&);
56
57
public
:
58
59
/**
60
* Constructor
61
* \param lock a lockable object (e.g. JScopedLock)
62
*/
63
explicit
JBasicScopedLock
(Lockable& lock)
64
:
65
m_lock
(&lock)
66
{
67
m_lock
->lock();
68
}
69
70
/**
71
* Destructor
72
*/
73
~JBasicScopedLock
()
74
{
75
unlock
();
76
}
77
78
/**
79
* Unlock the mutex.
80
* \return 0 on success, an error code otherwise.
81
* See man 3p pthread_mutex_lock for further info.
82
*/
83
int
unlock
()
84
{
85
return
m_lock
->unlock();
86
}
87
};
88
89
/**
90
* Mutex.
91
*/
92
class
JMutex
93
{
94
pthread_mutex_t
m_mutex
;
95
96
public
:
97
98
/**
99
* Constructor.
100
*/
101
JMutex
()
102
{
103
pthread_mutex_init(&
m_mutex
, 0);
104
}
105
106
/**
107
* Destructor.
108
*/
109
~JMutex
()
110
{
111
pthread_mutex_destroy(&
m_mutex
);
112
}
113
114
/**
115
* Lock the mutex.
116
* \return 0 on success, an error code otherwise.
117
* See man 3p pthread_mutex_lock for further info.
118
*/
119
int
lock
()
120
{
121
return
pthread_mutex_lock(&
m_mutex
);
122
}
123
124
/**
125
* Try lock the mutex.
126
* \return 0 on success, an error code otherwise.
127
* See man 3p pthread_mutex_lock for further info.
128
*/
129
int
try_lock
()
130
{
131
return
pthread_mutex_trylock(&
m_mutex
);
132
}
133
134
/**
135
* Unlock the mutex.
136
* \return 0 on success, an error code otherwise.
137
* See man 3p pthread_mutex_lock for further info.
138
*/
139
int
unlock
()
140
{
141
return
pthread_mutex_unlock(&
m_mutex
);
142
}
143
144
typedef
JBasicScopedLock<JMutex>
JScopedLock
;
145
};
146
147
}
// ns JSYNCHRONIZATION
148
149
#endif // __JLANG__JMUTEX__
JSYNCHRONIZATION::JMutex::~JMutex
~JMutex()
Destructor.
Definition:
JMutex.hh:109
JSYNCHRONIZATION::JMutex::JScopedLock
JBasicScopedLock< JMutex > JScopedLock
Definition:
JMutex.hh:144
JSYNCHRONIZATION::JBasicScopedLock::operator=
JBasicScopedLock operator=(const JBasicScopedLock &)
JSYNCHRONIZATION::JMutex::lock
int lock()
Lock the mutex.
Definition:
JMutex.hh:119
JSYNCHRONIZATION::JBasicScopedLock::unlock
int unlock()
Unlock the mutex.
Definition:
JMutex.hh:83
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:94
JSYNCHRONIZATION::JMutex::unlock
int unlock()
Unlock the mutex.
Definition:
JMutex.hh:139
JSYNCHRONIZATION::JBasicScopedLock::JBasicScopedLock
JBasicScopedLock(Lockable &lock)
Constructor.
Definition:
JMutex.hh:63
JSYNCHRONIZATION::JMutex::JMutex
JMutex()
Constructor.
Definition:
JMutex.hh:101
JSYNCHRONIZATION::JBasicScopedLock::~JBasicScopedLock
~JBasicScopedLock()
Destructor.
Definition:
JMutex.hh:73
JSYNCHRONIZATION::JMutex
Mutex.
Definition:
JMutex.hh:92
JSYNCHRONIZATION::JBasicScopedLock::m_lock
Lockable * m_lock
Definition:
JMutex.hh:49
JSYNCHRONIZATION::JMutex::try_lock
int try_lock()
Try lock the mutex.
Definition:
JMutex.hh:129
Generated by
1.8.5