blob: dd009de1462bc95b9e27bd689dcbe3027d2b919a [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
bart4bb53d82008-02-28 19:06:34 +000026#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_error.h"
28#include "drd_mutex.h"
sewardj721ad7b2007-11-30 08:30:29 +000029#include "priv_drd_clientreq.h"
bart9d5b7962008-05-14 12:25:00 +000030#include "pub_tool_vki.h"
sewardjaf44c822007-11-25 14:01:38 +000031#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
32#include "pub_tool_libcassert.h" // tl_assert()
bart5bd9f2d2008-03-03 20:31:58 +000033#include "pub_tool_libcbase.h" // VG_(strlen)
bart4bb53d82008-02-28 19:06:34 +000034#include "pub_tool_libcprint.h" // VG_(message)()
bart9d5b7962008-05-14 12:25:00 +000035#include "pub_tool_libcproc.h" // VG_(read_millisecond_timer)()
sewardjaf44c822007-11-25 14:01:38 +000036#include "pub_tool_machine.h" // VG_(get_IP)()
37#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
38
39
sewardj347eeba2008-01-21 14:19:07 +000040// Local functions.
41
bart46d5f172008-02-28 19:49:37 +000042static void mutex_cleanup(struct mutex_info* p);
bart5357fcb2008-02-27 15:46:00 +000043static Bool mutex_is_locked(struct mutex_info* const p);
sewardj347eeba2008-01-21 14:19:07 +000044
45
sewardjaf44c822007-11-25 14:01:38 +000046// Local variables.
47
48static Bool s_trace_mutex;
49static ULong s_mutex_lock_count;
bart6bbefaf2008-04-19 15:16:45 +000050static ULong s_mutex_segment_creation_count;
bart9d5b7962008-05-14 12:25:00 +000051static UInt s_mutex_lock_threshold_ms = 1000 * 1000;
sewardjaf44c822007-11-25 14:01:38 +000052
53
54// Function definitions.
55
56void mutex_set_trace(const Bool trace_mutex)
57{
58 tl_assert(!! trace_mutex == trace_mutex);
59 s_trace_mutex = trace_mutex;
60}
61
bart9d5b7962008-05-14 12:25:00 +000062void mutex_set_lock_threshold(const UInt lock_threshold_ms)
63{
64 s_mutex_lock_threshold_ms = lock_threshold_ms;
65}
66
sewardjaf44c822007-11-25 14:01:38 +000067static
68void mutex_initialize(struct mutex_info* const p,
bart0268dfa2008-03-11 20:10:21 +000069 const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000070{
71 tl_assert(mutex != 0);
sewardjaf44c822007-11-25 14:01:38 +000072
bart4bb53d82008-02-28 19:06:34 +000073 tl_assert(p->a1 == mutex);
barta2b6e1b2008-03-17 18:32:39 +000074 p->cleanup = (void(*)(DrdClientobj*))&mutex_cleanup;
75 p->mutex_type = mutex_type;
76 p->recursion_count = 0;
77 p->owner = DRD_INVALID_THREADID;
78 p->last_locked_segment = 0;
bart9d5b7962008-05-14 12:25:00 +000079 p->acquiry_time_ms = 0;
80 p->acquired_at = 0;
sewardjaf44c822007-11-25 14:01:38 +000081}
82
bart46d5f172008-02-28 19:49:37 +000083/** Deallocate the memory that was allocated by mutex_initialize(). */
84static void mutex_cleanup(struct mutex_info* p)
85{
bart6b717612008-03-24 09:29:38 +000086 tl_assert(p);
87
bartb78312c2008-02-29 11:00:17 +000088 if (s_trace_mutex)
89 {
bart3b1ee452008-02-29 19:28:15 +000090 VG_(message)(Vg_UserMsg,
91 "[%d/%d] mutex_destroy %s 0x%lx",
92 VG_(get_running_tid)(),
93 thread_get_running_tid(),
bartb78312c2008-02-29 11:00:17 +000094 mutex_get_typename(p),
95 p->a1);
96 }
97
bart46d5f172008-02-28 19:49:37 +000098 if (mutex_is_locked(p))
99 {
100 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
101 VG_(maybe_record_error)(VG_(get_running_tid)(),
102 MutexErr,
103 VG_(get_IP)(VG_(get_running_tid)()),
104 "Destroying locked mutex",
105 &MEI);
106 }
107
barta2b6e1b2008-03-17 18:32:39 +0000108 sg_put(p->last_locked_segment);
109 p->last_locked_segment = 0;
bart46d5f172008-02-28 19:49:37 +0000110}
111
bart6b717612008-03-24 09:29:38 +0000112static void not_a_mutex(const Addr mutex)
113{
114 MutexErrInfo MEI = { mutex, -1, DRD_INVALID_THREADID };
115 VG_(maybe_record_error)(VG_(get_running_tid)(),
116 MutexErr,
117 VG_(get_IP)(VG_(get_running_tid)()),
118 "Not a mutex",
119 &MEI);
120}
121
sewardjaf44c822007-11-25 14:01:38 +0000122static
sewardj721ad7b2007-11-30 08:30:29 +0000123struct mutex_info*
bart0268dfa2008-03-11 20:10:21 +0000124mutex_get_or_allocate(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000125{
bart4bb53d82008-02-28 19:06:34 +0000126 struct mutex_info* p;
sewardj721ad7b2007-11-30 08:30:29 +0000127
bart4bb53d82008-02-28 19:06:34 +0000128 tl_assert(offsetof(DrdClientobj, mutex) == 0);
bart72b751c2008-03-01 13:44:24 +0000129 p = &clientobj_get(mutex, ClientMutex)->mutex;
bart4bb53d82008-02-28 19:06:34 +0000130 if (p)
131 {
bart4bb53d82008-02-28 19:06:34 +0000132 return p;
133 }
sewardj721ad7b2007-11-30 08:30:29 +0000134
bart0268dfa2008-03-11 20:10:21 +0000135 if (clientobj_present(mutex, mutex + 1))
sewardj721ad7b2007-11-30 08:30:29 +0000136 {
bart6b717612008-03-24 09:29:38 +0000137 not_a_mutex(mutex);
bart5bd9f2d2008-03-03 20:31:58 +0000138 return 0;
sewardj721ad7b2007-11-30 08:30:29 +0000139 }
bart4bb53d82008-02-28 19:06:34 +0000140
bart0268dfa2008-03-11 20:10:21 +0000141 p = &clientobj_add(mutex, ClientMutex)->mutex;
142 mutex_initialize(p, mutex, mutex_type);
bart4bb53d82008-02-28 19:06:34 +0000143 return p;
sewardjaf44c822007-11-25 14:01:38 +0000144}
145
bart3b1ee452008-02-29 19:28:15 +0000146struct mutex_info* mutex_get(const Addr mutex)
147{
148 tl_assert(offsetof(DrdClientobj, mutex) == 0);
bart72b751c2008-03-01 13:44:24 +0000149 return &clientobj_get(mutex, ClientMutex)->mutex;
bart3b1ee452008-02-29 19:28:15 +0000150}
151
bart00344642008-03-01 15:27:41 +0000152/** Called before pthread_mutex_init(). */
sewardj721ad7b2007-11-30 08:30:29 +0000153struct mutex_info*
bart0268dfa2008-03-11 20:10:21 +0000154mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000155{
bart00344642008-03-01 15:27:41 +0000156 struct mutex_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000157
sewardjaf44c822007-11-25 14:01:38 +0000158 if (s_trace_mutex)
159 {
bart3b1ee452008-02-29 19:28:15 +0000160 VG_(message)(Vg_UserMsg,
161 "[%d/%d] mutex_init %s 0x%lx",
162 VG_(get_running_tid)(),
163 thread_get_running_tid(),
sewardj347eeba2008-01-21 14:19:07 +0000164 mutex_type_name(mutex_type),
sewardjaf44c822007-11-25 14:01:38 +0000165 mutex);
166 }
167
bart00344642008-03-01 15:27:41 +0000168 if (mutex_type == mutex_type_invalid_mutex)
169 {
bart6b717612008-03-24 09:29:38 +0000170 not_a_mutex(mutex);
bart00344642008-03-01 15:27:41 +0000171 return 0;
172 }
173
174 p = mutex_get(mutex);
175 if (p)
sewardj347eeba2008-01-21 14:19:07 +0000176 {
177 const ThreadId vg_tid = VG_(get_running_tid)();
178 MutexErrInfo MEI
bart00344642008-03-01 15:27:41 +0000179 = { p->a1, p->recursion_count, p->owner };
sewardj347eeba2008-01-21 14:19:07 +0000180 VG_(maybe_record_error)(vg_tid,
181 MutexErr,
182 VG_(get_IP)(vg_tid),
183 "Mutex reinitialization",
184 &MEI);
bart00344642008-03-01 15:27:41 +0000185 return p;
sewardj347eeba2008-01-21 14:19:07 +0000186 }
bart0268dfa2008-03-11 20:10:21 +0000187 p = mutex_get_or_allocate(mutex, mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000188
bart00344642008-03-01 15:27:41 +0000189 return p;
sewardjaf44c822007-11-25 14:01:38 +0000190}
191
bart46d5f172008-02-28 19:49:37 +0000192/** Called after pthread_mutex_destroy(). */
sewardj347eeba2008-01-21 14:19:07 +0000193void mutex_post_destroy(const Addr mutex)
194{
bart72b751c2008-03-01 13:44:24 +0000195 struct mutex_info* p;
sewardj347eeba2008-01-21 14:19:07 +0000196
bart72b751c2008-03-01 13:44:24 +0000197 p = mutex_get(mutex);
198 if (p == 0)
199 {
bart6b717612008-03-24 09:29:38 +0000200 not_a_mutex(mutex);
bart72b751c2008-03-01 13:44:24 +0000201 return;
202 }
203
204 clientobj_remove(mutex, ClientMutex);
sewardj347eeba2008-01-21 14:19:07 +0000205}
206
bart8bba1f72008-02-27 16:13:05 +0000207/** Called before pthread_mutex_lock() is invoked. If a data structure for
208 * the client-side object was not yet created, do this now. Also check whether
209 * an attempt is made to lock recursively a synchronization object that must
210 * not be locked recursively.
211 */
bart2e3a3c12008-03-24 08:33:47 +0000212void mutex_pre_lock(const Addr mutex, const MutexT mutex_type,
213 const Bool trylock)
bart8bba1f72008-02-27 16:13:05 +0000214{
bart635cb162008-02-28 08:30:43 +0000215 struct mutex_info* p;
216
bart0268dfa2008-03-11 20:10:21 +0000217 p = mutex_get_or_allocate(mutex, mutex_type);
bart00344642008-03-01 15:27:41 +0000218 if (s_trace_mutex)
219 {
220 VG_(message)(Vg_UserMsg,
221 "[%d/%d] pre_mutex_lock %s 0x%lx rc %d owner %d",
222 VG_(get_running_tid)(),
223 thread_get_running_tid(),
bart6b717612008-03-24 09:29:38 +0000224 p ? mutex_get_typename(p) : "(?)",
bart00344642008-03-01 15:27:41 +0000225 mutex,
bart2e3a3c12008-03-24 08:33:47 +0000226 p ? p->recursion_count : -1,
227 p ? p->owner : DRD_INVALID_THREADID);
bart00344642008-03-01 15:27:41 +0000228 }
229
bart2e3a3c12008-03-24 08:33:47 +0000230 if (p == 0)
231 {
bart6b717612008-03-24 09:29:38 +0000232 not_a_mutex(mutex);
bart2e3a3c12008-03-24 08:33:47 +0000233 return;
234 }
235
236 tl_assert(p);
237
bart00344642008-03-01 15:27:41 +0000238 if (mutex_type == mutex_type_invalid_mutex)
239 {
bart6b717612008-03-24 09:29:38 +0000240 not_a_mutex(mutex);
bart00344642008-03-01 15:27:41 +0000241 return;
242 }
243
bart2e3a3c12008-03-24 08:33:47 +0000244 if (! trylock
245 && p->owner == thread_get_running_tid()
bart8bba1f72008-02-27 16:13:05 +0000246 && p->recursion_count >= 1
247 && mutex_type != mutex_type_recursive_mutex)
248 {
bart4bb53d82008-02-28 19:06:34 +0000249 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
bart8bba1f72008-02-27 16:13:05 +0000250 VG_(maybe_record_error)(VG_(get_running_tid)(),
251 MutexErr,
252 VG_(get_IP)(VG_(get_running_tid)()),
253 "Recursive locking not allowed",
254 &MEI);
255 }
256}
257
sewardjaf44c822007-11-25 14:01:38 +0000258/**
259 * Update mutex_info state when locking the pthread_mutex_t mutex.
260 * Note: this function must be called after pthread_mutex_lock() has been
261 * called, or a race condition is triggered !
262 */
bart4a975e12008-03-30 13:28:33 +0000263void mutex_post_lock(const Addr mutex, const Bool took_lock,
264 const Bool post_cond_wait)
sewardjaf44c822007-11-25 14:01:38 +0000265{
bart3b1ee452008-02-29 19:28:15 +0000266 const DrdThreadId drd_tid = thread_get_running_tid();
bart00344642008-03-01 15:27:41 +0000267 struct mutex_info* p;
268
269 p = mutex_get(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000270
271 if (s_trace_mutex)
272 {
bart3b1ee452008-02-29 19:28:15 +0000273 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000274 "[%d/%d] %s %s 0x%lx rc %d owner %d%s",
bart3b1ee452008-02-29 19:28:15 +0000275 VG_(get_running_tid)(),
sewardjaf44c822007-11-25 14:01:38 +0000276 drd_tid,
bart4a975e12008-03-30 13:28:33 +0000277 post_cond_wait ? "cond_post_wait " : "post_mutex_lock",
bart00344642008-03-01 15:27:41 +0000278 p ? mutex_get_typename(p) : "(?)",
sewardjaf44c822007-11-25 14:01:38 +0000279 mutex,
280 p ? p->recursion_count : 0,
bartfa37c922008-03-30 08:41:59 +0000281 p ? p->owner : VG_INVALID_THREADID,
282 took_lock ? "" : " (locking failed)");
sewardjaf44c822007-11-25 14:01:38 +0000283 }
284
bart777f7fe2008-03-02 17:43:18 +0000285 if (! p || ! took_lock)
bart5bd9f2d2008-03-03 20:31:58 +0000286 return;
bart5357fcb2008-02-27 15:46:00 +0000287
sewardjaf44c822007-11-25 14:01:38 +0000288 if (p->recursion_count == 0)
289 {
bart5bd9f2d2008-03-03 20:31:58 +0000290 const DrdThreadId last_owner = p->owner;
291
292 if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID)
barta2b6e1b2008-03-17 18:32:39 +0000293 {
294 tl_assert(p->last_locked_segment);
295 thread_combine_vc2(drd_tid, &p->last_locked_segment->vc);
296 }
bart5bd9f2d2008-03-03 20:31:58 +0000297 thread_new_segment(drd_tid);
bart6bbefaf2008-04-19 15:16:45 +0000298 s_mutex_segment_creation_count++;
bart5bd9f2d2008-03-03 20:31:58 +0000299
bart9d5b7962008-05-14 12:25:00 +0000300 p->owner = drd_tid;
301 p->acquiry_time_ms = VG_(read_millisecond_timer)();
302 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
sewardjaf44c822007-11-25 14:01:38 +0000303 s_mutex_lock_count++;
304 }
305 else if (p->owner != drd_tid)
306 {
bart3b1ee452008-02-29 19:28:15 +0000307 VG_(message)(Vg_UserMsg,
sewardjaf44c822007-11-25 14:01:38 +0000308 "The impossible happened: mutex 0x%lx is locked"
309 " simultaneously by two threads (recursion count %d,"
310 " owners %d and %d) !",
bart4bb53d82008-02-28 19:06:34 +0000311 p->a1, p->recursion_count, p->owner, drd_tid);
sewardj347eeba2008-01-21 14:19:07 +0000312 p->owner = drd_tid;
sewardjaf44c822007-11-25 14:01:38 +0000313 }
314 p->recursion_count++;
sewardjaf44c822007-11-25 14:01:38 +0000315}
316
bart9d5b7962008-05-14 12:25:00 +0000317/** Update mutex_info state when unlocking the pthread_mutex_t mutex.
318 *
319 * @param mutex Pointer to pthread_mutex_t data structure in the client space.
320 * @param tid ThreadId of the thread calling pthread_mutex_unlock().
321 * @param vc Pointer to the current vector clock of thread tid.
322 *
323 * @return New value of the mutex recursion count.
324 *
325 * @note This function must be called before pthread_mutex_unlock() is called,
326 * or a race condition is triggered !
sewardjaf44c822007-11-25 14:01:38 +0000327 */
bart777f7fe2008-03-02 17:43:18 +0000328void mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000329{
bartb78312c2008-02-29 11:00:17 +0000330 const DrdThreadId drd_tid = thread_get_running_tid();
331 const ThreadId vg_tid = VG_(get_running_tid)();
sewardjaf44c822007-11-25 14:01:38 +0000332 struct mutex_info* const p = mutex_get(mutex);
333
bart777f7fe2008-03-02 17:43:18 +0000334 if (s_trace_mutex)
sewardjaf44c822007-11-25 14:01:38 +0000335 {
bart3b1ee452008-02-29 19:28:15 +0000336 VG_(message)(Vg_UserMsg,
337 "[%d/%d] mutex_unlock %s 0x%lx rc %d",
338 vg_tid,
339 drd_tid,
bart6b717612008-03-24 09:29:38 +0000340 p ? mutex_get_typename(p) : "(?)",
sewardjaf44c822007-11-25 14:01:38 +0000341 mutex,
barta2b6e1b2008-03-17 18:32:39 +0000342 p ? p->recursion_count : 0);
sewardjaf44c822007-11-25 14:01:38 +0000343 }
344
bart777f7fe2008-03-02 17:43:18 +0000345 if (p == 0 || mutex_type == mutex_type_invalid_mutex)
bartab7a6442008-02-25 19:46:14 +0000346 {
bart6b717612008-03-24 09:29:38 +0000347 not_a_mutex(mutex);
bart5bd9f2d2008-03-03 20:31:58 +0000348 return;
bartab7a6442008-02-25 19:46:14 +0000349 }
350
bart5357fcb2008-02-27 15:46:00 +0000351 if (p->owner == DRD_INVALID_THREADID)
352 {
bart4bb53d82008-02-28 19:06:34 +0000353 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
bart5357fcb2008-02-27 15:46:00 +0000354 VG_(maybe_record_error)(vg_tid,
355 MutexErr,
356 VG_(get_IP)(vg_tid),
357 "Mutex not locked",
358 &MEI);
bart5bd9f2d2008-03-03 20:31:58 +0000359 return;
bart5357fcb2008-02-27 15:46:00 +0000360 }
361
sewardjaf44c822007-11-25 14:01:38 +0000362 tl_assert(p);
bart5357fcb2008-02-27 15:46:00 +0000363 if (p->mutex_type != mutex_type)
364 {
barta2b6e1b2008-03-17 18:32:39 +0000365 VG_(message)(Vg_UserMsg, "??? mutex 0x%lx: type changed from %d into %d",
bart5bd9f2d2008-03-03 20:31:58 +0000366 p->a1, p->mutex_type, mutex_type);
bart5357fcb2008-02-27 15:46:00 +0000367 }
sewardj721ad7b2007-11-30 08:30:29 +0000368 tl_assert(p->mutex_type == mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000369 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000370
bart777f7fe2008-03-02 17:43:18 +0000371 if (p->owner != drd_tid || p->recursion_count <= 0)
sewardjaf44c822007-11-25 14:01:38 +0000372 {
bart4bb53d82008-02-28 19:06:34 +0000373 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
sewardjaf44c822007-11-25 14:01:38 +0000374 VG_(maybe_record_error)(vg_tid,
375 MutexErr,
376 VG_(get_IP)(vg_tid),
bart777f7fe2008-03-02 17:43:18 +0000377 "Mutex not locked by calling thread",
sewardjaf44c822007-11-25 14:01:38 +0000378 &MEI);
bart777f7fe2008-03-02 17:43:18 +0000379 return;
sewardjaf44c822007-11-25 14:01:38 +0000380 }
bart777f7fe2008-03-02 17:43:18 +0000381 tl_assert(p->recursion_count > 0);
sewardjaf44c822007-11-25 14:01:38 +0000382 p->recursion_count--;
bart777f7fe2008-03-02 17:43:18 +0000383 tl_assert(p->recursion_count >= 0);
sewardj347eeba2008-01-21 14:19:07 +0000384
sewardjaf44c822007-11-25 14:01:38 +0000385 if (p->recursion_count == 0)
386 {
bart9d5b7962008-05-14 12:25:00 +0000387 if (s_mutex_lock_threshold_ms > 0)
388 {
389 ULong held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
390 if (held > s_mutex_lock_threshold_ms)
391 {
392 HoldtimeErrInfo HEI
393 = { mutex, p->acquired_at, held, s_mutex_lock_threshold_ms };
394 VG_(maybe_record_error)(vg_tid,
395 HoldtimeErr,
396 VG_(get_IP)(vg_tid),
397 "mutex",
398 &HEI);
399 }
400 }
401
sewardjaf44c822007-11-25 14:01:38 +0000402 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
403 /* current vector clock of the thread such that it is available when */
404 /* this mutex is locked again. */
sewardjaf44c822007-11-25 14:01:38 +0000405
barta2b6e1b2008-03-17 18:32:39 +0000406 thread_get_latest_segment(&p->last_locked_segment, drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000407 thread_new_segment(drd_tid);
bart9d5b7962008-05-14 12:25:00 +0000408 p->acquired_at = 0;
bart6bbefaf2008-04-19 15:16:45 +0000409 s_mutex_segment_creation_count++;
sewardjaf44c822007-11-25 14:01:38 +0000410 }
sewardjaf44c822007-11-25 14:01:38 +0000411}
412
413const char* mutex_get_typename(struct mutex_info* const p)
414{
415 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000416
sewardj347eeba2008-01-21 14:19:07 +0000417 return mutex_type_name(p->mutex_type);
418}
419
420const char* mutex_type_name(const MutexT mt)
421{
422 switch (mt)
sewardjaf44c822007-11-25 14:01:38 +0000423 {
bart635cb162008-02-28 08:30:43 +0000424 case mutex_type_invalid_mutex:
425 return "invalid mutex";
bart5357fcb2008-02-27 15:46:00 +0000426 case mutex_type_recursive_mutex:
427 return "recursive mutex";
428 case mutex_type_errorcheck_mutex:
429 return "error checking mutex";
430 case mutex_type_default_mutex:
sewardjaf44c822007-11-25 14:01:38 +0000431 return "mutex";
sewardj721ad7b2007-11-30 08:30:29 +0000432 case mutex_type_spinlock:
sewardjaf44c822007-11-25 14:01:38 +0000433 return "spinlock";
434 default:
435 tl_assert(0);
436 }
437 return "?";
438}
439
bart5357fcb2008-02-27 15:46:00 +0000440/** Return true if the specified mutex is locked by any thread. */
441static Bool mutex_is_locked(struct mutex_info* const p)
442{
443 tl_assert(p);
444 return (p->recursion_count > 0);
445}
446
sewardjaf44c822007-11-25 14:01:38 +0000447Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid)
448{
449 struct mutex_info* const p = mutex_get(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000450 if (p)
451 {
452 return (p->recursion_count > 0 && p->owner == tid);
453 }
454 return False;
455}
456
sewardjaf44c822007-11-25 14:01:38 +0000457int mutex_get_recursion_count(const Addr mutex)
458{
459 struct mutex_info* const p = mutex_get(mutex);
460 tl_assert(p);
461 return p->recursion_count;
462}
463
464/**
bart301c3112008-02-24 18:22:37 +0000465 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000466 * "last owner" field can be cleared if it still refers to that thread.
sewardjaf44c822007-11-25 14:01:38 +0000467 */
bart301c3112008-02-24 18:22:37 +0000468void mutex_thread_delete(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000469{
bart4bb53d82008-02-28 19:06:34 +0000470 struct mutex_info* p;
471
bart72b751c2008-03-01 13:44:24 +0000472 clientobj_resetiter();
473 for ( ; (p = &clientobj_next(ClientMutex)->mutex) != 0; )
sewardjaf44c822007-11-25 14:01:38 +0000474 {
bart4bb53d82008-02-28 19:06:34 +0000475 if (p->owner == tid && p->recursion_count > 0)
sewardjaf44c822007-11-25 14:01:38 +0000476 {
bart5357fcb2008-02-27 15:46:00 +0000477 MutexErrInfo MEI
bart4bb53d82008-02-28 19:06:34 +0000478 = { p->a1, p->recursion_count, p->owner };
bart5357fcb2008-02-27 15:46:00 +0000479 VG_(maybe_record_error)(VG_(get_running_tid)(),
480 MutexErr,
481 VG_(get_IP)(VG_(get_running_tid)()),
482 "Mutex still locked at thread exit",
483 &MEI);
sewardjaf44c822007-11-25 14:01:38 +0000484 p->owner = VG_INVALID_THREADID;
485 }
486 }
487}
488
sewardjaf44c822007-11-25 14:01:38 +0000489ULong get_mutex_lock_count(void)
490{
491 return s_mutex_lock_count;
492}
bart6bbefaf2008-04-19 15:16:45 +0000493
494ULong get_mutex_segment_creation_count(void)
495{
496 return s_mutex_segment_creation_count;
497}