1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 This file is part of drd, a thread error detector.
5 Copyright (C) 2006-2010 Bart Van Assche <bart.vanassche@gmail.com>.
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.
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.
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
22 The GNU General Public License is contained in the file COPYING.
26 #include "drd_clientobj.h"
28 #include "drd_error.h"
29 #include "drd_mutex.h"
30 #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
31 #include "pub_tool_libcassert.h" /* tl_assert() */
32 #include "pub_tool_libcprint.h" /* VG_(printf)() */
33 #include "pub_tool_machine.h" /* VG_(get_IP)() */
34 #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
37 /* Local functions. */
39 static void DRD_(cond_cleanup)(struct cond_info* p);
42 /* Local variables. */
44 static Bool DRD_(s_report_signal_unlocked) = True;
45 static Bool DRD_(s_trace_cond);
48 /* Function definitions. */
50 void DRD_(cond_set_report_signal_unlocked)(const Bool r)
52 DRD_(s_report_signal_unlocked) = r;
55 void DRD_(cond_set_trace)(const Bool trace_cond)
57 DRD_(s_trace_cond) = trace_cond;
61 void DRD_(cond_initialize)(struct cond_info* const p, const Addr cond)
64 tl_assert(p->a1 == cond);
65 tl_assert(p->type == ClientCondvar);
67 p->cleanup = (void(*)(DrdClientobj*))(DRD_(cond_cleanup));
74 * Free the memory that was allocated by cond_initialize(). Called by
75 * DRD_(clientobj_remove)().
77 static void DRD_(cond_cleanup)(struct cond_info* p)
83 q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
85 CondDestrErrInfo cde = {
86 DRD_(thread_get_running_tid)(),
89 q ? q->owner : DRD_INVALID_THREADID
91 VG_(maybe_record_error)(VG_(get_running_tid)(),
93 VG_(get_IP)(VG_(get_running_tid)()),
94 "Destroying condition variable that is being"
102 * Report that the synchronization object at address 'addr' is of the
105 static void wrong_type(const Addr addr)
107 GenericErrInfo gei = {
108 .tid = DRD_(thread_get_running_tid)(),
111 VG_(maybe_record_error)(VG_(get_running_tid)(),
113 VG_(get_IP)(VG_(get_running_tid)()),
114 "wrong type of synchronization object",
118 static struct cond_info* cond_get_or_allocate(const Addr cond)
122 tl_assert(offsetof(DrdClientobj, cond) == 0);
123 p = &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
127 if (DRD_(clientobj_present)(cond, cond + 1))
133 p = &(DRD_(clientobj_add)(cond, ClientCondvar)->cond);
134 DRD_(cond_initialize)(p, cond);
138 struct cond_info* DRD_(cond_get)(const Addr cond)
140 tl_assert(offsetof(DrdClientobj, cond) == 0);
141 return &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
144 /** Called before pthread_cond_init(). */
145 void DRD_(cond_pre_init)(const Addr cond)
149 if (DRD_(s_trace_cond))
151 VG_(message)(Vg_UserMsg,
152 "[%d] cond_init cond 0x%lx\n",
153 DRD_(thread_get_running_tid)(),
157 p = DRD_(cond_get)(cond);
161 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
162 VG_(maybe_record_error)(VG_(get_running_tid)(),
164 VG_(get_IP)(VG_(get_running_tid)()),
169 p = cond_get_or_allocate(cond);
172 /** Called after pthread_cond_destroy(). */
173 void DRD_(cond_post_destroy)(const Addr cond)
177 if (DRD_(s_trace_cond))
179 VG_(message)(Vg_UserMsg,
180 "[%d] cond_destroy cond 0x%lx\n",
181 DRD_(thread_get_running_tid)(),
185 p = DRD_(cond_get)(cond);
188 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
189 VG_(maybe_record_error)(VG_(get_running_tid)(),
191 VG_(get_IP)(VG_(get_running_tid)()),
192 "not a condition variable",
197 if (p->waiter_count != 0)
199 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
200 VG_(maybe_record_error)(VG_(get_running_tid)(),
202 VG_(get_IP)(VG_(get_running_tid)()),
203 "destruction of condition variable being waited"
208 DRD_(clientobj_remove)(p->a1, ClientCondvar);
212 * Called before pthread_cond_wait(). Note: before this function is called,
213 * mutex_unlock() has already been called from drd_clientreq.c.
215 void DRD_(cond_pre_wait)(const Addr cond, const Addr mutex)
218 struct mutex_info* q;
220 if (DRD_(s_trace_cond))
222 VG_(message)(Vg_UserMsg,
223 "[%d] cond_pre_wait cond 0x%lx\n",
224 DRD_(thread_get_running_tid)(),
228 p = cond_get_or_allocate(cond);
231 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
232 VG_(maybe_record_error)(VG_(get_running_tid)(),
234 VG_(get_IP)(VG_(get_running_tid)()),
235 "not a condition variable",
240 if (p->waiter_count == 0)
244 else if (p->mutex != mutex)
247 = { .tid = DRD_(thread_get_running_tid)(),
248 .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
249 VG_(maybe_record_error)(VG_(get_running_tid)(),
251 VG_(get_IP)(VG_(get_running_tid)()),
252 "Inconsistent association of condition variable"
257 q = DRD_(mutex_get)(p->mutex);
259 && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0)
261 const ThreadId vg_tid = VG_(get_running_tid)();
262 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
263 q->a1, q->recursion_count, q->owner };
264 VG_(maybe_record_error)(vg_tid,
267 "Mutex locked recursively",
272 DRD_(not_a_mutex)(p->mutex);
279 * Called after pthread_cond_wait().
281 void DRD_(cond_post_wait)(const Addr cond)
285 if (DRD_(s_trace_cond))
287 VG_(message)(Vg_UserMsg,
288 "[%d] cond_post_wait cond 0x%lx\n",
289 DRD_(thread_get_running_tid)(),
293 p = DRD_(cond_get)(cond);
296 struct mutex_info* q;
297 q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
299 CondDestrErrInfo cde = {
300 DRD_(thread_get_running_tid)(),
303 q ? q->owner : DRD_INVALID_THREADID
305 VG_(maybe_record_error)(VG_(get_running_tid)(),
307 VG_(get_IP)(VG_(get_running_tid)()),
308 "condition variable has been destroyed while"
309 " being waited upon",
315 if (p->waiter_count > 0)
318 if (p->waiter_count == 0)
325 static void cond_signal(const DrdThreadId tid, struct cond_info* const cond_p)
327 const ThreadId vg_tid = VG_(get_running_tid)();
328 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
332 if (cond_p->waiter_count > 0)
334 if (DRD_(s_report_signal_unlocked)
335 && ! DRD_(mutex_is_locked_by)(cond_p->mutex, drd_tid))
338 * A signal is sent while the associated mutex has not been locked.
339 * This can indicate but is not necessarily a race condition.
341 CondRaceErrInfo cei = { .tid = DRD_(thread_get_running_tid)(),
343 .mutex = cond_p->mutex,
345 VG_(maybe_record_error)(vg_tid,
355 * No other thread is waiting for the signal, hence the signal will
356 * be lost. This is normal in a POSIX threads application.
361 static void not_initialized(Addr const cond)
363 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
364 VG_(maybe_record_error)(VG_(get_running_tid)(),
366 VG_(get_IP)(VG_(get_running_tid)()),
367 "condition variable has not been initialized",
371 /** Called before pthread_cond_signal(). */
372 void DRD_(cond_pre_signal)(Addr const cond)
376 p = DRD_(cond_get)(cond);
377 if (DRD_(s_trace_cond))
379 VG_(message)(Vg_UserMsg,
380 "[%d] cond_signal cond 0x%lx\n",
381 DRD_(thread_get_running_tid)(),
387 not_initialized(cond);
391 cond_signal(DRD_(thread_get_running_tid)(), p);
394 /** Called before pthread_cond_broadcast(). */
395 void DRD_(cond_pre_broadcast)(Addr const cond)
399 if (DRD_(s_trace_cond))
401 VG_(message)(Vg_UserMsg,
402 "[%d] cond_broadcast cond 0x%lx\n",
403 DRD_(thread_get_running_tid)(),
407 p = DRD_(cond_get)(cond);
410 not_initialized(cond);
414 cond_signal(DRD_(thread_get_running_tid)(), p);