blob: 4860fedfe82023f28419b60139898550a7bc29b9 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
bart3dbdc862009-02-14 12:14:50 +00004 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00005
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
bart3dbdc862009-02-14 12:14:50 +000025#include "drd_basics.h"
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"
bart9d5b7962008-05-14 12:25:00 +000029#include "pub_tool_vki.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)()
bart9d5b7962008-05-14 12:25:00 +000034#include "pub_tool_libcproc.h" // VG_(read_millisecond_timer)()
sewardjaf44c822007-11-25 14:01:38 +000035#include "pub_tool_machine.h" // VG_(get_IP)()
36#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
37
38
sewardj347eeba2008-01-21 14:19:07 +000039// Local functions.
40
bart46d5f172008-02-28 19:49:37 +000041static void mutex_cleanup(struct mutex_info* p);
bart5357fcb2008-02-27 15:46:00 +000042static Bool mutex_is_locked(struct mutex_info* const p);
sewardj347eeba2008-01-21 14:19:07 +000043
44
sewardjaf44c822007-11-25 14:01:38 +000045// Local variables.
46
47static Bool s_trace_mutex;
48static ULong s_mutex_lock_count;
bart6bbefaf2008-04-19 15:16:45 +000049static ULong s_mutex_segment_creation_count;
bart9d5b7962008-05-14 12:25:00 +000050static UInt s_mutex_lock_threshold_ms = 1000 * 1000;
sewardjaf44c822007-11-25 14:01:38 +000051
52
53// Function definitions.
54
55void mutex_set_trace(const Bool trace_mutex)
56{
57 tl_assert(!! trace_mutex == trace_mutex);
58 s_trace_mutex = trace_mutex;
59}
60
bart9d5b7962008-05-14 12:25:00 +000061void mutex_set_lock_threshold(const UInt lock_threshold_ms)
62{
63 s_mutex_lock_threshold_ms = lock_threshold_ms;
64}
65
sewardjaf44c822007-11-25 14:01:38 +000066static
67void mutex_initialize(struct mutex_info* const p,
bart0268dfa2008-03-11 20:10:21 +000068 const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000069{
bart3f4623e2008-07-07 16:53:07 +000070 tl_assert(mutex);
71 tl_assert(mutex_type != mutex_type_unknown);
bart4bb53d82008-02-28 19:06:34 +000072 tl_assert(p->a1 == mutex);
bart3f4623e2008-07-07 16:53:07 +000073
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,
bart3f4623e2008-07-07 16:53:07 +000091 "[%d/%d] mutex_destroy %s 0x%lx rc %d owner %d",
bart3b1ee452008-02-29 19:28:15 +000092 VG_(get_running_tid)(),
93 thread_get_running_tid(),
bartb78312c2008-02-29 11:00:17 +000094 mutex_get_typename(p),
bart3f4623e2008-07-07 16:53:07 +000095 p->a1,
96 p ? p->recursion_count : -1,
97 p ? p->owner : DRD_INVALID_THREADID);
bartb78312c2008-02-29 11:00:17 +000098 }
99
bart46d5f172008-02-28 19:49:37 +0000100 if (mutex_is_locked(p))
101 {
102 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
103 VG_(maybe_record_error)(VG_(get_running_tid)(),
104 MutexErr,
105 VG_(get_IP)(VG_(get_running_tid)()),
106 "Destroying locked mutex",
107 &MEI);
108 }
109
bart62ada3f2009-02-14 17:19:58 +0000110 DRD_(sg_put)(p->last_locked_segment);
barta2b6e1b2008-03-17 18:32:39 +0000111 p->last_locked_segment = 0;
bart46d5f172008-02-28 19:49:37 +0000112}
113
bart886b87c2008-06-28 13:40:41 +0000114/** Let Valgrind report that there is no mutex object at address 'mutex'. */
bart2225a032008-06-28 16:22:24 +0000115void not_a_mutex(const Addr mutex)
bart6b717612008-03-24 09:29:38 +0000116{
117 MutexErrInfo MEI = { mutex, -1, DRD_INVALID_THREADID };
118 VG_(maybe_record_error)(VG_(get_running_tid)(),
119 MutexErr,
120 VG_(get_IP)(VG_(get_running_tid)()),
121 "Not a mutex",
122 &MEI);
123}
124
sewardjaf44c822007-11-25 14:01:38 +0000125static
sewardj721ad7b2007-11-30 08:30:29 +0000126struct mutex_info*
bart0268dfa2008-03-11 20:10:21 +0000127mutex_get_or_allocate(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000128{
bart4bb53d82008-02-28 19:06:34 +0000129 struct mutex_info* p;
sewardj721ad7b2007-11-30 08:30:29 +0000130
bart4bb53d82008-02-28 19:06:34 +0000131 tl_assert(offsetof(DrdClientobj, mutex) == 0);
bart72b751c2008-03-01 13:44:24 +0000132 p = &clientobj_get(mutex, ClientMutex)->mutex;
bart4bb53d82008-02-28 19:06:34 +0000133 if (p)
134 {
bart4bb53d82008-02-28 19:06:34 +0000135 return p;
136 }
sewardj721ad7b2007-11-30 08:30:29 +0000137
bart0268dfa2008-03-11 20:10:21 +0000138 if (clientobj_present(mutex, mutex + 1))
sewardj721ad7b2007-11-30 08:30:29 +0000139 {
bart6b717612008-03-24 09:29:38 +0000140 not_a_mutex(mutex);
bart5bd9f2d2008-03-03 20:31:58 +0000141 return 0;
sewardj721ad7b2007-11-30 08:30:29 +0000142 }
bart4bb53d82008-02-28 19:06:34 +0000143
bart3f4623e2008-07-07 16:53:07 +0000144 tl_assert(mutex_type != mutex_type_unknown);
145
bart0268dfa2008-03-11 20:10:21 +0000146 p = &clientobj_add(mutex, ClientMutex)->mutex;
147 mutex_initialize(p, mutex, mutex_type);
bart4bb53d82008-02-28 19:06:34 +0000148 return p;
sewardjaf44c822007-11-25 14:01:38 +0000149}
150
bart3b1ee452008-02-29 19:28:15 +0000151struct mutex_info* mutex_get(const Addr mutex)
152{
153 tl_assert(offsetof(DrdClientobj, mutex) == 0);
bart72b751c2008-03-01 13:44:24 +0000154 return &clientobj_get(mutex, ClientMutex)->mutex;
bart3b1ee452008-02-29 19:28:15 +0000155}
156
bart00344642008-03-01 15:27:41 +0000157/** Called before pthread_mutex_init(). */
sewardj721ad7b2007-11-30 08:30:29 +0000158struct mutex_info*
bart0268dfa2008-03-11 20:10:21 +0000159mutex_init(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000160{
bart00344642008-03-01 15:27:41 +0000161 struct mutex_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000162
bart3f4623e2008-07-07 16:53:07 +0000163 tl_assert(mutex_type != mutex_type_unknown);
164
sewardjaf44c822007-11-25 14:01:38 +0000165 if (s_trace_mutex)
166 {
bart3b1ee452008-02-29 19:28:15 +0000167 VG_(message)(Vg_UserMsg,
168 "[%d/%d] mutex_init %s 0x%lx",
169 VG_(get_running_tid)(),
170 thread_get_running_tid(),
sewardj347eeba2008-01-21 14:19:07 +0000171 mutex_type_name(mutex_type),
sewardjaf44c822007-11-25 14:01:38 +0000172 mutex);
173 }
174
bart00344642008-03-01 15:27:41 +0000175 if (mutex_type == mutex_type_invalid_mutex)
176 {
bart6b717612008-03-24 09:29:38 +0000177 not_a_mutex(mutex);
bart00344642008-03-01 15:27:41 +0000178 return 0;
179 }
180
181 p = mutex_get(mutex);
182 if (p)
sewardj347eeba2008-01-21 14:19:07 +0000183 {
184 const ThreadId vg_tid = VG_(get_running_tid)();
185 MutexErrInfo MEI
bart00344642008-03-01 15:27:41 +0000186 = { p->a1, p->recursion_count, p->owner };
sewardj347eeba2008-01-21 14:19:07 +0000187 VG_(maybe_record_error)(vg_tid,
188 MutexErr,
189 VG_(get_IP)(vg_tid),
190 "Mutex reinitialization",
191 &MEI);
bart00344642008-03-01 15:27:41 +0000192 return p;
sewardj347eeba2008-01-21 14:19:07 +0000193 }
bart0268dfa2008-03-11 20:10:21 +0000194 p = mutex_get_or_allocate(mutex, mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000195
bart00344642008-03-01 15:27:41 +0000196 return p;
sewardjaf44c822007-11-25 14:01:38 +0000197}
198
bart46d5f172008-02-28 19:49:37 +0000199/** Called after pthread_mutex_destroy(). */
sewardj347eeba2008-01-21 14:19:07 +0000200void mutex_post_destroy(const Addr mutex)
201{
bart72b751c2008-03-01 13:44:24 +0000202 struct mutex_info* p;
sewardj347eeba2008-01-21 14:19:07 +0000203
bart72b751c2008-03-01 13:44:24 +0000204 p = mutex_get(mutex);
205 if (p == 0)
206 {
bart6b717612008-03-24 09:29:38 +0000207 not_a_mutex(mutex);
bart72b751c2008-03-01 13:44:24 +0000208 return;
209 }
210
211 clientobj_remove(mutex, ClientMutex);
sewardj347eeba2008-01-21 14:19:07 +0000212}
213
bart8bba1f72008-02-27 16:13:05 +0000214/** Called before pthread_mutex_lock() is invoked. If a data structure for
215 * the client-side object was not yet created, do this now. Also check whether
216 * an attempt is made to lock recursively a synchronization object that must
217 * not be locked recursively.
218 */
bart3f4623e2008-07-07 16:53:07 +0000219void mutex_pre_lock(const Addr mutex, MutexT mutex_type,
bart2e3a3c12008-03-24 08:33:47 +0000220 const Bool trylock)
bart8bba1f72008-02-27 16:13:05 +0000221{
bart635cb162008-02-28 08:30:43 +0000222 struct mutex_info* p;
223
bart0268dfa2008-03-11 20:10:21 +0000224 p = mutex_get_or_allocate(mutex, mutex_type);
bart3f4623e2008-07-07 16:53:07 +0000225 if (mutex_type == mutex_type_unknown)
226 mutex_type = p->mutex_type;
227
bart00344642008-03-01 15:27:41 +0000228 if (s_trace_mutex)
229 {
230 VG_(message)(Vg_UserMsg,
bart3f4623e2008-07-07 16:53:07 +0000231 "[%d/%d] %s %s 0x%lx rc %d owner %d",
bart00344642008-03-01 15:27:41 +0000232 VG_(get_running_tid)(),
233 thread_get_running_tid(),
bart3f4623e2008-07-07 16:53:07 +0000234 trylock ? "pre_mutex_lock " : "mutex_trylock ",
bart6b717612008-03-24 09:29:38 +0000235 p ? mutex_get_typename(p) : "(?)",
bart00344642008-03-01 15:27:41 +0000236 mutex,
bart2e3a3c12008-03-24 08:33:47 +0000237 p ? p->recursion_count : -1,
238 p ? p->owner : DRD_INVALID_THREADID);
bart00344642008-03-01 15:27:41 +0000239 }
240
bart2e3a3c12008-03-24 08:33:47 +0000241 if (p == 0)
242 {
bart6b717612008-03-24 09:29:38 +0000243 not_a_mutex(mutex);
bart2e3a3c12008-03-24 08:33:47 +0000244 return;
245 }
246
247 tl_assert(p);
248
bart00344642008-03-01 15:27:41 +0000249 if (mutex_type == mutex_type_invalid_mutex)
250 {
bart6b717612008-03-24 09:29:38 +0000251 not_a_mutex(mutex);
bart00344642008-03-01 15:27:41 +0000252 return;
253 }
254
bart2e3a3c12008-03-24 08:33:47 +0000255 if (! trylock
256 && p->owner == thread_get_running_tid()
bart8bba1f72008-02-27 16:13:05 +0000257 && p->recursion_count >= 1
258 && mutex_type != mutex_type_recursive_mutex)
259 {
bart4bb53d82008-02-28 19:06:34 +0000260 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
bart8bba1f72008-02-27 16:13:05 +0000261 VG_(maybe_record_error)(VG_(get_running_tid)(),
262 MutexErr,
263 VG_(get_IP)(VG_(get_running_tid)()),
264 "Recursive locking not allowed",
265 &MEI);
266 }
267}
268
sewardjaf44c822007-11-25 14:01:38 +0000269/**
270 * Update mutex_info state when locking the pthread_mutex_t mutex.
271 * Note: this function must be called after pthread_mutex_lock() has been
272 * called, or a race condition is triggered !
273 */
bart4a975e12008-03-30 13:28:33 +0000274void mutex_post_lock(const Addr mutex, const Bool took_lock,
275 const Bool post_cond_wait)
sewardjaf44c822007-11-25 14:01:38 +0000276{
bart3b1ee452008-02-29 19:28:15 +0000277 const DrdThreadId drd_tid = thread_get_running_tid();
bart00344642008-03-01 15:27:41 +0000278 struct mutex_info* p;
279
280 p = mutex_get(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000281
282 if (s_trace_mutex)
283 {
bart3b1ee452008-02-29 19:28:15 +0000284 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000285 "[%d/%d] %s %s 0x%lx rc %d owner %d%s",
bart3b1ee452008-02-29 19:28:15 +0000286 VG_(get_running_tid)(),
sewardjaf44c822007-11-25 14:01:38 +0000287 drd_tid,
bart4a975e12008-03-30 13:28:33 +0000288 post_cond_wait ? "cond_post_wait " : "post_mutex_lock",
bart00344642008-03-01 15:27:41 +0000289 p ? mutex_get_typename(p) : "(?)",
sewardjaf44c822007-11-25 14:01:38 +0000290 mutex,
291 p ? p->recursion_count : 0,
bartfa37c922008-03-30 08:41:59 +0000292 p ? p->owner : VG_INVALID_THREADID,
293 took_lock ? "" : " (locking failed)");
sewardjaf44c822007-11-25 14:01:38 +0000294 }
295
bart777f7fe2008-03-02 17:43:18 +0000296 if (! p || ! took_lock)
bart5bd9f2d2008-03-03 20:31:58 +0000297 return;
bart5357fcb2008-02-27 15:46:00 +0000298
sewardjaf44c822007-11-25 14:01:38 +0000299 if (p->recursion_count == 0)
300 {
bart5bd9f2d2008-03-03 20:31:58 +0000301 const DrdThreadId last_owner = p->owner;
302
303 if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID)
barta2b6e1b2008-03-17 18:32:39 +0000304 {
305 tl_assert(p->last_locked_segment);
306 thread_combine_vc2(drd_tid, &p->last_locked_segment->vc);
307 }
bart5bd9f2d2008-03-03 20:31:58 +0000308 thread_new_segment(drd_tid);
bart6bbefaf2008-04-19 15:16:45 +0000309 s_mutex_segment_creation_count++;
bart5bd9f2d2008-03-03 20:31:58 +0000310
bart9d5b7962008-05-14 12:25:00 +0000311 p->owner = drd_tid;
312 p->acquiry_time_ms = VG_(read_millisecond_timer)();
313 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
sewardjaf44c822007-11-25 14:01:38 +0000314 s_mutex_lock_count++;
315 }
316 else if (p->owner != drd_tid)
317 {
bart3b1ee452008-02-29 19:28:15 +0000318 VG_(message)(Vg_UserMsg,
sewardjaf44c822007-11-25 14:01:38 +0000319 "The impossible happened: mutex 0x%lx is locked"
320 " simultaneously by two threads (recursion count %d,"
321 " owners %d and %d) !",
bart4bb53d82008-02-28 19:06:34 +0000322 p->a1, p->recursion_count, p->owner, drd_tid);
sewardj347eeba2008-01-21 14:19:07 +0000323 p->owner = drd_tid;
sewardjaf44c822007-11-25 14:01:38 +0000324 }
325 p->recursion_count++;
sewardjaf44c822007-11-25 14:01:38 +0000326}
327
bart9d5b7962008-05-14 12:25:00 +0000328/** Update mutex_info state when unlocking the pthread_mutex_t mutex.
329 *
330 * @param mutex Pointer to pthread_mutex_t data structure in the client space.
331 * @param tid ThreadId of the thread calling pthread_mutex_unlock().
332 * @param vc Pointer to the current vector clock of thread tid.
333 *
334 * @return New value of the mutex recursion count.
335 *
336 * @note This function must be called before pthread_mutex_unlock() is called,
337 * or a race condition is triggered !
sewardjaf44c822007-11-25 14:01:38 +0000338 */
bart3f4623e2008-07-07 16:53:07 +0000339void mutex_unlock(const Addr mutex, MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000340{
bartb78312c2008-02-29 11:00:17 +0000341 const DrdThreadId drd_tid = thread_get_running_tid();
342 const ThreadId vg_tid = VG_(get_running_tid)();
bart3f4623e2008-07-07 16:53:07 +0000343 struct mutex_info* p;
344
345 p = mutex_get(mutex);
346 if (mutex_type == mutex_type_unknown)
347 mutex_type = p->mutex_type;
sewardjaf44c822007-11-25 14:01:38 +0000348
bart777f7fe2008-03-02 17:43:18 +0000349 if (s_trace_mutex)
sewardjaf44c822007-11-25 14:01:38 +0000350 {
bart3b1ee452008-02-29 19:28:15 +0000351 VG_(message)(Vg_UserMsg,
352 "[%d/%d] mutex_unlock %s 0x%lx rc %d",
353 vg_tid,
354 drd_tid,
bart6b717612008-03-24 09:29:38 +0000355 p ? mutex_get_typename(p) : "(?)",
sewardjaf44c822007-11-25 14:01:38 +0000356 mutex,
barta2b6e1b2008-03-17 18:32:39 +0000357 p ? p->recursion_count : 0);
sewardjaf44c822007-11-25 14:01:38 +0000358 }
359
bart777f7fe2008-03-02 17:43:18 +0000360 if (p == 0 || mutex_type == mutex_type_invalid_mutex)
bartab7a6442008-02-25 19:46:14 +0000361 {
bart6b717612008-03-24 09:29:38 +0000362 not_a_mutex(mutex);
bart5bd9f2d2008-03-03 20:31:58 +0000363 return;
bartab7a6442008-02-25 19:46:14 +0000364 }
365
bart5357fcb2008-02-27 15:46:00 +0000366 if (p->owner == DRD_INVALID_THREADID)
367 {
bart4bb53d82008-02-28 19:06:34 +0000368 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
bart5357fcb2008-02-27 15:46:00 +0000369 VG_(maybe_record_error)(vg_tid,
370 MutexErr,
371 VG_(get_IP)(vg_tid),
372 "Mutex not locked",
373 &MEI);
bart5bd9f2d2008-03-03 20:31:58 +0000374 return;
bart5357fcb2008-02-27 15:46:00 +0000375 }
376
sewardjaf44c822007-11-25 14:01:38 +0000377 tl_assert(p);
bart5357fcb2008-02-27 15:46:00 +0000378 if (p->mutex_type != mutex_type)
379 {
barta2b6e1b2008-03-17 18:32:39 +0000380 VG_(message)(Vg_UserMsg, "??? mutex 0x%lx: type changed from %d into %d",
bart5bd9f2d2008-03-03 20:31:58 +0000381 p->a1, p->mutex_type, mutex_type);
bart5357fcb2008-02-27 15:46:00 +0000382 }
sewardj721ad7b2007-11-30 08:30:29 +0000383 tl_assert(p->mutex_type == mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000384 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000385
bart777f7fe2008-03-02 17:43:18 +0000386 if (p->owner != drd_tid || p->recursion_count <= 0)
sewardjaf44c822007-11-25 14:01:38 +0000387 {
bart4bb53d82008-02-28 19:06:34 +0000388 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
sewardjaf44c822007-11-25 14:01:38 +0000389 VG_(maybe_record_error)(vg_tid,
390 MutexErr,
391 VG_(get_IP)(vg_tid),
bart777f7fe2008-03-02 17:43:18 +0000392 "Mutex not locked by calling thread",
sewardjaf44c822007-11-25 14:01:38 +0000393 &MEI);
bart777f7fe2008-03-02 17:43:18 +0000394 return;
sewardjaf44c822007-11-25 14:01:38 +0000395 }
bart777f7fe2008-03-02 17:43:18 +0000396 tl_assert(p->recursion_count > 0);
sewardjaf44c822007-11-25 14:01:38 +0000397 p->recursion_count--;
bart777f7fe2008-03-02 17:43:18 +0000398 tl_assert(p->recursion_count >= 0);
sewardj347eeba2008-01-21 14:19:07 +0000399
sewardjaf44c822007-11-25 14:01:38 +0000400 if (p->recursion_count == 0)
401 {
bart9d5b7962008-05-14 12:25:00 +0000402 if (s_mutex_lock_threshold_ms > 0)
403 {
404 ULong held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
405 if (held > s_mutex_lock_threshold_ms)
406 {
407 HoldtimeErrInfo HEI
408 = { mutex, p->acquired_at, held, s_mutex_lock_threshold_ms };
409 VG_(maybe_record_error)(vg_tid,
410 HoldtimeErr,
411 VG_(get_IP)(vg_tid),
412 "mutex",
413 &HEI);
414 }
415 }
416
sewardjaf44c822007-11-25 14:01:38 +0000417 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
418 /* current vector clock of the thread such that it is available when */
419 /* this mutex is locked again. */
sewardjaf44c822007-11-25 14:01:38 +0000420
barta2b6e1b2008-03-17 18:32:39 +0000421 thread_get_latest_segment(&p->last_locked_segment, drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000422 thread_new_segment(drd_tid);
bart9d5b7962008-05-14 12:25:00 +0000423 p->acquired_at = 0;
bart6bbefaf2008-04-19 15:16:45 +0000424 s_mutex_segment_creation_count++;
sewardjaf44c822007-11-25 14:01:38 +0000425 }
sewardjaf44c822007-11-25 14:01:38 +0000426}
427
bart3dbdc862009-02-14 12:14:50 +0000428void DRD_(spinlock_init_or_unlock)(const Addr spinlock)
429{
430 struct mutex_info* mutex_p = mutex_get(spinlock);
431 if (mutex_p)
432 {
433 mutex_unlock(spinlock, mutex_type_spinlock);
434 }
435 else
436 {
437 mutex_init(spinlock, mutex_type_spinlock);
438 }
439}
440
sewardjaf44c822007-11-25 14:01:38 +0000441const char* mutex_get_typename(struct mutex_info* const p)
442{
443 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000444
sewardj347eeba2008-01-21 14:19:07 +0000445 return mutex_type_name(p->mutex_type);
446}
447
448const char* mutex_type_name(const MutexT mt)
449{
450 switch (mt)
sewardjaf44c822007-11-25 14:01:38 +0000451 {
bart635cb162008-02-28 08:30:43 +0000452 case mutex_type_invalid_mutex:
453 return "invalid mutex";
bart5357fcb2008-02-27 15:46:00 +0000454 case mutex_type_recursive_mutex:
455 return "recursive mutex";
456 case mutex_type_errorcheck_mutex:
457 return "error checking mutex";
458 case mutex_type_default_mutex:
sewardjaf44c822007-11-25 14:01:38 +0000459 return "mutex";
sewardj721ad7b2007-11-30 08:30:29 +0000460 case mutex_type_spinlock:
sewardjaf44c822007-11-25 14:01:38 +0000461 return "spinlock";
462 default:
463 tl_assert(0);
464 }
465 return "?";
466}
467
bart5357fcb2008-02-27 15:46:00 +0000468/** Return true if the specified mutex is locked by any thread. */
469static Bool mutex_is_locked(struct mutex_info* const p)
470{
471 tl_assert(p);
472 return (p->recursion_count > 0);
473}
474
sewardjaf44c822007-11-25 14:01:38 +0000475Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid)
476{
477 struct mutex_info* const p = mutex_get(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000478 if (p)
479 {
480 return (p->recursion_count > 0 && p->owner == tid);
481 }
482 return False;
483}
484
sewardjaf44c822007-11-25 14:01:38 +0000485int mutex_get_recursion_count(const Addr mutex)
486{
487 struct mutex_info* const p = mutex_get(mutex);
488 tl_assert(p);
489 return p->recursion_count;
490}
491
492/**
bart301c3112008-02-24 18:22:37 +0000493 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000494 * "last owner" field can be cleared if it still refers to that thread.
sewardjaf44c822007-11-25 14:01:38 +0000495 */
bart301c3112008-02-24 18:22:37 +0000496void mutex_thread_delete(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000497{
bart4bb53d82008-02-28 19:06:34 +0000498 struct mutex_info* p;
499
bart72b751c2008-03-01 13:44:24 +0000500 clientobj_resetiter();
501 for ( ; (p = &clientobj_next(ClientMutex)->mutex) != 0; )
sewardjaf44c822007-11-25 14:01:38 +0000502 {
bart4bb53d82008-02-28 19:06:34 +0000503 if (p->owner == tid && p->recursion_count > 0)
sewardjaf44c822007-11-25 14:01:38 +0000504 {
bart5357fcb2008-02-27 15:46:00 +0000505 MutexErrInfo MEI
bart4bb53d82008-02-28 19:06:34 +0000506 = { p->a1, p->recursion_count, p->owner };
bart5357fcb2008-02-27 15:46:00 +0000507 VG_(maybe_record_error)(VG_(get_running_tid)(),
508 MutexErr,
509 VG_(get_IP)(VG_(get_running_tid)()),
510 "Mutex still locked at thread exit",
511 &MEI);
sewardjaf44c822007-11-25 14:01:38 +0000512 p->owner = VG_INVALID_THREADID;
513 }
514 }
515}
516
sewardjaf44c822007-11-25 14:01:38 +0000517ULong get_mutex_lock_count(void)
518{
519 return s_mutex_lock_count;
520}
bart6bbefaf2008-04-19 15:16:45 +0000521
522ULong get_mutex_segment_creation_count(void)
523{
524 return s_mutex_segment_creation_count;
525}