blob: 2e9eecc26ae288029602f02d40629c4b738ba4a7 [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
Elliott Hughesed398002017-06-21 14:41:24 -07004 Copyright (C) 2006-2017 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{
sewardjd56b7e22015-01-20 00:12:18 +000058 tl_assert((!! trace_mutex) == trace_mutex);
bartbedfd232009-03-26 19:07:15 +000059 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;
bart3cc26202014-06-09 09:19:26 +000079 p->ignore_ordering = False;
bartbedfd232009-03-26 19:07:15 +000080 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
bart3cc26202014-06-09 09:19:26 +000086void DRD_(mutex_ignore_ordering)(const Addr mutex)
87{
88 struct mutex_info* p = DRD_(mutex_get)(mutex);
89
90 if (s_trace_mutex)
florianea71ffb2015-08-05 14:38:57 +000091 DRD_(trace_msg)("[%u] mutex_ignore_ordering %s 0x%lx",
bart3cc26202014-06-09 09:19:26 +000092 DRD_(thread_get_running_tid)(),
93 p ? DRD_(mutex_type_name)(p->mutex_type) : "(?)",
94 mutex);
95
96 if (p) {
97 p->ignore_ordering = True;
98 } else {
99 DRD_(not_a_mutex)(mutex);
100 }
101}
102
bart46d5f172008-02-28 19:49:37 +0000103/** Deallocate the memory that was allocated by mutex_initialize(). */
bartd2c5eae2009-02-21 15:27:04 +0000104static void mutex_cleanup(struct mutex_info* p)
bart46d5f172008-02-28 19:49:37 +0000105{
bartbedfd232009-03-26 19:07:15 +0000106 tl_assert(p);
bart6b717612008-03-24 09:29:38 +0000107
bartbedfd232009-03-26 19:07:15 +0000108 if (s_trace_mutex)
florianea71ffb2015-08-05 14:38:57 +0000109 DRD_(trace_msg)("[%u] mutex_destroy %s 0x%lx rc %d owner %u",
bartb92ff0f2011-10-08 08:29:29 +0000110 DRD_(thread_get_running_tid)(),
111 DRD_(mutex_get_typename)(p), p->a1,
112 p ? p->recursion_count : -1,
113 p ? p->owner : DRD_INVALID_THREADID);
bartb78312c2008-02-29 11:00:17 +0000114
bartbedfd232009-03-26 19:07:15 +0000115 if (mutex_is_locked(p))
116 {
bartd45d9952009-05-31 18:53:54 +0000117 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
118 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000119 VG_(maybe_record_error)(VG_(get_running_tid)(),
120 MutexErr,
121 VG_(get_IP)(VG_(get_running_tid)()),
122 "Destroying locked mutex",
123 &MEI);
124 }
bart46d5f172008-02-28 19:49:37 +0000125
bartbedfd232009-03-26 19:07:15 +0000126 DRD_(sg_put)(p->last_locked_segment);
127 p->last_locked_segment = 0;
bart46d5f172008-02-28 19:49:37 +0000128}
129
bart62cc2322010-03-07 10:54:21 +0000130/** Report that address 'mutex' is not the address of a mutex object. */
bartdc1ef032009-02-15 14:18:02 +0000131void DRD_(not_a_mutex)(const Addr mutex)
bart6b717612008-03-24 09:29:38 +0000132{
bartd45d9952009-05-31 18:53:54 +0000133 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
134 mutex, -1, DRD_INVALID_THREADID };
bartbedfd232009-03-26 19:07:15 +0000135 VG_(maybe_record_error)(VG_(get_running_tid)(),
136 MutexErr,
137 VG_(get_IP)(VG_(get_running_tid)()),
138 "Not a mutex",
139 &MEI);
bart6b717612008-03-24 09:29:38 +0000140}
141
bart62cc2322010-03-07 10:54:21 +0000142/**
143 * Report that address 'mutex' is not the address of a mutex object of the
144 * expected type.
145 */
146static void wrong_mutex_type(const Addr mutex)
147{
148 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
149 mutex, -1, DRD_INVALID_THREADID };
150 VG_(maybe_record_error)(VG_(get_running_tid)(),
151 MutexErr,
152 VG_(get_IP)(VG_(get_running_tid)()),
153 "Mutex type mismatch",
154 &MEI);
155}
156
sewardjaf44c822007-11-25 14:01:38 +0000157static
sewardj721ad7b2007-11-30 08:30:29 +0000158struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000159DRD_(mutex_get_or_allocate)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000160{
bartbedfd232009-03-26 19:07:15 +0000161 struct mutex_info* p;
sewardj721ad7b2007-11-30 08:30:29 +0000162
bartbedfd232009-03-26 19:07:15 +0000163 tl_assert(offsetof(DrdClientobj, mutex) == 0);
164 p = &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
165 if (p)
166 {
bart62cc2322010-03-07 10:54:21 +0000167 if (mutex_type == mutex_type_unknown || p->mutex_type == mutex_type)
168 return p;
169 else
170 {
171 wrong_mutex_type(mutex);
172 return 0;
173 }
bartbedfd232009-03-26 19:07:15 +0000174 }
sewardj721ad7b2007-11-30 08:30:29 +0000175
bartbedfd232009-03-26 19:07:15 +0000176 if (DRD_(clientobj_present)(mutex, mutex + 1))
177 {
178 DRD_(not_a_mutex)(mutex);
179 return 0;
180 }
bart4bb53d82008-02-28 19:06:34 +0000181
bartbedfd232009-03-26 19:07:15 +0000182 p = &(DRD_(clientobj_add)(mutex, ClientMutex)->mutex);
183 DRD_(mutex_initialize)(p, mutex, mutex_type);
184 return p;
sewardjaf44c822007-11-25 14:01:38 +0000185}
186
bartdc1ef032009-02-15 14:18:02 +0000187struct mutex_info* DRD_(mutex_get)(const Addr mutex)
bart3b1ee452008-02-29 19:28:15 +0000188{
bartbedfd232009-03-26 19:07:15 +0000189 tl_assert(offsetof(DrdClientobj, mutex) == 0);
190 return &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
bart3b1ee452008-02-29 19:28:15 +0000191}
192
bart00344642008-03-01 15:27:41 +0000193/** Called before pthread_mutex_init(). */
sewardj721ad7b2007-11-30 08:30:29 +0000194struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000195DRD_(mutex_init)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000196{
bartbedfd232009-03-26 19:07:15 +0000197 struct mutex_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000198
bartbedfd232009-03-26 19:07:15 +0000199 if (s_trace_mutex)
florianea71ffb2015-08-05 14:38:57 +0000200 DRD_(trace_msg)("[%u] mutex_init %s 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000201 DRD_(thread_get_running_tid)(),
202 DRD_(mutex_type_name)(mutex_type),
203 mutex);
sewardjaf44c822007-11-25 14:01:38 +0000204
bartbedfd232009-03-26 19:07:15 +0000205 if (mutex_type == mutex_type_invalid_mutex)
206 {
207 DRD_(not_a_mutex)(mutex);
208 return 0;
209 }
bart00344642008-03-01 15:27:41 +0000210
bartbedfd232009-03-26 19:07:15 +0000211 p = DRD_(mutex_get)(mutex);
212 if (p)
213 {
214 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000215 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
216 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000217 VG_(maybe_record_error)(vg_tid,
218 MutexErr,
219 VG_(get_IP)(vg_tid),
220 "Mutex reinitialization",
221 &MEI);
bartfa358282009-05-16 06:22:46 +0000222 p->mutex_type = mutex_type;
bartbedfd232009-03-26 19:07:15 +0000223 return p;
224 }
225 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000226
bartbedfd232009-03-26 19:07:15 +0000227 return p;
sewardjaf44c822007-11-25 14:01:38 +0000228}
229
bart46d5f172008-02-28 19:49:37 +0000230/** Called after pthread_mutex_destroy(). */
bartdc1ef032009-02-15 14:18:02 +0000231void DRD_(mutex_post_destroy)(const Addr mutex)
sewardj347eeba2008-01-21 14:19:07 +0000232{
bartbedfd232009-03-26 19:07:15 +0000233 struct mutex_info* p;
sewardj347eeba2008-01-21 14:19:07 +0000234
bartbedfd232009-03-26 19:07:15 +0000235 p = DRD_(mutex_get)(mutex);
236 if (p == 0)
237 {
238 DRD_(not_a_mutex)(mutex);
239 return;
240 }
bart72b751c2008-03-01 13:44:24 +0000241
bartbedfd232009-03-26 19:07:15 +0000242 DRD_(clientobj_remove)(mutex, ClientMutex);
sewardj347eeba2008-01-21 14:19:07 +0000243}
244
bart62cc2322010-03-07 10:54:21 +0000245/**
246 * Called before pthread_mutex_lock() is invoked. If a data structure for the
247 * client-side object was not yet created, do this now. Also check whether an
248 * attempt is made to lock recursively a synchronization object that must not
249 * be locked recursively.
bart8bba1f72008-02-27 16:13:05 +0000250 */
bartdc1ef032009-02-15 14:18:02 +0000251void DRD_(mutex_pre_lock)(const Addr mutex, MutexT mutex_type,
252 const Bool trylock)
bart8bba1f72008-02-27 16:13:05 +0000253{
bartbedfd232009-03-26 19:07:15 +0000254 struct mutex_info* p;
bart635cb162008-02-28 08:30:43 +0000255
bartbedfd232009-03-26 19:07:15 +0000256 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
bart62cc2322010-03-07 10:54:21 +0000257 if (p && mutex_type == mutex_type_unknown)
bartbedfd232009-03-26 19:07:15 +0000258 mutex_type = p->mutex_type;
bart3f4623e2008-07-07 16:53:07 +0000259
bartbedfd232009-03-26 19:07:15 +0000260 if (s_trace_mutex)
florianea71ffb2015-08-05 14:38:57 +0000261 DRD_(trace_msg)("[%u] %s %s 0x%lx rc %d owner %u",
bartb92ff0f2011-10-08 08:29:29 +0000262 DRD_(thread_get_running_tid)(),
263 trylock ? "pre_mutex_lock " : "mutex_trylock ",
264 p ? DRD_(mutex_get_typename)(p) : "(?)",
265 mutex, p ? p->recursion_count : -1,
266 p ? p->owner : DRD_INVALID_THREADID);
bart00344642008-03-01 15:27:41 +0000267
bartbedfd232009-03-26 19:07:15 +0000268 if (p == 0)
269 {
270 DRD_(not_a_mutex)(mutex);
271 return;
272 }
bart2e3a3c12008-03-24 08:33:47 +0000273
bartbedfd232009-03-26 19:07:15 +0000274 tl_assert(p);
bart2e3a3c12008-03-24 08:33:47 +0000275
bartbedfd232009-03-26 19:07:15 +0000276 if (mutex_type == mutex_type_invalid_mutex)
277 {
278 DRD_(not_a_mutex)(mutex);
279 return;
280 }
bart00344642008-03-01 15:27:41 +0000281
bartbedfd232009-03-26 19:07:15 +0000282 if (! trylock
283 && p->owner == DRD_(thread_get_running_tid)()
284 && p->recursion_count >= 1
285 && mutex_type != mutex_type_recursive_mutex)
286 {
bartd45d9952009-05-31 18:53:54 +0000287 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
288 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000289 VG_(maybe_record_error)(VG_(get_running_tid)(),
290 MutexErr,
291 VG_(get_IP)(VG_(get_running_tid)()),
292 "Recursive locking not allowed",
293 &MEI);
294 }
bart8bba1f72008-02-27 16:13:05 +0000295}
296
sewardjaf44c822007-11-25 14:01:38 +0000297/**
298 * Update mutex_info state when locking the pthread_mutex_t mutex.
299 * Note: this function must be called after pthread_mutex_lock() has been
300 * called, or a race condition is triggered !
301 */
bartdc1ef032009-02-15 14:18:02 +0000302void DRD_(mutex_post_lock)(const Addr mutex, const Bool took_lock,
303 const Bool post_cond_wait)
sewardjaf44c822007-11-25 14:01:38 +0000304{
bartbedfd232009-03-26 19:07:15 +0000305 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
306 struct mutex_info* p;
bart00344642008-03-01 15:27:41 +0000307
bartbedfd232009-03-26 19:07:15 +0000308 p = DRD_(mutex_get)(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000309
bartbedfd232009-03-26 19:07:15 +0000310 if (s_trace_mutex)
florianea71ffb2015-08-05 14:38:57 +0000311 DRD_(trace_msg)("[%u] %s %s 0x%lx rc %d owner %u%s",
bartb92ff0f2011-10-08 08:29:29 +0000312 drd_tid,
313 post_cond_wait ? "cond_post_wait " : "post_mutex_lock",
314 p ? DRD_(mutex_get_typename)(p) : "(?)",
315 mutex, p ? p->recursion_count : 0,
316 p ? p->owner : VG_INVALID_THREADID,
317 took_lock ? "" : " (locking failed)");
sewardjaf44c822007-11-25 14:01:38 +0000318
bartbedfd232009-03-26 19:07:15 +0000319 if (! p || ! took_lock)
320 return;
bart5357fcb2008-02-27 15:46:00 +0000321
bart74b2d972011-10-08 08:54:57 +0000322 if (p->recursion_count == 0) {
bart3cc26202014-06-09 09:19:26 +0000323 if (!p->ignore_ordering) {
324 if (p->owner != drd_tid && p->owner != DRD_INVALID_THREADID) {
325 tl_assert(p->last_locked_segment);
bartf6ec1fe2009-06-21 18:07:35 +0000326
bart3cc26202014-06-09 09:19:26 +0000327 DRD_(thread_new_segment_and_combine_vc)(drd_tid,
328 p->last_locked_segment);
329 } else {
330 DRD_(thread_new_segment)(drd_tid);
331 }
332
333 s_mutex_segment_creation_count++;
bartbedfd232009-03-26 19:07:15 +0000334 }
bart5bd9f2d2008-03-03 20:31:58 +0000335
bartbedfd232009-03-26 19:07:15 +0000336 p->owner = drd_tid;
337 p->acquiry_time_ms = VG_(read_millisecond_timer)();
338 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
339 s_mutex_lock_count++;
bart74b2d972011-10-08 08:54:57 +0000340 } else if (p->owner != drd_tid) {
341 const ThreadId vg_tid = VG_(get_running_tid)();
342 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
343 p->a1, p->recursion_count, p->owner };
344 VG_(maybe_record_error)(vg_tid,
345 MutexErr,
346 VG_(get_IP)(vg_tid),
347 "The impossible happened: mutex is locked"
348 " simultaneously by two threads",
349 &MEI);
bartbedfd232009-03-26 19:07:15 +0000350 p->owner = drd_tid;
351 }
352 p->recursion_count++;
sewardjaf44c822007-11-25 14:01:38 +0000353}
354
bartdc1ef032009-02-15 14:18:02 +0000355/**
356 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
bart9d5b7962008-05-14 12:25:00 +0000357 *
bart7e6de962009-02-21 09:39:09 +0000358 * @param[in] mutex Address of the client mutex.
359 * @param[in] mutex_type Mutex type.
bart9d5b7962008-05-14 12:25:00 +0000360 *
bartdc1ef032009-02-15 14:18:02 +0000361 * @return New value of the mutex recursion count.
bart9d5b7962008-05-14 12:25:00 +0000362 *
bartdc1ef032009-02-15 14:18:02 +0000363 * @note This function must be called before pthread_mutex_unlock() is called,
364 * or a race condition is triggered !
sewardjaf44c822007-11-25 14:01:38 +0000365 */
bartdc1ef032009-02-15 14:18:02 +0000366void DRD_(mutex_unlock)(const Addr mutex, MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000367{
bartbedfd232009-03-26 19:07:15 +0000368 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
369 const ThreadId vg_tid = VG_(get_running_tid)();
370 struct mutex_info* p;
bart3f4623e2008-07-07 16:53:07 +0000371
bartbedfd232009-03-26 19:07:15 +0000372 p = DRD_(mutex_get)(mutex);
bartadb7a202009-07-21 12:39:25 +0000373 if (p && mutex_type == mutex_type_unknown)
bartbedfd232009-03-26 19:07:15 +0000374 mutex_type = p->mutex_type;
sewardjaf44c822007-11-25 14:01:38 +0000375
bartb92ff0f2011-10-08 08:29:29 +0000376 if (s_trace_mutex) {
florianea71ffb2015-08-05 14:38:57 +0000377 DRD_(trace_msg)("[%u] mutex_unlock %s 0x%lx rc %d",
bartb92ff0f2011-10-08 08:29:29 +0000378 drd_tid, p ? DRD_(mutex_get_typename)(p) : "(?)",
379 mutex, p ? p->recursion_count : 0);
bartbedfd232009-03-26 19:07:15 +0000380 }
sewardjaf44c822007-11-25 14:01:38 +0000381
bartbedfd232009-03-26 19:07:15 +0000382 if (p == 0 || mutex_type == mutex_type_invalid_mutex)
383 {
384 DRD_(not_a_mutex)(mutex);
385 return;
386 }
bartab7a6442008-02-25 19:46:14 +0000387
bartbedfd232009-03-26 19:07:15 +0000388 if (p->owner == DRD_INVALID_THREADID)
389 {
bartd45d9952009-05-31 18:53:54 +0000390 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
391 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000392 VG_(maybe_record_error)(vg_tid,
393 MutexErr,
394 VG_(get_IP)(vg_tid),
395 "Mutex not locked",
396 &MEI);
397 return;
398 }
bart5357fcb2008-02-27 15:46:00 +0000399
bartbedfd232009-03-26 19:07:15 +0000400 tl_assert(p);
bart74b2d972011-10-08 08:54:57 +0000401 if (p->mutex_type != mutex_type) {
402 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
403 p->a1, p->recursion_count, p->owner };
404 VG_(maybe_record_error)(vg_tid, MutexErr, VG_(get_IP)(vg_tid),
405 "Mutex type changed", &MEI);
bartbedfd232009-03-26 19:07:15 +0000406 }
407 tl_assert(p->mutex_type == mutex_type);
408 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000409
bartbedfd232009-03-26 19:07:15 +0000410 if (p->owner != drd_tid || p->recursion_count <= 0)
411 {
bartd45d9952009-05-31 18:53:54 +0000412 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
413 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000414 VG_(maybe_record_error)(vg_tid,
415 MutexErr,
416 VG_(get_IP)(vg_tid),
417 "Mutex not locked by calling thread",
418 &MEI);
419 return;
420 }
421 tl_assert(p->recursion_count > 0);
422 p->recursion_count--;
423 tl_assert(p->recursion_count >= 0);
sewardj347eeba2008-01-21 14:19:07 +0000424
bartbedfd232009-03-26 19:07:15 +0000425 if (p->recursion_count == 0)
426 {
427 if (s_mutex_lock_threshold_ms > 0)
bart9d5b7962008-05-14 12:25:00 +0000428 {
bart430c45f2009-04-13 08:05:18 +0000429 Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
bartbedfd232009-03-26 19:07:15 +0000430 if (held > s_mutex_lock_threshold_ms)
431 {
432 HoldtimeErrInfo HEI
bartd45d9952009-05-31 18:53:54 +0000433 = { DRD_(thread_get_running_tid)(),
434 mutex, p->acquired_at, held, s_mutex_lock_threshold_ms };
bartbedfd232009-03-26 19:07:15 +0000435 VG_(maybe_record_error)(vg_tid,
436 HoldtimeErr,
437 VG_(get_IP)(vg_tid),
438 "mutex",
439 &HEI);
440 }
bart9d5b7962008-05-14 12:25:00 +0000441 }
bart9d5b7962008-05-14 12:25:00 +0000442
bartbedfd232009-03-26 19:07:15 +0000443 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
444 /* current vector clock of the thread such that it is available when */
445 /* this mutex is locked again. */
sewardjaf44c822007-11-25 14:01:38 +0000446
bartbedfd232009-03-26 19:07:15 +0000447 DRD_(thread_get_latest_segment)(&p->last_locked_segment, drd_tid);
bart3cc26202014-06-09 09:19:26 +0000448 if (!p->ignore_ordering)
449 DRD_(thread_new_segment)(drd_tid);
bartbedfd232009-03-26 19:07:15 +0000450 p->acquired_at = 0;
451 s_mutex_segment_creation_count++;
452 }
sewardjaf44c822007-11-25 14:01:38 +0000453}
454
bart3dbdc862009-02-14 12:14:50 +0000455void DRD_(spinlock_init_or_unlock)(const Addr spinlock)
456{
bartbedfd232009-03-26 19:07:15 +0000457 struct mutex_info* mutex_p = DRD_(mutex_get)(spinlock);
458 if (mutex_p)
459 {
460 DRD_(mutex_unlock)(spinlock, mutex_type_spinlock);
461 }
462 else
463 {
464 DRD_(mutex_init)(spinlock, mutex_type_spinlock);
465 }
bart3dbdc862009-02-14 12:14:50 +0000466}
467
florian19f91bb2012-11-10 22:29:54 +0000468const HChar* DRD_(mutex_get_typename)(struct mutex_info* const p)
sewardjaf44c822007-11-25 14:01:38 +0000469{
bartbedfd232009-03-26 19:07:15 +0000470 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000471
bartbedfd232009-03-26 19:07:15 +0000472 return DRD_(mutex_type_name)(p->mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000473}
474
florian19f91bb2012-11-10 22:29:54 +0000475const HChar* DRD_(mutex_type_name)(const MutexT mt)
sewardj347eeba2008-01-21 14:19:07 +0000476{
bartbedfd232009-03-26 19:07:15 +0000477 switch (mt)
478 {
bartadb7a202009-07-21 12:39:25 +0000479 case mutex_type_unknown:
480 return "mutex";
bartbedfd232009-03-26 19:07:15 +0000481 case mutex_type_invalid_mutex:
482 return "invalid mutex";
483 case mutex_type_recursive_mutex:
484 return "recursive mutex";
485 case mutex_type_errorcheck_mutex:
486 return "error checking mutex";
487 case mutex_type_default_mutex:
488 return "mutex";
489 case mutex_type_spinlock:
490 return "spinlock";
bartf056aab2014-06-09 07:55:14 +0000491 case mutex_type_cxa_guard:
492 return "cxa_guard";
bartbedfd232009-03-26 19:07:15 +0000493 }
bart62cc2322010-03-07 10:54:21 +0000494 tl_assert(0);
bartbedfd232009-03-26 19:07:15 +0000495 return "?";
sewardjaf44c822007-11-25 14:01:38 +0000496}
497
bart5357fcb2008-02-27 15:46:00 +0000498/** Return true if the specified mutex is locked by any thread. */
bartd2c5eae2009-02-21 15:27:04 +0000499static Bool mutex_is_locked(struct mutex_info* const p)
bart5357fcb2008-02-27 15:46:00 +0000500{
bartbedfd232009-03-26 19:07:15 +0000501 tl_assert(p);
502 return (p->recursion_count > 0);
bart5357fcb2008-02-27 15:46:00 +0000503}
504
bartdc1ef032009-02-15 14:18:02 +0000505Bool DRD_(mutex_is_locked_by)(const Addr mutex, const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000506{
bartbedfd232009-03-26 19:07:15 +0000507 struct mutex_info* const p = DRD_(mutex_get)(mutex);
508 if (p)
509 {
510 return (p->recursion_count > 0 && p->owner == tid);
511 }
512 return False;
sewardjaf44c822007-11-25 14:01:38 +0000513}
514
bartdc1ef032009-02-15 14:18:02 +0000515int DRD_(mutex_get_recursion_count)(const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000516{
bartbedfd232009-03-26 19:07:15 +0000517 struct mutex_info* const p = DRD_(mutex_get)(mutex);
518 tl_assert(p);
519 return p->recursion_count;
sewardjaf44c822007-11-25 14:01:38 +0000520}
521
522/**
bart301c3112008-02-24 18:22:37 +0000523 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000524 * "last owner" field can be cleared if it still refers to that thread.
sewardjaf44c822007-11-25 14:01:38 +0000525 */
bartd2c5eae2009-02-21 15:27:04 +0000526static void mutex_delete_thread(struct mutex_info* p, const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000527{
bartbedfd232009-03-26 19:07:15 +0000528 tl_assert(p);
bart4bb53d82008-02-28 19:06:34 +0000529
bartbedfd232009-03-26 19:07:15 +0000530 if (p->owner == tid && p->recursion_count > 0)
531 {
bartd45d9952009-05-31 18:53:54 +0000532 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
533 p->a1, p->recursion_count, p->owner };
bartbedfd232009-03-26 19:07:15 +0000534 VG_(maybe_record_error)(VG_(get_running_tid)(),
535 MutexErr,
536 VG_(get_IP)(VG_(get_running_tid)()),
537 "Mutex still locked at thread exit",
538 &MEI);
539 p->owner = VG_INVALID_THREADID;
540 }
sewardjaf44c822007-11-25 14:01:38 +0000541}
542
bartdc1ef032009-02-15 14:18:02 +0000543ULong DRD_(get_mutex_lock_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000544{
bartbedfd232009-03-26 19:07:15 +0000545 return s_mutex_lock_count;
sewardjaf44c822007-11-25 14:01:38 +0000546}
bart6bbefaf2008-04-19 15:16:45 +0000547
bartdc1ef032009-02-15 14:18:02 +0000548ULong DRD_(get_mutex_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000549{
bartbedfd232009-03-26 19:07:15 +0000550 return s_mutex_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000551}