blob: d8e06cb1812d8664bf4ec9b1b08ece3a5cec5c53 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
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
sewardj9eecbbb2010-05-03 21:37:12 +00005 Copyright (C) 2006-2010 Bart Van Assche <bart.vanassche@gmail.com>.
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.
bartbedfd232009-03-26 19:07:15 +000046 Word iteration; // iteration of last pthread_barrier_wait()
bart62cc2322010-03-07 10:54:21 +000047 // call thread tid participated in.
bartbedfd232009-03-26 19:07:15 +000048 Segment* sg[2]; // Segments of the last two
bart62cc2322010-03-07 10:54:21 +000049 // pthread_barrier() calls by thread tid.
bartbedfd232009-03-26 19:07:15 +000050 ExeContext* wait_call_ctxt;// call stack for *_barrier_wait() call.
51 Segment* post_wait_sg; // Segment created after *_barrier_wait() finished
sewardj85642922008-01-14 11:54:56 +000052};
53
54
barta8cf7652009-02-15 11:00:29 +000055/* Local functions. */
bart28230a32008-02-29 17:27:03 +000056
bartd2c5eae2009-02-21 15:27:04 +000057static void barrier_cleanup(struct barrier_info* p);
58static void barrier_delete_thread(struct barrier_info* const p,
59 const DrdThreadId tid);
60static const char* barrier_get_typename(struct barrier_info* const p);
61static const char* barrier_type_name(const BarrierT bt);
62static
63void barrier_report_wait_delete_race(const struct barrier_info* const p,
bartbedfd232009-03-26 19:07:15 +000064 const struct barrier_thread_info* const q);
bart28230a32008-02-29 17:27:03 +000065
66
barta8cf7652009-02-15 11:00:29 +000067/* Local variables. */
sewardj85642922008-01-14 11:54:56 +000068
bartd2c5eae2009-02-21 15:27:04 +000069static Bool s_trace_barrier = False;
70static ULong s_barrier_segment_creation_count;
sewardj85642922008-01-14 11:54:56 +000071
72
barta8cf7652009-02-15 11:00:29 +000073/* Function definitions. */
sewardj85642922008-01-14 11:54:56 +000074
barta8cf7652009-02-15 11:00:29 +000075void DRD_(barrier_set_trace)(const Bool trace_barrier)
sewardj85642922008-01-14 11:54:56 +000076{
bartbedfd232009-03-26 19:07:15 +000077 s_trace_barrier = trace_barrier;
sewardj85642922008-01-14 11:54:56 +000078}
79
bartd2c5eae2009-02-21 15:27:04 +000080/**
81 * Initialize the structure *p with the specified thread ID and iteration
82 * information.
83 */
barta8cf7652009-02-15 11:00:29 +000084static
85void DRD_(barrier_thread_initialize)(struct barrier_thread_info* const p,
86 const DrdThreadId tid,
87 const Word iteration)
sewardj85642922008-01-14 11:54:56 +000088{
bartbedfd232009-03-26 19:07:15 +000089 p->tid = tid;
90 p->iteration = iteration;
91 p->sg[0] = 0;
92 p->sg[1] = 0;
93 p->wait_call_ctxt = 0;
94 p->post_wait_sg = 0;
sewardj85642922008-01-14 11:54:56 +000095}
96
bartd2c5eae2009-02-21 15:27:04 +000097/**
98 * Deallocate the memory that is owned by members of
99 * struct barrier_thread_info.
100 */
barta8cf7652009-02-15 11:00:29 +0000101static void DRD_(barrier_thread_destroy)(struct barrier_thread_info* const p)
sewardj85642922008-01-14 11:54:56 +0000102{
bartbedfd232009-03-26 19:07:15 +0000103 tl_assert(p);
104 DRD_(sg_put)(p->sg[0]);
105 DRD_(sg_put)(p->sg[1]);
106 DRD_(sg_put)(p->post_wait_sg);
sewardj85642922008-01-14 11:54:56 +0000107}
108
bartd2c5eae2009-02-21 15:27:04 +0000109/**
110 * Initialize the structure *p with the specified client-side barrier address,
111 * barrier object size and number of participants in each barrier.
112 */
sewardj85642922008-01-14 11:54:56 +0000113static
barta8cf7652009-02-15 11:00:29 +0000114void DRD_(barrier_initialize)(struct barrier_info* const p,
115 const Addr barrier,
116 const BarrierT barrier_type,
117 const Word count)
sewardj85642922008-01-14 11:54:56 +0000118{
bartbedfd232009-03-26 19:07:15 +0000119 tl_assert(barrier != 0);
120 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
121 tl_assert(p->a1 == barrier);
sewardj85642922008-01-14 11:54:56 +0000122
bartbedfd232009-03-26 19:07:15 +0000123 p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup;
124 p->delete_thread
125 = (void(*)(DrdClientobj*, DrdThreadId))barrier_delete_thread;
126 p->barrier_type = barrier_type;
127 p->count = count;
128 p->pre_iteration = 0;
129 p->post_iteration = 0;
130 p->pre_waiters_left = count;
131 p->post_waiters_left = count;
bartd2c5eae2009-02-21 15:27:04 +0000132
bartbedfd232009-03-26 19:07:15 +0000133 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",
137 VG_(free));
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;
sewardj85642922008-01-14 11:54:56 +0000150
bartbedfd232009-03-26 19:07:15 +0000151 tl_assert(p);
sewardj85642922008-01-14 11:54:56 +0000152
bartbedfd232009-03-26 19:07:15 +0000153 if (p->pre_waiters_left != p->count)
154 {
bartd45d9952009-05-31 18:53:54 +0000155 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000156 VG_(maybe_record_error)(VG_(get_running_tid)(),
157 BarrierErr,
158 VG_(get_IP)(VG_(get_running_tid)()),
159 "Destruction of barrier that is being waited"
160 " upon",
161 &bei);
162 }
sewardj85642922008-01-14 11:54:56 +0000163
bartbedfd232009-03-26 19:07:15 +0000164 DRD_(thread_get_latest_segment)(&latest_sg, DRD_(thread_get_running_tid)());
165 tl_assert(latest_sg);
bartd2c5eae2009-02-21 15:27:04 +0000166
bartbedfd232009-03-26 19:07:15 +0000167 VG_(OSetGen_ResetIter)(p->oset);
168 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
169 {
170 if (q->post_wait_sg
171 && ! DRD_(vc_lte)(&q->post_wait_sg->vc, &latest_sg->vc))
172 {
173 barrier_report_wait_delete_race(p, q);
174 }
bartd2c5eae2009-02-21 15:27:04 +0000175
bartbedfd232009-03-26 19:07:15 +0000176 DRD_(barrier_thread_destroy)(q);
177 }
178 VG_(OSetGen_Destroy)(p->oset);
bartd2c5eae2009-02-21 15:27:04 +0000179
bartbedfd232009-03-26 19:07:15 +0000180 DRD_(sg_put)(latest_sg);
sewardj85642922008-01-14 11:54:56 +0000181}
182
bartd2c5eae2009-02-21 15:27:04 +0000183/**
184 * Look up the client-side barrier address barrier in s_barrier[]. If not
185 * found, add it.
186 */
sewardj85642922008-01-14 11:54:56 +0000187static
188struct barrier_info*
barta8cf7652009-02-15 11:00:29 +0000189DRD_(barrier_get_or_allocate)(const Addr barrier,
190 const BarrierT barrier_type, const Word count)
sewardj85642922008-01-14 11:54:56 +0000191{
bartbedfd232009-03-26 19:07:15 +0000192 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000193
bartbedfd232009-03-26 19:07:15 +0000194 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
bart0268dfa2008-03-11 20:10:21 +0000195
bartbedfd232009-03-26 19:07:15 +0000196 tl_assert(offsetof(DrdClientobj, barrier) == 0);
197 p = &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
198 if (p == 0)
199 {
200 p = &(DRD_(clientobj_add)(barrier, ClientBarrier)->barrier);
201 DRD_(barrier_initialize)(p, barrier, barrier_type, count);
202 }
203 return p;
bart28230a32008-02-29 17:27:03 +0000204}
205
bartd2c5eae2009-02-21 15:27:04 +0000206/**
207 * Look up the address of the information associated with the client-side
208 * barrier object.
209 */
barta8cf7652009-02-15 11:00:29 +0000210static struct barrier_info* DRD_(barrier_get)(const Addr barrier)
bart28230a32008-02-29 17:27:03 +0000211{
bartbedfd232009-03-26 19:07:15 +0000212 tl_assert(offsetof(DrdClientobj, barrier) == 0);
213 return &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
sewardj85642922008-01-14 11:54:56 +0000214}
215
bartd2c5eae2009-02-21 15:27:04 +0000216/**
217 * Initialize a barrier with client address barrier, client size size, and
218 * where count threads participate in each barrier.
219 *
220 * Called before pthread_barrier_init().
bart28230a32008-02-29 17:27:03 +0000221 */
barta8cf7652009-02-15 11:00:29 +0000222void DRD_(barrier_init)(const Addr barrier,
223 const BarrierT barrier_type, const Word count,
224 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000225{
bartbedfd232009-03-26 19:07:15 +0000226 struct barrier_info* p;
bart306527d2008-03-12 17:23:07 +0000227
bartbedfd232009-03-26 19:07:15 +0000228 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
bart306527d2008-03-12 17:23:07 +0000229
bartbedfd232009-03-26 19:07:15 +0000230 if (count == 0)
231 {
bartd45d9952009-05-31 18:53:54 +0000232 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), barrier, 0, 0 };
bartd9e39ec2008-06-28 15:03:26 +0000233 VG_(maybe_record_error)(VG_(get_running_tid)(),
234 BarrierErr,
235 VG_(get_IP)(VG_(get_running_tid)()),
bartbedfd232009-03-26 19:07:15 +0000236 "pthread_barrier_init: 'count' argument is zero",
bartd9e39ec2008-06-28 15:03:26 +0000237 &bei);
bartbedfd232009-03-26 19:07:15 +0000238 }
bart306527d2008-03-12 17:23:07 +0000239
bartbedfd232009-03-26 19:07:15 +0000240 if (! reinitialization && barrier_type == pthread_barrier)
241 {
242 p = DRD_(barrier_get)(barrier);
243 if (p)
244 {
bartd45d9952009-05-31 18:53:54 +0000245 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), barrier, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000246 VG_(maybe_record_error)(VG_(get_running_tid)(),
247 BarrierErr,
248 VG_(get_IP)(VG_(get_running_tid)()),
249 "Barrier reinitialization",
250 &bei);
251 }
252 }
253 p = DRD_(barrier_get_or_allocate)(barrier, barrier_type, count);
bart306527d2008-03-12 17:23:07 +0000254
bartbedfd232009-03-26 19:07:15 +0000255 if (s_trace_barrier)
256 {
257 if (reinitialization)
258 {
259 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000260 "[%d] barrier_reinit %s 0x%lx count %ld -> %ld\n",
bartbedfd232009-03-26 19:07:15 +0000261 DRD_(thread_get_running_tid)(),
262 barrier_get_typename(p),
263 barrier,
264 p->count,
265 count);
266 }
267 else
268 {
269 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000270 "[%d] barrier_init %s 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000271 DRD_(thread_get_running_tid)(),
272 barrier_get_typename(p),
273 barrier);
274 }
275 }
276
277 if (reinitialization && p->count != count)
278 {
279 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
280 {
bartd45d9952009-05-31 18:53:54 +0000281 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000282 VG_(maybe_record_error)(VG_(get_running_tid)(),
283 BarrierErr,
284 VG_(get_IP)(VG_(get_running_tid)()),
285 "Reinitialization of barrier with active"
286 " waiters",
287 &bei);
288 }
289 p->count = count;
290 }
sewardj85642922008-01-14 11:54:56 +0000291}
292
bartd2c5eae2009-02-21 15:27:04 +0000293/** Called after pthread_barrier_destroy() / gomp_barrier_destroy(). */
barta8cf7652009-02-15 11:00:29 +0000294void DRD_(barrier_destroy)(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000295{
bartbedfd232009-03-26 19:07:15 +0000296 struct barrier_info* p;
bart72b751c2008-03-01 13:44:24 +0000297
bartbedfd232009-03-26 19:07:15 +0000298 p = DRD_(barrier_get)(barrier);
bart306527d2008-03-12 17:23:07 +0000299
bartbedfd232009-03-26 19:07:15 +0000300 if (s_trace_barrier)
301 {
302 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000303 "[%d] barrier_destroy %s 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000304 DRD_(thread_get_running_tid)(),
305 barrier_get_typename(p),
306 barrier);
307 }
bart72b751c2008-03-01 13:44:24 +0000308
bartbedfd232009-03-26 19:07:15 +0000309 if (p == 0)
310 {
bart62cc2322010-03-07 10:54:21 +0000311 GenericErrInfo GEI = {
312 .tid = DRD_(thread_get_running_tid)(),
313 .addr = barrier,
314 };
bartbedfd232009-03-26 19:07:15 +0000315 VG_(maybe_record_error)(VG_(get_running_tid)(),
316 GenericErr,
317 VG_(get_IP)(VG_(get_running_tid)()),
318 "Not a barrier",
319 &GEI);
320 return;
321 }
bart72b751c2008-03-01 13:44:24 +0000322
bartbedfd232009-03-26 19:07:15 +0000323 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
324 {
bartd45d9952009-05-31 18:53:54 +0000325 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000326 VG_(maybe_record_error)(VG_(get_running_tid)(),
327 BarrierErr,
328 VG_(get_IP)(VG_(get_running_tid)()),
329 "Destruction of a barrier with active waiters",
330 &bei);
331 }
bart195a3982008-07-01 13:15:31 +0000332
bartbedfd232009-03-26 19:07:15 +0000333 DRD_(clientobj_remove)(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000334}
335
bartd2c5eae2009-02-21 15:27:04 +0000336/** Called before pthread_barrier_wait() / gomp_barrier_wait(). */
barta8cf7652009-02-15 11:00:29 +0000337void DRD_(barrier_pre_wait)(const DrdThreadId tid, const Addr barrier,
338 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000339{
bartbedfd232009-03-26 19:07:15 +0000340 struct barrier_info* p;
341 struct barrier_thread_info* q;
342 const UWord word_tid = tid;
sewardj85642922008-01-14 11:54:56 +0000343
bartbedfd232009-03-26 19:07:15 +0000344 p = DRD_(barrier_get)(barrier);
345 if (p == 0 && barrier_type == gomp_barrier)
346 {
347 /*
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.
351 */
sewardj1e29ebc2009-07-15 14:49:17 +0000352 VG_(message)(Vg_UserMsg, "\n");
bartbedfd232009-03-26 19:07:15 +0000353 VG_(message)(Vg_UserMsg,
354 "Please verify whether gcc has been configured"
sewardj1e29ebc2009-07-15 14:49:17 +0000355 " with option --disable-linux-futex.\n");
bartbedfd232009-03-26 19:07:15 +0000356 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000357 "See also the section about OpenMP in the DRD manual.\n");
358 VG_(message)(Vg_UserMsg, "\n");
bartbedfd232009-03-26 19:07:15 +0000359 }
360 tl_assert(p);
sewardj85642922008-01-14 11:54:56 +0000361
bartbedfd232009-03-26 19:07:15 +0000362 if (s_trace_barrier)
363 {
364 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000365 "[%d] barrier_pre_wait %s 0x%lx iteration %ld\n",
bartbedfd232009-03-26 19:07:15 +0000366 DRD_(thread_get_running_tid)(),
367 barrier_get_typename(p),
368 barrier,
369 p->pre_iteration);
370 }
sewardj85642922008-01-14 11:54:56 +0000371
bartbedfd232009-03-26 19:07:15 +0000372 /* Allocate the per-thread data structure if necessary. */
373 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
374 if (q == 0)
375 {
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);
380 }
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 */
389 DRD_(thread_get_latest_segment)(&q->sg[p->pre_iteration], 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 {
398 p->pre_iteration = 1 - p->pre_iteration;
399 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;
sewardj85642922008-01-14 11:54:56 +0000412
bartbedfd232009-03-26 19:07:15 +0000413 p = DRD_(barrier_get)(barrier);
bartbebc5d62008-02-24 18:42:53 +0000414
bartbedfd232009-03-26 19:07:15 +0000415 if (s_trace_barrier)
416 {
417 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000418 "[%d] barrier_post_wait %s 0x%lx iteration %ld%s\n",
bartbedfd232009-03-26 19:07:15 +0000419 tid,
420 p ? barrier_get_typename(p) : "(?)",
421 barrier,
422 p ? p->post_iteration : -1,
423 serializing ? " (serializing)" : "");
424 }
sewardj85642922008-01-14 11:54:56 +0000425
bartbedfd232009-03-26 19:07:15 +0000426 /*
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.
431 */
432 if (p == 0)
433 return;
bart306527d2008-03-12 17:23:07 +0000434
bartbedfd232009-03-26 19:07:15 +0000435 /* If the *_barrier_wait() call returned an error code, exit. */
436 if (! waited)
437 return;
bartd2c5eae2009-02-21 15:27:04 +0000438
bartbedfd232009-03-26 19:07:15 +0000439 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
440 if (q == 0)
441 {
bartd45d9952009-05-31 18:53:54 +0000442 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000443 VG_(maybe_record_error)(VG_(get_running_tid)(),
444 BarrierErr,
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()",
450 &bei);
sewardj85642922008-01-14 11:54:56 +0000451
bartbedfd232009-03-26 19:07:15 +0000452 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);
456 }
bart7627be32009-06-06 12:26:05 +0000457
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++;
462
bartbedfd232009-03-26 19:07:15 +0000463 /*
464 * Combine all vector clocks that were stored in the pre_barrier_wait
465 * wrapper with the vector clock of the current thread.
466 */
bartbedfd232009-03-26 19:07:15 +0000467 {
bart8f822af2009-06-08 18:20:42 +0000468 VectorClock old_vc;
469
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; )
bartbedfd232009-03-26 19:07:15 +0000473 {
bart8f822af2009-06-08 18:20:42 +0000474 if (r != q)
475 {
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);
479 }
bartbedfd232009-03-26 19:07:15 +0000480 }
bart8f822af2009-06-08 18:20:42 +0000481 DRD_(thread_update_conflict_set)(tid, &old_vc);
482 DRD_(vc_cleanup)(&old_vc);
bartbedfd232009-03-26 19:07:15 +0000483 }
bartbebc5d62008-02-24 18:42:53 +0000484
bartbedfd232009-03-26 19:07:15 +0000485 /*
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.
489 */
490 if (--p->post_waiters_left <= 0)
491 {
492 p->post_iteration = 1 - p->post_iteration;
493 p->post_waiters_left = p->count;
494 }
sewardj85642922008-01-14 11:54:56 +0000495}
496
bartd2c5eae2009-02-21 15:27:04 +0000497/** Called when thread tid stops to exist. */
498static void barrier_delete_thread(struct barrier_info* const p,
499 const DrdThreadId tid)
sewardj85642922008-01-14 11:54:56 +0000500{
bartbedfd232009-03-26 19:07:15 +0000501 struct barrier_thread_info* q;
502 const UWord word_tid = tid;
sewardj85642922008-01-14 11:54:56 +0000503
bartbedfd232009-03-26 19:07:15 +0000504 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
bartd2c5eae2009-02-21 15:27:04 +0000505
bartbedfd232009-03-26 19:07:15 +0000506 /*
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.
509 */
510 if (q)
511 {
512 DRD_(barrier_thread_destroy)(q);
513 VG_(OSetGen_FreeNode)(p->oset, q);
514 }
sewardj85642922008-01-14 11:54:56 +0000515}
bart306527d2008-03-12 17:23:07 +0000516
bartd2c5eae2009-02-21 15:27:04 +0000517/**
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.
bart776a91e2009-02-22 09:29:07 +0000520 *
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>.
bartd2c5eae2009-02-21 15:27:04 +0000524 */
525static
526void barrier_report_wait_delete_race(const struct barrier_info* const p,
527 const struct barrier_thread_info* const q)
528{
bartbedfd232009-03-26 19:07:15 +0000529 tl_assert(p);
530 tl_assert(q);
bartd2c5eae2009-02-21 15:27:04 +0000531
bartbedfd232009-03-26 19:07:15 +0000532 {
533 BarrierErrInfo bei
bartd45d9952009-05-31 18:53:54 +0000534 = { DRD_(thread_get_running_tid)(), p->a1, q->tid, q->wait_call_ctxt };
bartbedfd232009-03-26 19:07:15 +0000535 VG_(maybe_record_error)(VG_(get_running_tid)(),
536 BarrierErr,
537 VG_(get_IP)(VG_(get_running_tid)()),
538 "Destruction of barrier not synchronized with"
539 " barrier wait call",
540 &bei);
541 }
bartd2c5eae2009-02-21 15:27:04 +0000542}
543
544static const char* barrier_get_typename(struct barrier_info* const p)
bart306527d2008-03-12 17:23:07 +0000545{
bartbedfd232009-03-26 19:07:15 +0000546 tl_assert(p);
bart306527d2008-03-12 17:23:07 +0000547
bartbedfd232009-03-26 19:07:15 +0000548 return barrier_type_name(p->barrier_type);
bart306527d2008-03-12 17:23:07 +0000549}
550
bartd2c5eae2009-02-21 15:27:04 +0000551static const char* barrier_type_name(const BarrierT bt)
bart306527d2008-03-12 17:23:07 +0000552{
bartbedfd232009-03-26 19:07:15 +0000553 switch (bt)
554 {
555 case pthread_barrier:
556 return "pthread barrier";
557 case gomp_barrier:
558 return "gomp barrier";
559 }
560 return "?";
bart306527d2008-03-12 17:23:07 +0000561}
bart6bbefaf2008-04-19 15:16:45 +0000562
barta8cf7652009-02-15 11:00:29 +0000563ULong DRD_(get_barrier_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000564{
bartbedfd232009-03-26 19:07:15 +0000565 return s_barrier_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000566}