blob: f1f57fb9fd50a47c1f8f788f4c31e26c8dccec9c [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
Elliott Hughesed398002017-06-21 14:41:24 -07004 Copyright (C) 2006-2017 Bart Van Assche <bvanassche@acm.org>.
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{
bartbedfd232009-03-26 19:07:15 +000043 UWord tid; // A DrdThreadId declared as UWord because
bart62cc2322010-03-07 10:54:21 +000044 // this member variable is the key of an OSet.
bartd61580e2011-07-29 12:39:44 +000045 Segment* sg; // Segment of the last pthread_barrier() call
46 // by thread tid.
bartbedfd232009-03-26 19:07:15 +000047 Segment* post_wait_sg; // Segment created after *_barrier_wait() finished
bartd61580e2011-07-29 12:39:44 +000048 ExeContext* wait_call_ctxt;// call stack for *_barrier_wait() call.
49 Bool thread_finished;// Whether thread 'tid' has finished.
sewardj85642922008-01-14 11:54:56 +000050};
51
52
barta8cf7652009-02-15 11:00:29 +000053/* Local functions. */
bart28230a32008-02-29 17:27:03 +000054
bartd2c5eae2009-02-21 15:27:04 +000055static void barrier_cleanup(struct barrier_info* p);
56static void barrier_delete_thread(struct barrier_info* const p,
57 const DrdThreadId tid);
florian19f91bb2012-11-10 22:29:54 +000058static const HChar* barrier_get_typename(struct barrier_info* const p);
59static const HChar* barrier_type_name(const BarrierT bt);
bartd2c5eae2009-02-21 15:27:04 +000060static
61void barrier_report_wait_delete_race(const struct barrier_info* const p,
bartbedfd232009-03-26 19:07:15 +000062 const struct barrier_thread_info* const q);
bart28230a32008-02-29 17:27:03 +000063
64
barta8cf7652009-02-15 11:00:29 +000065/* Local variables. */
sewardj85642922008-01-14 11:54:56 +000066
bartd2c5eae2009-02-21 15:27:04 +000067static Bool s_trace_barrier = False;
68static ULong s_barrier_segment_creation_count;
sewardj85642922008-01-14 11:54:56 +000069
70
barta8cf7652009-02-15 11:00:29 +000071/* Function definitions. */
sewardj85642922008-01-14 11:54:56 +000072
barta8cf7652009-02-15 11:00:29 +000073void DRD_(barrier_set_trace)(const Bool trace_barrier)
sewardj85642922008-01-14 11:54:56 +000074{
bartbedfd232009-03-26 19:07:15 +000075 s_trace_barrier = trace_barrier;
sewardj85642922008-01-14 11:54:56 +000076}
77
bartd2c5eae2009-02-21 15:27:04 +000078/**
79 * Initialize the structure *p with the specified thread ID and iteration
80 * information.
81 */
barta8cf7652009-02-15 11:00:29 +000082static
83void DRD_(barrier_thread_initialize)(struct barrier_thread_info* const p,
bartcdc3fdb2011-07-29 12:31:33 +000084 const DrdThreadId tid)
sewardj85642922008-01-14 11:54:56 +000085{
bartd61580e2011-07-29 12:39:44 +000086 p->tid = tid;
87 p->sg = NULL;
88 p->post_wait_sg = 0;
89 p->wait_call_ctxt = 0;
90 p->thread_finished = False;
sewardj85642922008-01-14 11:54:56 +000091}
92
bartd2c5eae2009-02-21 15:27:04 +000093/**
94 * Deallocate the memory that is owned by members of
95 * struct barrier_thread_info.
96 */
barta8cf7652009-02-15 11:00:29 +000097static void DRD_(barrier_thread_destroy)(struct barrier_thread_info* const p)
sewardj85642922008-01-14 11:54:56 +000098{
bartbedfd232009-03-26 19:07:15 +000099 tl_assert(p);
bartd61580e2011-07-29 12:39:44 +0000100 DRD_(sg_put)(p->sg);
bartbedfd232009-03-26 19:07:15 +0000101 DRD_(sg_put)(p->post_wait_sg);
sewardj85642922008-01-14 11:54:56 +0000102}
103
bartd2c5eae2009-02-21 15:27:04 +0000104/**
105 * Initialize the structure *p with the specified client-side barrier address,
106 * barrier object size and number of participants in each barrier.
107 */
sewardj85642922008-01-14 11:54:56 +0000108static
barta8cf7652009-02-15 11:00:29 +0000109void DRD_(barrier_initialize)(struct barrier_info* const p,
110 const Addr barrier,
111 const BarrierT barrier_type,
112 const Word count)
sewardj85642922008-01-14 11:54:56 +0000113{
bartd61580e2011-07-29 12:39:44 +0000114 int i;
115
bartbedfd232009-03-26 19:07:15 +0000116 tl_assert(barrier != 0);
117 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
118 tl_assert(p->a1 == barrier);
sewardj85642922008-01-14 11:54:56 +0000119
bartbedfd232009-03-26 19:07:15 +0000120 p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup;
121 p->delete_thread
122 = (void(*)(DrdClientobj*, DrdThreadId))barrier_delete_thread;
123 p->barrier_type = barrier_type;
124 p->count = count;
125 p->pre_iteration = 0;
126 p->post_iteration = 0;
127 p->pre_waiters_left = count;
128 p->post_waiters_left = count;
bartd2c5eae2009-02-21 15:27:04 +0000129
bartbedfd232009-03-26 19:07:15 +0000130 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word));
131 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid)
132 >= sizeof(DrdThreadId));
bartd61580e2011-07-29 12:39:44 +0000133 for (i = 0; i < 2; i++) {
134 p->oset[i] = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.barrier.bi.1",
135 VG_(free));
136 }
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{
bartbedfd232009-03-26 19:07:15 +0000147 struct barrier_thread_info* q;
148 Segment* latest_sg = 0;
bartd61580e2011-07-29 12:39:44 +0000149 OSet* oset;
150 int i;
sewardj85642922008-01-14 11:54:56 +0000151
bartbedfd232009-03-26 19:07:15 +0000152 tl_assert(p);
sewardj85642922008-01-14 11:54:56 +0000153
bartd61580e2011-07-29 12:39:44 +0000154 DRD_(thread_get_latest_segment)(&latest_sg, DRD_(thread_get_running_tid)());
155 tl_assert(latest_sg);
156
157 if (p->pre_waiters_left != p->count) {
bartd45d9952009-05-31 18:53:54 +0000158 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000159 VG_(maybe_record_error)(VG_(get_running_tid)(),
160 BarrierErr,
161 VG_(get_IP)(VG_(get_running_tid)()),
162 "Destruction of barrier that is being waited"
163 " upon",
164 &bei);
bartd61580e2011-07-29 12:39:44 +0000165 } else {
bartd378eaa2011-07-30 09:35:56 +0000166 oset = p->oset[1 - (p->pre_iteration & 1)];
bartd61580e2011-07-29 12:39:44 +0000167 VG_(OSetGen_ResetIter)(oset);
168 for ( ; (q = VG_(OSetGen_Next)(oset)) != 0; ) {
169 if (q->post_wait_sg && !DRD_(vc_lte)(&q->post_wait_sg->vc,
170 &latest_sg->vc))
171 {
172 barrier_report_wait_delete_race(p, q);
173 }
174 DRD_(barrier_thread_destroy)(q);
bartbedfd232009-03-26 19:07:15 +0000175 }
bartbedfd232009-03-26 19:07:15 +0000176 }
bartd61580e2011-07-29 12:39:44 +0000177
178 for (i = 0; i < 2; i++) {
179 VG_(OSetGen_Destroy)(p->oset[i]);
180 p->oset[i] = NULL;
181 }
bartd2c5eae2009-02-21 15:27:04 +0000182
bartbedfd232009-03-26 19:07:15 +0000183 DRD_(sg_put)(latest_sg);
sewardj85642922008-01-14 11:54:56 +0000184}
185
bartd2c5eae2009-02-21 15:27:04 +0000186/**
187 * Look up the client-side barrier address barrier in s_barrier[]. If not
188 * found, add it.
189 */
sewardj85642922008-01-14 11:54:56 +0000190static
191struct barrier_info*
barta8cf7652009-02-15 11:00:29 +0000192DRD_(barrier_get_or_allocate)(const Addr barrier,
193 const BarrierT barrier_type, const Word count)
sewardj85642922008-01-14 11:54:56 +0000194{
bartbedfd232009-03-26 19:07:15 +0000195 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000196
bartbedfd232009-03-26 19:07:15 +0000197 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
bart0268dfa2008-03-11 20:10:21 +0000198
bartbedfd232009-03-26 19:07:15 +0000199 tl_assert(offsetof(DrdClientobj, barrier) == 0);
200 p = &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
201 if (p == 0)
202 {
203 p = &(DRD_(clientobj_add)(barrier, ClientBarrier)->barrier);
204 DRD_(barrier_initialize)(p, barrier, barrier_type, count);
205 }
206 return p;
bart28230a32008-02-29 17:27:03 +0000207}
208
bartd2c5eae2009-02-21 15:27:04 +0000209/**
bartd61580e2011-07-29 12:39:44 +0000210 * Look up the address of the struct barrier_info associated with the
211 * client-side barrier object.
bartd2c5eae2009-02-21 15:27:04 +0000212 */
barta8cf7652009-02-15 11:00:29 +0000213static struct barrier_info* DRD_(barrier_get)(const Addr barrier)
bart28230a32008-02-29 17:27:03 +0000214{
bartbedfd232009-03-26 19:07:15 +0000215 tl_assert(offsetof(DrdClientobj, barrier) == 0);
216 return &(DRD_(clientobj_get)(barrier, ClientBarrier)->barrier);
sewardj85642922008-01-14 11:54:56 +0000217}
218
bartd2c5eae2009-02-21 15:27:04 +0000219/**
bartd61580e2011-07-29 12:39:44 +0000220 * Initialize a barrier with given client address, barrier type and number of
221 * participants. The 'reinitialization' argument indicates whether a barrier
222 * object is being initialized or reinitialized.
bartd2c5eae2009-02-21 15:27:04 +0000223 *
224 * Called before pthread_barrier_init().
bart28230a32008-02-29 17:27:03 +0000225 */
barta8cf7652009-02-15 11:00:29 +0000226void DRD_(barrier_init)(const Addr barrier,
227 const BarrierT barrier_type, const Word count,
228 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000229{
bartbedfd232009-03-26 19:07:15 +0000230 struct barrier_info* p;
bart306527d2008-03-12 17:23:07 +0000231
bartbedfd232009-03-26 19:07:15 +0000232 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
bart306527d2008-03-12 17:23:07 +0000233
bartbedfd232009-03-26 19:07:15 +0000234 if (count == 0)
235 {
bartd45d9952009-05-31 18:53:54 +0000236 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), barrier, 0, 0 };
bartd9e39ec2008-06-28 15:03:26 +0000237 VG_(maybe_record_error)(VG_(get_running_tid)(),
238 BarrierErr,
239 VG_(get_IP)(VG_(get_running_tid)()),
bartbedfd232009-03-26 19:07:15 +0000240 "pthread_barrier_init: 'count' argument is zero",
bartd9e39ec2008-06-28 15:03:26 +0000241 &bei);
bartbedfd232009-03-26 19:07:15 +0000242 }
bart306527d2008-03-12 17:23:07 +0000243
bartbedfd232009-03-26 19:07:15 +0000244 if (! reinitialization && barrier_type == pthread_barrier)
245 {
246 p = DRD_(barrier_get)(barrier);
247 if (p)
248 {
bartd45d9952009-05-31 18:53:54 +0000249 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), barrier, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000250 VG_(maybe_record_error)(VG_(get_running_tid)(),
251 BarrierErr,
252 VG_(get_IP)(VG_(get_running_tid)()),
253 "Barrier reinitialization",
254 &bei);
255 }
256 }
bartd61580e2011-07-29 12:39:44 +0000257
bartbedfd232009-03-26 19:07:15 +0000258 p = DRD_(barrier_get_or_allocate)(barrier, barrier_type, count);
bart306527d2008-03-12 17:23:07 +0000259
bartb92ff0f2011-10-08 08:29:29 +0000260 if (s_trace_barrier) {
bartbedfd232009-03-26 19:07:15 +0000261 if (reinitialization)
florianea71ffb2015-08-05 14:38:57 +0000262 DRD_(trace_msg)("[%u] barrier_reinit %s 0x%lx count %ld -> %ld",
bartb92ff0f2011-10-08 08:29:29 +0000263 DRD_(thread_get_running_tid)(),
264 barrier_get_typename(p), barrier, p->count, count);
bartbedfd232009-03-26 19:07:15 +0000265 else
florianea71ffb2015-08-05 14:38:57 +0000266 DRD_(trace_msg)("[%u] barrier_init %s 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000267 DRD_(thread_get_running_tid)(),
268 barrier_get_typename(p),
269 barrier);
bartbedfd232009-03-26 19:07:15 +0000270 }
271
272 if (reinitialization && p->count != count)
273 {
274 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
275 {
bartd45d9952009-05-31 18:53:54 +0000276 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000277 VG_(maybe_record_error)(VG_(get_running_tid)(),
278 BarrierErr,
279 VG_(get_IP)(VG_(get_running_tid)()),
280 "Reinitialization of barrier with active"
281 " waiters",
282 &bei);
283 }
284 p->count = count;
285 }
sewardj85642922008-01-14 11:54:56 +0000286}
287
bartd2c5eae2009-02-21 15:27:04 +0000288/** Called after pthread_barrier_destroy() / gomp_barrier_destroy(). */
barta8cf7652009-02-15 11:00:29 +0000289void DRD_(barrier_destroy)(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000290{
bartbedfd232009-03-26 19:07:15 +0000291 struct barrier_info* p;
bart72b751c2008-03-01 13:44:24 +0000292
bartbedfd232009-03-26 19:07:15 +0000293 p = DRD_(barrier_get)(barrier);
bart306527d2008-03-12 17:23:07 +0000294
bartbedfd232009-03-26 19:07:15 +0000295 if (s_trace_barrier)
florianea71ffb2015-08-05 14:38:57 +0000296 DRD_(trace_msg)("[%u] barrier_destroy %s 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000297 DRD_(thread_get_running_tid)(),
298 barrier_get_typename(p), barrier);
bart72b751c2008-03-01 13:44:24 +0000299
bartbedfd232009-03-26 19:07:15 +0000300 if (p == 0)
301 {
bart62cc2322010-03-07 10:54:21 +0000302 GenericErrInfo GEI = {
303 .tid = DRD_(thread_get_running_tid)(),
304 .addr = barrier,
305 };
bartbedfd232009-03-26 19:07:15 +0000306 VG_(maybe_record_error)(VG_(get_running_tid)(),
307 GenericErr,
308 VG_(get_IP)(VG_(get_running_tid)()),
309 "Not a barrier",
310 &GEI);
311 return;
312 }
bart72b751c2008-03-01 13:44:24 +0000313
bartbedfd232009-03-26 19:07:15 +0000314 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
315 {
bartd45d9952009-05-31 18:53:54 +0000316 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000317 VG_(maybe_record_error)(VG_(get_running_tid)(),
318 BarrierErr,
319 VG_(get_IP)(VG_(get_running_tid)()),
320 "Destruction of a barrier with active waiters",
321 &bei);
322 }
bart195a3982008-07-01 13:15:31 +0000323
bartbedfd232009-03-26 19:07:15 +0000324 DRD_(clientobj_remove)(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000325}
326
bartd2c5eae2009-02-21 15:27:04 +0000327/** Called before pthread_barrier_wait() / gomp_barrier_wait(). */
barta8cf7652009-02-15 11:00:29 +0000328void DRD_(barrier_pre_wait)(const DrdThreadId tid, const Addr barrier,
329 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000330{
bartbedfd232009-03-26 19:07:15 +0000331 struct barrier_info* p;
332 struct barrier_thread_info* q;
333 const UWord word_tid = tid;
bartd61580e2011-07-29 12:39:44 +0000334 OSet* oset;
sewardj85642922008-01-14 11:54:56 +0000335
bartbedfd232009-03-26 19:07:15 +0000336 p = DRD_(barrier_get)(barrier);
bart74b2d972011-10-08 08:54:57 +0000337 if (p == 0 && barrier_type == gomp_barrier) {
bartbedfd232009-03-26 19:07:15 +0000338 /*
339 * gomp_barrier_wait() call has been intercepted but gomp_barrier_init()
340 * not. The only cause I know of that can trigger this is that libgomp.so
341 * has been compiled with --enable-linux-futex.
342 */
bart74b2d972011-10-08 08:54:57 +0000343 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), 0, 0, 0 };
344 VG_(maybe_record_error)(VG_(get_running_tid)(),
345 BarrierErr,
346 VG_(get_IP)(VG_(get_running_tid)()),
347 "Please verify whether gcc has been configured"
348 " with option --disable-linux-futex. See also"
349 " the section about OpenMP in the DRD manual.",
350 &bei);
bartbedfd232009-03-26 19:07:15 +0000351 }
352 tl_assert(p);
sewardj85642922008-01-14 11:54:56 +0000353
bartbedfd232009-03-26 19:07:15 +0000354 if (s_trace_barrier)
florianea71ffb2015-08-05 14:38:57 +0000355 DRD_(trace_msg)("[%u] barrier_pre_wait %s 0x%lx iteration %ld",
bartb92ff0f2011-10-08 08:29:29 +0000356 DRD_(thread_get_running_tid)(),
357 barrier_get_typename(p), barrier, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000358
bartd61580e2011-07-29 12:39:44 +0000359 /* Clean up nodes associated with finished threads. */
bartd378eaa2011-07-30 09:35:56 +0000360 oset = p->oset[p->pre_iteration & 1];
bartd61580e2011-07-29 12:39:44 +0000361 tl_assert(oset);
362 VG_(OSetGen_ResetIter)(oset);
363 for ( ; (q = VG_(OSetGen_Next)(oset)) != 0; ) {
364 if (q->thread_finished) {
365 void* r = VG_(OSetGen_Remove)(oset, &q->tid);
366 tl_assert(r == q);
367 DRD_(barrier_thread_destroy)(q);
368 VG_(OSetGen_FreeNode)(oset, q);
369 VG_(OSetGen_ResetIterAt)(oset, &word_tid);
370 }
371 }
bartbedfd232009-03-26 19:07:15 +0000372 /* Allocate the per-thread data structure if necessary. */
bartd61580e2011-07-29 12:39:44 +0000373 q = VG_(OSetGen_Lookup)(oset, &word_tid);
374 if (q == NULL) {
375 q = VG_(OSetGen_AllocNode)(oset, sizeof(*q));
bartcdc3fdb2011-07-29 12:31:33 +0000376 DRD_(barrier_thread_initialize)(q, tid);
bartd61580e2011-07-29 12:39:44 +0000377 VG_(OSetGen_Insert)(oset, q);
378 tl_assert(VG_(OSetGen_Lookup)(oset, &word_tid) == q);
bartbedfd232009-03-26 19:07:15 +0000379 }
bartd2c5eae2009-02-21 15:27:04 +0000380
bartbedfd232009-03-26 19:07:15 +0000381 /* Record *_barrier_wait() call context. */
382 q->wait_call_ctxt = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
bartd2c5eae2009-02-21 15:27:04 +0000383
bartbedfd232009-03-26 19:07:15 +0000384 /*
385 * Store a pointer to the latest segment of the current thread in the
386 * per-thread data structure.
387 */
bartd61580e2011-07-29 12:39:44 +0000388 DRD_(thread_get_latest_segment)(&q->sg, tid);
bartbebc5d62008-02-24 18:42:53 +0000389
bartbedfd232009-03-26 19:07:15 +0000390 /*
391 * If the same number of threads as the barrier count indicates have
392 * called the pre *_barrier_wait() wrapper, toggle p->pre_iteration and
393 * reset the p->pre_waiters_left counter.
394 */
395 if (--p->pre_waiters_left <= 0)
396 {
bartd378eaa2011-07-30 09:35:56 +0000397 p->pre_iteration++;
bartbedfd232009-03-26 19:07:15 +0000398 p->pre_waiters_left = p->count;
399 }
sewardj85642922008-01-14 11:54:56 +0000400}
401
bartd2c5eae2009-02-21 15:27:04 +0000402/** Called after pthread_barrier_wait() / gomp_barrier_wait(). */
barta8cf7652009-02-15 11:00:29 +0000403void DRD_(barrier_post_wait)(const DrdThreadId tid, const Addr barrier,
bartd2c5eae2009-02-21 15:27:04 +0000404 const BarrierT barrier_type, const Bool waited,
405 const Bool serializing)
sewardj85642922008-01-14 11:54:56 +0000406{
bartbedfd232009-03-26 19:07:15 +0000407 struct barrier_info* p;
408 const UWord word_tid = tid;
409 struct barrier_thread_info* q;
410 struct barrier_thread_info* r;
bartd61580e2011-07-29 12:39:44 +0000411 OSet* oset;
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)
florianea71ffb2015-08-05 14:38:57 +0000416 DRD_(trace_msg)("[%u] barrier_post_wait %s 0x%lx iteration %ld%s",
bartb92ff0f2011-10-08 08:29:29 +0000417 tid, p ? barrier_get_typename(p) : "(?)",
418 barrier, p ? p->post_iteration : -1,
419 serializing ? " (serializing)" : "");
sewardj85642922008-01-14 11:54:56 +0000420
bartbedfd232009-03-26 19:07:15 +0000421 /*
422 * If p == 0, this means that the barrier has been destroyed after
423 * *_barrier_wait() returned and before this function was called. Just
424 * return in that case -- race conditions between *_barrier_wait()
425 * and *_barrier_destroy() are detected by the *_barrier_destroy() wrapper.
426 */
427 if (p == 0)
428 return;
bart306527d2008-03-12 17:23:07 +0000429
bartbedfd232009-03-26 19:07:15 +0000430 /* If the *_barrier_wait() call returned an error code, exit. */
431 if (! waited)
432 return;
bartd2c5eae2009-02-21 15:27:04 +0000433
bartd378eaa2011-07-30 09:35:56 +0000434 oset = p->oset[p->post_iteration & 1];
bartd61580e2011-07-29 12:39:44 +0000435 q = VG_(OSetGen_Lookup)(oset, &word_tid);
bartd378eaa2011-07-30 09:35:56 +0000436 if (p->pre_iteration - p->post_iteration > 1) {
437 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
438 VG_(maybe_record_error)(VG_(get_running_tid)(),
439 BarrierErr,
440 VG_(get_IP)(VG_(get_running_tid)()),
441 "Number of concurrent pthread_barrier_wait()"
442 " calls exceeds the barrier count",
443 &bei);
444 } else if (q == NULL) {
bartd45d9952009-05-31 18:53:54 +0000445 BarrierErrInfo bei = { DRD_(thread_get_running_tid)(), p->a1, 0, 0 };
bartbedfd232009-03-26 19:07:15 +0000446 VG_(maybe_record_error)(VG_(get_running_tid)(),
447 BarrierErr,
448 VG_(get_IP)(VG_(get_running_tid)()),
449 "Error in barrier implementation"
450 " -- barrier_wait() started before"
451 " barrier_destroy() and finished after"
452 " barrier_destroy()",
453 &bei);
bartd378eaa2011-07-30 09:35:56 +0000454 }
455 if (q == NULL) {
bartd61580e2011-07-29 12:39:44 +0000456 q = VG_(OSetGen_AllocNode)(oset, sizeof(*q));
bartcdc3fdb2011-07-29 12:31:33 +0000457 DRD_(barrier_thread_initialize)(q, tid);
bartd61580e2011-07-29 12:39:44 +0000458 VG_(OSetGen_Insert)(oset, q);
459 tl_assert(VG_(OSetGen_Lookup)(oset, &word_tid) == q);
bartd378eaa2011-07-30 09:35:56 +0000460 DRD_(thread_get_latest_segment)(&q->sg, tid);
bartbedfd232009-03-26 19:07:15 +0000461 }
bart7627be32009-06-06 12:26:05 +0000462
463 /* Create a new segment and store a pointer to that segment. */
464 DRD_(thread_new_segment)(tid);
465 DRD_(thread_get_latest_segment)(&q->post_wait_sg, tid);
466 s_barrier_segment_creation_count++;
467
bartbedfd232009-03-26 19:07:15 +0000468 /*
469 * Combine all vector clocks that were stored in the pre_barrier_wait
470 * wrapper with the vector clock of the current thread.
471 */
bartbedfd232009-03-26 19:07:15 +0000472 {
bart8f822af2009-06-08 18:20:42 +0000473 VectorClock old_vc;
474
bartae37e6d2012-01-22 08:58:31 +0000475 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(tid));
bartd61580e2011-07-29 12:39:44 +0000476 VG_(OSetGen_ResetIter)(oset);
477 for ( ; (r = VG_(OSetGen_Next)(oset)) != 0; )
bartbedfd232009-03-26 19:07:15 +0000478 {
bart8f822af2009-06-08 18:20:42 +0000479 if (r != q)
480 {
bartd61580e2011-07-29 12:39:44 +0000481 tl_assert(r->sg);
bartae37e6d2012-01-22 08:58:31 +0000482 DRD_(vc_combine)(DRD_(thread_get_vc)(tid), &r->sg->vc);
bart8f822af2009-06-08 18:20:42 +0000483 }
bartbedfd232009-03-26 19:07:15 +0000484 }
bart8f822af2009-06-08 18:20:42 +0000485 DRD_(thread_update_conflict_set)(tid, &old_vc);
486 DRD_(vc_cleanup)(&old_vc);
bartbedfd232009-03-26 19:07:15 +0000487 }
bartbebc5d62008-02-24 18:42:53 +0000488
bartbedfd232009-03-26 19:07:15 +0000489 /*
490 * If the same number of threads as the barrier count indicates have
491 * called the post *_barrier_wait() wrapper, toggle p->post_iteration and
492 * reset the p->post_waiters_left counter.
493 */
494 if (--p->post_waiters_left <= 0)
495 {
bartd378eaa2011-07-30 09:35:56 +0000496 p->post_iteration++;
bartbedfd232009-03-26 19:07:15 +0000497 p->post_waiters_left = p->count;
498 }
sewardj85642922008-01-14 11:54:56 +0000499}
500
bartd2c5eae2009-02-21 15:27:04 +0000501/** Called when thread tid stops to exist. */
502static void barrier_delete_thread(struct barrier_info* const p,
503 const DrdThreadId tid)
sewardj85642922008-01-14 11:54:56 +0000504{
bartbedfd232009-03-26 19:07:15 +0000505 struct barrier_thread_info* q;
506 const UWord word_tid = tid;
bartd61580e2011-07-29 12:39:44 +0000507 int i;
sewardj85642922008-01-14 11:54:56 +0000508
bartd61580e2011-07-29 12:39:44 +0000509 for (i = 0; i < 2; i++) {
510 q = VG_(OSetGen_Lookup)(p->oset[i], &word_tid);
511 if (q)
512 q->thread_finished = True;
bartbedfd232009-03-26 19:07:15 +0000513 }
sewardj85642922008-01-14 11:54:56 +0000514}
bart306527d2008-03-12 17:23:07 +0000515
bartd2c5eae2009-02-21 15:27:04 +0000516/**
517 * Report that *_barrier_destroy() has been called but that this call was
518 * not synchronized with the last *_barrier_wait() call on the same barrier.
bart776a91e2009-02-22 09:29:07 +0000519 *
520 * This topic has been discussed extensively on comp.programming.threads
521 * (February 3, 2009). See also
522 * <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 +0000523 */
524static
525void barrier_report_wait_delete_race(const struct barrier_info* const p,
526 const struct barrier_thread_info* const q)
527{
bartbedfd232009-03-26 19:07:15 +0000528 tl_assert(p);
529 tl_assert(q);
bartd2c5eae2009-02-21 15:27:04 +0000530
bartbedfd232009-03-26 19:07:15 +0000531 {
532 BarrierErrInfo bei
bartd45d9952009-05-31 18:53:54 +0000533 = { DRD_(thread_get_running_tid)(), p->a1, q->tid, q->wait_call_ctxt };
bartbedfd232009-03-26 19:07:15 +0000534 VG_(maybe_record_error)(VG_(get_running_tid)(),
535 BarrierErr,
536 VG_(get_IP)(VG_(get_running_tid)()),
537 "Destruction of barrier not synchronized with"
538 " barrier wait call",
539 &bei);
540 }
bartd2c5eae2009-02-21 15:27:04 +0000541}
542
florian19f91bb2012-11-10 22:29:54 +0000543static const HChar* barrier_get_typename(struct barrier_info* const p)
bart306527d2008-03-12 17:23:07 +0000544{
bartbedfd232009-03-26 19:07:15 +0000545 tl_assert(p);
bart306527d2008-03-12 17:23:07 +0000546
bartbedfd232009-03-26 19:07:15 +0000547 return barrier_type_name(p->barrier_type);
bart306527d2008-03-12 17:23:07 +0000548}
549
florian19f91bb2012-11-10 22:29:54 +0000550static const HChar* barrier_type_name(const BarrierT bt)
bart306527d2008-03-12 17:23:07 +0000551{
bartbedfd232009-03-26 19:07:15 +0000552 switch (bt)
553 {
554 case pthread_barrier:
555 return "pthread barrier";
556 case gomp_barrier:
557 return "gomp barrier";
558 }
559 return "?";
bart306527d2008-03-12 17:23:07 +0000560}
bart6bbefaf2008-04-19 15:16:45 +0000561
barta8cf7652009-02-15 11:00:29 +0000562ULong DRD_(get_barrier_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000563{
bartbedfd232009-03-26 19:07:15 +0000564 return s_barrier_segment_creation_count;
bart6bbefaf2008-04-19 15:16:45 +0000565}