blob: d2f477f69d7148a36b89db0183b59edcf409c443 [file] [log] [blame]
sewardj85642922008-01-14 11:54:56 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardj85642922008-01-14 11:54:56 +00003
bart86562bd2009-02-16 19:43:56 +00004 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardj85642922008-01-14 11:54:56 +00005
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.
10
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.
15
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.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
25#include "drd_barrier.h"
bart28230a32008-02-29 17:27:03 +000026#include "drd_clientobj.h"
sewardj85642922008-01-14 11:54:56 +000027#include "drd_error.h"
28#include "drd_suppression.h"
sewardj85642922008-01-14 11:54:56 +000029#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
30#include "pub_tool_libcassert.h" // tl_assert()
31#include "pub_tool_libcprint.h" // VG_(printf)()
32#include "pub_tool_machine.h" // VG_(get_IP)()
33#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
34#include "pub_tool_oset.h"
35#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
36
37
barta8cf7652009-02-15 11:00:29 +000038/* Type definitions. */
sewardj85642922008-01-14 11:54:56 +000039
bart8ddef882008-03-09 08:48:01 +000040/** Information associated with one thread participating in a barrier. */
bartbebc5d62008-02-24 18:42:53 +000041struct barrier_thread_info
42{
bartd2c5eae2009-02-21 15:27:04 +000043 UWord tid; // A DrdThreadId declared as UWord because
44 // this member variable is the key of an OSet.
bartbebc5d62008-02-24 18:42:53 +000045 Word iteration; // iteration of last pthread_barrier_wait()
46 // call thread tid participated in.
barta2b6e1b2008-03-17 18:32:39 +000047 Segment* sg[2]; // Segments of the last two
bartbebc5d62008-02-24 18:42:53 +000048 // pthread_barrier() calls by thread tid.
bartd2c5eae2009-02-21 15:27:04 +000049 ExeContext* wait_call_ctxt;// call stack for *_barrier_wait() call.
50 Segment* post_wait_sg; // Segment created after *_barrier_wait() 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,
63 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{
bartd2c5eae2009-02-21 15:27:04 +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,
85 const DrdThreadId tid,
86 const Word iteration)
sewardj85642922008-01-14 11:54:56 +000087{
bartd2c5eae2009-02-21 15:27:04 +000088 p->tid = tid;
89 p->iteration = iteration;
90 p->sg[0] = 0;
91 p->sg[1] = 0;
92 p->wait_call_ctxt = 0;
93 p->post_wait_sg = 0;
sewardj85642922008-01-14 11:54:56 +000094}
95
bartd2c5eae2009-02-21 15:27:04 +000096/**
97 * Deallocate the memory that is owned by members of
98 * struct barrier_thread_info.
99 */
barta8cf7652009-02-15 11:00:29 +0000100static void DRD_(barrier_thread_destroy)(struct barrier_thread_info* const p)
sewardj85642922008-01-14 11:54:56 +0000101{
bart22f74572008-07-07 08:17:55 +0000102 tl_assert(p);
bart62ada3f2009-02-14 17:19:58 +0000103 DRD_(sg_put)(p->sg[0]);
104 DRD_(sg_put)(p->sg[1]);
bartd2c5eae2009-02-21 15:27:04 +0000105 DRD_(sg_put)(p->post_wait_sg);
sewardj85642922008-01-14 11:54:56 +0000106}
107
bartd2c5eae2009-02-21 15:27:04 +0000108/**
109 * Initialize the structure *p with the specified client-side barrier address,
110 * barrier object size and number of participants in each barrier.
111 */
sewardj85642922008-01-14 11:54:56 +0000112static
barta8cf7652009-02-15 11:00:29 +0000113void DRD_(barrier_initialize)(struct barrier_info* const p,
114 const Addr barrier,
115 const BarrierT barrier_type,
116 const Word count)
sewardj85642922008-01-14 11:54:56 +0000117{
118 tl_assert(barrier != 0);
bart0268dfa2008-03-11 20:10:21 +0000119 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
bart28230a32008-02-29 17:27:03 +0000120 tl_assert(p->a1 == barrier);
sewardj85642922008-01-14 11:54:56 +0000121
bartd2c5eae2009-02-21 15:27:04 +0000122 p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup;
123 p->delete_thread
124 = (void(*)(DrdClientobj*, DrdThreadId))barrier_delete_thread;
bart306527d2008-03-12 17:23:07 +0000125 p->barrier_type = barrier_type;
bartbebc5d62008-02-24 18:42:53 +0000126 p->count = count;
127 p->pre_iteration = 0;
128 p->post_iteration = 0;
129 p->pre_waiters_left = count;
130 p->post_waiters_left = count;
bartd2c5eae2009-02-21 15:27:04 +0000131
sewardj85642922008-01-14 11:54:56 +0000132 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word));
133 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid)
134 >= sizeof(DrdThreadId));
sewardj9c606bd2008-09-18 18:12:50 +0000135 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.barrier.bi.1",
136 VG_(free));
sewardj85642922008-01-14 11:54:56 +0000137}
138
bart195e41f2009-02-15 11:34:57 +0000139/**
bartd2c5eae2009-02-21 15:27:04 +0000140 * Deallocate the memory owned by the struct barrier_info object and also
141 * all the nodes in the OSet p->oset.
142 *
bart195e41f2009-02-15 11:34:57 +0000143 * Called by clientobj_destroy().
bart28230a32008-02-29 17:27:03 +0000144 */
bartd2c5eae2009-02-21 15:27:04 +0000145static void barrier_cleanup(struct barrier_info* p)
sewardj85642922008-01-14 11:54:56 +0000146{
147 struct barrier_thread_info* q;
bartd2c5eae2009-02-21 15:27:04 +0000148 Segment* latest_sg = 0;
sewardj85642922008-01-14 11:54:56 +0000149
150 tl_assert(p);
151
bart306527d2008-03-12 17:23:07 +0000152 if (p->pre_waiters_left != p->count)
bart28230a32008-02-29 17:27:03 +0000153 {
bartd2c5eae2009-02-21 15:27:04 +0000154 BarrierErrInfo bei = { p->a1, 0, 0 };
bart3b1ee452008-02-29 19:28:15 +0000155 VG_(maybe_record_error)(VG_(get_running_tid)(),
156 BarrierErr,
157 VG_(get_IP)(VG_(get_running_tid)()),
158 "Destruction of barrier that is being waited"
159 " upon",
160 &bei);
bart28230a32008-02-29 17:27:03 +0000161 }
sewardj85642922008-01-14 11:54:56 +0000162
bartd2c5eae2009-02-21 15:27:04 +0000163 DRD_(thread_get_latest_segment)(&latest_sg, DRD_(thread_get_running_tid)());
164 tl_assert(latest_sg);
165
sewardj85642922008-01-14 11:54:56 +0000166 VG_(OSetGen_ResetIter)(p->oset);
167 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
168 {
bartd2c5eae2009-02-21 15:27:04 +0000169 if (q->post_wait_sg
170 && ! DRD_(vc_lte)(&q->post_wait_sg->vc, &latest_sg->vc))
171 {
172 barrier_report_wait_delete_race(p, q);
173 }
174
barta8cf7652009-02-15 11:00:29 +0000175 DRD_(barrier_thread_destroy)(q);
sewardj85642922008-01-14 11:54:56 +0000176 }
177 VG_(OSetGen_Destroy)(p->oset);
bartd2c5eae2009-02-21 15:27:04 +0000178
179 DRD_(sg_put)(latest_sg);
sewardj85642922008-01-14 11:54:56 +0000180}
181
bartd2c5eae2009-02-21 15:27:04 +0000182/**
183 * Look up the client-side barrier address barrier in s_barrier[]. If not
184 * found, add it.
185 */
sewardj85642922008-01-14 11:54:56 +0000186static
187struct barrier_info*
barta8cf7652009-02-15 11:00:29 +0000188DRD_(barrier_get_or_allocate)(const Addr barrier,
189 const BarrierT barrier_type, const Word count)
sewardj85642922008-01-14 11:54:56 +0000190{
bart28230a32008-02-29 17:27:03 +0000191 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000192
bart0268dfa2008-03-11 20:10:21 +0000193 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
194
bart28230a32008-02-29 17:27:03 +0000195 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart195e41f2009-02-15 11:34:57 +0000196 p = &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
bart28230a32008-02-29 17:27:03 +0000197 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000198 {
bart195e41f2009-02-15 11:34:57 +0000199 p = &(DRD_(clientobj_add)(barrier, ClientBarrier)->barrier);
barta8cf7652009-02-15 11:00:29 +0000200 DRD_(barrier_initialize)(p, barrier, barrier_type, count);
sewardj85642922008-01-14 11:54:56 +0000201 }
bart28230a32008-02-29 17:27:03 +0000202 return p;
203}
204
bartd2c5eae2009-02-21 15:27:04 +0000205/**
206 * Look up the address of the information associated with the client-side
207 * barrier object.
208 */
barta8cf7652009-02-15 11:00:29 +0000209static struct barrier_info* DRD_(barrier_get)(const Addr barrier)
bart28230a32008-02-29 17:27:03 +0000210{
211 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart195e41f2009-02-15 11:34:57 +0000212 return &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
sewardj85642922008-01-14 11:54:56 +0000213}
214
bartd2c5eae2009-02-21 15:27:04 +0000215/**
216 * Initialize a barrier with client address barrier, client size size, and
217 * where count threads participate in each barrier.
218 *
219 * Called before pthread_barrier_init().
bart28230a32008-02-29 17:27:03 +0000220 */
barta8cf7652009-02-15 11:00:29 +0000221void DRD_(barrier_init)(const Addr barrier,
222 const BarrierT barrier_type, const Word count,
223 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000224{
bart306527d2008-03-12 17:23:07 +0000225 struct barrier_info* p;
226
227 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
228
bartfcff0b92008-11-17 17:35:26 +0000229 if (count == 0)
230 {
bartd2c5eae2009-02-21 15:27:04 +0000231 BarrierErrInfo bei = { barrier, 0, 0 };
bartfcff0b92008-11-17 17:35:26 +0000232 VG_(maybe_record_error)(VG_(get_running_tid)(),
233 BarrierErr,
234 VG_(get_IP)(VG_(get_running_tid)()),
235 "pthread_barrier_init: 'count' argument is zero",
236 &bei);
237 }
238
bartd9e39ec2008-06-28 15:03:26 +0000239 if (! reinitialization && barrier_type == pthread_barrier)
240 {
barta8cf7652009-02-15 11:00:29 +0000241 p = DRD_(barrier_get)(barrier);
bartd9e39ec2008-06-28 15:03:26 +0000242 if (p)
243 {
bartd2c5eae2009-02-21 15:27:04 +0000244 BarrierErrInfo bei = { barrier, 0, 0 };
bartd9e39ec2008-06-28 15:03:26 +0000245 VG_(maybe_record_error)(VG_(get_running_tid)(),
246 BarrierErr,
247 VG_(get_IP)(VG_(get_running_tid)()),
bartfcff0b92008-11-17 17:35:26 +0000248 "Barrier reinitialization",
bartd9e39ec2008-06-28 15:03:26 +0000249 &bei);
250 }
251 }
barta8cf7652009-02-15 11:00:29 +0000252 p = DRD_(barrier_get_or_allocate)(barrier, barrier_type, count);
bart306527d2008-03-12 17:23:07 +0000253
bartd2c5eae2009-02-21 15:27:04 +0000254 if (s_trace_barrier)
bart3b1ee452008-02-29 19:28:15 +0000255 {
bart306527d2008-03-12 17:23:07 +0000256 if (reinitialization)
257 {
258 VG_(message)(Vg_UserMsg,
barta2b6e1b2008-03-17 18:32:39 +0000259 "[%d/%d] barrier_reinit %s 0x%lx count %ld -> %ld",
bart306527d2008-03-12 17:23:07 +0000260 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000261 DRD_(thread_get_running_tid)(),
bartd2c5eae2009-02-21 15:27:04 +0000262 barrier_get_typename(p),
bart306527d2008-03-12 17:23:07 +0000263 barrier,
264 p->count,
265 count);
266 }
267 else
268 {
269 VG_(message)(Vg_UserMsg,
270 "[%d/%d] barrier_init %s 0x%lx",
271 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000272 DRD_(thread_get_running_tid)(),
bartd2c5eae2009-02-21 15:27:04 +0000273 barrier_get_typename(p),
bart306527d2008-03-12 17:23:07 +0000274 barrier);
275 }
bart3b1ee452008-02-29 19:28:15 +0000276 }
bart306527d2008-03-12 17:23:07 +0000277
278 if (reinitialization && p->count != count)
279 {
280 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
281 {
bartd2c5eae2009-02-21 15:27:04 +0000282 BarrierErrInfo bei = { p->a1, 0, 0 };
bart195a3982008-07-01 13:15:31 +0000283 VG_(maybe_record_error)(VG_(get_running_tid)(),
284 BarrierErr,
285 VG_(get_IP)(VG_(get_running_tid)()),
286 "Reinitialization of barrier with active"
287 " waiters",
288 &bei);
bart306527d2008-03-12 17:23:07 +0000289 }
290 p->count = count;
291 }
sewardj85642922008-01-14 11:54:56 +0000292}
293
bartd2c5eae2009-02-21 15:27:04 +0000294/** Called after pthread_barrier_destroy() / gomp_barrier_destroy(). */
barta8cf7652009-02-15 11:00:29 +0000295void DRD_(barrier_destroy)(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000296{
bart72b751c2008-03-01 13:44:24 +0000297 struct barrier_info* p;
298
barta8cf7652009-02-15 11:00:29 +0000299 p = DRD_(barrier_get)(barrier);
bart306527d2008-03-12 17:23:07 +0000300
bartd2c5eae2009-02-21 15:27:04 +0000301 if (s_trace_barrier)
bart3b1ee452008-02-29 19:28:15 +0000302 {
303 VG_(message)(Vg_UserMsg,
bart306527d2008-03-12 17:23:07 +0000304 "[%d/%d] barrier_destroy %s 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000305 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000306 DRD_(thread_get_running_tid)(),
bartd2c5eae2009-02-21 15:27:04 +0000307 barrier_get_typename(p),
bart72b751c2008-03-01 13:44:24 +0000308 barrier);
bart3b1ee452008-02-29 19:28:15 +0000309 }
bart72b751c2008-03-01 13:44:24 +0000310
bart72b751c2008-03-01 13:44:24 +0000311 if (p == 0)
312 {
313 GenericErrInfo GEI;
314 VG_(maybe_record_error)(VG_(get_running_tid)(),
315 GenericErr,
316 VG_(get_IP)(VG_(get_running_tid)()),
317 "Not a barrier",
318 &GEI);
319 return;
320 }
321
bart195a3982008-07-01 13:15:31 +0000322 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
323 {
bartd2c5eae2009-02-21 15:27:04 +0000324 BarrierErrInfo bei = { p->a1, 0, 0 };
bart195a3982008-07-01 13:15:31 +0000325 VG_(maybe_record_error)(VG_(get_running_tid)(),
326 BarrierErr,
327 VG_(get_IP)(VG_(get_running_tid)()),
328 "Destruction of a barrier with active waiters",
329 &bei);
330 }
331
bart195e41f2009-02-15 11:34:57 +0000332 DRD_(clientobj_remove)(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000333}
334
bartd2c5eae2009-02-21 15:27:04 +0000335/** Called before pthread_barrier_wait() / gomp_barrier_wait(). */
barta8cf7652009-02-15 11:00:29 +0000336void DRD_(barrier_pre_wait)(const DrdThreadId tid, const Addr barrier,
337 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000338{
339 struct barrier_info* p;
340 struct barrier_thread_info* q;
341 const UWord word_tid = tid;
342
barta8cf7652009-02-15 11:00:29 +0000343 p = DRD_(barrier_get)(barrier);
bartc68bd602008-03-16 10:04:58 +0000344 if (p == 0 && barrier_type == gomp_barrier)
345 {
bartd2c5eae2009-02-21 15:27:04 +0000346 /*
347 * gomp_barrier_wait() call has been intercepted but gomp_barrier_init()
348 * not. The only cause I know of that can trigger this is that libgomp.so
349 * has been compiled with --enable-linux-futex.
350 */
bart654013c2008-06-23 12:41:00 +0000351 VG_(message)(Vg_UserMsg, "");
bartc68bd602008-03-16 10:04:58 +0000352 VG_(message)(Vg_UserMsg,
353 "Please verify whether gcc has been configured"
354 " with option --disable-linux-futex.");
355 VG_(message)(Vg_UserMsg,
356 "See also the section about OpenMP in the DRD manual.");
bart654013c2008-06-23 12:41:00 +0000357 VG_(message)(Vg_UserMsg, "");
bartc68bd602008-03-16 10:04:58 +0000358 }
sewardj85642922008-01-14 11:54:56 +0000359 tl_assert(p);
360
bartd2c5eae2009-02-21 15:27:04 +0000361 if (s_trace_barrier)
sewardj85642922008-01-14 11:54:56 +0000362 {
bart3b1ee452008-02-29 19:28:15 +0000363 VG_(message)(Vg_UserMsg,
barta2b6e1b2008-03-17 18:32:39 +0000364 "[%d/%d] barrier_pre_wait %s 0x%lx iteration %ld",
bart3b1ee452008-02-29 19:28:15 +0000365 VG_(get_running_tid)(),
bart62a784c2009-02-15 13:11:14 +0000366 DRD_(thread_get_running_tid)(),
bartd2c5eae2009-02-21 15:27:04 +0000367 barrier_get_typename(p),
bart3b1ee452008-02-29 19:28:15 +0000368 barrier,
369 p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000370 }
371
bartd2c5eae2009-02-21 15:27:04 +0000372 /* Allocate the per-thread data structure if necessary. */
sewardj85642922008-01-14 11:54:56 +0000373 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
374 if (q == 0)
375 {
376 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
barta8cf7652009-02-15 11:00:29 +0000377 DRD_(barrier_thread_initialize)(q, tid, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000378 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
382 /* Record *_barrier_wait() call context. */
383 q->wait_call_ctxt = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
384
385 /*
386 * Store a pointer to the latest segment of the current thread in the
387 * per-thread data structure.
388 */
bart62a784c2009-02-15 13:11:14 +0000389 DRD_(thread_get_latest_segment)(&q->sg[p->pre_iteration], tid);
bartbebc5d62008-02-24 18:42:53 +0000390
bartd2c5eae2009-02-21 15:27:04 +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 */
bartbebc5d62008-02-24 18:42:53 +0000396 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{
bart3b1ee452008-02-29 19:28:15 +0000408 struct barrier_info* p;
bartd2c5eae2009-02-21 15:27:04 +0000409 const UWord word_tid = tid;
410 struct barrier_thread_info* q;
411 struct barrier_thread_info* r;
sewardj85642922008-01-14 11:54:56 +0000412
barta8cf7652009-02-15 11:00:29 +0000413 p = DRD_(barrier_get)(barrier);
bartbebc5d62008-02-24 18:42:53 +0000414
bartd2c5eae2009-02-21 15:27:04 +0000415 if (s_trace_barrier)
sewardj85642922008-01-14 11:54:56 +0000416 {
bart3b1ee452008-02-29 19:28:15 +0000417 VG_(message)(Vg_UserMsg,
bartd2c5eae2009-02-21 15:27:04 +0000418 "[%d/%d] barrier_post_wait %s 0x%lx iteration %ld%s",
bart3b1ee452008-02-29 19:28:15 +0000419 VG_(get_running_tid)(),
420 tid,
bartd2c5eae2009-02-21 15:27:04 +0000421 p ? barrier_get_typename(p) : "(?)",
bart3b1ee452008-02-29 19:28:15 +0000422 barrier,
bartd2c5eae2009-02-21 15:27:04 +0000423 p ? p->post_iteration : -1,
424 serializing ? " (serializing)" : "");
sewardj85642922008-01-14 11:54:56 +0000425 }
426
bartd2c5eae2009-02-21 15:27:04 +0000427 /*
428 * If p == 0, this means that the barrier has been destroyed after
429 * *_barrier_wait() returned and before this function was called. Just
430 * return in that case -- race conditions between *_barrier_wait()
431 * and *_barrier_destroy() are detected by the *_barrier_destroy() wrapper.
432 */
bart306527d2008-03-12 17:23:07 +0000433 if (p == 0)
434 return;
435
bartd2c5eae2009-02-21 15:27:04 +0000436 /* If the *_barrier_wait() call returned an error code, exit. */
437 if (! waited)
438 return;
439
440 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
441 if (q == 0)
sewardj85642922008-01-14 11:54:56 +0000442 {
bartd2c5eae2009-02-21 15:27:04 +0000443 BarrierErrInfo bei = { p->a1, 0, 0 };
444 VG_(maybe_record_error)(VG_(get_running_tid)(),
445 BarrierErr,
446 VG_(get_IP)(VG_(get_running_tid)()),
447 "Error in barrier implementation"
448 " -- barrier_wait() started before"
449 " barrier_destroy() and finished after"
450 " barrier_destroy()",
451 &bei);
sewardj85642922008-01-14 11:54:56 +0000452
bartd2c5eae2009-02-21 15:27:04 +0000453 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
454 DRD_(barrier_thread_initialize)(q, tid, p->pre_iteration);
455 VG_(OSetGen_Insert)(p->oset, q);
456 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
457 }
458 /*
459 * Combine all vector clocks that were stored in the pre_barrier_wait
460 * wrapper with the vector clock of the current thread.
461 */
462 VG_(OSetGen_ResetIter)(p->oset);
463 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
464 {
465 if (r != q)
bart195a3982008-07-01 13:15:31 +0000466 {
bartd2c5eae2009-02-21 15:27:04 +0000467 tl_assert(r->sg[p->post_iteration]);
468 DRD_(thread_combine_vc2)(tid, &r->sg[p->post_iteration]->vc);
bart195a3982008-07-01 13:15:31 +0000469 }
bartd2c5eae2009-02-21 15:27:04 +0000470 }
bartbebc5d62008-02-24 18:42:53 +0000471
bartd2c5eae2009-02-21 15:27:04 +0000472 /* Create a new segment and store a pointer to that segment. */
473 DRD_(thread_new_segment)(tid);
474 DRD_(thread_get_latest_segment)(&q->post_wait_sg, tid);
475 s_barrier_segment_creation_count++;
bartbebc5d62008-02-24 18:42:53 +0000476
bartd2c5eae2009-02-21 15:27:04 +0000477 /*
478 * If the same number of threads as the barrier count indicates have
479 * called the post *_barrier_wait() wrapper, toggle p->post_iteration and
480 * reset the p->post_waiters_left counter.
481 */
482 if (--p->post_waiters_left <= 0)
483 {
484 p->post_iteration = 1 - p->post_iteration;
485 p->post_waiters_left = p->count;
sewardj85642922008-01-14 11:54:56 +0000486 }
487}
488
bartd2c5eae2009-02-21 15:27:04 +0000489/** Called when thread tid stops to exist. */
490static void barrier_delete_thread(struct barrier_info* const p,
491 const DrdThreadId tid)
sewardj85642922008-01-14 11:54:56 +0000492{
bartd2c5eae2009-02-21 15:27:04 +0000493 struct barrier_thread_info* q;
494 const UWord word_tid = tid;
sewardj85642922008-01-14 11:54:56 +0000495
bartd2c5eae2009-02-21 15:27:04 +0000496 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
497
498 /*
499 * q is only non-zero if the barrier object has been used by thread tid
500 * after the barrier_init() call and before the thread finished.
501 */
502 if (q)
sewardj85642922008-01-14 11:54:56 +0000503 {
bartd2c5eae2009-02-21 15:27:04 +0000504 DRD_(barrier_thread_destroy)(q);
505 VG_(OSetGen_FreeNode)(p->oset, q);
sewardj85642922008-01-14 11:54:56 +0000506 }
507}
bart306527d2008-03-12 17:23:07 +0000508
bartd2c5eae2009-02-21 15:27:04 +0000509/**
510 * Report that *_barrier_destroy() has been called but that this call was
511 * not synchronized with the last *_barrier_wait() call on the same barrier.
bart776a91e2009-02-22 09:29:07 +0000512 *
513 * This topic has been discussed extensively on comp.programming.threads
514 * (February 3, 2009). See also
515 * <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 +0000516 */
517static
518void barrier_report_wait_delete_race(const struct barrier_info* const p,
519 const struct barrier_thread_info* const q)
520{
521 tl_assert(p);
522 tl_assert(q);
523
524 {
525 BarrierErrInfo bei
526 = { p->a1, q->tid, q->wait_call_ctxt };
527 VG_(maybe_record_error)(VG_(get_running_tid)(),
528 BarrierErr,
529 VG_(get_IP)(VG_(get_running_tid)()),
530 "Destruction of barrier not synchronized with"
531 " barrier wait call",
532 &bei);
533 }
534}
535
536static const char* barrier_get_typename(struct barrier_info* const p)
bart306527d2008-03-12 17:23:07 +0000537{
538 tl_assert(p);
539
bartd2c5eae2009-02-21 15:27:04 +0000540 return barrier_type_name(p->barrier_type);
bart306527d2008-03-12 17:23:07 +0000541}
542
bartd2c5eae2009-02-21 15:27:04 +0000543static const char* barrier_type_name(const BarrierT bt)
bart306527d2008-03-12 17:23:07 +0000544{
545 switch (bt)
546 {
547 case pthread_barrier:
548 return "pthread barrier";
549 case gomp_barrier:
550 return "gomp barrier";
551 }
552 return "?";
553}
bart6bbefaf2008-04-19 15:16:45 +0000554
barta8cf7652009-02-15 11:00:29 +0000555ULong DRD_(get_barrier_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000556{
bartd2c5eae2009-02-21 15:27:04 +0000557 return s_barrier_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000558}