blob: fceb644f6e87491c451c35d8ae59ba3fe7c3d0bf [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
bart86562bd2009-02-16 19:43:56 +00005 Copyright (C) 2006-2009 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
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
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,
260 "[%d/%d] barrier_reinit %s 0x%lx count %ld -> %ld",
261 VG_(get_running_tid)(),
262 DRD_(thread_get_running_tid)(),
263 barrier_get_typename(p),
264 barrier,
265 p->count,
266 count);
267 }
268 else
269 {
270 VG_(message)(Vg_UserMsg,
271 "[%d/%d] barrier_init %s 0x%lx",
272 VG_(get_running_tid)(),
273 DRD_(thread_get_running_tid)(),
274 barrier_get_typename(p),
275 barrier);
276 }
277 }
278
279 if (reinitialization && p->count != count)
280 {
281 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
282 {
bartd45d9952009-05-31 18:53:54 +0000283 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000284 VG_(maybe_record_error)(VG_(get_running_tid)(),
285 BarrierErr,
286 VG_(get_IP)(VG_(get_running_tid)()),
287 "Reinitialization of barrier with active"
288 " waiters",
289 &bei);
290 }
291 p->count = count;
292 }
sewardj85642922008-01-14 11:54:56 +0000293}
294
bartd2c5eae2009-02-21 15:27:04 +0000295/** Called after pthread_barrier_destroy() / gomp_barrier_destroy(). */
barta8cf7652009-02-15 11:00:29 +0000296void DRD_(barrier_destroy)(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000297{
bartbedfd232009-03-26 19:07:15 +0000298 struct barrier_info* p;
bart72b751c2008-03-01 13:44:24 +0000299
bartbedfd232009-03-26 19:07:15 +0000300 p = DRD_(barrier_get)(barrier);
bart306527d2008-03-12 17:23:07 +0000301
bartbedfd232009-03-26 19:07:15 +0000302 if (s_trace_barrier)
303 {
304 VG_(message)(Vg_UserMsg,
305 "[%d/%d] barrier_destroy %s 0x%lx",
306 VG_(get_running_tid)(),
307 DRD_(thread_get_running_tid)(),
308 barrier_get_typename(p),
309 barrier);
310 }
bart72b751c2008-03-01 13:44:24 +0000311
bartbedfd232009-03-26 19:07:15 +0000312 if (p == 0)
313 {
bartd45d9952009-05-31 18:53:54 +0000314 GenericErrInfo GEI = { DRD_(thread_get_running_tid)() };
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 */
352 VG_(message)(Vg_UserMsg, "");
353 VG_(message)(Vg_UserMsg,
354 "Please verify whether gcc has been configured"
355 " with option --disable-linux-futex.");
356 VG_(message)(Vg_UserMsg,
357 "See also the section about OpenMP in the DRD manual.");
358 VG_(message)(Vg_UserMsg, "");
359 }
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,
365 "[%d/%d] barrier_pre_wait %s 0x%lx iteration %ld",
366 VG_(get_running_tid)(),
367 DRD_(thread_get_running_tid)(),
368 barrier_get_typename(p),
369 barrier,
370 p->pre_iteration);
371 }
sewardj85642922008-01-14 11:54:56 +0000372
bartbedfd232009-03-26 19:07:15 +0000373 /* Allocate the per-thread data structure if necessary. */
374 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
375 if (q == 0)
376 {
377 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
378 DRD_(barrier_thread_initialize)(q, tid, p->pre_iteration);
379 VG_(OSetGen_Insert)(p->oset, q);
380 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
381 }
bartd2c5eae2009-02-21 15:27:04 +0000382
bartbedfd232009-03-26 19:07:15 +0000383 /* Record *_barrier_wait() call context. */
384 q->wait_call_ctxt = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
bartd2c5eae2009-02-21 15:27:04 +0000385
bartbedfd232009-03-26 19:07:15 +0000386 /*
387 * Store a pointer to the latest segment of the current thread in the
388 * per-thread data structure.
389 */
390 DRD_(thread_get_latest_segment)(&q->sg[p->pre_iteration], tid);
bartbebc5d62008-02-24 18:42:53 +0000391
bartbedfd232009-03-26 19:07:15 +0000392 /*
393 * If the same number of threads as the barrier count indicates have
394 * called the pre *_barrier_wait() wrapper, toggle p->pre_iteration and
395 * reset the p->pre_waiters_left counter.
396 */
397 if (--p->pre_waiters_left <= 0)
398 {
399 p->pre_iteration = 1 - p->pre_iteration;
400 p->pre_waiters_left = p->count;
401 }
sewardj85642922008-01-14 11:54:56 +0000402}
403
bartd2c5eae2009-02-21 15:27:04 +0000404/** Called after pthread_barrier_wait() / gomp_barrier_wait(). */
barta8cf7652009-02-15 11:00:29 +0000405void DRD_(barrier_post_wait)(const DrdThreadId tid, const Addr barrier,
bartd2c5eae2009-02-21 15:27:04 +0000406 const BarrierT barrier_type, const Bool waited,
407 const Bool serializing)
sewardj85642922008-01-14 11:54:56 +0000408{
bartbedfd232009-03-26 19:07:15 +0000409 struct barrier_info* p;
410 const UWord word_tid = tid;
411 struct barrier_thread_info* q;
412 struct barrier_thread_info* r;
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)
417 {
418 VG_(message)(Vg_UserMsg,
419 "[%d/%d] barrier_post_wait %s 0x%lx iteration %ld%s",
420 VG_(get_running_tid)(),
421 tid,
422 p ? barrier_get_typename(p) : "(?)",
423 barrier,
424 p ? p->post_iteration : -1,
425 serializing ? " (serializing)" : "");
426 }
sewardj85642922008-01-14 11:54:56 +0000427
bartbedfd232009-03-26 19:07:15 +0000428 /*
429 * If p == 0, this means that the barrier has been destroyed after
430 * *_barrier_wait() returned and before this function was called. Just
431 * return in that case -- race conditions between *_barrier_wait()
432 * and *_barrier_destroy() are detected by the *_barrier_destroy() wrapper.
433 */
434 if (p == 0)
435 return;
bart306527d2008-03-12 17:23:07 +0000436
bartbedfd232009-03-26 19:07:15 +0000437 /* If the *_barrier_wait() call returned an error code, exit. */
438 if (! waited)
439 return;
bartd2c5eae2009-02-21 15:27:04 +0000440
bartbedfd232009-03-26 19:07:15 +0000441 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
442 if (q == 0)
443 {
bartd45d9952009-05-31 18:53:54 +0000444 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000445 VG_(maybe_record_error)(VG_(get_running_tid)(),
446 BarrierErr,
447 VG_(get_IP)(VG_(get_running_tid)()),
448 "Error in barrier implementation"
449 " -- barrier_wait() started before"
450 " barrier_destroy() and finished after"
451 " barrier_destroy()",
452 &bei);
sewardj85642922008-01-14 11:54:56 +0000453
bartbedfd232009-03-26 19:07:15 +0000454 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
455 DRD_(barrier_thread_initialize)(q, tid, p->pre_iteration);
456 VG_(OSetGen_Insert)(p->oset, q);
457 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
458 }
bart7627be32009-06-06 12:26:05 +0000459
460 /* Create a new segment and store a pointer to that segment. */
461 DRD_(thread_new_segment)(tid);
462 DRD_(thread_get_latest_segment)(&q->post_wait_sg, tid);
463 s_barrier_segment_creation_count++;
464
bartbedfd232009-03-26 19:07:15 +0000465 /*
466 * Combine all vector clocks that were stored in the pre_barrier_wait
467 * wrapper with the vector clock of the current thread.
468 */
469 VG_(OSetGen_ResetIter)(p->oset);
470 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
471 {
472 if (r != q)
473 {
474 tl_assert(r->sg[p->post_iteration]);
475 DRD_(thread_combine_vc2)(tid, &r->sg[p->post_iteration]->vc);
476 }
477 }
bartbebc5d62008-02-24 18:42:53 +0000478
bartbedfd232009-03-26 19:07:15 +0000479 /*
480 * If the same number of threads as the barrier count indicates have
481 * called the post *_barrier_wait() wrapper, toggle p->post_iteration and
482 * reset the p->post_waiters_left counter.
483 */
484 if (--p->post_waiters_left <= 0)
485 {
486 p->post_iteration = 1 - p->post_iteration;
487 p->post_waiters_left = p->count;
488 }
sewardj85642922008-01-14 11:54:56 +0000489}
490
bartd2c5eae2009-02-21 15:27:04 +0000491/** Called when thread tid stops to exist. */
492static void barrier_delete_thread(struct barrier_info* const p,
493 const DrdThreadId tid)
sewardj85642922008-01-14 11:54:56 +0000494{
bartbedfd232009-03-26 19:07:15 +0000495 struct barrier_thread_info* q;
496 const UWord word_tid = tid;
sewardj85642922008-01-14 11:54:56 +0000497
bartbedfd232009-03-26 19:07:15 +0000498 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
bartd2c5eae2009-02-21 15:27:04 +0000499
bartbedfd232009-03-26 19:07:15 +0000500 /*
501 * q is only non-zero if the barrier object has been used by thread tid
502 * after the barrier_init() call and before the thread finished.
503 */
504 if (q)
505 {
506 DRD_(barrier_thread_destroy)(q);
507 VG_(OSetGen_FreeNode)(p->oset, q);
508 }
sewardj85642922008-01-14 11:54:56 +0000509}
bart306527d2008-03-12 17:23:07 +0000510
bartd2c5eae2009-02-21 15:27:04 +0000511/**
512 * Report that *_barrier_destroy() has been called but that this call was
513 * not synchronized with the last *_barrier_wait() call on the same barrier.
bart776a91e2009-02-22 09:29:07 +0000514 *
515 * This topic has been discussed extensively on comp.programming.threads
516 * (February 3, 2009). See also
517 * <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 +0000518 */
519static
520void barrier_report_wait_delete_race(const struct barrier_info* const p,
521 const struct barrier_thread_info* const q)
522{
bartbedfd232009-03-26 19:07:15 +0000523 tl_assert(p);
524 tl_assert(q);
bartd2c5eae2009-02-21 15:27:04 +0000525
bartbedfd232009-03-26 19:07:15 +0000526 {
527 BarrierErrInfo bei
bartd45d9952009-05-31 18:53:54 +0000528 = { DRD_(thread_get_running_tid)(), p->a1, q->tid, q->wait_call_ctxt };
bartbedfd232009-03-26 19:07:15 +0000529 VG_(maybe_record_error)(VG_(get_running_tid)(),
530 BarrierErr,
531 VG_(get_IP)(VG_(get_running_tid)()),
532 "Destruction of barrier not synchronized with"
533 " barrier wait call",
534 &bei);
535 }
bartd2c5eae2009-02-21 15:27:04 +0000536}
537
538static const char* barrier_get_typename(struct barrier_info* const p)
bart306527d2008-03-12 17:23:07 +0000539{
bartbedfd232009-03-26 19:07:15 +0000540 tl_assert(p);
bart306527d2008-03-12 17:23:07 +0000541
bartbedfd232009-03-26 19:07:15 +0000542 return barrier_type_name(p->barrier_type);
bart306527d2008-03-12 17:23:07 +0000543}
544
bartd2c5eae2009-02-21 15:27:04 +0000545static const char* barrier_type_name(const BarrierT bt)
bart306527d2008-03-12 17:23:07 +0000546{
bartbedfd232009-03-26 19:07:15 +0000547 switch (bt)
548 {
549 case pthread_barrier:
550 return "pthread barrier";
551 case gomp_barrier:
552 return "gomp barrier";
553 }
554 return "?";
bart306527d2008-03-12 17:23:07 +0000555}
bart6bbefaf2008-04-19 15:16:45 +0000556
barta8cf7652009-02-15 11:00:29 +0000557ULong DRD_(get_barrier_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000558{
bartbedfd232009-03-26 19:07:15 +0000559 return s_barrier_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000560}