1 /* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
3 This file is part of drd, a thread error detector.
5 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
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_barrier.h"
27 #include "drd_clientobj.h"
28 #include "drd_error.h"
29 #include "drd_suppression.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_mallocfree.h" // VG_(malloc)(), VG_(free)()
35 #include "pub_tool_oset.h"
36 #include "pub_tool_threadstate.h" // VG_(get_running_tid)()
39 /* Type definitions. */
41 /** Information associated with one thread participating in a barrier. */
42 struct barrier_thread_info
44 UWord tid; // A DrdThreadId declared as UWord because
45 // this member variable is the key of an OSet.
46 Word iteration; // iteration of last pthread_barrier_wait()
47 // call thread tid participated in.
48 Segment* sg[2]; // Segments of the last two
49 // pthread_barrier() calls by thread tid.
50 ExeContext* wait_call_ctxt;// call stack for *_barrier_wait() call.
51 Segment* post_wait_sg; // Segment created after *_barrier_wait() finished
55 /* Local functions. */
57 static void barrier_cleanup(struct barrier_info* p);
58 static void barrier_delete_thread(struct barrier_info* const p,
59 const DrdThreadId tid);
60 static const char* barrier_get_typename(struct barrier_info* const p);
61 static const char* barrier_type_name(const BarrierT bt);
63 void barrier_report_wait_delete_race(const struct barrier_info* const p,
64 const struct barrier_thread_info* const q);
67 /* Local variables. */
69 static Bool s_trace_barrier = False;
70 static ULong s_barrier_segment_creation_count;
73 /* Function definitions. */
75 void DRD_(barrier_set_trace)(const Bool trace_barrier)
77 s_trace_barrier = trace_barrier;
81 * Initialize the structure *p with the specified thread ID and iteration
85 void DRD_(barrier_thread_initialize)(struct barrier_thread_info* const p,
86 const DrdThreadId tid,
90 p->iteration = iteration;
93 p->wait_call_ctxt = 0;
98 * Deallocate the memory that is owned by members of
99 * struct barrier_thread_info.
101 static void DRD_(barrier_thread_destroy)(struct barrier_thread_info* const p)
104 DRD_(sg_put)(p->sg[0]);
105 DRD_(sg_put)(p->sg[1]);
106 DRD_(sg_put)(p->post_wait_sg);
110 * Initialize the structure *p with the specified client-side barrier address,
111 * barrier object size and number of participants in each barrier.
114 void DRD_(barrier_initialize)(struct barrier_info* const p,
116 const BarrierT barrier_type,
119 tl_assert(barrier != 0);
120 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
121 tl_assert(p->a1 == barrier);
123 p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup;
125 = (void(*)(DrdClientobj*, DrdThreadId))barrier_delete_thread;
126 p->barrier_type = barrier_type;
128 p->pre_iteration = 0;
129 p->post_iteration = 0;
130 p->pre_waiters_left = count;
131 p->post_waiters_left = count;
133 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word));
134 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid)
135 >= sizeof(DrdThreadId));
136 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.barrier.bi.1",
141 * Deallocate the memory owned by the struct barrier_info object and also
142 * all the nodes in the OSet p->oset.
144 * Called by clientobj_destroy().
146 static void barrier_cleanup(struct barrier_info* p)
148 struct barrier_thread_info* q;
149 Segment* latest_sg = 0;
153 if (p->pre_waiters_left != p->count)
155 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
156 VG_(maybe_record_error)(VG_(get_running_tid)(),
158 VG_(get_IP)(VG_(get_running_tid)()),
159 "Destruction of barrier that is being waited"
164 DRD_(thread_get_latest_segment)(&latest_sg, DRD_(thread_get_running_tid)());
165 tl_assert(latest_sg);
167 VG_(OSetGen_ResetIter)(p->oset);
168 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
171 && ! DRD_(vc_lte)(&q->post_wait_sg->vc, &latest_sg->vc))
173 barrier_report_wait_delete_race(p, q);
176 DRD_(barrier_thread_destroy)(q);
178 VG_(OSetGen_Destroy)(p->oset);
180 DRD_(sg_put)(latest_sg);
184 * Look up the client-side barrier address barrier in s_barrier[]. If not
189 DRD_(barrier_get_or_allocate)(const Addr barrier,
190 const BarrierT barrier_type, const Word count)
192 struct barrier_info *p;
194 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
196 tl_assert(offsetof(DrdClientobj, barrier) == 0);
197 p = &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
200 p = &(DRD_(clientobj_add)(barrier, ClientBarrier)->barrier);
201 DRD_(barrier_initialize)(p, barrier, barrier_type, count);
207 * Look up the address of the information associated with the client-side
210 static struct barrier_info* DRD_(barrier_get)(const Addr barrier)
212 tl_assert(offsetof(DrdClientobj, barrier) == 0);
213 return &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
217 * Initialize a barrier with client address barrier, client size size, and
218 * where count threads participate in each barrier.
220 * Called before pthread_barrier_init().
222 void DRD_(barrier_init)(const Addr barrier,
223 const BarrierT barrier_type, const Word count,
224 const Bool reinitialization)
226 struct barrier_info* p;
228 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
232 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), barrier, 0, 0 };
233 VG_(maybe_record_error)(VG_(get_running_tid)(),
235 VG_(get_IP)(VG_(get_running_tid)()),
236 "pthread_barrier_init: 'count' argument is zero",
240 if (! reinitialization && barrier_type == pthread_barrier)
242 p = DRD_(barrier_get)(barrier);
245 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), barrier, 0, 0 };
246 VG_(maybe_record_error)(VG_(get_running_tid)(),
248 VG_(get_IP)(VG_(get_running_tid)()),
249 "Barrier reinitialization",
253 p = DRD_(barrier_get_or_allocate)(barrier, barrier_type, count);
257 if (reinitialization)
259 VG_(message)(Vg_UserMsg,
260 "[%d] barrier_reinit %s 0x%lx count %ld -> %ld\n",
261 DRD_(thread_get_running_tid)(),
262 barrier_get_typename(p),
269 VG_(message)(Vg_UserMsg,
270 "[%d] barrier_init %s 0x%lx\n",
271 DRD_(thread_get_running_tid)(),
272 barrier_get_typename(p),
277 if (reinitialization && p->count != count)
279 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
281 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
282 VG_(maybe_record_error)(VG_(get_running_tid)(),
284 VG_(get_IP)(VG_(get_running_tid)()),
285 "Reinitialization of barrier with active"
293 /** Called after pthread_barrier_destroy() / gomp_barrier_destroy(). */
294 void DRD_(barrier_destroy)(const Addr barrier, const BarrierT barrier_type)
296 struct barrier_info* p;
298 p = DRD_(barrier_get)(barrier);
302 VG_(message)(Vg_UserMsg,
303 "[%d] barrier_destroy %s 0x%lx\n",
304 DRD_(thread_get_running_tid)(),
305 barrier_get_typename(p),
311 GenericErrInfo GEI = {
312 .tid = DRD_(thread_get_running_tid)(),
315 VG_(maybe_record_error)(VG_(get_running_tid)(),
317 VG_(get_IP)(VG_(get_running_tid)()),
323 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
325 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
326 VG_(maybe_record_error)(VG_(get_running_tid)(),
328 VG_(get_IP)(VG_(get_running_tid)()),
329 "Destruction of a barrier with active waiters",
333 DRD_(clientobj_remove)(p->a1, ClientBarrier);
336 /** Called before pthread_barrier_wait() / gomp_barrier_wait(). */
337 void DRD_(barrier_pre_wait)(const DrdThreadId tid, const Addr barrier,
338 const BarrierT barrier_type)
340 struct barrier_info* p;
341 struct barrier_thread_info* q;
342 const UWord word_tid = tid;
344 p = DRD_(barrier_get)(barrier);
345 if (p == 0 && barrier_type == gomp_barrier)
348 * gomp_barrier_wait() call has been intercepted but gomp_barrier_init()
349 * not. The only cause I know of that can trigger this is that libgomp.so
350 * has been compiled with --enable-linux-futex.
352 VG_(message)(Vg_UserMsg, "\n");
353 VG_(message)(Vg_UserMsg,
354 "Please verify whether gcc has been configured"
355 " with option --disable-linux-futex.\n");
356 VG_(message)(Vg_UserMsg,
357 "See also the section about OpenMP in the DRD manual.\n");
358 VG_(message)(Vg_UserMsg, "\n");
364 VG_(message)(Vg_UserMsg,
365 "[%d] barrier_pre_wait %s 0x%lx iteration %ld\n",
366 DRD_(thread_get_running_tid)(),
367 barrier_get_typename(p),
372 /* Allocate the per-thread data structure if necessary. */
373 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
376 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
377 DRD_(barrier_thread_initialize)(q, tid, p->pre_iteration);
378 VG_(OSetGen_Insert)(p->oset, q);
379 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
382 /* Record *_barrier_wait() call context. */
383 q->wait_call_ctxt = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
386 * Store a pointer to the latest segment of the current thread in the
387 * per-thread data structure.
389 DRD_(thread_get_latest_segment)(&q->sg[p->pre_iteration], tid);
392 * If the same number of threads as the barrier count indicates have
393 * called the pre *_barrier_wait() wrapper, toggle p->pre_iteration and
394 * reset the p->pre_waiters_left counter.
396 if (--p->pre_waiters_left <= 0)
398 p->pre_iteration = 1 - p->pre_iteration;
399 p->pre_waiters_left = p->count;
403 /** Called after pthread_barrier_wait() / gomp_barrier_wait(). */
404 void DRD_(barrier_post_wait)(const DrdThreadId tid, const Addr barrier,
405 const BarrierT barrier_type, const Bool waited,
406 const Bool serializing)
408 struct barrier_info* p;
409 const UWord word_tid = tid;
410 struct barrier_thread_info* q;
411 struct barrier_thread_info* r;
413 p = DRD_(barrier_get)(barrier);
417 VG_(message)(Vg_UserMsg,
418 "[%d] barrier_post_wait %s 0x%lx iteration %ld%s\n",
420 p ? barrier_get_typename(p) : "(?)",
422 p ? p->post_iteration : -1,
423 serializing ? " (serializing)" : "");
427 * If p == 0, this means that the barrier has been destroyed after
428 * *_barrier_wait() returned and before this function was called. Just
429 * return in that case -- race conditions between *_barrier_wait()
430 * and *_barrier_destroy() are detected by the *_barrier_destroy() wrapper.
435 /* If the *_barrier_wait() call returned an error code, exit. */
439 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
442 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
443 VG_(maybe_record_error)(VG_(get_running_tid)(),
445 VG_(get_IP)(VG_(get_running_tid)()),
446 "Error in barrier implementation"
447 " -- barrier_wait() started before"
448 " barrier_destroy() and finished after"
449 " barrier_destroy()",
452 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
453 DRD_(barrier_thread_initialize)(q, tid, p->pre_iteration);
454 VG_(OSetGen_Insert)(p->oset, q);
455 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
458 /* Create a new segment and store a pointer to that segment. */
459 DRD_(thread_new_segment)(tid);
460 DRD_(thread_get_latest_segment)(&q->post_wait_sg, tid);
461 s_barrier_segment_creation_count++;
464 * Combine all vector clocks that were stored in the pre_barrier_wait
465 * wrapper with the vector clock of the current thread.
470 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
471 VG_(OSetGen_ResetIter)(p->oset);
472 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
476 tl_assert(r->sg[p->post_iteration]);
477 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc,
478 &r->sg[p->post_iteration]->vc);
481 DRD_(thread_update_conflict_set)(tid, &old_vc);
482 DRD_(vc_cleanup)(&old_vc);
486 * If the same number of threads as the barrier count indicates have
487 * called the post *_barrier_wait() wrapper, toggle p->post_iteration and
488 * reset the p->post_waiters_left counter.
490 if (--p->post_waiters_left <= 0)
492 p->post_iteration = 1 - p->post_iteration;
493 p->post_waiters_left = p->count;
497 /** Called when thread tid stops to exist. */
498 static void barrier_delete_thread(struct barrier_info* const p,
499 const DrdThreadId tid)
501 struct barrier_thread_info* q;
502 const UWord word_tid = tid;
504 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
507 * q is only non-zero if the barrier object has been used by thread tid
508 * after the barrier_init() call and before the thread finished.
512 DRD_(barrier_thread_destroy)(q);
513 VG_(OSetGen_FreeNode)(p->oset, q);
518 * Report that *_barrier_destroy() has been called but that this call was
519 * not synchronized with the last *_barrier_wait() call on the same barrier.
521 * This topic has been discussed extensively on comp.programming.threads
522 * (February 3, 2009). See also
523 * <a href="http://groups.google.com/group/comp.programming.threads/browse_thread/thread/4f65535d6192aa50/a5f4bf1e3b437c4d">Immediately destroying pthread barriers</a>.
526 void barrier_report_wait_delete_race(const struct barrier_info* const p,
527 const struct barrier_thread_info* const q)
534 = { DRD_(thread_get_running_tid)(), p->a1, q->tid, q->wait_call_ctxt };
535 VG_(maybe_record_error)(VG_(get_running_tid)(),
537 VG_(get_IP)(VG_(get_running_tid)()),
538 "Destruction of barrier not synchronized with"
539 " barrier wait call",
544 static const char* barrier_get_typename(struct barrier_info* const p)
548 return barrier_type_name(p->barrier_type);
551 static const char* barrier_type_name(const BarrierT bt)
555 case pthread_barrier:
556 return "pthread barrier";
558 return "gomp barrier";
563 ULong DRD_(get_barrier_segment_creation_count)(void)
565 return s_barrier_segment_creation_count;