blob: cf5a4a86ec1b3a54b208c62e9e2f51e59bb8cca1 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
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
bart3dbdc862009-02-14 12:14:50 +00005 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
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"
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
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;
52static UInt s_mutex_lock_threshold_ms = 1000 * 1000;
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)
92 {
93 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +000094 "[%d] mutex_destroy %s 0x%lx rc %d owner %d\n",
bartbedfd232009-03-26 19:07:15 +000095 DRD_(thread_get_running_tid)(),
96 DRD_(mutex_get_typename)(p),
97 p->a1,
98 p ? p->recursion_count : -1,
99 p ? p->owner : DRD_INVALID_THREADID);
100 }
bartb78312c2008-02-29 11:00:17 +0000101
bartbedfd232009-03-26 19:07:15 +0000102 if (mutex_is_locked(p))
103 {
bartd45d9952009-05-31 18:53:54 +0000104 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
105 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000106 VG_(maybe_record_error)(VG_(get_running_tid)(),
107 MutexErr,
108 VG_(get_IP)(VG_(get_running_tid)()),
109 "Destroying locked mutex",
110 &MEI);
111 }
bart46d5f172008-02-28 19:49:37 +0000112
bartbedfd232009-03-26 19:07:15 +0000113 DRD_(sg_put)(p->last_locked_segment);
114 p->last_locked_segment = 0;
bart46d5f172008-02-28 19:49:37 +0000115}
116
bart886b87c2008-06-28 13:40:41 +0000117/** Let Valgrind report that there is no mutex object at address 'mutex'. */
bartdc1ef032009-02-15 14:18:02 +0000118void DRD_(not_a_mutex)(const Addr mutex)
bart6b717612008-03-24 09:29:38 +0000119{
bartd45d9952009-05-31 18:53:54 +0000120 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
121 mutex, -1, DRD_INVALID_THREADID };
bartbedfd232009-03-26 19:07:15 +0000122 VG_(maybe_record_error)(VG_(get_running_tid)(),
123 MutexErr,
124 VG_(get_IP)(VG_(get_running_tid)()),
125 "Not a mutex",
126 &MEI);
bart6b717612008-03-24 09:29:38 +0000127}
128
sewardjaf44c822007-11-25 14:01:38 +0000129static
sewardj721ad7b2007-11-30 08:30:29 +0000130struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000131DRD_(mutex_get_or_allocate)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000132{
bartbedfd232009-03-26 19:07:15 +0000133 struct mutex_info* p;
sewardj721ad7b2007-11-30 08:30:29 +0000134
bartbedfd232009-03-26 19:07:15 +0000135 tl_assert(offsetof(DrdClientobj, mutex) == 0);
136 p = &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
137 if (p)
138 {
139 return p;
140 }
sewardj721ad7b2007-11-30 08:30:29 +0000141
bartbedfd232009-03-26 19:07:15 +0000142 if (DRD_(clientobj_present)(mutex, mutex + 1))
143 {
144 DRD_(not_a_mutex)(mutex);
145 return 0;
146 }
bart4bb53d82008-02-28 19:06:34 +0000147
bartbedfd232009-03-26 19:07:15 +0000148 p = &(DRD_(clientobj_add)(mutex, ClientMutex)->mutex);
149 DRD_(mutex_initialize)(p, mutex, mutex_type);
150 return p;
sewardjaf44c822007-11-25 14:01:38 +0000151}
152
bartdc1ef032009-02-15 14:18:02 +0000153struct mutex_info* DRD_(mutex_get)(const Addr mutex)
bart3b1ee452008-02-29 19:28:15 +0000154{
bartbedfd232009-03-26 19:07:15 +0000155 tl_assert(offsetof(DrdClientobj, mutex) == 0);
156 return &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
bart3b1ee452008-02-29 19:28:15 +0000157}
158
bart00344642008-03-01 15:27:41 +0000159/** Called before pthread_mutex_init(). */
sewardj721ad7b2007-11-30 08:30:29 +0000160struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000161DRD_(mutex_init)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000162{
bartbedfd232009-03-26 19:07:15 +0000163 struct mutex_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000164
bartbedfd232009-03-26 19:07:15 +0000165 if (s_trace_mutex)
166 {
167 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000168 "[%d] mutex_init %s 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000169 DRD_(thread_get_running_tid)(),
170 DRD_(mutex_type_name)(mutex_type),
171 mutex);
172 }
sewardjaf44c822007-11-25 14:01:38 +0000173
bartbedfd232009-03-26 19:07:15 +0000174 if (mutex_type == mutex_type_invalid_mutex)
175 {
176 DRD_(not_a_mutex)(mutex);
177 return 0;
178 }
bart00344642008-03-01 15:27:41 +0000179
bartbedfd232009-03-26 19:07:15 +0000180 p = DRD_(mutex_get)(mutex);
181 if (p)
182 {
183 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000184 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
185 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000186 VG_(maybe_record_error)(vg_tid,
187 MutexErr,
188 VG_(get_IP)(vg_tid),
189 "Mutex reinitialization",
190 &MEI);
bartfa358282009-05-16 06:22:46 +0000191 p->mutex_type = mutex_type;
bartbedfd232009-03-26 19:07:15 +0000192 return p;
193 }
194 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000195
bartbedfd232009-03-26 19:07:15 +0000196 return p;
sewardjaf44c822007-11-25 14:01:38 +0000197}
198
bart46d5f172008-02-28 19:49:37 +0000199/** Called after pthread_mutex_destroy(). */
bartdc1ef032009-02-15 14:18:02 +0000200void DRD_(mutex_post_destroy)(const Addr mutex)
sewardj347eeba2008-01-21 14:19:07 +0000201{
bartbedfd232009-03-26 19:07:15 +0000202 struct mutex_info* p;
sewardj347eeba2008-01-21 14:19:07 +0000203
bartbedfd232009-03-26 19:07:15 +0000204 p = DRD_(mutex_get)(mutex);
205 if (p == 0)
206 {
207 DRD_(not_a_mutex)(mutex);
208 return;
209 }
bart72b751c2008-03-01 13:44:24 +0000210
bartbedfd232009-03-26 19:07:15 +0000211 DRD_(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 */
bartdc1ef032009-02-15 14:18:02 +0000219void DRD_(mutex_pre_lock)(const Addr mutex, MutexT mutex_type,
220 const Bool trylock)
bart8bba1f72008-02-27 16:13:05 +0000221{
bartbedfd232009-03-26 19:07:15 +0000222 struct mutex_info* p;
bart635cb162008-02-28 08:30:43 +0000223
bartbedfd232009-03-26 19:07:15 +0000224 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
225 if (mutex_type == mutex_type_unknown)
226 mutex_type = p->mutex_type;
bart3f4623e2008-07-07 16:53:07 +0000227
bartbedfd232009-03-26 19:07:15 +0000228 if (s_trace_mutex)
229 {
230 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000231 "[%d] %s %s 0x%lx rc %d owner %d\n",
bartbedfd232009-03-26 19:07:15 +0000232 DRD_(thread_get_running_tid)(),
233 trylock ? "pre_mutex_lock " : "mutex_trylock ",
234 p ? DRD_(mutex_get_typename)(p) : "(?)",
235 mutex,
236 p ? p->recursion_count : -1,
237 p ? p->owner : DRD_INVALID_THREADID);
238 }
bart00344642008-03-01 15:27:41 +0000239
bartbedfd232009-03-26 19:07:15 +0000240 if (p == 0)
241 {
242 DRD_(not_a_mutex)(mutex);
243 return;
244 }
bart2e3a3c12008-03-24 08:33:47 +0000245
bartbedfd232009-03-26 19:07:15 +0000246 tl_assert(p);
bart2e3a3c12008-03-24 08:33:47 +0000247
bartbedfd232009-03-26 19:07:15 +0000248 if (mutex_type == mutex_type_invalid_mutex)
249 {
250 DRD_(not_a_mutex)(mutex);
251 return;
252 }
bart00344642008-03-01 15:27:41 +0000253
bartbedfd232009-03-26 19:07:15 +0000254 if (! trylock
255 && p->owner == DRD_(thread_get_running_tid)()
256 && p->recursion_count >= 1
257 && mutex_type != mutex_type_recursive_mutex)
258 {
bartd45d9952009-05-31 18:53:54 +0000259 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
260 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +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 }
bart8bba1f72008-02-27 16:13:05 +0000267}
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 */
bartdc1ef032009-02-15 14:18:02 +0000274void DRD_(mutex_post_lock)(const Addr mutex, const Bool took_lock,
275 const Bool post_cond_wait)
sewardjaf44c822007-11-25 14:01:38 +0000276{
bartbedfd232009-03-26 19:07:15 +0000277 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
278 struct mutex_info* p;
bart00344642008-03-01 15:27:41 +0000279
bartbedfd232009-03-26 19:07:15 +0000280 p = DRD_(mutex_get)(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000281
bartbedfd232009-03-26 19:07:15 +0000282 if (s_trace_mutex)
283 {
284 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000285 "[%d] %s %s 0x%lx rc %d owner %d%s\n",
bartbedfd232009-03-26 19:07:15 +0000286 drd_tid,
287 post_cond_wait ? "cond_post_wait " : "post_mutex_lock",
288 p ? DRD_(mutex_get_typename)(p) : "(?)",
289 mutex,
290 p ? p->recursion_count : 0,
291 p ? p->owner : VG_INVALID_THREADID,
292 took_lock ? "" : " (locking failed)");
293 }
sewardjaf44c822007-11-25 14:01:38 +0000294
bartbedfd232009-03-26 19:07:15 +0000295 if (! p || ! took_lock)
296 return;
bart5357fcb2008-02-27 15:46:00 +0000297
bartbedfd232009-03-26 19:07:15 +0000298 if (p->recursion_count == 0)
299 {
bartf6ec1fe2009-06-21 18:07:35 +0000300 if (p->owner != drd_tid && p->owner != DRD_INVALID_THREADID)
bartbedfd232009-03-26 19:07:15 +0000301 {
302 tl_assert(p->last_locked_segment);
bartf6ec1fe2009-06-21 18:07:35 +0000303
304 DRD_(thread_new_segment_and_combine_vc)(drd_tid,
305 p->last_locked_segment);
bartbedfd232009-03-26 19:07:15 +0000306 }
bartf6ec1fe2009-06-21 18:07:35 +0000307 else
308 DRD_(thread_new_segment)(drd_tid);
309
310 s_mutex_segment_creation_count++;
bart5bd9f2d2008-03-03 20:31:58 +0000311
bartbedfd232009-03-26 19:07:15 +0000312 p->owner = drd_tid;
313 p->acquiry_time_ms = VG_(read_millisecond_timer)();
314 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
315 s_mutex_lock_count++;
316 }
317 else if (p->owner != drd_tid)
318 {
319 VG_(message)(Vg_UserMsg,
320 "The impossible happened: mutex 0x%lx is locked"
321 " simultaneously by two threads (recursion count %d,"
sewardj1e29ebc2009-07-15 14:49:17 +0000322 " owners %d and %d) !\n",
bartbedfd232009-03-26 19:07:15 +0000323 p->a1, p->recursion_count, p->owner, drd_tid);
324 p->owner = drd_tid;
325 }
326 p->recursion_count++;
sewardjaf44c822007-11-25 14:01:38 +0000327}
328
bartdc1ef032009-02-15 14:18:02 +0000329/**
330 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
bart9d5b7962008-05-14 12:25:00 +0000331 *
bart7e6de962009-02-21 09:39:09 +0000332 * @param[in] mutex Address of the client mutex.
333 * @param[in] mutex_type Mutex type.
bart9d5b7962008-05-14 12:25:00 +0000334 *
bartdc1ef032009-02-15 14:18:02 +0000335 * @return New value of the mutex recursion count.
bart9d5b7962008-05-14 12:25:00 +0000336 *
bartdc1ef032009-02-15 14:18:02 +0000337 * @note This function must be called before pthread_mutex_unlock() is called,
338 * or a race condition is triggered !
sewardjaf44c822007-11-25 14:01:38 +0000339 */
bartdc1ef032009-02-15 14:18:02 +0000340void DRD_(mutex_unlock)(const Addr mutex, MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000341{
bartbedfd232009-03-26 19:07:15 +0000342 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
343 const ThreadId vg_tid = VG_(get_running_tid)();
344 struct mutex_info* p;
bart3f4623e2008-07-07 16:53:07 +0000345
bartbedfd232009-03-26 19:07:15 +0000346 p = DRD_(mutex_get)(mutex);
bartadb7a202009-07-21 12:39:25 +0000347 if (p && mutex_type == mutex_type_unknown)
bartbedfd232009-03-26 19:07:15 +0000348 mutex_type = p->mutex_type;
sewardjaf44c822007-11-25 14:01:38 +0000349
bartbedfd232009-03-26 19:07:15 +0000350 if (s_trace_mutex)
351 {
352 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000353 "[%d] mutex_unlock %s 0x%lx rc %d\n",
bartbedfd232009-03-26 19:07:15 +0000354 drd_tid,
355 p ? DRD_(mutex_get_typename)(p) : "(?)",
356 mutex,
357 p ? p->recursion_count : 0);
358 }
sewardjaf44c822007-11-25 14:01:38 +0000359
bartbedfd232009-03-26 19:07:15 +0000360 if (p == 0 || mutex_type == mutex_type_invalid_mutex)
361 {
362 DRD_(not_a_mutex)(mutex);
363 return;
364 }
bartab7a6442008-02-25 19:46:14 +0000365
bartbedfd232009-03-26 19:07:15 +0000366 if (p->owner == DRD_INVALID_THREADID)
367 {
bartd45d9952009-05-31 18:53:54 +0000368 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
369 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000370 VG_(maybe_record_error)(vg_tid,
371 MutexErr,
372 VG_(get_IP)(vg_tid),
373 "Mutex not locked",
374 &MEI);
375 return;
376 }
bart5357fcb2008-02-27 15:46:00 +0000377
bartbedfd232009-03-26 19:07:15 +0000378 tl_assert(p);
379 if (p->mutex_type != mutex_type)
380 {
sewardj1e29ebc2009-07-15 14:49:17 +0000381 VG_(message)(Vg_UserMsg, "??? mutex 0x%lx: type changed from %d into %d\n",
bartbedfd232009-03-26 19:07:15 +0000382 p->a1, p->mutex_type, mutex_type);
383 }
384 tl_assert(p->mutex_type == mutex_type);
385 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000386
bartbedfd232009-03-26 19:07:15 +0000387 if (p->owner != drd_tid || p->recursion_count <= 0)
388 {
bartd45d9952009-05-31 18:53:54 +0000389 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
390 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000391 VG_(maybe_record_error)(vg_tid,
392 MutexErr,
393 VG_(get_IP)(vg_tid),
394 "Mutex not locked by calling thread",
395 &MEI);
396 return;
397 }
398 tl_assert(p->recursion_count > 0);
399 p->recursion_count--;
400 tl_assert(p->recursion_count >= 0);
sewardj347eeba2008-01-21 14:19:07 +0000401
bartbedfd232009-03-26 19:07:15 +0000402 if (p->recursion_count == 0)
403 {
404 if (s_mutex_lock_threshold_ms > 0)
bart9d5b7962008-05-14 12:25:00 +0000405 {
bart430c45f2009-04-13 08:05:18 +0000406 Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
bartbedfd232009-03-26 19:07:15 +0000407 if (held > s_mutex_lock_threshold_ms)
408 {
409 HoldtimeErrInfo HEI
bartd45d9952009-05-31 18:53:54 +0000410 = { DRD_(thread_get_running_tid)(),
411 mutex, p->acquired_at, held, s_mutex_lock_threshold_ms };
bartbedfd232009-03-26 19:07:15 +0000412 VG_(maybe_record_error)(vg_tid,
413 HoldtimeErr,
414 VG_(get_IP)(vg_tid),
415 "mutex",
416 &HEI);
417 }
bart9d5b7962008-05-14 12:25:00 +0000418 }
bart9d5b7962008-05-14 12:25:00 +0000419
bartbedfd232009-03-26 19:07:15 +0000420 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
421 /* current vector clock of the thread such that it is available when */
422 /* this mutex is locked again. */
sewardjaf44c822007-11-25 14:01:38 +0000423
bartbedfd232009-03-26 19:07:15 +0000424 DRD_(thread_get_latest_segment)(&p->last_locked_segment, drd_tid);
425 DRD_(thread_new_segment)(drd_tid);
426 p->acquired_at = 0;
427 s_mutex_segment_creation_count++;
428 }
sewardjaf44c822007-11-25 14:01:38 +0000429}
430
bart3dbdc862009-02-14 12:14:50 +0000431void DRD_(spinlock_init_or_unlock)(const Addr spinlock)
432{
bartbedfd232009-03-26 19:07:15 +0000433 struct mutex_info* mutex_p = DRD_(mutex_get)(spinlock);
434 if (mutex_p)
435 {
436 DRD_(mutex_unlock)(spinlock, mutex_type_spinlock);
437 }
438 else
439 {
440 DRD_(mutex_init)(spinlock, mutex_type_spinlock);
441 }
bart3dbdc862009-02-14 12:14:50 +0000442}
443
bartdc1ef032009-02-15 14:18:02 +0000444const char* DRD_(mutex_get_typename)(struct mutex_info* const p)
sewardjaf44c822007-11-25 14:01:38 +0000445{
bartbedfd232009-03-26 19:07:15 +0000446 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000447
bartbedfd232009-03-26 19:07:15 +0000448 return DRD_(mutex_type_name)(p->mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000449}
450
bartdc1ef032009-02-15 14:18:02 +0000451const char* DRD_(mutex_type_name)(const MutexT mt)
sewardj347eeba2008-01-21 14:19:07 +0000452{
bartbedfd232009-03-26 19:07:15 +0000453 switch (mt)
454 {
bartadb7a202009-07-21 12:39:25 +0000455 case mutex_type_unknown:
456 return "mutex";
bartbedfd232009-03-26 19:07:15 +0000457 case mutex_type_invalid_mutex:
458 return "invalid mutex";
459 case mutex_type_recursive_mutex:
460 return "recursive mutex";
461 case mutex_type_errorcheck_mutex:
462 return "error checking mutex";
463 case mutex_type_default_mutex:
464 return "mutex";
465 case mutex_type_spinlock:
466 return "spinlock";
bart52b7d8a2009-10-20 18:13:26 +0000467 case mutex_type_order_annotation:
468 return "order annotation mutex";
bartbedfd232009-03-26 19:07:15 +0000469 default:
470 tl_assert(0);
471 }
472 return "?";
sewardjaf44c822007-11-25 14:01:38 +0000473}
474
bart5357fcb2008-02-27 15:46:00 +0000475/** Return true if the specified mutex is locked by any thread. */
bartd2c5eae2009-02-21 15:27:04 +0000476static Bool mutex_is_locked(struct mutex_info* const p)
bart5357fcb2008-02-27 15:46:00 +0000477{
bartbedfd232009-03-26 19:07:15 +0000478 tl_assert(p);
479 return (p->recursion_count > 0);
bart5357fcb2008-02-27 15:46:00 +0000480}
481
bartdc1ef032009-02-15 14:18:02 +0000482Bool DRD_(mutex_is_locked_by)(const Addr mutex, const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000483{
bartbedfd232009-03-26 19:07:15 +0000484 struct mutex_info* const p = DRD_(mutex_get)(mutex);
485 if (p)
486 {
487 return (p->recursion_count > 0 && p->owner == tid);
488 }
489 return False;
sewardjaf44c822007-11-25 14:01:38 +0000490}
491
bartdc1ef032009-02-15 14:18:02 +0000492int DRD_(mutex_get_recursion_count)(const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000493{
bartbedfd232009-03-26 19:07:15 +0000494 struct mutex_info* const p = DRD_(mutex_get)(mutex);
495 tl_assert(p);
496 return p->recursion_count;
sewardjaf44c822007-11-25 14:01:38 +0000497}
498
499/**
bart301c3112008-02-24 18:22:37 +0000500 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000501 * "last owner" field can be cleared if it still refers to that thread.
sewardjaf44c822007-11-25 14:01:38 +0000502 */
bartd2c5eae2009-02-21 15:27:04 +0000503static void mutex_delete_thread(struct mutex_info* p, const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000504{
bartbedfd232009-03-26 19:07:15 +0000505 tl_assert(p);
bart4bb53d82008-02-28 19:06:34 +0000506
bartbedfd232009-03-26 19:07:15 +0000507 if (p->owner == tid && p->recursion_count > 0)
508 {
bartd45d9952009-05-31 18:53:54 +0000509 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
510 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000511 VG_(maybe_record_error)(VG_(get_running_tid)(),
512 MutexErr,
513 VG_(get_IP)(VG_(get_running_tid)()),
514 "Mutex still locked at thread exit",
515 &MEI);
516 p->owner = VG_INVALID_THREADID;
517 }
sewardjaf44c822007-11-25 14:01:38 +0000518}
519
bartdc1ef032009-02-15 14:18:02 +0000520ULong DRD_(get_mutex_lock_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000521{
bartbedfd232009-03-26 19:07:15 +0000522 return s_mutex_lock_count;
sewardjaf44c822007-11-25 14:01:38 +0000523}
bart6bbefaf2008-04-19 15:16:45 +0000524
bartdc1ef032009-02-15 14:18:02 +0000525ULong DRD_(get_mutex_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000526{
bartbedfd232009-03-26 19:07:15 +0000527 return s_mutex_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000528}