blob: c9392ee74d3ea0897394c383fa206e3d6f2e0917 [file] [log] [blame]
bart922304f2011-03-13 12:02:44 +00001/* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
bart922304f2011-03-13 12:02:44 +00005 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00006
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
bart3dbdc862009-02-14 12:14:50 +000026#include "drd_basics.h"
bart4bb53d82008-02-28 19:06:34 +000027#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000028#include "drd_error.h"
29#include "drd_mutex.h"
bart9d5b7962008-05-14 12:25:00 +000030#include "pub_tool_vki.h"
bart74b2d972011-10-08 08:54:57 +000031#include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
32#include "pub_tool_libcassert.h" /* tl_assert() */
33#include "pub_tool_libcbase.h" /* VG_(strlen) */
34#include "pub_tool_libcprint.h" /* VG_(message)() */
35#include "pub_tool_libcproc.h" /* VG_(read_millisecond_timer)() */
36#include "pub_tool_machine.h" /* VG_(get_IP)() */
37#include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
sewardjaf44c822007-11-25 14:01:38 +000038
39
bartdc1ef032009-02-15 14:18:02 +000040/* Local functions. */
sewardj347eeba2008-01-21 14:19:07 +000041
bartd2c5eae2009-02-21 15:27:04 +000042static void mutex_cleanup(struct mutex_info* p);
43static Bool mutex_is_locked(struct mutex_info* const p);
44static void mutex_delete_thread(struct mutex_info* p, const DrdThreadId tid);
sewardj347eeba2008-01-21 14:19:07 +000045
46
bartdc1ef032009-02-15 14:18:02 +000047/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000048
bartd2c5eae2009-02-21 15:27:04 +000049static Bool s_trace_mutex;
50static ULong s_mutex_lock_count;
51static ULong s_mutex_segment_creation_count;
bart8600c422010-10-25 18:18:54 +000052static UInt s_mutex_lock_threshold_ms;
sewardjaf44c822007-11-25 14:01:38 +000053
54
bartdc1ef032009-02-15 14:18:02 +000055/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000056
bartdc1ef032009-02-15 14:18:02 +000057void DRD_(mutex_set_trace)(const Bool trace_mutex)
sewardjaf44c822007-11-25 14:01:38 +000058{
bartbedfd232009-03-26 19:07:15 +000059 tl_assert(!! trace_mutex == trace_mutex);
60 s_trace_mutex = trace_mutex;
sewardjaf44c822007-11-25 14:01:38 +000061}
62
bartdc1ef032009-02-15 14:18:02 +000063void DRD_(mutex_set_lock_threshold)(const UInt lock_threshold_ms)
bart9d5b7962008-05-14 12:25:00 +000064{
bartbedfd232009-03-26 19:07:15 +000065 s_mutex_lock_threshold_ms = lock_threshold_ms;
bart9d5b7962008-05-14 12:25:00 +000066}
67
sewardjaf44c822007-11-25 14:01:38 +000068static
bartdc1ef032009-02-15 14:18:02 +000069void DRD_(mutex_initialize)(struct mutex_info* const p,
70 const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000071{
bartbedfd232009-03-26 19:07:15 +000072 tl_assert(mutex);
bartbedfd232009-03-26 19:07:15 +000073 tl_assert(p->a1 == mutex);
bart3f4623e2008-07-07 16:53:07 +000074
bartbedfd232009-03-26 19:07:15 +000075 p->cleanup = (void(*)(DrdClientobj*))mutex_cleanup;
76 p->delete_thread
77 = (void(*)(DrdClientobj*, DrdThreadId))mutex_delete_thread;
78 p->mutex_type = mutex_type;
79 p->recursion_count = 0;
80 p->owner = DRD_INVALID_THREADID;
81 p->last_locked_segment = 0;
82 p->acquiry_time_ms = 0;
83 p->acquired_at = 0;
sewardjaf44c822007-11-25 14:01:38 +000084}
85
bart46d5f172008-02-28 19:49:37 +000086/** Deallocate the memory that was allocated by mutex_initialize(). */
bartd2c5eae2009-02-21 15:27:04 +000087static void mutex_cleanup(struct mutex_info* p)
bart46d5f172008-02-28 19:49:37 +000088{
bartbedfd232009-03-26 19:07:15 +000089 tl_assert(p);
bart6b717612008-03-24 09:29:38 +000090
bartbedfd232009-03-26 19:07:15 +000091 if (s_trace_mutex)
bartad994e82011-10-13 18:04:30 +000092 DRD_(trace_msg)("[%d] mutex_destroy %s 0x%lx rc %d owner %d",
bartb92ff0f2011-10-08 08:29:29 +000093 DRD_(thread_get_running_tid)(),
94 DRD_(mutex_get_typename)(p), p->a1,
95 p ? p->recursion_count : -1,
96 p ? p->owner : DRD_INVALID_THREADID);
bartb78312c2008-02-29 11:00:17 +000097
bartbedfd232009-03-26 19:07:15 +000098 if (mutex_is_locked(p))
99 {
bartd45d9952009-05-31 18:53:54 +0000100 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
101 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000102 VG_(maybe_record_error)(VG_(get_running_tid)(),
103 MutexErr,
104 VG_(get_IP)(VG_(get_running_tid)()),
105 "Destroying locked mutex",
106 &MEI);
107 }
bart46d5f172008-02-28 19:49:37 +0000108
bartbedfd232009-03-26 19:07:15 +0000109 DRD_(sg_put)(p->last_locked_segment);
110 p->last_locked_segment = 0;
bart46d5f172008-02-28 19:49:37 +0000111}
112
bart62cc2322010-03-07 10:54:21 +0000113/** Report that address 'mutex' is not the address of a mutex object. */
bartdc1ef032009-02-15 14:18:02 +0000114void DRD_(not_a_mutex)(const Addr mutex)
bart6b717612008-03-24 09:29:38 +0000115{
bartd45d9952009-05-31 18:53:54 +0000116 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
117 mutex, -1, DRD_INVALID_THREADID };
bartbedfd232009-03-26 19:07:15 +0000118 VG_(maybe_record_error)(VG_(get_running_tid)(),
119 MutexErr,
120 VG_(get_IP)(VG_(get_running_tid)()),
121 "Not a mutex",
122 &MEI);
bart6b717612008-03-24 09:29:38 +0000123}
124
bart62cc2322010-03-07 10:54:21 +0000125/**
126 * Report that address 'mutex' is not the address of a mutex object of the
127 * expected type.
128 */
129static void wrong_mutex_type(const Addr mutex)
130{
131 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
132 mutex, -1, DRD_INVALID_THREADID };
133 VG_(maybe_record_error)(VG_(get_running_tid)(),
134 MutexErr,
135 VG_(get_IP)(VG_(get_running_tid)()),
136 "Mutex type mismatch",
137 &MEI);
138}
139
sewardjaf44c822007-11-25 14:01:38 +0000140static
sewardj721ad7b2007-11-30 08:30:29 +0000141struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000142DRD_(mutex_get_or_allocate)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000143{
bartbedfd232009-03-26 19:07:15 +0000144 struct mutex_info* p;
sewardj721ad7b2007-11-30 08:30:29 +0000145
bartbedfd232009-03-26 19:07:15 +0000146 tl_assert(offsetof(DrdClientobj, mutex) == 0);
147 p = &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
148 if (p)
149 {
bart62cc2322010-03-07 10:54:21 +0000150 if (mutex_type == mutex_type_unknown || p->mutex_type == mutex_type)
151 return p;
152 else
153 {
154 wrong_mutex_type(mutex);
155 return 0;
156 }
bartbedfd232009-03-26 19:07:15 +0000157 }
sewardj721ad7b2007-11-30 08:30:29 +0000158
bartbedfd232009-03-26 19:07:15 +0000159 if (DRD_(clientobj_present)(mutex, mutex + 1))
160 {
161 DRD_(not_a_mutex)(mutex);
162 return 0;
163 }
bart4bb53d82008-02-28 19:06:34 +0000164
bartbedfd232009-03-26 19:07:15 +0000165 p = &(DRD_(clientobj_add)(mutex, ClientMutex)->mutex);
166 DRD_(mutex_initialize)(p, mutex, mutex_type);
167 return p;
sewardjaf44c822007-11-25 14:01:38 +0000168}
169
bartdc1ef032009-02-15 14:18:02 +0000170struct mutex_info* DRD_(mutex_get)(const Addr mutex)
bart3b1ee452008-02-29 19:28:15 +0000171{
bartbedfd232009-03-26 19:07:15 +0000172 tl_assert(offsetof(DrdClientobj, mutex) == 0);
173 return &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
bart3b1ee452008-02-29 19:28:15 +0000174}
175
bart00344642008-03-01 15:27:41 +0000176/** Called before pthread_mutex_init(). */
sewardj721ad7b2007-11-30 08:30:29 +0000177struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000178DRD_(mutex_init)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000179{
bartbedfd232009-03-26 19:07:15 +0000180 struct mutex_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000181
bartbedfd232009-03-26 19:07:15 +0000182 if (s_trace_mutex)
bartad994e82011-10-13 18:04:30 +0000183 DRD_(trace_msg)("[%d] mutex_init %s 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000184 DRD_(thread_get_running_tid)(),
185 DRD_(mutex_type_name)(mutex_type),
186 mutex);
sewardjaf44c822007-11-25 14:01:38 +0000187
bartbedfd232009-03-26 19:07:15 +0000188 if (mutex_type == mutex_type_invalid_mutex)
189 {
190 DRD_(not_a_mutex)(mutex);
191 return 0;
192 }
bart00344642008-03-01 15:27:41 +0000193
bartbedfd232009-03-26 19:07:15 +0000194 p = DRD_(mutex_get)(mutex);
195 if (p)
196 {
197 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000198 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
199 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000200 VG_(maybe_record_error)(vg_tid,
201 MutexErr,
202 VG_(get_IP)(vg_tid),
203 "Mutex reinitialization",
204 &MEI);
bartfa358282009-05-16 06:22:46 +0000205 p->mutex_type = mutex_type;
bartbedfd232009-03-26 19:07:15 +0000206 return p;
207 }
208 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000209
bartbedfd232009-03-26 19:07:15 +0000210 return p;
sewardjaf44c822007-11-25 14:01:38 +0000211}
212
bart46d5f172008-02-28 19:49:37 +0000213/** Called after pthread_mutex_destroy(). */
bartdc1ef032009-02-15 14:18:02 +0000214void DRD_(mutex_post_destroy)(const Addr mutex)
sewardj347eeba2008-01-21 14:19:07 +0000215{
bartbedfd232009-03-26 19:07:15 +0000216 struct mutex_info* p;
sewardj347eeba2008-01-21 14:19:07 +0000217
bartbedfd232009-03-26 19:07:15 +0000218 p = DRD_(mutex_get)(mutex);
219 if (p == 0)
220 {
221 DRD_(not_a_mutex)(mutex);
222 return;
223 }
bart72b751c2008-03-01 13:44:24 +0000224
bartbedfd232009-03-26 19:07:15 +0000225 DRD_(clientobj_remove)(mutex, ClientMutex);
sewardj347eeba2008-01-21 14:19:07 +0000226}
227
bart62cc2322010-03-07 10:54:21 +0000228/**
229 * Called before pthread_mutex_lock() is invoked. If a data structure for the
230 * client-side object was not yet created, do this now. Also check whether an
231 * attempt is made to lock recursively a synchronization object that must not
232 * be locked recursively.
bart8bba1f72008-02-27 16:13:05 +0000233 */
bartdc1ef032009-02-15 14:18:02 +0000234void DRD_(mutex_pre_lock)(const Addr mutex, MutexT mutex_type,
235 const Bool trylock)
bart8bba1f72008-02-27 16:13:05 +0000236{
bartbedfd232009-03-26 19:07:15 +0000237 struct mutex_info* p;
bart635cb162008-02-28 08:30:43 +0000238
bartbedfd232009-03-26 19:07:15 +0000239 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
bart62cc2322010-03-07 10:54:21 +0000240 if (p && mutex_type == mutex_type_unknown)
bartbedfd232009-03-26 19:07:15 +0000241 mutex_type = p->mutex_type;
bart3f4623e2008-07-07 16:53:07 +0000242
bartbedfd232009-03-26 19:07:15 +0000243 if (s_trace_mutex)
bartad994e82011-10-13 18:04:30 +0000244 DRD_(trace_msg)("[%d] %s %s 0x%lx rc %d owner %d",
bartb92ff0f2011-10-08 08:29:29 +0000245 DRD_(thread_get_running_tid)(),
246 trylock ? "pre_mutex_lock " : "mutex_trylock ",
247 p ? DRD_(mutex_get_typename)(p) : "(?)",
248 mutex, p ? p->recursion_count : -1,
249 p ? p->owner : DRD_INVALID_THREADID);
bart00344642008-03-01 15:27:41 +0000250
bartbedfd232009-03-26 19:07:15 +0000251 if (p == 0)
252 {
253 DRD_(not_a_mutex)(mutex);
254 return;
255 }
bart2e3a3c12008-03-24 08:33:47 +0000256
bartbedfd232009-03-26 19:07:15 +0000257 tl_assert(p);
bart2e3a3c12008-03-24 08:33:47 +0000258
bartbedfd232009-03-26 19:07:15 +0000259 if (mutex_type == mutex_type_invalid_mutex)
260 {
261 DRD_(not_a_mutex)(mutex);
262 return;
263 }
bart00344642008-03-01 15:27:41 +0000264
bartbedfd232009-03-26 19:07:15 +0000265 if (! trylock
266 && p->owner == DRD_(thread_get_running_tid)()
267 && p->recursion_count >= 1
268 && mutex_type != mutex_type_recursive_mutex)
269 {
bartd45d9952009-05-31 18:53:54 +0000270 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
271 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000272 VG_(maybe_record_error)(VG_(get_running_tid)(),
273 MutexErr,
274 VG_(get_IP)(VG_(get_running_tid)()),
275 "Recursive locking not allowed",
276 &MEI);
277 }
bart8bba1f72008-02-27 16:13:05 +0000278}
279
sewardjaf44c822007-11-25 14:01:38 +0000280/**
281 * Update mutex_info state when locking the pthread_mutex_t mutex.
282 * Note: this function must be called after pthread_mutex_lock() has been
283 * called, or a race condition is triggered !
284 */
bartdc1ef032009-02-15 14:18:02 +0000285void DRD_(mutex_post_lock)(const Addr mutex, const Bool took_lock,
286 const Bool post_cond_wait)
sewardjaf44c822007-11-25 14:01:38 +0000287{
bartbedfd232009-03-26 19:07:15 +0000288 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
289 struct mutex_info* p;
bart00344642008-03-01 15:27:41 +0000290
bartbedfd232009-03-26 19:07:15 +0000291 p = DRD_(mutex_get)(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000292
bartbedfd232009-03-26 19:07:15 +0000293 if (s_trace_mutex)
bartad994e82011-10-13 18:04:30 +0000294 DRD_(trace_msg)("[%d] %s %s 0x%lx rc %d owner %d%s",
bartb92ff0f2011-10-08 08:29:29 +0000295 drd_tid,
296 post_cond_wait ? "cond_post_wait " : "post_mutex_lock",
297 p ? DRD_(mutex_get_typename)(p) : "(?)",
298 mutex, p ? p->recursion_count : 0,
299 p ? p->owner : VG_INVALID_THREADID,
300 took_lock ? "" : " (locking failed)");
sewardjaf44c822007-11-25 14:01:38 +0000301
bartbedfd232009-03-26 19:07:15 +0000302 if (! p || ! took_lock)
303 return;
bart5357fcb2008-02-27 15:46:00 +0000304
bart74b2d972011-10-08 08:54:57 +0000305 if (p->recursion_count == 0) {
bartf6ec1fe2009-06-21 18:07:35 +0000306 if (p->owner != drd_tid && p->owner != DRD_INVALID_THREADID)
bartbedfd232009-03-26 19:07:15 +0000307 {
308 tl_assert(p->last_locked_segment);
bartf6ec1fe2009-06-21 18:07:35 +0000309
310 DRD_(thread_new_segment_and_combine_vc)(drd_tid,
311 p->last_locked_segment);
bartbedfd232009-03-26 19:07:15 +0000312 }
bartf6ec1fe2009-06-21 18:07:35 +0000313 else
314 DRD_(thread_new_segment)(drd_tid);
315
316 s_mutex_segment_creation_count++;
bart5bd9f2d2008-03-03 20:31:58 +0000317
bartbedfd232009-03-26 19:07:15 +0000318 p->owner = drd_tid;
319 p->acquiry_time_ms = VG_(read_millisecond_timer)();
320 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
321 s_mutex_lock_count++;
bart74b2d972011-10-08 08:54:57 +0000322 } else if (p->owner != drd_tid) {
323 const ThreadId vg_tid = VG_(get_running_tid)();
324 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
325 p->a1, p->recursion_count, p->owner };
326 VG_(maybe_record_error)(vg_tid,
327 MutexErr,
328 VG_(get_IP)(vg_tid),
329 "The impossible happened: mutex is locked"
330 " simultaneously by two threads",
331 &MEI);
bartbedfd232009-03-26 19:07:15 +0000332 p->owner = drd_tid;
333 }
334 p->recursion_count++;
sewardjaf44c822007-11-25 14:01:38 +0000335}
336
bartdc1ef032009-02-15 14:18:02 +0000337/**
338 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
bart9d5b7962008-05-14 12:25:00 +0000339 *
bart7e6de962009-02-21 09:39:09 +0000340 * @param[in] mutex Address of the client mutex.
341 * @param[in] mutex_type Mutex type.
bart9d5b7962008-05-14 12:25:00 +0000342 *
bartdc1ef032009-02-15 14:18:02 +0000343 * @return New value of the mutex recursion count.
bart9d5b7962008-05-14 12:25:00 +0000344 *
bartdc1ef032009-02-15 14:18:02 +0000345 * @note This function must be called before pthread_mutex_unlock() is called,
346 * or a race condition is triggered !
sewardjaf44c822007-11-25 14:01:38 +0000347 */
bartdc1ef032009-02-15 14:18:02 +0000348void DRD_(mutex_unlock)(const Addr mutex, MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000349{
bartbedfd232009-03-26 19:07:15 +0000350 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
351 const ThreadId vg_tid = VG_(get_running_tid)();
352 struct mutex_info* p;
bart3f4623e2008-07-07 16:53:07 +0000353
bartbedfd232009-03-26 19:07:15 +0000354 p = DRD_(mutex_get)(mutex);
bartadb7a202009-07-21 12:39:25 +0000355 if (p && mutex_type == mutex_type_unknown)
bartbedfd232009-03-26 19:07:15 +0000356 mutex_type = p->mutex_type;
sewardjaf44c822007-11-25 14:01:38 +0000357
bartb92ff0f2011-10-08 08:29:29 +0000358 if (s_trace_mutex) {
bartad994e82011-10-13 18:04:30 +0000359 DRD_(trace_msg)("[%d] mutex_unlock %s 0x%lx rc %d",
bartb92ff0f2011-10-08 08:29:29 +0000360 drd_tid, p ? DRD_(mutex_get_typename)(p) : "(?)",
361 mutex, p ? p->recursion_count : 0);
bartbedfd232009-03-26 19:07:15 +0000362 }
sewardjaf44c822007-11-25 14:01:38 +0000363
bartbedfd232009-03-26 19:07:15 +0000364 if (p == 0 || mutex_type == mutex_type_invalid_mutex)
365 {
366 DRD_(not_a_mutex)(mutex);
367 return;
368 }
bartab7a6442008-02-25 19:46:14 +0000369
bartbedfd232009-03-26 19:07:15 +0000370 if (p->owner == DRD_INVALID_THREADID)
371 {
bartd45d9952009-05-31 18:53:54 +0000372 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
373 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000374 VG_(maybe_record_error)(vg_tid,
375 MutexErr,
376 VG_(get_IP)(vg_tid),
377 "Mutex not locked",
378 &MEI);
379 return;
380 }
bart5357fcb2008-02-27 15:46:00 +0000381
bartbedfd232009-03-26 19:07:15 +0000382 tl_assert(p);
bart74b2d972011-10-08 08:54:57 +0000383 if (p->mutex_type != mutex_type) {
384 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
385 p->a1, p->recursion_count, p->owner };
386 VG_(maybe_record_error)(vg_tid, MutexErr, VG_(get_IP)(vg_tid),
387 "Mutex type changed", &MEI);
bartbedfd232009-03-26 19:07:15 +0000388 }
389 tl_assert(p->mutex_type == mutex_type);
390 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000391
bartbedfd232009-03-26 19:07:15 +0000392 if (p->owner != drd_tid || p->recursion_count <= 0)
393 {
bartd45d9952009-05-31 18:53:54 +0000394 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
395 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000396 VG_(maybe_record_error)(vg_tid,
397 MutexErr,
398 VG_(get_IP)(vg_tid),
399 "Mutex not locked by calling thread",
400 &MEI);
401 return;
402 }
403 tl_assert(p->recursion_count > 0);
404 p->recursion_count--;
405 tl_assert(p->recursion_count >= 0);
sewardj347eeba2008-01-21 14:19:07 +0000406
bartbedfd232009-03-26 19:07:15 +0000407 if (p->recursion_count == 0)
408 {
409 if (s_mutex_lock_threshold_ms > 0)
bart9d5b7962008-05-14 12:25:00 +0000410 {
bart430c45f2009-04-13 08:05:18 +0000411 Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
bartbedfd232009-03-26 19:07:15 +0000412 if (held > s_mutex_lock_threshold_ms)
413 {
414 HoldtimeErrInfo HEI
bartd45d9952009-05-31 18:53:54 +0000415 = { DRD_(thread_get_running_tid)(),
416 mutex, p->acquired_at, held, s_mutex_lock_threshold_ms };
bartbedfd232009-03-26 19:07:15 +0000417 VG_(maybe_record_error)(vg_tid,
418 HoldtimeErr,
419 VG_(get_IP)(vg_tid),
420 "mutex",
421 &HEI);
422 }
bart9d5b7962008-05-14 12:25:00 +0000423 }
bart9d5b7962008-05-14 12:25:00 +0000424
bartbedfd232009-03-26 19:07:15 +0000425 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
426 /* current vector clock of the thread such that it is available when */
427 /* this mutex is locked again. */
sewardjaf44c822007-11-25 14:01:38 +0000428
bartbedfd232009-03-26 19:07:15 +0000429 DRD_(thread_get_latest_segment)(&p->last_locked_segment, drd_tid);
430 DRD_(thread_new_segment)(drd_tid);
431 p->acquired_at = 0;
432 s_mutex_segment_creation_count++;
433 }
sewardjaf44c822007-11-25 14:01:38 +0000434}
435
bart3dbdc862009-02-14 12:14:50 +0000436void DRD_(spinlock_init_or_unlock)(const Addr spinlock)
437{
bartbedfd232009-03-26 19:07:15 +0000438 struct mutex_info* mutex_p = DRD_(mutex_get)(spinlock);
439 if (mutex_p)
440 {
441 DRD_(mutex_unlock)(spinlock, mutex_type_spinlock);
442 }
443 else
444 {
445 DRD_(mutex_init)(spinlock, mutex_type_spinlock);
446 }
bart3dbdc862009-02-14 12:14:50 +0000447}
448
bartdc1ef032009-02-15 14:18:02 +0000449const char* DRD_(mutex_get_typename)(struct mutex_info* const p)
sewardjaf44c822007-11-25 14:01:38 +0000450{
bartbedfd232009-03-26 19:07:15 +0000451 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000452
bartbedfd232009-03-26 19:07:15 +0000453 return DRD_(mutex_type_name)(p->mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000454}
455
bartdc1ef032009-02-15 14:18:02 +0000456const char* DRD_(mutex_type_name)(const MutexT mt)
sewardj347eeba2008-01-21 14:19:07 +0000457{
bartbedfd232009-03-26 19:07:15 +0000458 switch (mt)
459 {
bartadb7a202009-07-21 12:39:25 +0000460 case mutex_type_unknown:
461 return "mutex";
bartbedfd232009-03-26 19:07:15 +0000462 case mutex_type_invalid_mutex:
463 return "invalid mutex";
464 case mutex_type_recursive_mutex:
465 return "recursive mutex";
466 case mutex_type_errorcheck_mutex:
467 return "error checking mutex";
468 case mutex_type_default_mutex:
469 return "mutex";
470 case mutex_type_spinlock:
471 return "spinlock";
bartbedfd232009-03-26 19:07:15 +0000472 }
bart62cc2322010-03-07 10:54:21 +0000473 tl_assert(0);
bartbedfd232009-03-26 19:07:15 +0000474 return "?";
sewardjaf44c822007-11-25 14:01:38 +0000475}
476
bart5357fcb2008-02-27 15:46:00 +0000477/** Return true if the specified mutex is locked by any thread. */
bartd2c5eae2009-02-21 15:27:04 +0000478static Bool mutex_is_locked(struct mutex_info* const p)
bart5357fcb2008-02-27 15:46:00 +0000479{
bartbedfd232009-03-26 19:07:15 +0000480 tl_assert(p);
481 return (p->recursion_count > 0);
bart5357fcb2008-02-27 15:46:00 +0000482}
483
bartdc1ef032009-02-15 14:18:02 +0000484Bool DRD_(mutex_is_locked_by)(const Addr mutex, const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000485{
bartbedfd232009-03-26 19:07:15 +0000486 struct mutex_info* const p = DRD_(mutex_get)(mutex);
487 if (p)
488 {
489 return (p->recursion_count > 0 && p->owner == tid);
490 }
491 return False;
sewardjaf44c822007-11-25 14:01:38 +0000492}
493
bartdc1ef032009-02-15 14:18:02 +0000494int DRD_(mutex_get_recursion_count)(const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000495{
bartbedfd232009-03-26 19:07:15 +0000496 struct mutex_info* const p = DRD_(mutex_get)(mutex);
497 tl_assert(p);
498 return p->recursion_count;
sewardjaf44c822007-11-25 14:01:38 +0000499}
500
501/**
bart301c3112008-02-24 18:22:37 +0000502 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000503 * "last owner" field can be cleared if it still refers to that thread.
sewardjaf44c822007-11-25 14:01:38 +0000504 */
bartd2c5eae2009-02-21 15:27:04 +0000505static void mutex_delete_thread(struct mutex_info* p, const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000506{
bartbedfd232009-03-26 19:07:15 +0000507 tl_assert(p);
bart4bb53d82008-02-28 19:06:34 +0000508
bartbedfd232009-03-26 19:07:15 +0000509 if (p->owner == tid && p->recursion_count > 0)
510 {
bartd45d9952009-05-31 18:53:54 +0000511 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
512 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000513 VG_(maybe_record_error)(VG_(get_running_tid)(),
514 MutexErr,
515 VG_(get_IP)(VG_(get_running_tid)()),
516 "Mutex still locked at thread exit",
517 &MEI);
518 p->owner = VG_INVALID_THREADID;
519 }
sewardjaf44c822007-11-25 14:01:38 +0000520}
521
bartdc1ef032009-02-15 14:18:02 +0000522ULong DRD_(get_mutex_lock_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000523{
bartbedfd232009-03-26 19:07:15 +0000524 return s_mutex_lock_count;
sewardjaf44c822007-11-25 14:01:38 +0000525}
bart6bbefaf2008-04-19 15:16:45 +0000526
bartdc1ef032009-02-15 14:18:02 +0000527ULong DRD_(get_mutex_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000528{
bartbedfd232009-03-26 19:07:15 +0000529 return s_mutex_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000530}