blob: dca84a74fee5e9a0841ac6c7d0a8f3dd3b128a20 [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"
sewardjaf44c822007-11-25 14:01:38 +000030#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
31#include "pub_tool_libcassert.h" // tl_assert()
bart5bd9f2d2008-03-03 20:31:58 +000032#include "pub_tool_libcbase.h" // VG_(strlen)
bart4bb53d82008-02-28 19:06:34 +000033#include "pub_tool_libcprint.h" // VG_(message)()
sewardjaf44c822007-11-25 14:01:38 +000034#include "pub_tool_machine.h" // VG_(get_IP)()
35#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
36
37
sewardj347eeba2008-01-21 14:19:07 +000038// Local functions.
39
bart46d5f172008-02-28 19:49:37 +000040static void mutex_cleanup(struct mutex_info* p);
bart5357fcb2008-02-27 15:46:00 +000041static Bool mutex_is_locked(struct mutex_info* const p);
sewardj347eeba2008-01-21 14:19:07 +000042
43
sewardjaf44c822007-11-25 14:01:38 +000044// Local variables.
45
46static Bool s_trace_mutex;
47static ULong s_mutex_lock_count;
bart6bbefaf2008-04-19 15:16:45 +000048static ULong s_mutex_segment_creation_count;
sewardjaf44c822007-11-25 14:01:38 +000049
50
51// Function definitions.
52
53void mutex_set_trace(const Bool trace_mutex)
54{
55 tl_assert(!! trace_mutex == trace_mutex);
56 s_trace_mutex = trace_mutex;
57}
58
59static
60void mutex_initialize(struct mutex_info* const p,
bart0268dfa2008-03-11 20:10:21 +000061 const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000062{
63 tl_assert(mutex != 0);
sewardjaf44c822007-11-25 14:01:38 +000064
bart4bb53d82008-02-28 19:06:34 +000065 tl_assert(p->a1 == mutex);
barta2b6e1b2008-03-17 18:32:39 +000066 p->cleanup = (void(*)(DrdClientobj*))&mutex_cleanup;
67 p->mutex_type = mutex_type;
68 p->recursion_count = 0;
69 p->owner = DRD_INVALID_THREADID;
70 p->last_locked_segment = 0;
sewardjaf44c822007-11-25 14:01:38 +000071}
72
bart46d5f172008-02-28 19:49:37 +000073/** Deallocate the memory that was allocated by mutex_initialize(). */
74static void mutex_cleanup(struct mutex_info* p)
75{
bart6b717612008-03-24 09:29:38 +000076 tl_assert(p);
77
bartb78312c2008-02-29 11:00:17 +000078 if (s_trace_mutex)
79 {
bart3b1ee452008-02-29 19:28:15 +000080 VG_(message)(Vg_UserMsg,
81 "[%d/%d] mutex_destroy %s 0x%lx",
82 VG_(get_running_tid)(),
83 thread_get_running_tid(),
bartb78312c2008-02-29 11:00:17 +000084 mutex_get_typename(p),
85 p->a1);
86 }
87
bart46d5f172008-02-28 19:49:37 +000088 if (mutex_is_locked(p))
89 {
90 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
91 VG_(maybe_record_error)(VG_(get_running_tid)(),
92 MutexErr,
93 VG_(get_IP)(VG_(get_running_tid)()),
94 "Destroying locked mutex",
95 &MEI);
96 }
97
barta2b6e1b2008-03-17 18:32:39 +000098 sg_put(p->last_locked_segment);
99 p->last_locked_segment = 0;
bart46d5f172008-02-28 19:49:37 +0000100}
101
bart6b717612008-03-24 09:29:38 +0000102static void not_a_mutex(const Addr mutex)
103{
104 MutexErrInfo MEI = { mutex, -1, DRD_INVALID_THREADID };
105 VG_(maybe_record_error)(VG_(get_running_tid)(),
106 MutexErr,
107 VG_(get_IP)(VG_(get_running_tid)()),
108 "Not a mutex",
109 &MEI);
110}
111
sewardjaf44c822007-11-25 14:01:38 +0000112static
sewardj721ad7b2007-11-30 08:30:29 +0000113struct mutex_info*
bart0268dfa2008-03-11 20:10:21 +0000114mutex_get_or_allocate(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000115{
bart4bb53d82008-02-28 19:06:34 +0000116 struct mutex_info* p;
sewardj721ad7b2007-11-30 08:30:29 +0000117
bart4bb53d82008-02-28 19:06:34 +0000118 tl_assert(offsetof(DrdClientobj, mutex) == 0);
bart72b751c2008-03-01 13:44:24 +0000119 p = &clientobj_get(mutex, ClientMutex)->mutex;
bart4bb53d82008-02-28 19:06:34 +0000120 if (p)
121 {
bart4bb53d82008-02-28 19:06:34 +0000122 return p;
123 }
sewardj721ad7b2007-11-30 08:30:29 +0000124
bart0268dfa2008-03-11 20:10:21 +0000125 if (clientobj_present(mutex, mutex + 1))
sewardj721ad7b2007-11-30 08:30:29 +0000126 {
bart6b717612008-03-24 09:29:38 +0000127 not_a_mutex(mutex);
bart5bd9f2d2008-03-03 20:31:58 +0000128 return 0;
sewardj721ad7b2007-11-30 08:30:29 +0000129 }
bart4bb53d82008-02-28 19:06:34 +0000130
bart0268dfa2008-03-11 20:10:21 +0000131 p = &clientobj_add(mutex, ClientMutex)->mutex;
132 mutex_initialize(p, mutex, mutex_type);
bart4bb53d82008-02-28 19:06:34 +0000133 return p;
sewardjaf44c822007-11-25 14:01:38 +0000134}
135
bart3b1ee452008-02-29 19:28:15 +0000136struct mutex_info* mutex_get(const Addr mutex)
137{
138 tl_assert(offsetof(DrdClientobj, mutex) == 0);
bart72b751c2008-03-01 13:44:24 +0000139 return &clientobj_get(mutex, ClientMutex)->mutex;
bart3b1ee452008-02-29 19:28:15 +0000140}
141
bart00344642008-03-01 15:27:41 +0000142/** Called before pthread_mutex_init(). */
sewardj721ad7b2007-11-30 08:30:29 +0000143struct mutex_info*
bart0268dfa2008-03-11 20:10:21 +0000144mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000145{
bart00344642008-03-01 15:27:41 +0000146 struct mutex_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000147
sewardjaf44c822007-11-25 14:01:38 +0000148 if (s_trace_mutex)
149 {
bart3b1ee452008-02-29 19:28:15 +0000150 VG_(message)(Vg_UserMsg,
151 "[%d/%d] mutex_init %s 0x%lx",
152 VG_(get_running_tid)(),
153 thread_get_running_tid(),
sewardj347eeba2008-01-21 14:19:07 +0000154 mutex_type_name(mutex_type),
sewardjaf44c822007-11-25 14:01:38 +0000155 mutex);
156 }
157
bart00344642008-03-01 15:27:41 +0000158 if (mutex_type == mutex_type_invalid_mutex)
159 {
bart6b717612008-03-24 09:29:38 +0000160 not_a_mutex(mutex);
bart00344642008-03-01 15:27:41 +0000161 return 0;
162 }
163
164 p = mutex_get(mutex);
165 if (p)
sewardj347eeba2008-01-21 14:19:07 +0000166 {
167 const ThreadId vg_tid = VG_(get_running_tid)();
168 MutexErrInfo MEI
bart00344642008-03-01 15:27:41 +0000169 = { p->a1, p->recursion_count, p->owner };
sewardj347eeba2008-01-21 14:19:07 +0000170 VG_(maybe_record_error)(vg_tid,
171 MutexErr,
172 VG_(get_IP)(vg_tid),
173 "Mutex reinitialization",
174 &MEI);
bart00344642008-03-01 15:27:41 +0000175 return p;
sewardj347eeba2008-01-21 14:19:07 +0000176 }
bart0268dfa2008-03-11 20:10:21 +0000177 p = mutex_get_or_allocate(mutex, mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000178
bart00344642008-03-01 15:27:41 +0000179 return p;
sewardjaf44c822007-11-25 14:01:38 +0000180}
181
bart46d5f172008-02-28 19:49:37 +0000182/** Called after pthread_mutex_destroy(). */
sewardj347eeba2008-01-21 14:19:07 +0000183void mutex_post_destroy(const Addr mutex)
184{
bart72b751c2008-03-01 13:44:24 +0000185 struct mutex_info* p;
sewardj347eeba2008-01-21 14:19:07 +0000186
bart72b751c2008-03-01 13:44:24 +0000187 p = mutex_get(mutex);
188 if (p == 0)
189 {
bart6b717612008-03-24 09:29:38 +0000190 not_a_mutex(mutex);
bart72b751c2008-03-01 13:44:24 +0000191 return;
192 }
193
194 clientobj_remove(mutex, ClientMutex);
sewardj347eeba2008-01-21 14:19:07 +0000195}
196
bart8bba1f72008-02-27 16:13:05 +0000197/** Called before pthread_mutex_lock() is invoked. If a data structure for
198 * the client-side object was not yet created, do this now. Also check whether
199 * an attempt is made to lock recursively a synchronization object that must
200 * not be locked recursively.
201 */
bart2e3a3c12008-03-24 08:33:47 +0000202void mutex_pre_lock(const Addr mutex, const MutexT mutex_type,
203 const Bool trylock)
bart8bba1f72008-02-27 16:13:05 +0000204{
bart635cb162008-02-28 08:30:43 +0000205 struct mutex_info* p;
206
bart0268dfa2008-03-11 20:10:21 +0000207 p = mutex_get_or_allocate(mutex, mutex_type);
bart00344642008-03-01 15:27:41 +0000208 if (s_trace_mutex)
209 {
210 VG_(message)(Vg_UserMsg,
211 "[%d/%d] pre_mutex_lock %s 0x%lx rc %d owner %d",
212 VG_(get_running_tid)(),
213 thread_get_running_tid(),
bart6b717612008-03-24 09:29:38 +0000214 p ? mutex_get_typename(p) : "(?)",
bart00344642008-03-01 15:27:41 +0000215 mutex,
bart2e3a3c12008-03-24 08:33:47 +0000216 p ? p->recursion_count : -1,
217 p ? p->owner : DRD_INVALID_THREADID);
bart00344642008-03-01 15:27:41 +0000218 }
219
bart2e3a3c12008-03-24 08:33:47 +0000220 if (p == 0)
221 {
bart6b717612008-03-24 09:29:38 +0000222 not_a_mutex(mutex);
bart2e3a3c12008-03-24 08:33:47 +0000223 return;
224 }
225
226 tl_assert(p);
227
bart00344642008-03-01 15:27:41 +0000228 if (mutex_type == mutex_type_invalid_mutex)
229 {
bart6b717612008-03-24 09:29:38 +0000230 not_a_mutex(mutex);
bart00344642008-03-01 15:27:41 +0000231 return;
232 }
233
bart2e3a3c12008-03-24 08:33:47 +0000234 if (! trylock
235 && p->owner == thread_get_running_tid()
bart8bba1f72008-02-27 16:13:05 +0000236 && p->recursion_count >= 1
237 && mutex_type != mutex_type_recursive_mutex)
238 {
bart4bb53d82008-02-28 19:06:34 +0000239 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
bart8bba1f72008-02-27 16:13:05 +0000240 VG_(maybe_record_error)(VG_(get_running_tid)(),
241 MutexErr,
242 VG_(get_IP)(VG_(get_running_tid)()),
243 "Recursive locking not allowed",
244 &MEI);
245 }
246}
247
sewardjaf44c822007-11-25 14:01:38 +0000248/**
249 * Update mutex_info state when locking the pthread_mutex_t mutex.
250 * Note: this function must be called after pthread_mutex_lock() has been
251 * called, or a race condition is triggered !
252 */
bart4a975e12008-03-30 13:28:33 +0000253void mutex_post_lock(const Addr mutex, const Bool took_lock,
254 const Bool post_cond_wait)
sewardjaf44c822007-11-25 14:01:38 +0000255{
bart3b1ee452008-02-29 19:28:15 +0000256 const DrdThreadId drd_tid = thread_get_running_tid();
bart00344642008-03-01 15:27:41 +0000257 struct mutex_info* p;
258
259 p = mutex_get(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000260
261 if (s_trace_mutex)
262 {
bart3b1ee452008-02-29 19:28:15 +0000263 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000264 "[%d/%d] %s %s 0x%lx rc %d owner %d%s",
bart3b1ee452008-02-29 19:28:15 +0000265 VG_(get_running_tid)(),
sewardjaf44c822007-11-25 14:01:38 +0000266 drd_tid,
bart4a975e12008-03-30 13:28:33 +0000267 post_cond_wait ? "cond_post_wait " : "post_mutex_lock",
bart00344642008-03-01 15:27:41 +0000268 p ? mutex_get_typename(p) : "(?)",
sewardjaf44c822007-11-25 14:01:38 +0000269 mutex,
270 p ? p->recursion_count : 0,
bartfa37c922008-03-30 08:41:59 +0000271 p ? p->owner : VG_INVALID_THREADID,
272 took_lock ? "" : " (locking failed)");
sewardjaf44c822007-11-25 14:01:38 +0000273 }
274
bart777f7fe2008-03-02 17:43:18 +0000275 if (! p || ! took_lock)
bart5bd9f2d2008-03-03 20:31:58 +0000276 return;
bart5357fcb2008-02-27 15:46:00 +0000277
sewardjaf44c822007-11-25 14:01:38 +0000278 if (p->recursion_count == 0)
279 {
bart5bd9f2d2008-03-03 20:31:58 +0000280 const DrdThreadId last_owner = p->owner;
281
282 if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID)
barta2b6e1b2008-03-17 18:32:39 +0000283 {
284 tl_assert(p->last_locked_segment);
285 thread_combine_vc2(drd_tid, &p->last_locked_segment->vc);
286 }
bart5bd9f2d2008-03-03 20:31:58 +0000287 thread_new_segment(drd_tid);
bart6bbefaf2008-04-19 15:16:45 +0000288 s_mutex_segment_creation_count++;
bart5bd9f2d2008-03-03 20:31:58 +0000289
sewardjaf44c822007-11-25 14:01:38 +0000290 p->owner = drd_tid;
291 s_mutex_lock_count++;
292 }
293 else if (p->owner != drd_tid)
294 {
bart3b1ee452008-02-29 19:28:15 +0000295 VG_(message)(Vg_UserMsg,
sewardjaf44c822007-11-25 14:01:38 +0000296 "The impossible happened: mutex 0x%lx is locked"
297 " simultaneously by two threads (recursion count %d,"
298 " owners %d and %d) !",
bart4bb53d82008-02-28 19:06:34 +0000299 p->a1, p->recursion_count, p->owner, drd_tid);
sewardj347eeba2008-01-21 14:19:07 +0000300 p->owner = drd_tid;
sewardjaf44c822007-11-25 14:01:38 +0000301 }
302 p->recursion_count++;
sewardjaf44c822007-11-25 14:01:38 +0000303}
304
305/**
306 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
307 * Note: this function must be called before pthread_mutex_unlock() is called,
308 * or a race condition is triggered !
bartb78312c2008-02-29 11:00:17 +0000309 * @return New value of the mutex recursion count.
sewardjaf44c822007-11-25 14:01:38 +0000310 * @param mutex Pointer to pthread_mutex_t data structure in the client space.
311 * @param tid ThreadId of the thread calling pthread_mutex_unlock().
312 * @param vc Pointer to the current vector clock of thread tid.
313 */
bart777f7fe2008-03-02 17:43:18 +0000314void mutex_unlock(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000315{
bartb78312c2008-02-29 11:00:17 +0000316 const DrdThreadId drd_tid = thread_get_running_tid();
317 const ThreadId vg_tid = VG_(get_running_tid)();
sewardjaf44c822007-11-25 14:01:38 +0000318 struct mutex_info* const p = mutex_get(mutex);
319
bart777f7fe2008-03-02 17:43:18 +0000320 if (s_trace_mutex)
sewardjaf44c822007-11-25 14:01:38 +0000321 {
bart3b1ee452008-02-29 19:28:15 +0000322 VG_(message)(Vg_UserMsg,
323 "[%d/%d] mutex_unlock %s 0x%lx rc %d",
324 vg_tid,
325 drd_tid,
bart6b717612008-03-24 09:29:38 +0000326 p ? mutex_get_typename(p) : "(?)",
sewardjaf44c822007-11-25 14:01:38 +0000327 mutex,
barta2b6e1b2008-03-17 18:32:39 +0000328 p ? p->recursion_count : 0);
sewardjaf44c822007-11-25 14:01:38 +0000329 }
330
bart777f7fe2008-03-02 17:43:18 +0000331 if (p == 0 || mutex_type == mutex_type_invalid_mutex)
bartab7a6442008-02-25 19:46:14 +0000332 {
bart6b717612008-03-24 09:29:38 +0000333 not_a_mutex(mutex);
bart5bd9f2d2008-03-03 20:31:58 +0000334 return;
bartab7a6442008-02-25 19:46:14 +0000335 }
336
bart5357fcb2008-02-27 15:46:00 +0000337 if (p->owner == DRD_INVALID_THREADID)
338 {
bart4bb53d82008-02-28 19:06:34 +0000339 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
bart5357fcb2008-02-27 15:46:00 +0000340 VG_(maybe_record_error)(vg_tid,
341 MutexErr,
342 VG_(get_IP)(vg_tid),
343 "Mutex not locked",
344 &MEI);
bart5bd9f2d2008-03-03 20:31:58 +0000345 return;
bart5357fcb2008-02-27 15:46:00 +0000346 }
347
sewardjaf44c822007-11-25 14:01:38 +0000348 tl_assert(p);
bart5357fcb2008-02-27 15:46:00 +0000349 if (p->mutex_type != mutex_type)
350 {
barta2b6e1b2008-03-17 18:32:39 +0000351 VG_(message)(Vg_UserMsg, "??? mutex 0x%lx: type changed from %d into %d",
bart5bd9f2d2008-03-03 20:31:58 +0000352 p->a1, p->mutex_type, mutex_type);
bart5357fcb2008-02-27 15:46:00 +0000353 }
sewardj721ad7b2007-11-30 08:30:29 +0000354 tl_assert(p->mutex_type == mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000355 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000356
bart777f7fe2008-03-02 17:43:18 +0000357 if (p->owner != drd_tid || p->recursion_count <= 0)
sewardjaf44c822007-11-25 14:01:38 +0000358 {
bart4bb53d82008-02-28 19:06:34 +0000359 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
sewardjaf44c822007-11-25 14:01:38 +0000360 VG_(maybe_record_error)(vg_tid,
361 MutexErr,
362 VG_(get_IP)(vg_tid),
bart777f7fe2008-03-02 17:43:18 +0000363 "Mutex not locked by calling thread",
sewardjaf44c822007-11-25 14:01:38 +0000364 &MEI);
bart777f7fe2008-03-02 17:43:18 +0000365 return;
sewardjaf44c822007-11-25 14:01:38 +0000366 }
bart777f7fe2008-03-02 17:43:18 +0000367 tl_assert(p->recursion_count > 0);
sewardjaf44c822007-11-25 14:01:38 +0000368 p->recursion_count--;
bart777f7fe2008-03-02 17:43:18 +0000369 tl_assert(p->recursion_count >= 0);
sewardj347eeba2008-01-21 14:19:07 +0000370
sewardjaf44c822007-11-25 14:01:38 +0000371 if (p->recursion_count == 0)
372 {
373 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
374 /* current vector clock of the thread such that it is available when */
375 /* this mutex is locked again. */
sewardjaf44c822007-11-25 14:01:38 +0000376
barta2b6e1b2008-03-17 18:32:39 +0000377 thread_get_latest_segment(&p->last_locked_segment, drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000378 thread_new_segment(drd_tid);
bart6bbefaf2008-04-19 15:16:45 +0000379 s_mutex_segment_creation_count++;
sewardjaf44c822007-11-25 14:01:38 +0000380 }
sewardjaf44c822007-11-25 14:01:38 +0000381}
382
383const char* mutex_get_typename(struct mutex_info* const p)
384{
385 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000386
sewardj347eeba2008-01-21 14:19:07 +0000387 return mutex_type_name(p->mutex_type);
388}
389
390const char* mutex_type_name(const MutexT mt)
391{
392 switch (mt)
sewardjaf44c822007-11-25 14:01:38 +0000393 {
bart635cb162008-02-28 08:30:43 +0000394 case mutex_type_invalid_mutex:
395 return "invalid mutex";
bart5357fcb2008-02-27 15:46:00 +0000396 case mutex_type_recursive_mutex:
397 return "recursive mutex";
398 case mutex_type_errorcheck_mutex:
399 return "error checking mutex";
400 case mutex_type_default_mutex:
sewardjaf44c822007-11-25 14:01:38 +0000401 return "mutex";
sewardj721ad7b2007-11-30 08:30:29 +0000402 case mutex_type_spinlock:
sewardjaf44c822007-11-25 14:01:38 +0000403 return "spinlock";
404 default:
405 tl_assert(0);
406 }
407 return "?";
408}
409
bart5357fcb2008-02-27 15:46:00 +0000410/** Return true if the specified mutex is locked by any thread. */
411static Bool mutex_is_locked(struct mutex_info* const p)
412{
413 tl_assert(p);
414 return (p->recursion_count > 0);
415}
416
sewardjaf44c822007-11-25 14:01:38 +0000417Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid)
418{
419 struct mutex_info* const p = mutex_get(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000420 if (p)
421 {
422 return (p->recursion_count > 0 && p->owner == tid);
423 }
424 return False;
425}
426
sewardjaf44c822007-11-25 14:01:38 +0000427int mutex_get_recursion_count(const Addr mutex)
428{
429 struct mutex_info* const p = mutex_get(mutex);
430 tl_assert(p);
431 return p->recursion_count;
432}
433
434/**
bart301c3112008-02-24 18:22:37 +0000435 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000436 * "last owner" field can be cleared if it still refers to that thread.
sewardjaf44c822007-11-25 14:01:38 +0000437 */
bart301c3112008-02-24 18:22:37 +0000438void mutex_thread_delete(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000439{
bart4bb53d82008-02-28 19:06:34 +0000440 struct mutex_info* p;
441
bart72b751c2008-03-01 13:44:24 +0000442 clientobj_resetiter();
443 for ( ; (p = &clientobj_next(ClientMutex)->mutex) != 0; )
sewardjaf44c822007-11-25 14:01:38 +0000444 {
bart4bb53d82008-02-28 19:06:34 +0000445 if (p->owner == tid && p->recursion_count > 0)
sewardjaf44c822007-11-25 14:01:38 +0000446 {
bart5357fcb2008-02-27 15:46:00 +0000447 MutexErrInfo MEI
bart4bb53d82008-02-28 19:06:34 +0000448 = { p->a1, p->recursion_count, p->owner };
bart5357fcb2008-02-27 15:46:00 +0000449 VG_(maybe_record_error)(VG_(get_running_tid)(),
450 MutexErr,
451 VG_(get_IP)(VG_(get_running_tid)()),
452 "Mutex still locked at thread exit",
453 &MEI);
sewardjaf44c822007-11-25 14:01:38 +0000454 p->owner = VG_INVALID_THREADID;
455 }
456 }
457}
458
sewardjaf44c822007-11-25 14:01:38 +0000459ULong get_mutex_lock_count(void)
460{
461 return s_mutex_lock_count;
462}
bart6bbefaf2008-04-19 15:16:45 +0000463
464ULong get_mutex_segment_creation_count(void)
465{
466 return s_mutex_segment_creation_count;
467}