Check some known PATHs for mpicc in configure.
[valgrind.git] / drd / drd_cond.c
blobbcdef3066173deefc8d59b97d0319470c1521f38
1 /*
2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2013 Bart Van Assche <bvanassche@acm.org>.
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.
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.
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.
21 The GNU General Public License is contained in the file COPYING.
25 #include "drd_clientobj.h"
26 #include "drd_cond.h"
27 #include "drd_error.h"
28 #include "drd_mutex.h"
29 #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
30 #include "pub_tool_libcassert.h" /* tl_assert() */
31 #include "pub_tool_libcbase.h" /* VG_(memcmp)() */
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;
60 static
61 void DRD_(cond_initialize)(struct cond_info* const p, const Addr cond)
63 tl_assert(cond != 0);
64 tl_assert(p->a1 == cond);
65 tl_assert(p->type == ClientCondvar);
67 p->cleanup = (void(*)(DrdClientobj*))(DRD_(cond_cleanup));
68 p->delete_thread = 0;
69 p->waiter_count = 0;
70 p->mutex = 0;
73 /**
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)
79 tl_assert(p);
80 if (p->mutex)
82 struct mutex_info* q;
83 q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex);
85 CondDestrErrInfo cde = {
86 DRD_(thread_get_running_tid)(),
87 p->a1,
88 q ? q->a1 : 0,
89 q ? q->owner : DRD_INVALID_THREADID
91 VG_(maybe_record_error)(VG_(get_running_tid)(),
92 CondDestrErr,
93 VG_(get_IP)(VG_(get_running_tid)()),
94 "Destroying condition variable that is being"
95 " waited upon",
96 &cde);
102 * Report that the synchronization object at address 'addr' is of the
103 * wrong type.
105 static void wrong_type(const Addr addr)
107 GenericErrInfo gei = {
108 .tid = DRD_(thread_get_running_tid)(),
109 .addr = addr,
111 VG_(maybe_record_error)(VG_(get_running_tid)(),
112 GenericErr,
113 VG_(get_IP)(VG_(get_running_tid)()),
114 "wrong type of synchronization object",
115 &gei);
118 static struct cond_info* cond_get_or_allocate(const Addr cond)
120 struct cond_info *p;
122 tl_assert(offsetof(DrdClientobj, cond) == 0);
123 p = &(DRD_(clientobj_get)(cond, ClientCondvar)->cond);
124 if (p)
125 return p;
127 if (DRD_(clientobj_present)(cond, cond + 1))
129 wrong_type(cond);
130 return 0;
133 p = &(DRD_(clientobj_add)(cond, ClientCondvar)->cond);
134 DRD_(cond_initialize)(p, cond);
135 return p;
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)
147 struct cond_info* p;
149 if (DRD_(s_trace_cond))
150 DRD_(trace_msg)("[%d] cond_init cond 0x%lx",
151 DRD_(thread_get_running_tid)(), cond);
153 p = DRD_(cond_get)(cond);
155 if (p) {
156 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
157 VG_(maybe_record_error)(VG_(get_running_tid)(),
158 CondErr,
159 VG_(get_IP)(VG_(get_running_tid)()),
160 "initialized twice",
161 &cei);
164 cond_get_or_allocate(cond);
167 /** Called after pthread_cond_destroy(). */
168 void DRD_(cond_post_destroy)(const Addr cond, const Bool destroy_succeeded)
170 struct cond_info* p;
172 if (DRD_(s_trace_cond))
173 DRD_(trace_msg)("[%d] cond_destroy cond 0x%lx",
174 DRD_(thread_get_running_tid)(), cond);
176 p = DRD_(cond_get)(cond);
177 if (p == 0)
179 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
180 VG_(maybe_record_error)(VG_(get_running_tid)(),
181 CondErr,
182 VG_(get_IP)(VG_(get_running_tid)()),
183 "not a condition variable",
184 &cei);
185 return;
188 if (p->waiter_count != 0)
190 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
191 VG_(maybe_record_error)(VG_(get_running_tid)(),
192 CondErr,
193 VG_(get_IP)(VG_(get_running_tid)()),
194 "destruction of condition variable being waited"
195 " upon",
196 &cei);
199 if (destroy_succeeded)
200 DRD_(clientobj_remove)(p->a1, ClientCondvar);
204 * Called before pthread_cond_wait(). Note: before this function is called,
205 * mutex_unlock() has already been called from drd_clientreq.c.
207 void DRD_(cond_pre_wait)(const Addr cond, const Addr mutex)
209 struct cond_info* p;
210 struct mutex_info* q;
212 if (DRD_(s_trace_cond))
213 DRD_(trace_msg)("[%d] cond_pre_wait cond 0x%lx",
214 DRD_(thread_get_running_tid)(), cond);
216 p = cond_get_or_allocate(cond);
217 if (!p)
219 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
220 VG_(maybe_record_error)(VG_(get_running_tid)(),
221 CondErr,
222 VG_(get_IP)(VG_(get_running_tid)()),
223 "not a condition variable",
224 &cei);
225 return;
228 if (p->waiter_count == 0)
230 p->mutex = mutex;
232 else if (p->mutex != mutex)
234 CondWaitErrInfo cwei
235 = { .tid = DRD_(thread_get_running_tid)(),
236 .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
237 VG_(maybe_record_error)(VG_(get_running_tid)(),
238 CondWaitErr,
239 VG_(get_IP)(VG_(get_running_tid)()),
240 "Inconsistent association of condition variable"
241 " and mutex",
242 &cwei);
244 tl_assert(p->mutex);
245 q = DRD_(mutex_get)(p->mutex);
246 if (q
247 && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0)
249 const ThreadId vg_tid = VG_(get_running_tid)();
250 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
251 q->a1, q->recursion_count, q->owner };
252 VG_(maybe_record_error)(vg_tid,
253 MutexErr,
254 VG_(get_IP)(vg_tid),
255 "Mutex locked recursively",
256 &MEI);
258 else if (q == 0)
260 DRD_(not_a_mutex)(p->mutex);
263 ++p->waiter_count;
267 * Called after pthread_cond_wait().
269 void DRD_(cond_post_wait)(const Addr cond)
271 struct cond_info* p;
273 if (DRD_(s_trace_cond))
274 DRD_(trace_msg)("[%d] cond_post_wait cond 0x%lx",
275 DRD_(thread_get_running_tid)(), cond);
277 p = DRD_(cond_get)(cond);
278 if (!p)
280 CondDestrErrInfo cde = {
281 DRD_(thread_get_running_tid)(), cond, 0, DRD_INVALID_THREADID
283 VG_(maybe_record_error)(VG_(get_running_tid)(),
284 CondDestrErr,
285 VG_(get_IP)(VG_(get_running_tid)()),
286 "condition variable has been destroyed while"
287 " being waited upon",
288 &cde);
289 return;
292 if (p->waiter_count > 0)
294 --p->waiter_count;
295 if (p->waiter_count == 0)
297 p->mutex = 0;
302 static void cond_signal(const DrdThreadId tid, struct cond_info* const cond_p)
304 const ThreadId vg_tid = VG_(get_running_tid)();
305 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
307 tl_assert(cond_p);
309 if (cond_p->waiter_count > 0)
311 if (DRD_(s_report_signal_unlocked)
312 && ! DRD_(mutex_is_locked_by)(cond_p->mutex, drd_tid))
315 * A signal is sent while the associated mutex has not been locked.
316 * This can indicate but is not necessarily a race condition.
318 CondRaceErrInfo cei = { .tid = DRD_(thread_get_running_tid)(),
319 .cond = cond_p->a1,
320 .mutex = cond_p->mutex,
322 VG_(maybe_record_error)(vg_tid,
323 CondRaceErr,
324 VG_(get_IP)(vg_tid),
325 "CondErr",
326 &cei);
329 else
332 * No other thread is waiting for the signal, hence the signal will
333 * be lost. This is normal in a POSIX threads application.
338 static void not_initialized(Addr const cond)
340 CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond };
341 VG_(maybe_record_error)(VG_(get_running_tid)(),
342 CondErr,
343 VG_(get_IP)(VG_(get_running_tid)()),
344 "condition variable has not been initialized",
345 &cei);
348 /** Called before pthread_cond_signal(). */
349 void DRD_(cond_pre_signal)(Addr const cond)
351 struct cond_info* p;
353 p = DRD_(cond_get)(cond);
354 if (DRD_(s_trace_cond))
355 DRD_(trace_msg)("[%d] cond_signal cond 0x%lx",
356 DRD_(thread_get_running_tid)(), cond);
358 tl_assert(DRD_(pthread_cond_initializer));
359 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
360 DRD_(pthread_cond_initializer_size)) != 0)
362 not_initialized(cond);
363 return;
366 if (!p)
367 p = cond_get_or_allocate(cond);
369 cond_signal(DRD_(thread_get_running_tid)(), p);
372 /** Called before pthread_cond_broadcast(). */
373 void DRD_(cond_pre_broadcast)(Addr const cond)
375 struct cond_info* p;
377 if (DRD_(s_trace_cond))
378 DRD_(trace_msg)("[%d] cond_broadcast cond 0x%lx",
379 DRD_(thread_get_running_tid)(), cond);
381 p = DRD_(cond_get)(cond);
382 tl_assert(DRD_(pthread_cond_initializer));
383 if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer),
384 DRD_(pthread_cond_initializer_size)) != 0)
386 not_initialized(cond);
387 return;
390 if (!p)
391 p = cond_get_or_allocate(cond);
393 cond_signal(DRD_(thread_get_running_tid)(), p);