1 //===-- Mutex.h -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Mutex_h_
11 #define liblldb_Mutex_h_
12 #if defined(__cplusplus)
14 #include "lldb/lldb-types.h"
17 #ifdef LLDB_CONFIGURATION_DEBUG
21 namespace lldb_private {
23 //----------------------------------------------------------------------
24 /// @class Mutex Mutex.h "lldb/Host/Mutex.h"
25 /// @brief A C++ wrapper class for pthread mutexes.
26 //----------------------------------------------------------------------
31 friend class Condition;
35 eMutexTypeNormal, ///< Mutex that can't recursively entered by the same thread
36 eMutexTypeRecursive ///< Mutex can be recursively entered by the same thread
39 //------------------------------------------------------------------
40 /// @class Mutex::Locker
42 /// A scoped locking class that allows a variety of pthread mutex
43 /// objects to have a mutex locked when an Mutex::Locker
44 /// object is created, and unlocked when it goes out of scope or
45 /// when the Mutex::Locker::Reset(pthread_mutex_t *)
46 /// is called. This provides an exception safe way to lock a mutex
48 //------------------------------------------------------------------
52 //--------------------------------------------------------------
53 /// Default constructor.
55 /// This will create a scoped mutex locking object that doesn't
56 /// have a mutex to lock. One will need to be provided using the
57 /// Mutex::Locker::Reset(pthread_mutex_t *) method.
59 /// @see Mutex::Locker::Reset(pthread_mutex_t *)
60 //--------------------------------------------------------------
63 //--------------------------------------------------------------
64 /// Constructor with a Mutex object.
66 /// This will create a scoped mutex locking object that extracts
67 /// the mutex owned by \a m and locks it.
70 /// An instance of a Mutex object that contains a
71 /// valid mutex object.
72 //--------------------------------------------------------------
75 //--------------------------------------------------------------
76 /// Constructor with a Mutex object pointer.
78 /// This will create a scoped mutex locking object that extracts
79 /// the mutex owned by a m and locks it.
82 /// A pointer to instance of a Mutex object that
83 /// contains a valid mutex object.
84 //--------------------------------------------------------------
87 //--------------------------------------------------------------
90 /// Unlocks any valid pthread_mutex_t that this object may
92 //--------------------------------------------------------------
95 //--------------------------------------------------------------
96 /// Change the contained mutex.
98 /// Unlock the current mutex in this object (if it contains a
99 /// valid mutex) and lock the new \a mutex object if it is
101 //--------------------------------------------------------------
112 //--------------------------------------------------------------
113 /// Change the contained mutex only if the mutex can be locked.
115 /// Unlock the current mutex in this object (if it contains a
116 /// valid mutex) and try to lock \a mutex. If \a mutex can be
117 /// locked this object will take ownership of the lock and will
118 /// unlock it when it goes out of scope or Reset or TryLock are
119 /// called again. If the mutex is already locked, this object
120 /// will not take ownership of the mutex.
123 /// Returns \b true if the lock was aquired and the this
124 /// object will unlock the mutex when it goes out of scope,
125 /// returns \b false otherwise.
126 //--------------------------------------------------------------
128 TryLock (Mutex &mutex, const char *failure_message = NULL);
131 TryLock (Mutex *mutex, const char *failure_message = NULL)
134 return TryLock(*mutex, failure_message);
143 //--------------------------------------------------------------
145 //--------------------------------------------------------------
149 Locker(const Locker&);
150 const Locker& operator=(const Locker&);
154 //------------------------------------------------------------------
155 /// Default constructor.
157 /// Creates a pthread mutex with no attributes.
158 //------------------------------------------------------------------
161 //------------------------------------------------------------------
162 /// Default constructor.
164 /// Creates a pthread mutex with \a type as the mutex type.
165 /// Valid values for \a type include:
166 /// @li Mutex::Type::eMutexTypeNormal
167 /// @li Mutex::Type::eMutexTypeRecursive
170 /// The type of the mutex.
172 /// @see ::pthread_mutexattr_settype()
173 //------------------------------------------------------------------
174 Mutex(Mutex::Type type);
176 //------------------------------------------------------------------
179 /// Destroys the mutex owned by this object.
180 //------------------------------------------------------------------
181 #ifdef LLDB_CONFIGURATION_DEBUG
186 //------------------------------------------------------------------
189 /// Locks the mutex owned by this object. If the mutex is already
190 /// locked, the calling thread will block until the mutex becomes
194 /// The error code from \c pthread_mutex_lock().
195 //------------------------------------------------------------------
196 #ifdef LLDB_CONFIGURATION_DEBUG
202 //------------------------------------------------------------------
203 /// Try to lock the mutex.
205 /// Attempts to lock the mutex owned by this object without blocking.
206 /// If the mutex is already locked, TryLock() will not block waiting
207 /// for the mutex, but will return an error condition.
210 /// The error code from \c pthread_mutex_trylock().
211 //------------------------------------------------------------------
212 #ifdef LLDB_CONFIGURATION_DEBUG
216 TryLock(const char *failure_message = NULL);
218 //------------------------------------------------------------------
219 /// Unlock the mutex.
221 /// If the current thread holds the lock on the owned mutex, then
222 /// Unlock() will unlock the mutex. Calling Unlock() on this object
223 /// when the calling thread does not hold the lock will result in
224 /// undefined behavior.
227 /// The error code from \c pthread_mutex_unlock().
228 //------------------------------------------------------------------
229 #ifdef LLDB_CONFIGURATION_DEBUG
236 //------------------------------------------------------------------
238 //------------------------------------------------------------------
239 // TODO: Hide the mutex in the implementation file in case we ever need to port to an
240 // architecture that doesn't have pthread mutexes.
241 lldb::mutex_t m_mutex; ///< The OS mutex object.
244 //------------------------------------------------------------------
245 /// Mutex get accessor.
248 /// A pointer to the pthread mutex object owned by this object.
249 //------------------------------------------------------------------
254 const Mutex& operator=(const Mutex&);
257 #ifdef LLDB_CONFIGURATION_DEBUG
258 class TrackingMutex : public Mutex
261 TrackingMutex() : Mutex() {}
262 TrackingMutex(Mutex::Type type) : Mutex (type) {}
271 TryLock (const char *failure_message = NULL)
273 int return_value = Mutex::TryLock();
274 if (return_value != 0 && failure_message != NULL)
276 m_failure_message.assign(failure_message);
277 m_thread_that_tried = pthread_self();
283 pthread_t m_thread_that_tried;
284 std::string m_failure_message;
287 class LoggingMutex : public Mutex
290 LoggingMutex() : Mutex(),m_locked(false) {}
291 LoggingMutex(Mutex::Type type) : Mutex (type),m_locked(false) {}
303 TryLock (const char *failure_message = NULL);
309 } // namespace lldb_private
311 #endif // #if defined(__cplusplus)