1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
//===-- Mutex.h -------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_Mutex_h_
#define liblldb_Mutex_h_
// C Includes
// C++ Includes
#ifdef LLDB_CONFIGURATION_DEBUG
#include <string>
#endif
// Other libraries and framework includes
// Project includes
#include "lldb/lldb-types.h"
namespace lldb_private {
//----------------------------------------------------------------------
/// @class Mutex Mutex.h "lldb/Host/Mutex.h"
/// @brief A C++ wrapper class for pthread mutexes.
//----------------------------------------------------------------------
class Mutex
{
public:
friend class Locker;
friend class Condition;
enum Type
{
eMutexTypeNormal, ///< Mutex that can't recursively entered by the same thread
eMutexTypeRecursive ///< Mutex can be recursively entered by the same thread
};
//------------------------------------------------------------------
/// @class Mutex::Locker
///
/// A scoped locking class that allows a variety of pthread mutex
/// objects to have a mutex locked when an Mutex::Locker
/// object is created, and unlocked when it goes out of scope or
/// when the Mutex::Locker::Reset(pthread_mutex_t *)
/// is called. This provides an exception safe way to lock a mutex
/// in a scope.
//------------------------------------------------------------------
class Locker
{
public:
//--------------------------------------------------------------
/// Default constructor.
///
/// This will create a scoped mutex locking object that doesn't
/// have a mutex to lock. One will need to be provided using the
/// Mutex::Locker::Reset(pthread_mutex_t *) method.
///
/// @see Mutex::Locker::Reset(pthread_mutex_t *)
//--------------------------------------------------------------
Locker();
//--------------------------------------------------------------
/// Constructor with a Mutex object.
///
/// This will create a scoped mutex locking object that extracts
/// the mutex owned by \a m and locks it.
///
/// @param[in] m
/// An instance of a Mutex object that contains a
/// valid mutex object.
//--------------------------------------------------------------
Locker(Mutex& m);
//--------------------------------------------------------------
/// Constructor with a Mutex object pointer.
///
/// This will create a scoped mutex locking object that extracts
/// the mutex owned by a m and locks it.
///
/// @param[in] m
/// A pointer to instance of a Mutex object that
/// contains a valid mutex object.
//--------------------------------------------------------------
Locker(Mutex* m);
//--------------------------------------------------------------
/// Destructor
///
/// Unlocks any valid pthread_mutex_t that this object may
/// contain.
//--------------------------------------------------------------
~Locker();
//--------------------------------------------------------------
/// Change the contained mutex.
///
/// Unlock the current mutex in this object (if it contains a
/// valid mutex) and lock the new \a mutex object if it is
/// non-nullptr.
//--------------------------------------------------------------
void
Lock (Mutex &mutex);
void
Lock (Mutex *mutex)
{
if (mutex)
Lock(*mutex);
}
//--------------------------------------------------------------
/// Change the contained mutex only if the mutex can be locked.
///
/// Unlock the current mutex in this object (if it contains a
/// valid mutex) and try to lock \a mutex. If \a mutex can be
/// locked this object will take ownership of the lock and will
/// unlock it when it goes out of scope or Reset or TryLock are
/// called again. If the mutex is already locked, this object
/// will not take ownership of the mutex.
///
/// @return
/// Returns \b true if the lock was acquired and the this
/// object will unlock the mutex when it goes out of scope,
/// returns \b false otherwise.
//--------------------------------------------------------------
bool
TryLock(Mutex &mutex, const char *failure_message = nullptr);
bool
TryLock(Mutex *mutex, const char *failure_message = nullptr)
{
if (mutex)
return TryLock(*mutex, failure_message);
else
return false;
}
void
Unlock ();
protected:
//--------------------------------------------------------------
/// Member variables
//--------------------------------------------------------------
Mutex *m_mutex_ptr;
private:
Locker(const Locker&);
const Locker& operator=(const Locker&);
};
//------------------------------------------------------------------
/// Default constructor.
///
/// Creates a pthread mutex with no attributes.
//------------------------------------------------------------------
Mutex();
//------------------------------------------------------------------
/// Default constructor.
///
/// Creates a pthread mutex with \a type as the mutex type.
/// Valid values for \a type include:
/// @li Mutex::Type::eMutexTypeNormal
/// @li Mutex::Type::eMutexTypeRecursive
///
/// @param[in] type
/// The type of the mutex.
///
/// @see ::pthread_mutexattr_settype()
//------------------------------------------------------------------
Mutex(Mutex::Type type);
//------------------------------------------------------------------
/// Destructor.
///
/// Destroys the mutex owned by this object.
//------------------------------------------------------------------
#ifdef LLDB_CONFIGURATION_DEBUG
virtual
#endif
~Mutex();
//------------------------------------------------------------------
/// Lock the mutex.
///
/// Locks the mutex owned by this object. If the mutex is already
/// locked, the calling thread will block until the mutex becomes
/// available.
///
/// @return
/// The error code from \c pthread_mutex_lock().
//------------------------------------------------------------------
#ifdef LLDB_CONFIGURATION_DEBUG
virtual
#endif
int
Lock();
//------------------------------------------------------------------
/// Try to lock the mutex.
///
/// Attempts to lock the mutex owned by this object without blocking.
/// If the mutex is already locked, TryLock() will not block waiting
/// for the mutex, but will return an error condition.
///
/// @return
/// The error code from \c pthread_mutex_trylock().
//------------------------------------------------------------------
#ifdef LLDB_CONFIGURATION_DEBUG
virtual
#endif
int
TryLock(const char *failure_message = nullptr);
//------------------------------------------------------------------
/// Unlock the mutex.
///
/// If the current thread holds the lock on the owned mutex, then
/// Unlock() will unlock the mutex. Calling Unlock() on this object
/// when the calling thread does not hold the lock will result in
/// undefined behavior.
///
/// @return
/// The error code from \c pthread_mutex_unlock().
//------------------------------------------------------------------
#ifdef LLDB_CONFIGURATION_DEBUG
virtual
#endif
int
Unlock();
protected:
//------------------------------------------------------------------
// Member variables
//------------------------------------------------------------------
// TODO: Hide the mutex in the implementation file in case we ever need to port to an
// architecture that doesn't have pthread mutexes.
lldb::mutex_t m_mutex; ///< The OS mutex object.
private:
//------------------------------------------------------------------
/// Mutex get accessor.
///
/// @return
/// A pointer to the pthread mutex object owned by this object.
//------------------------------------------------------------------
lldb::mutex_t *
GetMutex();
Mutex(const Mutex&);
const Mutex& operator=(const Mutex&);
};
#ifdef LLDB_CONFIGURATION_DEBUG
class TrackingMutex : public Mutex
{
public:
TrackingMutex() : Mutex() {}
TrackingMutex(Mutex::Type type) : Mutex (type) {}
virtual
~TrackingMutex() = default;
virtual int
Unlock ();
virtual int
TryLock(const char *failure_message = nullptr)
{
int return_value = Mutex::TryLock();
if (return_value != 0 && failure_message != nullptr)
{
m_failure_message.assign(failure_message);
m_thread_that_tried = pthread_self();
}
return return_value;
}
protected:
pthread_t m_thread_that_tried;
std::string m_failure_message;
};
class LoggingMutex : public Mutex
{
public:
LoggingMutex() : Mutex(),m_locked(false) {}
LoggingMutex(Mutex::Type type) : Mutex (type),m_locked(false) {}
virtual
~LoggingMutex() = default;
virtual int
Lock ();
virtual int
Unlock ();
virtual int
TryLock(const char *failure_message = nullptr);
protected:
bool m_locked;
};
#endif // LLDB_CONFIGURATION_DEBUG
} // namespace lldb_private
#endif // liblldb_Mutex_h_
|