blob: ab0b67aa0511188b5e41e321c1d2478a03d46344 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Mutex.cpp -----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Host/Mutex.h"
11#include "lldb/Core/Log.h"
12
13#if 0
14#include "lldb/Host/Host.h"
15#define DEBUG_LOG(fmt, ...) printf(fmt, ## __VA_ARGS__)
16#else
17#define DEBUG_LOG(fmt, ...)
18#endif
19
20using namespace lldb_private;
21
22//----------------------------------------------------------------------
23// Default constructor.
24//
25// This will create a scoped mutex locking object that doesn't have
26// a mutex to lock. One will need to be provided using the Reset()
27// method.
28//----------------------------------------------------------------------
29Mutex::Locker::Locker () :
30 m_mutex_ptr(NULL)
31{
32}
33
34//----------------------------------------------------------------------
35// Constructor with a Mutex object.
36//
37// This will create a scoped mutex locking object that extracts the
38// mutex owned by "m" and locks it.
39//----------------------------------------------------------------------
40Mutex::Locker::Locker (Mutex& m) :
41 m_mutex_ptr(m.GetMutex())
42{
43 if (m_mutex_ptr)
44 Mutex::Lock (m_mutex_ptr);
45}
46
47//----------------------------------------------------------------------
48// Constructor with a Mutex object pointer.
49//
50// This will create a scoped mutex locking object that extracts the
51// mutex owned by "m" and locks it.
52//----------------------------------------------------------------------
53Mutex::Locker::Locker (Mutex* m) :
54 m_mutex_ptr(m ? m->GetMutex() : NULL)
55{
56 if (m_mutex_ptr)
57 Mutex::Lock (m_mutex_ptr);
58}
59
60//----------------------------------------------------------------------
61// Constructor with a raw pthread mutex object pointer.
62//
63// This will create a scoped mutex locking object that locks "mutex"
64//----------------------------------------------------------------------
65Mutex::Locker::Locker (pthread_mutex_t *mutex_ptr) :
66 m_mutex_ptr(mutex_ptr)
67{
68 if (m_mutex_ptr)
69 Mutex::Lock (m_mutex_ptr);
70}
71
72//----------------------------------------------------------------------
73// Desstructor
74//
75// Unlocks any owned mutex object (if it is valid).
76//----------------------------------------------------------------------
77Mutex::Locker::~Locker ()
78{
79 if (m_mutex_ptr)
80 Mutex::Unlock (m_mutex_ptr);
81}
82
83//----------------------------------------------------------------------
84// Unlock the current mutex in this object (if this owns a valid
85// mutex) and lock the new "mutex" object if it is non-NULL.
86//----------------------------------------------------------------------
87void
88Mutex::Locker::Reset (pthread_mutex_t *mutex_ptr)
89{
90 if (m_mutex_ptr)
91 Mutex::Unlock (m_mutex_ptr);
92
93 m_mutex_ptr = mutex_ptr;
94 if (m_mutex_ptr)
95 Mutex::Lock (m_mutex_ptr);
96}
97
98bool
99Mutex::Locker::TryLock (pthread_mutex_t *mutex_ptr)
100{
101 if (m_mutex_ptr)
102 Mutex::Unlock (m_mutex_ptr);
103 m_mutex_ptr = NULL;
104 if (mutex_ptr)
105 {
106 if (Mutex::TryLock (mutex_ptr) == 0)
107 m_mutex_ptr = mutex_ptr;
108 }
109 return m_mutex_ptr != NULL;
110}
111
112//----------------------------------------------------------------------
113// Default constructor.
114//
115// Creates a pthread mutex with no attributes.
116//----------------------------------------------------------------------
117Mutex::Mutex () :
118 m_mutex()
119{
120 int err;
121 err = ::pthread_mutex_init (&m_mutex, NULL);
122 assert(err == 0);
123}
124
125//----------------------------------------------------------------------
126// Default constructor.
127//
128// Creates a pthread mutex with "type" as the mutex type.
129//----------------------------------------------------------------------
130Mutex::Mutex (Mutex::Type type) :
131 m_mutex()
132{
133 int err;
134 ::pthread_mutexattr_t attr;
135 err = ::pthread_mutexattr_init (&attr);
136 assert(err == 0);
137 switch (type)
138 {
139 case eMutexTypeNormal:
140 err = ::pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_NORMAL);
141 break;
142
143 case eMutexTypeRecursive:
144 err = ::pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
145 break;
146
147 default:
148 err = -1;
149 break;
150 }
151 assert(err == 0);
152 err = ::pthread_mutex_init (&m_mutex, &attr);
153 assert(err == 0);
154 err = ::pthread_mutexattr_destroy (&attr);
155 assert(err == 0);
156}
157
158//----------------------------------------------------------------------
159// Destructor.
160//
161// Destroys the mutex owned by this object.
162//----------------------------------------------------------------------
163Mutex::~Mutex()
164{
165 int err;
166 err = ::pthread_mutex_destroy (&m_mutex);
167}
168
169//----------------------------------------------------------------------
170// Mutex get accessor.
171//----------------------------------------------------------------------
172pthread_mutex_t *
173Mutex::GetMutex()
174{
175 return &m_mutex;
176}
177
178int
179Mutex::Lock (pthread_mutex_t *mutex_ptr)
180{
181 DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_lock (%p)...\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr);
182 int err = ::pthread_mutex_lock (mutex_ptr);
183 DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_lock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, err);
184 return err;
185}
186
187int
188Mutex::TryLock (pthread_mutex_t *mutex_ptr)
189{
190 int err = ::pthread_mutex_trylock (mutex_ptr);
191 DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_trylock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, err);
192 return err;
193}
194
195int
196Mutex::Unlock (pthread_mutex_t *mutex_ptr)
197{
198 int err = ::pthread_mutex_unlock (mutex_ptr);
199 DEBUG_LOG ("[%4.4x/%4.4x] pthread_mutex_unlock (%p) => %i\n", Host::GetCurrentProcessID(), Host::GetCurrentThreadID(), mutex_ptr, err);
200 return err;
201}
202
203//----------------------------------------------------------------------
204// Locks the mutex owned by this object, if the mutex is already
205// locked, the calling thread will block until the mutex becomes
206// available.
207//
208// RETURNS
209// The error code from the pthread_mutex_lock() function call.
210//----------------------------------------------------------------------
211int
212Mutex::Lock()
213{
214 return Mutex::Lock (&m_mutex);
215}
216
217//----------------------------------------------------------------------
218// Attempts to lock the mutex owned by this object without blocking.
219// If the mutex is already locked, TryLock() will not block waiting
220// for the mutex, but will return an error condition.
221//
222// RETURNS
223// The error code from the pthread_mutex_trylock() function call.
224//----------------------------------------------------------------------
225int
226Mutex::TryLock()
227{
228 return Mutex::TryLock (&m_mutex);
229}
230
231//----------------------------------------------------------------------
232// If the current thread holds the lock on the owned mutex, then
233// Unlock() will unlock the mutex. Calling Unlock() on this object
234// that the calling thread does not hold will result in undefined
235// behavior.
236//
237// RETURNS
238// The error code from the pthread_mutex_unlock() function call.
239//----------------------------------------------------------------------
240int
241Mutex::Unlock()
242{
243 return Mutex::Unlock (&m_mutex);
244}