blob: b45977aa78d1700379414c49bafa0b95ad4b9f97 [file] [log] [blame]
bart922304f2011-03-13 12:02:44 +00001/* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
sewardj85642922008-01-14 11:54:56 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardj85642922008-01-14 11:54:56 +00004
bart922304f2011-03-13 12:02:44 +00005 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
sewardj85642922008-01-14 11:54:56 +00006
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.
11
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.
16
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
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
26#include "drd_barrier.h"
bart28230a32008-02-29 17:27:03 +000027#include "drd_clientobj.h"
sewardj85642922008-01-14 11:54:56 +000028#include "drd_error.h"
29#include "drd_suppression.h"
sewardj85642922008-01-14 11:54:56 +000030#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)()
37
38
barta8cf7652009-02-15 11:00:29 +000039/* Type definitions. */
sewardj85642922008-01-14 11:54:56 +000040
bart8ddef882008-03-09 08:48:01 +000041/** Information associated with one thread participating in a barrier. */
bartbebc5d62008-02-24 18:42:53 +000042struct barrier_thread_info
43{
bartbedfd232009-03-26 19:07:15 +000044 UWord tid; // A DrdThreadId declared as UWord because
bart62cc2322010-03-07 10:54:21 +000045 // this member variable is the key of an OSet.
bartd61580e2011-07-29 12:39:44 +000046 Segment* sg; // Segment of the last pthread_barrier() call
47 // by thread tid.
bartbedfd232009-03-26 19:07:15 +000048 Segment* post_wait_sg; // Segment created after *_barrier_wait() finished
bartd61580e2011-07-29 12:39:44 +000049 ExeContext* wait_call_ctxt;// call stack for *_barrier_wait() call.
50 Bool thread_finished;// Whether thread 'tid' has finished.
sewardj85642922008-01-14 11:54:56 +000051};
52
53
barta8cf7652009-02-15 11:00:29 +000054/* Local functions. */
bart28230a32008-02-29 17:27:03 +000055
bartd2c5eae2009-02-21 15:27:04 +000056static void barrier_cleanup(struct barrier_info* p);
57static void barrier_delete_thread(struct barrier_info* const p,
58 const DrdThreadId tid);
59static const char* barrier_get_typename(struct barrier_info* const p);
60static const char* barrier_type_name(const BarrierT bt);
61static
62void barrier_report_wait_delete_race(const struct barrier_info* const p,
bartbedfd232009-03-26 19:07:15 +000063 const struct barrier_thread_info* const q);
bart28230a32008-02-29 17:27:03 +000064
65
barta8cf7652009-02-15 11:00:29 +000066/* Local variables. */
sewardj85642922008-01-14 11:54:56 +000067
bartd2c5eae2009-02-21 15:27:04 +000068static Bool s_trace_barrier = False;
69static ULong s_barrier_segment_creation_count;
sewardj85642922008-01-14 11:54:56 +000070
71
barta8cf7652009-02-15 11:00:29 +000072/* Function definitions. */
sewardj85642922008-01-14 11:54:56 +000073
barta8cf7652009-02-15 11:00:29 +000074void DRD_(barrier_set_trace)(const Bool trace_barrier)
sewardj85642922008-01-14 11:54:56 +000075{
bartbedfd232009-03-26 19:07:15 +000076 s_trace_barrier = trace_barrier;
sewardj85642922008-01-14 11:54:56 +000077}
78
bartd2c5eae2009-02-21 15:27:04 +000079/**
80 * Initialize the structure *p with the specified thread ID and iteration
81 * information.
82 */
barta8cf7652009-02-15 11:00:29 +000083static
84void DRD_(barrier_thread_initialize)(struct barrier_thread_info* const p,
bartcdc3fdb2011-07-29 12:31:33 +000085 const DrdThreadId tid)
sewardj85642922008-01-14 11:54:56 +000086{
bartd61580e2011-07-29 12:39:44 +000087 p->tid = tid;
88 p->sg = NULL;
89 p->post_wait_sg = 0;
90 p->wait_call_ctxt = 0;
91 p->thread_finished = False;
sewardj85642922008-01-14 11:54:56 +000092}
93
bartd2c5eae2009-02-21 15:27:04 +000094/**
95 * Deallocate the memory that is owned by members of
96 * struct barrier_thread_info.
97 */
barta8cf7652009-02-15 11:00:29 +000098static void DRD_(barrier_thread_destroy)(struct barrier_thread_info* const p)
sewardj85642922008-01-14 11:54:56 +000099{
bartbedfd232009-03-26 19:07:15 +0000100 tl_assert(p);
bartd61580e2011-07-29 12:39:44 +0000101 DRD_(sg_put)(p->sg);
bartbedfd232009-03-26 19:07:15 +0000102 DRD_(sg_put)(p->post_wait_sg);
sewardj85642922008-01-14 11:54:56 +0000103}
104
bartd2c5eae2009-02-21 15:27:04 +0000105/**
106 * Initialize the structure *p with the specified client-side barrier address,
107 * barrier object size and number of participants in each barrier.
108 */
sewardj85642922008-01-14 11:54:56 +0000109static
barta8cf7652009-02-15 11:00:29 +0000110void DRD_(barrier_initialize)(struct barrier_info* const p,
111 const Addr barrier,
112 const BarrierT barrier_type,
113 const Word count)
sewardj85642922008-01-14 11:54:56 +0000114{
bartd61580e2011-07-29 12:39:44 +0000115 int i;
116
bartbedfd232009-03-26 19:07:15 +0000117 tl_assert(barrier != 0);
118 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
119 tl_assert(p->a1 == barrier);
sewardj85642922008-01-14 11:54:56 +0000120
bartbedfd232009-03-26 19:07:15 +0000121 p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup;
122 p->delete_thread
123 = (void(*)(DrdClientobj*, DrdThreadId))barrier_delete_thread;
124 p->barrier_type = barrier_type;
125 p->count = count;
126 p->pre_iteration = 0;
127 p->post_iteration = 0;
128 p->pre_waiters_left = count;
129 p->post_waiters_left = count;
bartd2c5eae2009-02-21 15:27:04 +0000130
bartbedfd232009-03-26 19:07:15 +0000131 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word));
132 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid)
133 >= sizeof(DrdThreadId));
bartd61580e2011-07-29 12:39:44 +0000134 for (i = 0; i < 2; i++) {
135 p->oset[i] = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.barrier.bi.1",
136 VG_(free));
137 }
sewardj85642922008-01-14 11:54:56 +0000138}
139
bart195e41f2009-02-15 11:34:57 +0000140/**
bartd2c5eae2009-02-21 15:27:04 +0000141 * Deallocate the memory owned by the struct barrier_info object and also
142 * all the nodes in the OSet p->oset.
143 *
bart195e41f2009-02-15 11:34:57 +0000144 * Called by clientobj_destroy().
bart28230a32008-02-29 17:27:03 +0000145 */
bartd2c5eae2009-02-21 15:27:04 +0000146static void barrier_cleanup(struct barrier_info* p)
sewardj85642922008-01-14 11:54:56 +0000147{
bartbedfd232009-03-26 19:07:15 +0000148 struct barrier_thread_info* q;
149 Segment* latest_sg = 0;
bartd61580e2011-07-29 12:39:44 +0000150 OSet* oset;
151 int i;
sewardj85642922008-01-14 11:54:56 +0000152
bartbedfd232009-03-26 19:07:15 +0000153 tl_assert(p);
sewardj85642922008-01-14 11:54:56 +0000154
bartd61580e2011-07-29 12:39:44 +0000155 DRD_(thread_get_latest_segment)(&latest_sg, DRD_(thread_get_running_tid)());
156 tl_assert(latest_sg);
157
158 if (p->pre_waiters_left != p->count) {
bartd45d9952009-05-31 18:53:54 +0000159 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000160 VG_(maybe_record_error)(VG_(get_running_tid)(),
161 BarrierErr,
162 VG_(get_IP)(VG_(get_running_tid)()),
163 "Destruction of barrier that is being waited"
164 " upon",
165 &bei);
bartd61580e2011-07-29 12:39:44 +0000166 } else {
bartd378eaa2011-07-30 09:35:56 +0000167 oset = p->oset[1 - (p->pre_iteration & 1)];
bartd61580e2011-07-29 12:39:44 +0000168 VG_(OSetGen_ResetIter)(oset);
169 for ( ; (q = VG_(OSetGen_Next)(oset)) != 0; ) {
170 if (q->post_wait_sg && !DRD_(vc_lte)(&q->post_wait_sg->vc,
171 &latest_sg->vc))
172 {
173 barrier_report_wait_delete_race(p, q);
174 }
175 DRD_(barrier_thread_destroy)(q);
bartbedfd232009-03-26 19:07:15 +0000176 }
bartbedfd232009-03-26 19:07:15 +0000177 }
bartd61580e2011-07-29 12:39:44 +0000178
179 for (i = 0; i < 2; i++) {
180 VG_(OSetGen_Destroy)(p->oset[i]);
181 p->oset[i] = NULL;
182 }
bartd2c5eae2009-02-21 15:27:04 +0000183
bartbedfd232009-03-26 19:07:15 +0000184 DRD_(sg_put)(latest_sg);
sewardj85642922008-01-14 11:54:56 +0000185}
186
bartd2c5eae2009-02-21 15:27:04 +0000187/**
188 * Look up the client-side barrier address barrier in s_barrier[]. If not
189 * found, add it.
190 */
sewardj85642922008-01-14 11:54:56 +0000191static
192struct barrier_info*
barta8cf7652009-02-15 11:00:29 +0000193DRD_(barrier_get_or_allocate)(const Addr barrier,
194 const BarrierT barrier_type, const Word count)
sewardj85642922008-01-14 11:54:56 +0000195{
bartbedfd232009-03-26 19:07:15 +0000196 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000197
bartbedfd232009-03-26 19:07:15 +0000198 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
bart0268dfa2008-03-11 20:10:21 +0000199
bartbedfd232009-03-26 19:07:15 +0000200 tl_assert(offsetof(DrdClientobj, barrier) == 0);
201 p = &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
202 if (p == 0)
203 {
204 p = &(DRD_(clientobj_add)(barrier, ClientBarrier)->barrier);
205 DRD_(barrier_initialize)(p, barrier, barrier_type, count);
206 }
207 return p;
bart28230a32008-02-29 17:27:03 +0000208}
209
bartd2c5eae2009-02-21 15:27:04 +0000210/**
bartd61580e2011-07-29 12:39:44 +0000211 * Look up the address of the struct barrier_info associated with the
212 * client-side barrier object.
bartd2c5eae2009-02-21 15:27:04 +0000213 */
barta8cf7652009-02-15 11:00:29 +0000214static struct barrier_info* DRD_(barrier_get)(const Addr barrier)
bart28230a32008-02-29 17:27:03 +0000215{
bartbedfd232009-03-26 19:07:15 +0000216 tl_assert(offsetof(DrdClientobj, barrier) == 0);
217 return &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
sewardj85642922008-01-14 11:54:56 +0000218}
219
bartd2c5eae2009-02-21 15:27:04 +0000220/**
bartd61580e2011-07-29 12:39:44 +0000221 * Initialize a barrier with given client address, barrier type and number of
222 * participants. The 'reinitialization' argument indicates whether a barrier
223 * object is being initialized or reinitialized.
bartd2c5eae2009-02-21 15:27:04 +0000224 *
225 * Called before pthread_barrier_init().
bart28230a32008-02-29 17:27:03 +0000226 */
barta8cf7652009-02-15 11:00:29 +0000227void DRD_(barrier_init)(const Addr barrier,
228 const BarrierT barrier_type, const Word count,
229 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000230{
bartbedfd232009-03-26 19:07:15 +0000231 struct barrier_info* p;
bart306527d2008-03-12 17:23:07 +0000232
bartbedfd232009-03-26 19:07:15 +0000233 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
bart306527d2008-03-12 17:23:07 +0000234
bartbedfd232009-03-26 19:07:15 +0000235 if (count == 0)
236 {
bartd45d9952009-05-31 18:53:54 +0000237 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), barrier, 0, 0 };
bartd9e39ec2008-06-28 15:03:26 +0000238 VG_(maybe_record_error)(VG_(get_running_tid)(),
239 BarrierErr,
240 VG_(get_IP)(VG_(get_running_tid)()),
bartbedfd232009-03-26 19:07:15 +0000241 "pthread_barrier_init: 'count' argument is zero",
bartd9e39ec2008-06-28 15:03:26 +0000242 &bei);
bartbedfd232009-03-26 19:07:15 +0000243 }
bart306527d2008-03-12 17:23:07 +0000244
bartbedfd232009-03-26 19:07:15 +0000245 if (! reinitialization && barrier_type == pthread_barrier)
246 {
247 p = DRD_(barrier_get)(barrier);
248 if (p)
249 {
bartd45d9952009-05-31 18:53:54 +0000250 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), barrier, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000251 VG_(maybe_record_error)(VG_(get_running_tid)(),
252 BarrierErr,
253 VG_(get_IP)(VG_(get_running_tid)()),
254 "Barrier reinitialization",
255 &bei);
256 }
257 }
bartd61580e2011-07-29 12:39:44 +0000258
bartbedfd232009-03-26 19:07:15 +0000259 p = DRD_(barrier_get_or_allocate)(barrier, barrier_type, count);
bart306527d2008-03-12 17:23:07 +0000260
bartb92ff0f2011-10-08 08:29:29 +0000261 if (s_trace_barrier) {
bartbedfd232009-03-26 19:07:15 +0000262 if (reinitialization)
bartb92ff0f2011-10-08 08:29:29 +0000263 DRD_(trace_msg)("[%d] barrier_reinit %s 0x%lx count %ld -> %ld\n",
264 DRD_(thread_get_running_tid)(),
265 barrier_get_typename(p), barrier, p->count, count);
bartbedfd232009-03-26 19:07:15 +0000266 else
bartb92ff0f2011-10-08 08:29:29 +0000267 DRD_(trace_msg)("[%d] barrier_init %s 0x%lx\n",
268 DRD_(thread_get_running_tid)(),
269 barrier_get_typename(p),
270 barrier);
bartbedfd232009-03-26 19:07:15 +0000271 }
272
273 if (reinitialization && p->count != count)
274 {
275 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
276 {
bartd45d9952009-05-31 18:53:54 +0000277 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000278 VG_(maybe_record_error)(VG_(get_running_tid)(),
279 BarrierErr,
280 VG_(get_IP)(VG_(get_running_tid)()),
281 "Reinitialization of barrier with active"
282 " waiters",
283 &bei);
284 }
285 p->count = count;
286 }
sewardj85642922008-01-14 11:54:56 +0000287}
288
bartd2c5eae2009-02-21 15:27:04 +0000289/** Called after pthread_barrier_destroy() / gomp_barrier_destroy(). */
barta8cf7652009-02-15 11:00:29 +0000290void DRD_(barrier_destroy)(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000291{
bartbedfd232009-03-26 19:07:15 +0000292 struct barrier_info* p;
bart72b751c2008-03-01 13:44:24 +0000293
bartbedfd232009-03-26 19:07:15 +0000294 p = DRD_(barrier_get)(barrier);
bart306527d2008-03-12 17:23:07 +0000295
bartbedfd232009-03-26 19:07:15 +0000296 if (s_trace_barrier)
bartb92ff0f2011-10-08 08:29:29 +0000297 DRD_(trace_msg)("[%d] barrier_destroy %s 0x%lx\n",
298 DRD_(thread_get_running_tid)(),
299 barrier_get_typename(p), barrier);
bart72b751c2008-03-01 13:44:24 +0000300
bartbedfd232009-03-26 19:07:15 +0000301 if (p == 0)
302 {
bart62cc2322010-03-07 10:54:21 +0000303 GenericErrInfo GEI = {
304 .tid = DRD_(thread_get_running_tid)(),
305 .addr = barrier,
306 };
bartbedfd232009-03-26 19:07:15 +0000307 VG_(maybe_record_error)(VG_(get_running_tid)(),
308 GenericErr,
309 VG_(get_IP)(VG_(get_running_tid)()),
310 "Not a barrier",
311 &GEI);
312 return;
313 }
bart72b751c2008-03-01 13:44:24 +0000314
bartbedfd232009-03-26 19:07:15 +0000315 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
316 {
bartd45d9952009-05-31 18:53:54 +0000317 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000318 VG_(maybe_record_error)(VG_(get_running_tid)(),
319 BarrierErr,
320 VG_(get_IP)(VG_(get_running_tid)()),
321 "Destruction of a barrier with active waiters",
322 &bei);
323 }
bart195a3982008-07-01 13:15:31 +0000324
bartbedfd232009-03-26 19:07:15 +0000325 DRD_(clientobj_remove)(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000326}
327
bartd2c5eae2009-02-21 15:27:04 +0000328/** Called before pthread_barrier_wait() / gomp_barrier_wait(). */
barta8cf7652009-02-15 11:00:29 +0000329void DRD_(barrier_pre_wait)(const DrdThreadId tid, const Addr barrier,
330 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000331{
bartbedfd232009-03-26 19:07:15 +0000332 struct barrier_info* p;
333 struct barrier_thread_info* q;
334 const UWord word_tid = tid;
bartd61580e2011-07-29 12:39:44 +0000335 OSet* oset;
sewardj85642922008-01-14 11:54:56 +0000336
bartbedfd232009-03-26 19:07:15 +0000337 p = DRD_(barrier_get)(barrier);
bart74b2d972011-10-08 08:54:57 +0000338 if (p == 0 && barrier_type == gomp_barrier) {
bartbedfd232009-03-26 19:07:15 +0000339 /*
340 * gomp_barrier_wait() call has been intercepted but gomp_barrier_init()
341 * not. The only cause I know of that can trigger this is that libgomp.so
342 * has been compiled with --enable-linux-futex.
343 */
bart74b2d972011-10-08 08:54:57 +0000344 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), 0, 0, 0 };
345 VG_(maybe_record_error)(VG_(get_running_tid)(),
346 BarrierErr,
347 VG_(get_IP)(VG_(get_running_tid)()),
348 "Please verify whether gcc has been configured"
349 " with option --disable-linux-futex. See also"
350 " the section about OpenMP in the DRD manual.",
351 &bei);
bartbedfd232009-03-26 19:07:15 +0000352 }
353 tl_assert(p);
sewardj85642922008-01-14 11:54:56 +0000354
bartbedfd232009-03-26 19:07:15 +0000355 if (s_trace_barrier)
bartb92ff0f2011-10-08 08:29:29 +0000356 DRD_(trace_msg)("[%d] barrier_pre_wait %s 0x%lx iteration %ld\n",
357 DRD_(thread_get_running_tid)(),
358 barrier_get_typename(p), barrier, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000359
bartd61580e2011-07-29 12:39:44 +0000360 /* Clean up nodes associated with finished threads. */
bartd378eaa2011-07-30 09:35:56 +0000361 oset = p->oset[p->pre_iteration & 1];
bartd61580e2011-07-29 12:39:44 +0000362 tl_assert(oset);
363 VG_(OSetGen_ResetIter)(oset);
364 for ( ; (q = VG_(OSetGen_Next)(oset)) != 0; ) {
365 if (q->thread_finished) {
366 void* r = VG_(OSetGen_Remove)(oset, &q->tid);
367 tl_assert(r == q);
368 DRD_(barrier_thread_destroy)(q);
369 VG_(OSetGen_FreeNode)(oset, q);
370 VG_(OSetGen_ResetIterAt)(oset, &word_tid);
371 }
372 }
bartbedfd232009-03-26 19:07:15 +0000373 /* Allocate the per-thread data structure if necessary. */
bartd61580e2011-07-29 12:39:44 +0000374 q = VG_(OSetGen_Lookup)(oset, &word_tid);
375 if (q == NULL) {
376 q = VG_(OSetGen_AllocNode)(oset, sizeof(*q));
bartcdc3fdb2011-07-29 12:31:33 +0000377 DRD_(barrier_thread_initialize)(q, tid);
bartd61580e2011-07-29 12:39:44 +0000378 VG_(OSetGen_Insert)(oset, q);
379 tl_assert(VG_(OSetGen_Lookup)(oset, &word_tid) == q);
bartbedfd232009-03-26 19:07:15 +0000380 }
bartd2c5eae2009-02-21 15:27:04 +0000381
bartbedfd232009-03-26 19:07:15 +0000382 /* Record *_barrier_wait() call context. */
383 q->wait_call_ctxt = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
bartd2c5eae2009-02-21 15:27:04 +0000384
bartbedfd232009-03-26 19:07:15 +0000385 /*
386 * Store a pointer to the latest segment of the current thread in the
387 * per-thread data structure.
388 */
bartd61580e2011-07-29 12:39:44 +0000389 DRD_(thread_get_latest_segment)(&q->sg, tid);
bartbebc5d62008-02-24 18:42:53 +0000390
bartbedfd232009-03-26 19:07:15 +0000391 /*
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.
395 */
396 if (--p->pre_waiters_left <= 0)
397 {
bartd378eaa2011-07-30 09:35:56 +0000398 p->pre_iteration++;
bartbedfd232009-03-26 19:07:15 +0000399 p->pre_waiters_left = p->count;
400 }
sewardj85642922008-01-14 11:54:56 +0000401}
402
bartd2c5eae2009-02-21 15:27:04 +0000403/** Called after pthread_barrier_wait() / gomp_barrier_wait(). */
barta8cf7652009-02-15 11:00:29 +0000404void DRD_(barrier_post_wait)(const DrdThreadId tid, const Addr barrier,
bartd2c5eae2009-02-21 15:27:04 +0000405 const BarrierT barrier_type, const Bool waited,
406 const Bool serializing)
sewardj85642922008-01-14 11:54:56 +0000407{
bartbedfd232009-03-26 19:07:15 +0000408 struct barrier_info* p;
409 const UWord word_tid = tid;
410 struct barrier_thread_info* q;
411 struct barrier_thread_info* r;
bartd61580e2011-07-29 12:39:44 +0000412 OSet* oset;
sewardj85642922008-01-14 11:54:56 +0000413
bartbedfd232009-03-26 19:07:15 +0000414 p = DRD_(barrier_get)(barrier);
bartbebc5d62008-02-24 18:42:53 +0000415
bartbedfd232009-03-26 19:07:15 +0000416 if (s_trace_barrier)
bartb92ff0f2011-10-08 08:29:29 +0000417 DRD_(trace_msg)("[%d] barrier_post_wait %s 0x%lx iteration %ld%s\n",
418 tid, p ? barrier_get_typename(p) : "(?)",
419 barrier, p ? p->post_iteration : -1,
420 serializing ? " (serializing)" : "");
sewardj85642922008-01-14 11:54:56 +0000421
bartbedfd232009-03-26 19:07:15 +0000422 /*
423 * If p == 0, this means that the barrier has been destroyed after
424 * *_barrier_wait() returned and before this function was called. Just
425 * return in that case -- race conditions between *_barrier_wait()
426 * and *_barrier_destroy() are detected by the *_barrier_destroy() wrapper.
427 */
428 if (p == 0)
429 return;
bart306527d2008-03-12 17:23:07 +0000430
bartbedfd232009-03-26 19:07:15 +0000431 /* If the *_barrier_wait() call returned an error code, exit. */
432 if (! waited)
433 return;
bartd2c5eae2009-02-21 15:27:04 +0000434
bartd378eaa2011-07-30 09:35:56 +0000435 oset = p->oset[p->post_iteration & 1];
bartd61580e2011-07-29 12:39:44 +0000436 q = VG_(OSetGen_Lookup)(oset, &word_tid);
bartd378eaa2011-07-30 09:35:56 +0000437 if (p->pre_iteration - p->post_iteration > 1) {
438 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
439 VG_(maybe_record_error)(VG_(get_running_tid)(),
440 BarrierErr,
441 VG_(get_IP)(VG_(get_running_tid)()),
442 "Number of concurrent pthread_barrier_wait()"
443 " calls exceeds the barrier count",
444 &bei);
445 } else if (q == NULL) {
bartd45d9952009-05-31 18:53:54 +0000446 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000447 VG_(maybe_record_error)(VG_(get_running_tid)(),
448 BarrierErr,
449 VG_(get_IP)(VG_(get_running_tid)()),
450 "Error in barrier implementation"
451 " -- barrier_wait() started before"
452 " barrier_destroy() and finished after"
453 " barrier_destroy()",
454 &bei);
bartd378eaa2011-07-30 09:35:56 +0000455 }
456 if (q == NULL) {
bartd61580e2011-07-29 12:39:44 +0000457 q = VG_(OSetGen_AllocNode)(oset, sizeof(*q));
bartcdc3fdb2011-07-29 12:31:33 +0000458 DRD_(barrier_thread_initialize)(q, tid);
bartd61580e2011-07-29 12:39:44 +0000459 VG_(OSetGen_Insert)(oset, q);
460 tl_assert(VG_(OSetGen_Lookup)(oset, &word_tid) == q);
bartd378eaa2011-07-30 09:35:56 +0000461 DRD_(thread_get_latest_segment)(&q->sg, tid);
bartbedfd232009-03-26 19:07:15 +0000462 }
bart7627be32009-06-06 12:26:05 +0000463
464 /* Create a new segment and store a pointer to that segment. */
465 DRD_(thread_new_segment)(tid);
466 DRD_(thread_get_latest_segment)(&q->post_wait_sg, tid);
467 s_barrier_segment_creation_count++;
468
bartbedfd232009-03-26 19:07:15 +0000469 /*
470 * Combine all vector clocks that were stored in the pre_barrier_wait
471 * wrapper with the vector clock of the current thread.
472 */
bartbedfd232009-03-26 19:07:15 +0000473 {
bart8f822af2009-06-08 18:20:42 +0000474 VectorClock old_vc;
475
476 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
bartd61580e2011-07-29 12:39:44 +0000477 VG_(OSetGen_ResetIter)(oset);
478 for ( ; (r = VG_(OSetGen_Next)(oset)) != 0; )
bartbedfd232009-03-26 19:07:15 +0000479 {
bart8f822af2009-06-08 18:20:42 +0000480 if (r != q)
481 {
bartd61580e2011-07-29 12:39:44 +0000482 tl_assert(r->sg);
bart8f822af2009-06-08 18:20:42 +0000483 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc,
bartd61580e2011-07-29 12:39:44 +0000484 &r->sg->vc);
bart8f822af2009-06-08 18:20:42 +0000485 }
bartbedfd232009-03-26 19:07:15 +0000486 }
bart8f822af2009-06-08 18:20:42 +0000487 DRD_(thread_update_conflict_set)(tid, &old_vc);
488 DRD_(vc_cleanup)(&old_vc);
bartbedfd232009-03-26 19:07:15 +0000489 }
bartbebc5d62008-02-24 18:42:53 +0000490
bartbedfd232009-03-26 19:07:15 +0000491 /*
492 * If the same number of threads as the barrier count indicates have
493 * called the post *_barrier_wait() wrapper, toggle p->post_iteration and
494 * reset the p->post_waiters_left counter.
495 */
496 if (--p->post_waiters_left <= 0)
497 {
bartd378eaa2011-07-30 09:35:56 +0000498 p->post_iteration++;
bartbedfd232009-03-26 19:07:15 +0000499 p->post_waiters_left = p->count;
500 }
sewardj85642922008-01-14 11:54:56 +0000501}
502
bartd2c5eae2009-02-21 15:27:04 +0000503/** Called when thread tid stops to exist. */
504static void barrier_delete_thread(struct barrier_info* const p,
505 const DrdThreadId tid)
sewardj85642922008-01-14 11:54:56 +0000506{
bartbedfd232009-03-26 19:07:15 +0000507 struct barrier_thread_info* q;
508 const UWord word_tid = tid;
bartd61580e2011-07-29 12:39:44 +0000509 int i;
sewardj85642922008-01-14 11:54:56 +0000510
bartd61580e2011-07-29 12:39:44 +0000511 for (i = 0; i < 2; i++) {
512 q = VG_(OSetGen_Lookup)(p->oset[i], &word_tid);
513 if (q)
514 q->thread_finished = True;
bartbedfd232009-03-26 19:07:15 +0000515 }
sewardj85642922008-01-14 11:54:56 +0000516}
bart306527d2008-03-12 17:23:07 +0000517
bartd2c5eae2009-02-21 15:27:04 +0000518/**
519 * Report that *_barrier_destroy() has been called but that this call was
520 * not synchronized with the last *_barrier_wait() call on the same barrier.
bart776a91e2009-02-22 09:29:07 +0000521 *
522 * This topic has been discussed extensively on comp.programming.threads
523 * (February 3, 2009). See also
524 * <a href="http://groups.google.com/group/comp.programming.threads/browse_thread/thread/4f65535d6192aa50/a5f4bf1e3b437c4d">Immediately destroying pthread barriers</a>.
bartd2c5eae2009-02-21 15:27:04 +0000525 */
526static
527void barrier_report_wait_delete_race(const struct barrier_info* const p,
528 const struct barrier_thread_info* const q)
529{
bartbedfd232009-03-26 19:07:15 +0000530 tl_assert(p);
531 tl_assert(q);
bartd2c5eae2009-02-21 15:27:04 +0000532
bartbedfd232009-03-26 19:07:15 +0000533 {
534 BarrierErrInfo bei
bartd45d9952009-05-31 18:53:54 +0000535 = { DRD_(thread_get_running_tid)(), p->a1, q->tid, q->wait_call_ctxt };
bartbedfd232009-03-26 19:07:15 +0000536 VG_(maybe_record_error)(VG_(get_running_tid)(),
537 BarrierErr,
538 VG_(get_IP)(VG_(get_running_tid)()),
539 "Destruction of barrier not synchronized with"
540 " barrier wait call",
541 &bei);
542 }
bartd2c5eae2009-02-21 15:27:04 +0000543}
544
545static const char* barrier_get_typename(struct barrier_info* const p)
bart306527d2008-03-12 17:23:07 +0000546{
bartbedfd232009-03-26 19:07:15 +0000547 tl_assert(p);
bart306527d2008-03-12 17:23:07 +0000548
bartbedfd232009-03-26 19:07:15 +0000549 return barrier_type_name(p->barrier_type);
bart306527d2008-03-12 17:23:07 +0000550}
551
bartd2c5eae2009-02-21 15:27:04 +0000552static const char* barrier_type_name(const BarrierT bt)
bart306527d2008-03-12 17:23:07 +0000553{
bartbedfd232009-03-26 19:07:15 +0000554 switch (bt)
555 {
556 case pthread_barrier:
557 return "pthread barrier";
558 case gomp_barrier:
559 return "gomp barrier";
560 }
561 return "?";
bart306527d2008-03-12 17:23:07 +0000562}
bart6bbefaf2008-04-19 15:16:45 +0000563
barta8cf7652009-02-15 11:00:29 +0000564ULong DRD_(get_barrier_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000565{
bartbedfd232009-03-26 19:07:15 +0000566 return s_barrier_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000567}