blob: 4c836fde09fef3a617096187d2716caad21edad3 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
bartd4bab992013-10-04 05:55:30 +00004 Copyright (C) 2006-2013 Bart Van Assche <bvanassche@acm.org>.
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"
bart74b2d972011-10-08 08:54:57 +000030#include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
31#include "pub_tool_libcassert.h" /* tl_assert() */
32#include "pub_tool_libcbase.h" /* VG_(strlen) */
33#include "pub_tool_libcprint.h" /* VG_(message)() */
34#include "pub_tool_libcproc.h" /* VG_(read_millisecond_timer)() */
35#include "pub_tool_machine.h" /* VG_(get_IP)() */
36#include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
sewardjaf44c822007-11-25 14:01:38 +000037
38
bartdc1ef032009-02-15 14:18:02 +000039/* Local functions. */
sewardj347eeba2008-01-21 14:19:07 +000040
bartd2c5eae2009-02-21 15:27:04 +000041static void mutex_cleanup(struct mutex_info* p);
42static Bool mutex_is_locked(struct mutex_info* const p);
43static void mutex_delete_thread(struct mutex_info* p, const DrdThreadId tid);
sewardj347eeba2008-01-21 14:19:07 +000044
45
bartdc1ef032009-02-15 14:18:02 +000046/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000047
bartd2c5eae2009-02-21 15:27:04 +000048static Bool s_trace_mutex;
49static ULong s_mutex_lock_count;
50static ULong s_mutex_segment_creation_count;
bart8600c422010-10-25 18:18:54 +000051static UInt s_mutex_lock_threshold_ms;
sewardjaf44c822007-11-25 14:01:38 +000052
53
bartdc1ef032009-02-15 14:18:02 +000054/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000055
bartdc1ef032009-02-15 14:18:02 +000056void DRD_(mutex_set_trace)(const Bool trace_mutex)
sewardjaf44c822007-11-25 14:01:38 +000057{
bartbedfd232009-03-26 19:07:15 +000058 tl_assert(!! trace_mutex == trace_mutex);
59 s_trace_mutex = trace_mutex;
sewardjaf44c822007-11-25 14:01:38 +000060}
61
bartdc1ef032009-02-15 14:18:02 +000062void DRD_(mutex_set_lock_threshold)(const UInt lock_threshold_ms)
bart9d5b7962008-05-14 12:25:00 +000063{
bartbedfd232009-03-26 19:07:15 +000064 s_mutex_lock_threshold_ms = lock_threshold_ms;
bart9d5b7962008-05-14 12:25:00 +000065}
66
sewardjaf44c822007-11-25 14:01:38 +000067static
bartdc1ef032009-02-15 14:18:02 +000068void DRD_(mutex_initialize)(struct mutex_info* const p,
69 const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000070{
bartbedfd232009-03-26 19:07:15 +000071 tl_assert(mutex);
bartbedfd232009-03-26 19:07:15 +000072 tl_assert(p->a1 == mutex);
bart3f4623e2008-07-07 16:53:07 +000073
bartbedfd232009-03-26 19:07:15 +000074 p->cleanup = (void(*)(DrdClientobj*))mutex_cleanup;
75 p->delete_thread
76 = (void(*)(DrdClientobj*, DrdThreadId))mutex_delete_thread;
77 p->mutex_type = mutex_type;
78 p->recursion_count = 0;
79 p->owner = DRD_INVALID_THREADID;
80 p->last_locked_segment = 0;
81 p->acquiry_time_ms = 0;
82 p->acquired_at = 0;
sewardjaf44c822007-11-25 14:01:38 +000083}
84
bart46d5f172008-02-28 19:49:37 +000085/** Deallocate the memory that was allocated by mutex_initialize(). */
bartd2c5eae2009-02-21 15:27:04 +000086static void mutex_cleanup(struct mutex_info* p)
bart46d5f172008-02-28 19:49:37 +000087{
bartbedfd232009-03-26 19:07:15 +000088 tl_assert(p);
bart6b717612008-03-24 09:29:38 +000089
bartbedfd232009-03-26 19:07:15 +000090 if (s_trace_mutex)
bartad994e82011-10-13 18:04:30 +000091 DRD_(trace_msg)("[%d] mutex_destroy %s 0x%lx rc %d owner %d",
bartb92ff0f2011-10-08 08:29:29 +000092 DRD_(thread_get_running_tid)(),
93 DRD_(mutex_get_typename)(p), p->a1,
94 p ? p->recursion_count : -1,
95 p ? p->owner : DRD_INVALID_THREADID);
bartb78312c2008-02-29 11:00:17 +000096
bartbedfd232009-03-26 19:07:15 +000097 if (mutex_is_locked(p))
98 {
bartd45d9952009-05-31 18:53:54 +000099 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
100 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000101 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 }
bart46d5f172008-02-28 19:49:37 +0000107
bartbedfd232009-03-26 19:07:15 +0000108 DRD_(sg_put)(p->last_locked_segment);
109 p->last_locked_segment = 0;
bart46d5f172008-02-28 19:49:37 +0000110}
111
bart62cc2322010-03-07 10:54:21 +0000112/** Report that address 'mutex' is not the address of a mutex object. */
bartdc1ef032009-02-15 14:18:02 +0000113void DRD_(not_a_mutex)(const Addr mutex)
bart6b717612008-03-24 09:29:38 +0000114{
bartd45d9952009-05-31 18:53:54 +0000115 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
116 mutex, -1, DRD_INVALID_THREADID };
bartbedfd232009-03-26 19:07:15 +0000117 VG_(maybe_record_error)(VG_(get_running_tid)(),
118 MutexErr,
119 VG_(get_IP)(VG_(get_running_tid)()),
120 "Not a mutex",
121 &MEI);
bart6b717612008-03-24 09:29:38 +0000122}
123
bart62cc2322010-03-07 10:54:21 +0000124/**
125 * Report that address 'mutex' is not the address of a mutex object of the
126 * expected type.
127 */
128static void wrong_mutex_type(const Addr mutex)
129{
130 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
131 mutex, -1, DRD_INVALID_THREADID };
132 VG_(maybe_record_error)(VG_(get_running_tid)(),
133 MutexErr,
134 VG_(get_IP)(VG_(get_running_tid)()),
135 "Mutex type mismatch",
136 &MEI);
137}
138
sewardjaf44c822007-11-25 14:01:38 +0000139static
sewardj721ad7b2007-11-30 08:30:29 +0000140struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000141DRD_(mutex_get_or_allocate)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000142{
bartbedfd232009-03-26 19:07:15 +0000143 struct mutex_info* p;
sewardj721ad7b2007-11-30 08:30:29 +0000144
bartbedfd232009-03-26 19:07:15 +0000145 tl_assert(offsetof(DrdClientobj, mutex) == 0);
146 p = &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
147 if (p)
148 {
bart62cc2322010-03-07 10:54:21 +0000149 if (mutex_type == mutex_type_unknown || p->mutex_type == mutex_type)
150 return p;
151 else
152 {
153 wrong_mutex_type(mutex);
154 return 0;
155 }
bartbedfd232009-03-26 19:07:15 +0000156 }
sewardj721ad7b2007-11-30 08:30:29 +0000157
bartbedfd232009-03-26 19:07:15 +0000158 if (DRD_(clientobj_present)(mutex, mutex + 1))
159 {
160 DRD_(not_a_mutex)(mutex);
161 return 0;
162 }
bart4bb53d82008-02-28 19:06:34 +0000163
bartbedfd232009-03-26 19:07:15 +0000164 p = &(DRD_(clientobj_add)(mutex, ClientMutex)->mutex);
165 DRD_(mutex_initialize)(p, mutex, mutex_type);
166 return p;
sewardjaf44c822007-11-25 14:01:38 +0000167}
168
bartdc1ef032009-02-15 14:18:02 +0000169struct mutex_info* DRD_(mutex_get)(const Addr mutex)
bart3b1ee452008-02-29 19:28:15 +0000170{
bartbedfd232009-03-26 19:07:15 +0000171 tl_assert(offsetof(DrdClientobj, mutex) == 0);
172 return &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
bart3b1ee452008-02-29 19:28:15 +0000173}
174
bart00344642008-03-01 15:27:41 +0000175/** Called before pthread_mutex_init(). */
sewardj721ad7b2007-11-30 08:30:29 +0000176struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000177DRD_(mutex_init)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000178{
bartbedfd232009-03-26 19:07:15 +0000179 struct mutex_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000180
bartbedfd232009-03-26 19:07:15 +0000181 if (s_trace_mutex)
bartad994e82011-10-13 18:04:30 +0000182 DRD_(trace_msg)("[%d] mutex_init %s 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000183 DRD_(thread_get_running_tid)(),
184 DRD_(mutex_type_name)(mutex_type),
185 mutex);
sewardjaf44c822007-11-25 14:01:38 +0000186
bartbedfd232009-03-26 19:07:15 +0000187 if (mutex_type == mutex_type_invalid_mutex)
188 {
189 DRD_(not_a_mutex)(mutex);
190 return 0;
191 }
bart00344642008-03-01 15:27:41 +0000192
bartbedfd232009-03-26 19:07:15 +0000193 p = DRD_(mutex_get)(mutex);
194 if (p)
195 {
196 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000197 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
198 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000199 VG_(maybe_record_error)(vg_tid,
200 MutexErr,
201 VG_(get_IP)(vg_tid),
202 "Mutex reinitialization",
203 &MEI);
bartfa358282009-05-16 06:22:46 +0000204 p->mutex_type = mutex_type;
bartbedfd232009-03-26 19:07:15 +0000205 return p;
206 }
207 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000208
bartbedfd232009-03-26 19:07:15 +0000209 return p;
sewardjaf44c822007-11-25 14:01:38 +0000210}
211
bart46d5f172008-02-28 19:49:37 +0000212/** Called after pthread_mutex_destroy(). */
bartdc1ef032009-02-15 14:18:02 +0000213void DRD_(mutex_post_destroy)(const Addr mutex)
sewardj347eeba2008-01-21 14:19:07 +0000214{
bartbedfd232009-03-26 19:07:15 +0000215 struct mutex_info* p;
sewardj347eeba2008-01-21 14:19:07 +0000216
bartbedfd232009-03-26 19:07:15 +0000217 p = DRD_(mutex_get)(mutex);
218 if (p == 0)
219 {
220 DRD_(not_a_mutex)(mutex);
221 return;
222 }
bart72b751c2008-03-01 13:44:24 +0000223
bartbedfd232009-03-26 19:07:15 +0000224 DRD_(clientobj_remove)(mutex, ClientMutex);
sewardj347eeba2008-01-21 14:19:07 +0000225}
226
bart62cc2322010-03-07 10:54:21 +0000227/**
228 * Called before pthread_mutex_lock() is invoked. If a data structure for the
229 * client-side object was not yet created, do this now. Also check whether an
230 * attempt is made to lock recursively a synchronization object that must not
231 * be locked recursively.
bart8bba1f72008-02-27 16:13:05 +0000232 */
bartdc1ef032009-02-15 14:18:02 +0000233void DRD_(mutex_pre_lock)(const Addr mutex, MutexT mutex_type,
234 const Bool trylock)
bart8bba1f72008-02-27 16:13:05 +0000235{
bartbedfd232009-03-26 19:07:15 +0000236 struct mutex_info* p;
bart635cb162008-02-28 08:30:43 +0000237
bartbedfd232009-03-26 19:07:15 +0000238 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
bart62cc2322010-03-07 10:54:21 +0000239 if (p && mutex_type == mutex_type_unknown)
bartbedfd232009-03-26 19:07:15 +0000240 mutex_type = p->mutex_type;
bart3f4623e2008-07-07 16:53:07 +0000241
bartbedfd232009-03-26 19:07:15 +0000242 if (s_trace_mutex)
bartad994e82011-10-13 18:04:30 +0000243 DRD_(trace_msg)("[%d] %s %s 0x%lx rc %d owner %d",
bartb92ff0f2011-10-08 08:29:29 +0000244 DRD_(thread_get_running_tid)(),
245 trylock ? "pre_mutex_lock " : "mutex_trylock ",
246 p ? DRD_(mutex_get_typename)(p) : "(?)",
247 mutex, p ? p->recursion_count : -1,
248 p ? p->owner : DRD_INVALID_THREADID);
bart00344642008-03-01 15:27:41 +0000249
bartbedfd232009-03-26 19:07:15 +0000250 if (p == 0)
251 {
252 DRD_(not_a_mutex)(mutex);
253 return;
254 }
bart2e3a3c12008-03-24 08:33:47 +0000255
bartbedfd232009-03-26 19:07:15 +0000256 tl_assert(p);
bart2e3a3c12008-03-24 08:33:47 +0000257
bartbedfd232009-03-26 19:07:15 +0000258 if (mutex_type == mutex_type_invalid_mutex)
259 {
260 DRD_(not_a_mutex)(mutex);
261 return;
262 }
bart00344642008-03-01 15:27:41 +0000263
bartbedfd232009-03-26 19:07:15 +0000264 if (! trylock
265 && p->owner == DRD_(thread_get_running_tid)()
266 && p->recursion_count >= 1
267 && mutex_type != mutex_type_recursive_mutex)
268 {
bartd45d9952009-05-31 18:53:54 +0000269 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
270 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000271 VG_(maybe_record_error)(VG_(get_running_tid)(),
272 MutexErr,
273 VG_(get_IP)(VG_(get_running_tid)()),
274 "Recursive locking not allowed",
275 &MEI);
276 }
bart8bba1f72008-02-27 16:13:05 +0000277}
278
sewardjaf44c822007-11-25 14:01:38 +0000279/**
280 * Update mutex_info state when locking the pthread_mutex_t mutex.
281 * Note: this function must be called after pthread_mutex_lock() has been
282 * called, or a race condition is triggered !
283 */
bartdc1ef032009-02-15 14:18:02 +0000284void DRD_(mutex_post_lock)(const Addr mutex, const Bool took_lock,
285 const Bool post_cond_wait)
sewardjaf44c822007-11-25 14:01:38 +0000286{
bartbedfd232009-03-26 19:07:15 +0000287 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
288 struct mutex_info* p;
bart00344642008-03-01 15:27:41 +0000289
bartbedfd232009-03-26 19:07:15 +0000290 p = DRD_(mutex_get)(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000291
bartbedfd232009-03-26 19:07:15 +0000292 if (s_trace_mutex)
bartad994e82011-10-13 18:04:30 +0000293 DRD_(trace_msg)("[%d] %s %s 0x%lx rc %d owner %d%s",
bartb92ff0f2011-10-08 08:29:29 +0000294 drd_tid,
295 post_cond_wait ? "cond_post_wait " : "post_mutex_lock",
296 p ? DRD_(mutex_get_typename)(p) : "(?)",
297 mutex, p ? p->recursion_count : 0,
298 p ? p->owner : VG_INVALID_THREADID,
299 took_lock ? "" : " (locking failed)");
sewardjaf44c822007-11-25 14:01:38 +0000300
bartbedfd232009-03-26 19:07:15 +0000301 if (! p || ! took_lock)
302 return;
bart5357fcb2008-02-27 15:46:00 +0000303
bart74b2d972011-10-08 08:54:57 +0000304 if (p->recursion_count == 0) {
bartf6ec1fe2009-06-21 18:07:35 +0000305 if (p->owner != drd_tid && p->owner != DRD_INVALID_THREADID)
bartbedfd232009-03-26 19:07:15 +0000306 {
307 tl_assert(p->last_locked_segment);
bartf6ec1fe2009-06-21 18:07:35 +0000308
309 DRD_(thread_new_segment_and_combine_vc)(drd_tid,
310 p->last_locked_segment);
bartbedfd232009-03-26 19:07:15 +0000311 }
bartf6ec1fe2009-06-21 18:07:35 +0000312 else
313 DRD_(thread_new_segment)(drd_tid);
314
315 s_mutex_segment_creation_count++;
bart5bd9f2d2008-03-03 20:31:58 +0000316
bartbedfd232009-03-26 19:07:15 +0000317 p->owner = drd_tid;
318 p->acquiry_time_ms = VG_(read_millisecond_timer)();
319 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
320 s_mutex_lock_count++;
bart74b2d972011-10-08 08:54:57 +0000321 } else if (p->owner != drd_tid) {
322 const ThreadId vg_tid = VG_(get_running_tid)();
323 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
324 p->a1, p->recursion_count, p->owner };
325 VG_(maybe_record_error)(vg_tid,
326 MutexErr,
327 VG_(get_IP)(vg_tid),
328 "The impossible happened: mutex is locked"
329 " simultaneously by two threads",
330 &MEI);
bartbedfd232009-03-26 19:07:15 +0000331 p->owner = drd_tid;
332 }
333 p->recursion_count++;
sewardjaf44c822007-11-25 14:01:38 +0000334}
335
bartdc1ef032009-02-15 14:18:02 +0000336/**
337 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
bart9d5b7962008-05-14 12:25:00 +0000338 *
bart7e6de962009-02-21 09:39:09 +0000339 * @param[in] mutex Address of the client mutex.
340 * @param[in] mutex_type Mutex type.
bart9d5b7962008-05-14 12:25:00 +0000341 *
bartdc1ef032009-02-15 14:18:02 +0000342 * @return New value of the mutex recursion count.
bart9d5b7962008-05-14 12:25:00 +0000343 *
bartdc1ef032009-02-15 14:18:02 +0000344 * @note This function must be called before pthread_mutex_unlock() is called,
345 * or a race condition is triggered !
sewardjaf44c822007-11-25 14:01:38 +0000346 */
bartdc1ef032009-02-15 14:18:02 +0000347void DRD_(mutex_unlock)(const Addr mutex, MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000348{
bartbedfd232009-03-26 19:07:15 +0000349 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
350 const ThreadId vg_tid = VG_(get_running_tid)();
351 struct mutex_info* p;
bart3f4623e2008-07-07 16:53:07 +0000352
bartbedfd232009-03-26 19:07:15 +0000353 p = DRD_(mutex_get)(mutex);
bartadb7a202009-07-21 12:39:25 +0000354 if (p && mutex_type == mutex_type_unknown)
bartbedfd232009-03-26 19:07:15 +0000355 mutex_type = p->mutex_type;
sewardjaf44c822007-11-25 14:01:38 +0000356
bartb92ff0f2011-10-08 08:29:29 +0000357 if (s_trace_mutex) {
bartad994e82011-10-13 18:04:30 +0000358 DRD_(trace_msg)("[%d] mutex_unlock %s 0x%lx rc %d",
bartb92ff0f2011-10-08 08:29:29 +0000359 drd_tid, p ? DRD_(mutex_get_typename)(p) : "(?)",
360 mutex, p ? p->recursion_count : 0);
bartbedfd232009-03-26 19:07:15 +0000361 }
sewardjaf44c822007-11-25 14:01:38 +0000362
bartbedfd232009-03-26 19:07:15 +0000363 if (p == 0 || mutex_type == mutex_type_invalid_mutex)
364 {
365 DRD_(not_a_mutex)(mutex);
366 return;
367 }
bartab7a6442008-02-25 19:46:14 +0000368
bartbedfd232009-03-26 19:07:15 +0000369 if (p->owner == DRD_INVALID_THREADID)
370 {
bartd45d9952009-05-31 18:53:54 +0000371 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
372 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000373 VG_(maybe_record_error)(vg_tid,
374 MutexErr,
375 VG_(get_IP)(vg_tid),
376 "Mutex not locked",
377 &MEI);
378 return;
379 }
bart5357fcb2008-02-27 15:46:00 +0000380
bartbedfd232009-03-26 19:07:15 +0000381 tl_assert(p);
bart74b2d972011-10-08 08:54:57 +0000382 if (p->mutex_type != mutex_type) {
383 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
384 p->a1, p->recursion_count, p->owner };
385 VG_(maybe_record_error)(vg_tid, MutexErr, VG_(get_IP)(vg_tid),
386 "Mutex type changed", &MEI);
bartbedfd232009-03-26 19:07:15 +0000387 }
388 tl_assert(p->mutex_type == mutex_type);
389 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000390
bartbedfd232009-03-26 19:07:15 +0000391 if (p->owner != drd_tid || p->recursion_count <= 0)
392 {
bartd45d9952009-05-31 18:53:54 +0000393 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
394 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000395 VG_(maybe_record_error)(vg_tid,
396 MutexErr,
397 VG_(get_IP)(vg_tid),
398 "Mutex not locked by calling thread",
399 &MEI);
400 return;
401 }
402 tl_assert(p->recursion_count > 0);
403 p->recursion_count--;
404 tl_assert(p->recursion_count >= 0);
sewardj347eeba2008-01-21 14:19:07 +0000405
bartbedfd232009-03-26 19:07:15 +0000406 if (p->recursion_count == 0)
407 {
408 if (s_mutex_lock_threshold_ms > 0)
bart9d5b7962008-05-14 12:25:00 +0000409 {
bart430c45f2009-04-13 08:05:18 +0000410 Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
bartbedfd232009-03-26 19:07:15 +0000411 if (held > s_mutex_lock_threshold_ms)
412 {
413 HoldtimeErrInfo HEI
bartd45d9952009-05-31 18:53:54 +0000414 = { DRD_(thread_get_running_tid)(),
415 mutex, p->acquired_at, held, s_mutex_lock_threshold_ms };
bartbedfd232009-03-26 19:07:15 +0000416 VG_(maybe_record_error)(vg_tid,
417 HoldtimeErr,
418 VG_(get_IP)(vg_tid),
419 "mutex",
420 &HEI);
421 }
bart9d5b7962008-05-14 12:25:00 +0000422 }
bart9d5b7962008-05-14 12:25:00 +0000423
bartbedfd232009-03-26 19:07:15 +0000424 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
425 /* current vector clock of the thread such that it is available when */
426 /* this mutex is locked again. */
sewardjaf44c822007-11-25 14:01:38 +0000427
bartbedfd232009-03-26 19:07:15 +0000428 DRD_(thread_get_latest_segment)(&p->last_locked_segment, drd_tid);
429 DRD_(thread_new_segment)(drd_tid);
430 p->acquired_at = 0;
431 s_mutex_segment_creation_count++;
432 }
sewardjaf44c822007-11-25 14:01:38 +0000433}
434
bart3dbdc862009-02-14 12:14:50 +0000435void DRD_(spinlock_init_or_unlock)(const Addr spinlock)
436{
bartbedfd232009-03-26 19:07:15 +0000437 struct mutex_info* mutex_p = DRD_(mutex_get)(spinlock);
438 if (mutex_p)
439 {
440 DRD_(mutex_unlock)(spinlock, mutex_type_spinlock);
441 }
442 else
443 {
444 DRD_(mutex_init)(spinlock, mutex_type_spinlock);
445 }
bart3dbdc862009-02-14 12:14:50 +0000446}
447
florian19f91bb2012-11-10 22:29:54 +0000448const HChar* DRD_(mutex_get_typename)(struct mutex_info* const p)
sewardjaf44c822007-11-25 14:01:38 +0000449{
bartbedfd232009-03-26 19:07:15 +0000450 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000451
bartbedfd232009-03-26 19:07:15 +0000452 return DRD_(mutex_type_name)(p->mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000453}
454
florian19f91bb2012-11-10 22:29:54 +0000455const HChar* DRD_(mutex_type_name)(const MutexT mt)
sewardj347eeba2008-01-21 14:19:07 +0000456{
bartbedfd232009-03-26 19:07:15 +0000457 switch (mt)
458 {
bartadb7a202009-07-21 12:39:25 +0000459 case mutex_type_unknown:
460 return "mutex";
bartbedfd232009-03-26 19:07:15 +0000461 case mutex_type_invalid_mutex:
462 return "invalid mutex";
463 case mutex_type_recursive_mutex:
464 return "recursive mutex";
465 case mutex_type_errorcheck_mutex:
466 return "error checking mutex";
467 case mutex_type_default_mutex:
468 return "mutex";
469 case mutex_type_spinlock:
470 return "spinlock";
bartbedfd232009-03-26 19:07:15 +0000471 }
bart62cc2322010-03-07 10:54:21 +0000472 tl_assert(0);
bartbedfd232009-03-26 19:07:15 +0000473 return "?";
sewardjaf44c822007-11-25 14:01:38 +0000474}
475
bart5357fcb2008-02-27 15:46:00 +0000476/** Return true if the specified mutex is locked by any thread. */
bartd2c5eae2009-02-21 15:27:04 +0000477static Bool mutex_is_locked(struct mutex_info* const p)
bart5357fcb2008-02-27 15:46:00 +0000478{
bartbedfd232009-03-26 19:07:15 +0000479 tl_assert(p);
480 return (p->recursion_count > 0);
bart5357fcb2008-02-27 15:46:00 +0000481}
482
bartdc1ef032009-02-15 14:18:02 +0000483Bool DRD_(mutex_is_locked_by)(const Addr mutex, const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000484{
bartbedfd232009-03-26 19:07:15 +0000485 struct mutex_info* const p = DRD_(mutex_get)(mutex);
486 if (p)
487 {
488 return (p->recursion_count > 0 && p->owner == tid);
489 }
490 return False;
sewardjaf44c822007-11-25 14:01:38 +0000491}
492
bartdc1ef032009-02-15 14:18:02 +0000493int DRD_(mutex_get_recursion_count)(const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000494{
bartbedfd232009-03-26 19:07:15 +0000495 struct mutex_info* const p = DRD_(mutex_get)(mutex);
496 tl_assert(p);
497 return p->recursion_count;
sewardjaf44c822007-11-25 14:01:38 +0000498}
499
500/**
bart301c3112008-02-24 18:22:37 +0000501 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000502 * "last owner" field can be cleared if it still refers to that thread.
sewardjaf44c822007-11-25 14:01:38 +0000503 */
bartd2c5eae2009-02-21 15:27:04 +0000504static void mutex_delete_thread(struct mutex_info* p, const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000505{
bartbedfd232009-03-26 19:07:15 +0000506 tl_assert(p);
bart4bb53d82008-02-28 19:06:34 +0000507
bartbedfd232009-03-26 19:07:15 +0000508 if (p->owner == tid && p->recursion_count > 0)
509 {
bartd45d9952009-05-31 18:53:54 +0000510 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
511 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000512 VG_(maybe_record_error)(VG_(get_running_tid)(),
513 MutexErr,
514 VG_(get_IP)(VG_(get_running_tid)()),
515 "Mutex still locked at thread exit",
516 &MEI);
517 p->owner = VG_INVALID_THREADID;
518 }
sewardjaf44c822007-11-25 14:01:38 +0000519}
520
bartdc1ef032009-02-15 14:18:02 +0000521ULong DRD_(get_mutex_lock_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000522{
bartbedfd232009-03-26 19:07:15 +0000523 return s_mutex_lock_count;
sewardjaf44c822007-11-25 14:01:38 +0000524}
bart6bbefaf2008-04-19 15:16:45 +0000525
bartdc1ef032009-02-15 14:18:02 +0000526ULong DRD_(get_mutex_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000527{
bartbedfd232009-03-26 19:07:15 +0000528 return s_mutex_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000529}