blob: a55959b6726a50b7d86ddf4ccac1a48954862d3d [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
bart3dbdc862009-02-14 12:14:50 +00004 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00005
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
bart3dbdc862009-02-14 12:14:50 +000025#include "drd_basics.h"
bart4bb53d82008-02-28 19:06:34 +000026#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_error.h"
28#include "drd_mutex.h"
bart9d5b7962008-05-14 12:25:00 +000029#include "pub_tool_vki.h"
sewardjaf44c822007-11-25 14:01:38 +000030#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
31#include "pub_tool_libcassert.h" // tl_assert()
bart5bd9f2d2008-03-03 20:31:58 +000032#include "pub_tool_libcbase.h" // VG_(strlen)
bart4bb53d82008-02-28 19:06:34 +000033#include "pub_tool_libcprint.h" // VG_(message)()
bart9d5b7962008-05-14 12:25:00 +000034#include "pub_tool_libcproc.h" // VG_(read_millisecond_timer)()
sewardjaf44c822007-11-25 14:01:38 +000035#include "pub_tool_machine.h" // VG_(get_IP)()
36#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
37
38
bartdc1ef032009-02-15 14:18:02 +000039/* Local functions. */
sewardj347eeba2008-01-21 14:19:07 +000040
bartdc1ef032009-02-15 14:18:02 +000041static void DRD_(mutex_cleanup)(struct mutex_info* p);
42static Bool DRD_(mutex_is_locked)(struct mutex_info* const p);
sewardj347eeba2008-01-21 14:19:07 +000043
44
bartdc1ef032009-02-15 14:18:02 +000045/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000046
bartdc1ef032009-02-15 14:18:02 +000047static Bool DRD_(s_trace_mutex);
48static ULong DRD_(s_mutex_lock_count);
49static ULong DRD_(s_mutex_segment_creation_count);
50static UInt DRD_(s_mutex_lock_threshold_ms) = 1000 * 1000;
sewardjaf44c822007-11-25 14:01:38 +000051
52
bartdc1ef032009-02-15 14:18:02 +000053/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000054
bartdc1ef032009-02-15 14:18:02 +000055void DRD_(mutex_set_trace)(const Bool trace_mutex)
sewardjaf44c822007-11-25 14:01:38 +000056{
57 tl_assert(!! trace_mutex == trace_mutex);
bartdc1ef032009-02-15 14:18:02 +000058 DRD_(s_trace_mutex) = trace_mutex;
sewardjaf44c822007-11-25 14:01:38 +000059}
60
bartdc1ef032009-02-15 14:18:02 +000061void DRD_(mutex_set_lock_threshold)(const UInt lock_threshold_ms)
bart9d5b7962008-05-14 12:25:00 +000062{
bartdc1ef032009-02-15 14:18:02 +000063 DRD_(s_mutex_lock_threshold_ms) = lock_threshold_ms;
bart9d5b7962008-05-14 12:25:00 +000064}
65
sewardjaf44c822007-11-25 14:01:38 +000066static
bartdc1ef032009-02-15 14:18:02 +000067void DRD_(mutex_initialize)(struct mutex_info* const p,
68 const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000069{
bart3f4623e2008-07-07 16:53:07 +000070 tl_assert(mutex);
71 tl_assert(mutex_type != mutex_type_unknown);
bart4bb53d82008-02-28 19:06:34 +000072 tl_assert(p->a1 == mutex);
bart3f4623e2008-07-07 16:53:07 +000073
bartdc1ef032009-02-15 14:18:02 +000074 p->cleanup = (void(*)(DrdClientobj*))&(DRD_(mutex_cleanup));
barta2b6e1b2008-03-17 18:32:39 +000075 p->mutex_type = mutex_type;
76 p->recursion_count = 0;
77 p->owner = DRD_INVALID_THREADID;
78 p->last_locked_segment = 0;
bart9d5b7962008-05-14 12:25:00 +000079 p->acquiry_time_ms = 0;
80 p->acquired_at = 0;
sewardjaf44c822007-11-25 14:01:38 +000081}
82
bart46d5f172008-02-28 19:49:37 +000083/** Deallocate the memory that was allocated by mutex_initialize(). */
bartdc1ef032009-02-15 14:18:02 +000084static void DRD_(mutex_cleanup)(struct mutex_info* p)
bart46d5f172008-02-28 19:49:37 +000085{
bart6b717612008-03-24 09:29:38 +000086 tl_assert(p);
87
bartdc1ef032009-02-15 14:18:02 +000088 if (DRD_(s_trace_mutex))
bartb78312c2008-02-29 11:00:17 +000089 {
bart3b1ee452008-02-29 19:28:15 +000090 VG_(message)(Vg_UserMsg,
bart3f4623e2008-07-07 16:53:07 +000091 "[%d/%d] mutex_destroy %s 0x%lx rc %d owner %d",
bart3b1ee452008-02-29 19:28:15 +000092 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +000093 DRD_(thread_get_running_tid)(),
bartdc1ef032009-02-15 14:18:02 +000094 DRD_(mutex_get_typename)(p),
bart3f4623e2008-07-07 16:53:07 +000095 p->a1,
96 p ? p->recursion_count : -1,
97 p ? p->owner : DRD_INVALID_THREADID);
bartb78312c2008-02-29 11:00:17 +000098 }
99
bartdc1ef032009-02-15 14:18:02 +0000100 if (DRD_(mutex_is_locked)(p))
bart46d5f172008-02-28 19:49:37 +0000101 {
102 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
103 VG_(maybe_record_error)(VG_(get_running_tid)(),
104 MutexErr,
105 VG_(get_IP)(VG_(get_running_tid)()),
106 "Destroying locked mutex",
107 &MEI);
108 }
109
bart62ada3f2009-02-14 17:19:58 +0000110 DRD_(sg_put)(p->last_locked_segment);
barta2b6e1b2008-03-17 18:32:39 +0000111 p->last_locked_segment = 0;
bart46d5f172008-02-28 19:49:37 +0000112}
113
bart886b87c2008-06-28 13:40:41 +0000114/** Let Valgrind report that there is no mutex object at address 'mutex'. */
bartdc1ef032009-02-15 14:18:02 +0000115void DRD_(not_a_mutex)(const Addr mutex)
bart6b717612008-03-24 09:29:38 +0000116{
117 MutexErrInfo MEI = { mutex, -1, DRD_INVALID_THREADID };
118 VG_(maybe_record_error)(VG_(get_running_tid)(),
119 MutexErr,
120 VG_(get_IP)(VG_(get_running_tid)()),
121 "Not a mutex",
122 &MEI);
123}
124
sewardjaf44c822007-11-25 14:01:38 +0000125static
sewardj721ad7b2007-11-30 08:30:29 +0000126struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000127DRD_(mutex_get_or_allocate)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000128{
bart4bb53d82008-02-28 19:06:34 +0000129 struct mutex_info* p;
sewardj721ad7b2007-11-30 08:30:29 +0000130
bart4bb53d82008-02-28 19:06:34 +0000131 tl_assert(offsetof(DrdClientobj, mutex) == 0);
bart195e41f2009-02-15 11:34:57 +0000132 p = &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
bart4bb53d82008-02-28 19:06:34 +0000133 if (p)
134 {
bart4bb53d82008-02-28 19:06:34 +0000135 return p;
136 }
sewardj721ad7b2007-11-30 08:30:29 +0000137
bart195e41f2009-02-15 11:34:57 +0000138 if (DRD_(clientobj_present)(mutex, mutex + 1))
sewardj721ad7b2007-11-30 08:30:29 +0000139 {
bartdc1ef032009-02-15 14:18:02 +0000140 DRD_(not_a_mutex)(mutex);
bart5bd9f2d2008-03-03 20:31:58 +0000141 return 0;
sewardj721ad7b2007-11-30 08:30:29 +0000142 }
bart4bb53d82008-02-28 19:06:34 +0000143
bart3f4623e2008-07-07 16:53:07 +0000144 tl_assert(mutex_type != mutex_type_unknown);
145
bart195e41f2009-02-15 11:34:57 +0000146 p = &(DRD_(clientobj_add)(mutex, ClientMutex)->mutex);
bartdc1ef032009-02-15 14:18:02 +0000147 DRD_(mutex_initialize)(p, mutex, mutex_type);
bart4bb53d82008-02-28 19:06:34 +0000148 return p;
sewardjaf44c822007-11-25 14:01:38 +0000149}
150
bartdc1ef032009-02-15 14:18:02 +0000151struct mutex_info* DRD_(mutex_get)(const Addr mutex)
bart3b1ee452008-02-29 19:28:15 +0000152{
153 tl_assert(offsetof(DrdClientobj, mutex) == 0);
bart195e41f2009-02-15 11:34:57 +0000154 return &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
bart3b1ee452008-02-29 19:28:15 +0000155}
156
bart00344642008-03-01 15:27:41 +0000157/** Called before pthread_mutex_init(). */
sewardj721ad7b2007-11-30 08:30:29 +0000158struct mutex_info*
bartdc1ef032009-02-15 14:18:02 +0000159DRD_(mutex_init)(const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000160{
bart00344642008-03-01 15:27:41 +0000161 struct mutex_info* p;
sewardjaf44c822007-11-25 14:01:38 +0000162
bart3f4623e2008-07-07 16:53:07 +0000163 tl_assert(mutex_type != mutex_type_unknown);
164
bartdc1ef032009-02-15 14:18:02 +0000165 if (DRD_(s_trace_mutex))
sewardjaf44c822007-11-25 14:01:38 +0000166 {
bart3b1ee452008-02-29 19:28:15 +0000167 VG_(message)(Vg_UserMsg,
168 "[%d/%d] mutex_init %s 0x%lx",
169 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000170 DRD_(thread_get_running_tid)(),
bartdc1ef032009-02-15 14:18:02 +0000171 DRD_(mutex_type_name)(mutex_type),
sewardjaf44c822007-11-25 14:01:38 +0000172 mutex);
173 }
174
bart00344642008-03-01 15:27:41 +0000175 if (mutex_type == mutex_type_invalid_mutex)
176 {
bartdc1ef032009-02-15 14:18:02 +0000177 DRD_(not_a_mutex)(mutex);
bart00344642008-03-01 15:27:41 +0000178 return 0;
179 }
180
bartdc1ef032009-02-15 14:18:02 +0000181 p = DRD_(mutex_get)(mutex);
bart00344642008-03-01 15:27:41 +0000182 if (p)
sewardj347eeba2008-01-21 14:19:07 +0000183 {
184 const ThreadId vg_tid = VG_(get_running_tid)();
185 MutexErrInfo MEI
bart00344642008-03-01 15:27:41 +0000186 = { p->a1, p->recursion_count, p->owner };
sewardj347eeba2008-01-21 14:19:07 +0000187 VG_(maybe_record_error)(vg_tid,
188 MutexErr,
189 VG_(get_IP)(vg_tid),
190 "Mutex reinitialization",
191 &MEI);
bart00344642008-03-01 15:27:41 +0000192 return p;
sewardj347eeba2008-01-21 14:19:07 +0000193 }
bartdc1ef032009-02-15 14:18:02 +0000194 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000195
bart00344642008-03-01 15:27:41 +0000196 return p;
sewardjaf44c822007-11-25 14:01:38 +0000197}
198
bart46d5f172008-02-28 19:49:37 +0000199/** Called after pthread_mutex_destroy(). */
bartdc1ef032009-02-15 14:18:02 +0000200void DRD_(mutex_post_destroy)(const Addr mutex)
sewardj347eeba2008-01-21 14:19:07 +0000201{
bart72b751c2008-03-01 13:44:24 +0000202 struct mutex_info* p;
sewardj347eeba2008-01-21 14:19:07 +0000203
bartdc1ef032009-02-15 14:18:02 +0000204 p = DRD_(mutex_get)(mutex);
bart72b751c2008-03-01 13:44:24 +0000205 if (p == 0)
206 {
bartdc1ef032009-02-15 14:18:02 +0000207 DRD_(not_a_mutex)(mutex);
bart72b751c2008-03-01 13:44:24 +0000208 return;
209 }
210
bart195e41f2009-02-15 11:34:57 +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{
bart635cb162008-02-28 08:30:43 +0000222 struct mutex_info* p;
223
bartdc1ef032009-02-15 14:18:02 +0000224 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
bart3f4623e2008-07-07 16:53:07 +0000225 if (mutex_type == mutex_type_unknown)
226 mutex_type = p->mutex_type;
227
bartdc1ef032009-02-15 14:18:02 +0000228 if (DRD_(s_trace_mutex))
bart00344642008-03-01 15:27:41 +0000229 {
230 VG_(message)(Vg_UserMsg,
bart3f4623e2008-07-07 16:53:07 +0000231 "[%d/%d] %s %s 0x%lx rc %d owner %d",
bart00344642008-03-01 15:27:41 +0000232 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000233 DRD_(thread_get_running_tid)(),
bart3f4623e2008-07-07 16:53:07 +0000234 trylock ? "pre_mutex_lock " : "mutex_trylock ",
bartdc1ef032009-02-15 14:18:02 +0000235 p ? DRD_(mutex_get_typename)(p) : "(?)",
bart00344642008-03-01 15:27:41 +0000236 mutex,
bart2e3a3c12008-03-24 08:33:47 +0000237 p ? p->recursion_count : -1,
238 p ? p->owner : DRD_INVALID_THREADID);
bart00344642008-03-01 15:27:41 +0000239 }
240
bart2e3a3c12008-03-24 08:33:47 +0000241 if (p == 0)
242 {
bartdc1ef032009-02-15 14:18:02 +0000243 DRD_(not_a_mutex)(mutex);
bart2e3a3c12008-03-24 08:33:47 +0000244 return;
245 }
246
247 tl_assert(p);
248
bart00344642008-03-01 15:27:41 +0000249 if (mutex_type == mutex_type_invalid_mutex)
250 {
bartdc1ef032009-02-15 14:18:02 +0000251 DRD_(not_a_mutex)(mutex);
bart00344642008-03-01 15:27:41 +0000252 return;
253 }
254
bart2e3a3c12008-03-24 08:33:47 +0000255 if (! trylock
bart62a784c2009-02-15 13:11:14 +0000256 && p->owner == DRD_(thread_get_running_tid)()
bart8bba1f72008-02-27 16:13:05 +0000257 && p->recursion_count >= 1
258 && mutex_type != mutex_type_recursive_mutex)
259 {
bart4bb53d82008-02-28 19:06:34 +0000260 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
bart8bba1f72008-02-27 16:13:05 +0000261 VG_(maybe_record_error)(VG_(get_running_tid)(),
262 MutexErr,
263 VG_(get_IP)(VG_(get_running_tid)()),
264 "Recursive locking not allowed",
265 &MEI);
266 }
267}
268
sewardjaf44c822007-11-25 14:01:38 +0000269/**
270 * Update mutex_info state when locking the pthread_mutex_t mutex.
271 * Note: this function must be called after pthread_mutex_lock() has been
272 * called, or a race condition is triggered !
273 */
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{
bart62a784c2009-02-15 13:11:14 +0000277 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
bart00344642008-03-01 15:27:41 +0000278 struct mutex_info* p;
279
bartdc1ef032009-02-15 14:18:02 +0000280 p = DRD_(mutex_get)(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000281
bartdc1ef032009-02-15 14:18:02 +0000282 if (DRD_(s_trace_mutex))
sewardjaf44c822007-11-25 14:01:38 +0000283 {
bart3b1ee452008-02-29 19:28:15 +0000284 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000285 "[%d/%d] %s %s 0x%lx rc %d owner %d%s",
bart3b1ee452008-02-29 19:28:15 +0000286 VG_(get_running_tid)(),
sewardjaf44c822007-11-25 14:01:38 +0000287 drd_tid,
bart4a975e12008-03-30 13:28:33 +0000288 post_cond_wait ? "cond_post_wait " : "post_mutex_lock",
bartdc1ef032009-02-15 14:18:02 +0000289 p ? DRD_(mutex_get_typename)(p) : "(?)",
sewardjaf44c822007-11-25 14:01:38 +0000290 mutex,
291 p ? p->recursion_count : 0,
bartfa37c922008-03-30 08:41:59 +0000292 p ? p->owner : VG_INVALID_THREADID,
293 took_lock ? "" : " (locking failed)");
sewardjaf44c822007-11-25 14:01:38 +0000294 }
295
bart777f7fe2008-03-02 17:43:18 +0000296 if (! p || ! took_lock)
bart5bd9f2d2008-03-03 20:31:58 +0000297 return;
bart5357fcb2008-02-27 15:46:00 +0000298
sewardjaf44c822007-11-25 14:01:38 +0000299 if (p->recursion_count == 0)
300 {
bart5bd9f2d2008-03-03 20:31:58 +0000301 const DrdThreadId last_owner = p->owner;
302
303 if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID)
barta2b6e1b2008-03-17 18:32:39 +0000304 {
305 tl_assert(p->last_locked_segment);
bart62a784c2009-02-15 13:11:14 +0000306 DRD_(thread_combine_vc2)(drd_tid, &p->last_locked_segment->vc);
barta2b6e1b2008-03-17 18:32:39 +0000307 }
bart62a784c2009-02-15 13:11:14 +0000308 DRD_(thread_new_segment)(drd_tid);
bartdc1ef032009-02-15 14:18:02 +0000309 DRD_(s_mutex_segment_creation_count)++;
bart5bd9f2d2008-03-03 20:31:58 +0000310
bart9d5b7962008-05-14 12:25:00 +0000311 p->owner = drd_tid;
312 p->acquiry_time_ms = VG_(read_millisecond_timer)();
313 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
bartdc1ef032009-02-15 14:18:02 +0000314 DRD_(s_mutex_lock_count)++;
sewardjaf44c822007-11-25 14:01:38 +0000315 }
316 else if (p->owner != drd_tid)
317 {
bart3b1ee452008-02-29 19:28:15 +0000318 VG_(message)(Vg_UserMsg,
sewardjaf44c822007-11-25 14:01:38 +0000319 "The impossible happened: mutex 0x%lx is locked"
320 " simultaneously by two threads (recursion count %d,"
321 " owners %d and %d) !",
bart4bb53d82008-02-28 19:06:34 +0000322 p->a1, p->recursion_count, p->owner, drd_tid);
sewardj347eeba2008-01-21 14:19:07 +0000323 p->owner = drd_tid;
sewardjaf44c822007-11-25 14:01:38 +0000324 }
325 p->recursion_count++;
sewardjaf44c822007-11-25 14:01:38 +0000326}
327
bartdc1ef032009-02-15 14:18:02 +0000328/**
329 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
bart9d5b7962008-05-14 12:25:00 +0000330 *
bart7e6de962009-02-21 09:39:09 +0000331 * @param[in] mutex Address of the client mutex.
332 * @param[in] mutex_type Mutex type.
bart9d5b7962008-05-14 12:25:00 +0000333 *
bartdc1ef032009-02-15 14:18:02 +0000334 * @return New value of the mutex recursion count.
bart9d5b7962008-05-14 12:25:00 +0000335 *
bartdc1ef032009-02-15 14:18:02 +0000336 * @note This function must be called before pthread_mutex_unlock() is called,
337 * or a race condition is triggered !
sewardjaf44c822007-11-25 14:01:38 +0000338 */
bartdc1ef032009-02-15 14:18:02 +0000339void DRD_(mutex_unlock)(const Addr mutex, MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +0000340{
bart62a784c2009-02-15 13:11:14 +0000341 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
bartb78312c2008-02-29 11:00:17 +0000342 const ThreadId vg_tid = VG_(get_running_tid)();
bart3f4623e2008-07-07 16:53:07 +0000343 struct mutex_info* p;
344
bartdc1ef032009-02-15 14:18:02 +0000345 p = DRD_(mutex_get)(mutex);
bart3f4623e2008-07-07 16:53:07 +0000346 if (mutex_type == mutex_type_unknown)
347 mutex_type = p->mutex_type;
sewardjaf44c822007-11-25 14:01:38 +0000348
bartdc1ef032009-02-15 14:18:02 +0000349 if (DRD_(s_trace_mutex))
sewardjaf44c822007-11-25 14:01:38 +0000350 {
bart3b1ee452008-02-29 19:28:15 +0000351 VG_(message)(Vg_UserMsg,
352 "[%d/%d] mutex_unlock %s 0x%lx rc %d",
353 vg_tid,
354 drd_tid,
bartdc1ef032009-02-15 14:18:02 +0000355 p ? DRD_(mutex_get_typename)(p) : "(?)",
sewardjaf44c822007-11-25 14:01:38 +0000356 mutex,
barta2b6e1b2008-03-17 18:32:39 +0000357 p ? p->recursion_count : 0);
sewardjaf44c822007-11-25 14:01:38 +0000358 }
359
bart777f7fe2008-03-02 17:43:18 +0000360 if (p == 0 || mutex_type == mutex_type_invalid_mutex)
bartab7a6442008-02-25 19:46:14 +0000361 {
bartdc1ef032009-02-15 14:18:02 +0000362 DRD_(not_a_mutex)(mutex);
bart5bd9f2d2008-03-03 20:31:58 +0000363 return;
bartab7a6442008-02-25 19:46:14 +0000364 }
365
bart5357fcb2008-02-27 15:46:00 +0000366 if (p->owner == DRD_INVALID_THREADID)
367 {
bart4bb53d82008-02-28 19:06:34 +0000368 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
bart5357fcb2008-02-27 15:46:00 +0000369 VG_(maybe_record_error)(vg_tid,
370 MutexErr,
371 VG_(get_IP)(vg_tid),
372 "Mutex not locked",
373 &MEI);
bart5bd9f2d2008-03-03 20:31:58 +0000374 return;
bart5357fcb2008-02-27 15:46:00 +0000375 }
376
sewardjaf44c822007-11-25 14:01:38 +0000377 tl_assert(p);
bart5357fcb2008-02-27 15:46:00 +0000378 if (p->mutex_type != mutex_type)
379 {
barta2b6e1b2008-03-17 18:32:39 +0000380 VG_(message)(Vg_UserMsg, "??? mutex 0x%lx: type changed from %d into %d",
bart5bd9f2d2008-03-03 20:31:58 +0000381 p->a1, p->mutex_type, mutex_type);
bart5357fcb2008-02-27 15:46:00 +0000382 }
sewardj721ad7b2007-11-30 08:30:29 +0000383 tl_assert(p->mutex_type == mutex_type);
sewardjaf44c822007-11-25 14:01:38 +0000384 tl_assert(p->owner != DRD_INVALID_THREADID);
sewardj721ad7b2007-11-30 08:30:29 +0000385
bart777f7fe2008-03-02 17:43:18 +0000386 if (p->owner != drd_tid || p->recursion_count <= 0)
sewardjaf44c822007-11-25 14:01:38 +0000387 {
bart4bb53d82008-02-28 19:06:34 +0000388 MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner };
sewardjaf44c822007-11-25 14:01:38 +0000389 VG_(maybe_record_error)(vg_tid,
390 MutexErr,
391 VG_(get_IP)(vg_tid),
bart777f7fe2008-03-02 17:43:18 +0000392 "Mutex not locked by calling thread",
sewardjaf44c822007-11-25 14:01:38 +0000393 &MEI);
bart777f7fe2008-03-02 17:43:18 +0000394 return;
sewardjaf44c822007-11-25 14:01:38 +0000395 }
bart777f7fe2008-03-02 17:43:18 +0000396 tl_assert(p->recursion_count > 0);
sewardjaf44c822007-11-25 14:01:38 +0000397 p->recursion_count--;
bart777f7fe2008-03-02 17:43:18 +0000398 tl_assert(p->recursion_count >= 0);
sewardj347eeba2008-01-21 14:19:07 +0000399
sewardjaf44c822007-11-25 14:01:38 +0000400 if (p->recursion_count == 0)
401 {
bartdc1ef032009-02-15 14:18:02 +0000402 if (DRD_(s_mutex_lock_threshold_ms) > 0)
bart9d5b7962008-05-14 12:25:00 +0000403 {
404 ULong held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
bartdc1ef032009-02-15 14:18:02 +0000405 if (held > DRD_(s_mutex_lock_threshold_ms))
bart9d5b7962008-05-14 12:25:00 +0000406 {
407 HoldtimeErrInfo HEI
bartdc1ef032009-02-15 14:18:02 +0000408 = { mutex, p->acquired_at, held, DRD_(s_mutex_lock_threshold_ms) };
bart9d5b7962008-05-14 12:25:00 +0000409 VG_(maybe_record_error)(vg_tid,
410 HoldtimeErr,
411 VG_(get_IP)(vg_tid),
412 "mutex",
413 &HEI);
414 }
415 }
416
sewardjaf44c822007-11-25 14:01:38 +0000417 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
418 /* current vector clock of the thread such that it is available when */
419 /* this mutex is locked again. */
sewardjaf44c822007-11-25 14:01:38 +0000420
bart62a784c2009-02-15 13:11:14 +0000421 DRD_(thread_get_latest_segment)(&p->last_locked_segment, drd_tid);
422 DRD_(thread_new_segment)(drd_tid);
bart9d5b7962008-05-14 12:25:00 +0000423 p->acquired_at = 0;
bartdc1ef032009-02-15 14:18:02 +0000424 DRD_(s_mutex_segment_creation_count)++;
sewardjaf44c822007-11-25 14:01:38 +0000425 }
sewardjaf44c822007-11-25 14:01:38 +0000426}
427
bart3dbdc862009-02-14 12:14:50 +0000428void DRD_(spinlock_init_or_unlock)(const Addr spinlock)
429{
bartdc1ef032009-02-15 14:18:02 +0000430 struct mutex_info* mutex_p = DRD_(mutex_get)(spinlock);
bart3dbdc862009-02-14 12:14:50 +0000431 if (mutex_p)
432 {
bartdc1ef032009-02-15 14:18:02 +0000433 DRD_(mutex_unlock)(spinlock, mutex_type_spinlock);
bart3dbdc862009-02-14 12:14:50 +0000434 }
435 else
436 {
bartdc1ef032009-02-15 14:18:02 +0000437 DRD_(mutex_init)(spinlock, mutex_type_spinlock);
bart3dbdc862009-02-14 12:14:50 +0000438 }
439}
440
bartdc1ef032009-02-15 14:18:02 +0000441const char* DRD_(mutex_get_typename)(struct mutex_info* const p)
sewardjaf44c822007-11-25 14:01:38 +0000442{
443 tl_assert(p);
sewardj721ad7b2007-11-30 08:30:29 +0000444
bartdc1ef032009-02-15 14:18:02 +0000445 return DRD_(mutex_type_name)(p->mutex_type);
sewardj347eeba2008-01-21 14:19:07 +0000446}
447
bartdc1ef032009-02-15 14:18:02 +0000448const char* DRD_(mutex_type_name)(const MutexT mt)
sewardj347eeba2008-01-21 14:19:07 +0000449{
450 switch (mt)
sewardjaf44c822007-11-25 14:01:38 +0000451 {
bart635cb162008-02-28 08:30:43 +0000452 case mutex_type_invalid_mutex:
453 return "invalid mutex";
bart5357fcb2008-02-27 15:46:00 +0000454 case mutex_type_recursive_mutex:
455 return "recursive mutex";
456 case mutex_type_errorcheck_mutex:
457 return "error checking mutex";
458 case mutex_type_default_mutex:
sewardjaf44c822007-11-25 14:01:38 +0000459 return "mutex";
sewardj721ad7b2007-11-30 08:30:29 +0000460 case mutex_type_spinlock:
sewardjaf44c822007-11-25 14:01:38 +0000461 return "spinlock";
462 default:
463 tl_assert(0);
464 }
465 return "?";
466}
467
bart5357fcb2008-02-27 15:46:00 +0000468/** Return true if the specified mutex is locked by any thread. */
bartdc1ef032009-02-15 14:18:02 +0000469static Bool DRD_(mutex_is_locked)(struct mutex_info* const p)
bart5357fcb2008-02-27 15:46:00 +0000470{
471 tl_assert(p);
472 return (p->recursion_count > 0);
473}
474
bartdc1ef032009-02-15 14:18:02 +0000475Bool DRD_(mutex_is_locked_by)(const Addr mutex, const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000476{
bartdc1ef032009-02-15 14:18:02 +0000477 struct mutex_info* const p = DRD_(mutex_get)(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000478 if (p)
479 {
480 return (p->recursion_count > 0 && p->owner == tid);
481 }
482 return False;
483}
484
bartdc1ef032009-02-15 14:18:02 +0000485int DRD_(mutex_get_recursion_count)(const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000486{
bartdc1ef032009-02-15 14:18:02 +0000487 struct mutex_info* const p = DRD_(mutex_get)(mutex);
sewardjaf44c822007-11-25 14:01:38 +0000488 tl_assert(p);
489 return p->recursion_count;
490}
491
492/**
bart301c3112008-02-24 18:22:37 +0000493 * Call this function when thread tid stops to exist, such that the
sewardjaf44c822007-11-25 14:01:38 +0000494 * "last owner" field can be cleared if it still refers to that thread.
sewardjaf44c822007-11-25 14:01:38 +0000495 */
bartdc1ef032009-02-15 14:18:02 +0000496void DRD_(mutex_thread_delete)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000497{
bart4bb53d82008-02-28 19:06:34 +0000498 struct mutex_info* p;
499
bart195e41f2009-02-15 11:34:57 +0000500 DRD_(clientobj_resetiter)();
501 for ( ; (p = &(DRD_(clientobj_next)(ClientMutex)->mutex)) != 0; )
sewardjaf44c822007-11-25 14:01:38 +0000502 {
bart4bb53d82008-02-28 19:06:34 +0000503 if (p->owner == tid && p->recursion_count > 0)
sewardjaf44c822007-11-25 14:01:38 +0000504 {
bart5357fcb2008-02-27 15:46:00 +0000505 MutexErrInfo MEI
bart4bb53d82008-02-28 19:06:34 +0000506 = { p->a1, p->recursion_count, p->owner };
bart5357fcb2008-02-27 15:46:00 +0000507 VG_(maybe_record_error)(VG_(get_running_tid)(),
508 MutexErr,
509 VG_(get_IP)(VG_(get_running_tid)()),
510 "Mutex still locked at thread exit",
511 &MEI);
sewardjaf44c822007-11-25 14:01:38 +0000512 p->owner = VG_INVALID_THREADID;
513 }
514 }
515}
516
bartdc1ef032009-02-15 14:18:02 +0000517ULong DRD_(get_mutex_lock_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000518{
bartdc1ef032009-02-15 14:18:02 +0000519 return DRD_(s_mutex_lock_count);
sewardjaf44c822007-11-25 14:01:38 +0000520}
bart6bbefaf2008-04-19 15:16:45 +0000521
bartdc1ef032009-02-15 14:18:02 +0000522ULong DRD_(get_mutex_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000523{
bartdc1ef032009-02-15 14:18:02 +0000524 return DRD_(s_mutex_segment_creation_count);
bart6bbefaf2008-04-19 15:16:45 +0000525}