blob: 0d5f053cfa3dd18ef7f61b17f0b4ef059836818d [file] [log] [blame]
sewardj85642922008-01-14 11:54:56 +00001/*
2 This file is part of drd, a data race detector.
3
4 Copyright (C) 2006-2008 Bart Van Assche
5 bart.vanassche@gmail.com
6
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"
30#include "priv_drd_clientreq.h"
31#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
32#include "pub_tool_libcassert.h" // tl_assert()
33#include "pub_tool_libcprint.h" // VG_(printf)()
34#include "pub_tool_machine.h" // VG_(get_IP)()
35#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
36#include "pub_tool_oset.h"
37#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
38
39
40// Type definitions.
41
bart8ddef882008-03-09 08:48:01 +000042/** Information associated with one thread participating in a barrier. */
bartbebc5d62008-02-24 18:42:53 +000043struct barrier_thread_info
44{
45 UWord tid; // A DrdThreadId
46 Word iteration; // iteration of last pthread_barrier_wait()
47 // call thread tid participated in.
48 VectorClock vc[2]; // vector clocks corresponding to the last two
49 // pthread_barrier() calls by thread tid.
sewardj85642922008-01-14 11:54:56 +000050};
51
52
bart28230a32008-02-29 17:27:03 +000053// Local functions.
54
bart306527d2008-03-12 17:23:07 +000055static void barrier_cleanup(struct barrier_info* p);
56static const char* barrier_get_typename(struct barrier_info* const p);
57static const char* barrier_type_name(const BarrierT bt);
bart28230a32008-02-29 17:27:03 +000058
59
sewardj85642922008-01-14 11:54:56 +000060// Local variables.
61
62static Bool s_trace_barrier = False;
sewardj85642922008-01-14 11:54:56 +000063
64
65// Function definitions.
66
67void barrier_set_trace(const Bool trace_barrier)
68{
69 s_trace_barrier = trace_barrier;
70}
71
bartbebc5d62008-02-24 18:42:53 +000072/** Initialize the structure *p with the specified thread ID and iteration
73 * information. */
sewardj85642922008-01-14 11:54:56 +000074static void barrier_thread_initialize(struct barrier_thread_info* const p,
75 const DrdThreadId tid,
76 const Word iteration)
77{
78 p->tid = tid;
79 p->iteration = iteration;
80 vc_init(&p->vc[0], 0, 0);
81 vc_init(&p->vc[1], 0, 0);
82}
83
bartbebc5d62008-02-24 18:42:53 +000084/** Deallocate the memory that was allocated in barrier_thread_initialize(). */
sewardj85642922008-01-14 11:54:56 +000085static void barrier_thread_destroy(struct barrier_thread_info* const p)
86{
87 vc_cleanup(&p->vc[0]);
88 vc_cleanup(&p->vc[1]);
89}
90
bartbebc5d62008-02-24 18:42:53 +000091/** Initialize the structure *p with the specified client-side barrier address,
92 * barrier object size and number of participants in each barrier. */
sewardj85642922008-01-14 11:54:56 +000093static
94void barrier_initialize(struct barrier_info* const p,
95 const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +000096 const BarrierT barrier_type,
sewardj85642922008-01-14 11:54:56 +000097 const Word count)
98{
99 tl_assert(barrier != 0);
bart0268dfa2008-03-11 20:10:21 +0000100 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
sewardj85642922008-01-14 11:54:56 +0000101 tl_assert(count > 0);
bart28230a32008-02-29 17:27:03 +0000102 tl_assert(p->a1 == barrier);
sewardj85642922008-01-14 11:54:56 +0000103
bart28230a32008-02-29 17:27:03 +0000104 p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup;
bart306527d2008-03-12 17:23:07 +0000105 p->barrier_type = barrier_type;
bartbebc5d62008-02-24 18:42:53 +0000106 p->count = count;
107 p->pre_iteration = 0;
108 p->post_iteration = 0;
109 p->pre_waiters_left = count;
110 p->post_waiters_left = count;
sewardj85642922008-01-14 11:54:56 +0000111 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word));
112 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid)
113 >= sizeof(DrdThreadId));
114 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free));
bart8ddef882008-03-09 08:48:01 +0000115 vc_init(&p->finished_threads_vc, 0, 0);
sewardj85642922008-01-14 11:54:56 +0000116}
117
bart28230a32008-02-29 17:27:03 +0000118/** Deallocate the memory allocated by barrier_initialize() and in p->oset.
bart72b751c2008-03-01 13:44:24 +0000119 * Called by clientobj_destroy().
bart28230a32008-02-29 17:27:03 +0000120 */
121void barrier_cleanup(struct barrier_info* p)
sewardj85642922008-01-14 11:54:56 +0000122{
123 struct barrier_thread_info* q;
124
125 tl_assert(p);
126
bart306527d2008-03-12 17:23:07 +0000127 if (p->pre_waiters_left != p->count)
bart28230a32008-02-29 17:27:03 +0000128 {
bart3b1ee452008-02-29 19:28:15 +0000129 BarrierErrInfo bei = { p->a1 };
130 VG_(maybe_record_error)(VG_(get_running_tid)(),
131 BarrierErr,
132 VG_(get_IP)(VG_(get_running_tid)()),
133 "Destruction of barrier that is being waited"
134 " upon",
135 &bei);
bart28230a32008-02-29 17:27:03 +0000136 }
sewardj85642922008-01-14 11:54:56 +0000137
138 VG_(OSetGen_ResetIter)(p->oset);
139 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
140 {
141 barrier_thread_destroy(q);
142 }
143 VG_(OSetGen_Destroy)(p->oset);
bart8ddef882008-03-09 08:48:01 +0000144 vc_cleanup(&p->finished_threads_vc);
sewardj85642922008-01-14 11:54:56 +0000145}
146
bartbebc5d62008-02-24 18:42:53 +0000147/** Look up the client-side barrier address barrier in s_barrier[]. If not
148 * found, add it. */
sewardj85642922008-01-14 11:54:56 +0000149static
150struct barrier_info*
bart0268dfa2008-03-11 20:10:21 +0000151barrier_get_or_allocate(const Addr barrier,
152 const BarrierT barrier_type, const Word count)
sewardj85642922008-01-14 11:54:56 +0000153{
bart28230a32008-02-29 17:27:03 +0000154 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000155
bart0268dfa2008-03-11 20:10:21 +0000156 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
157
bart28230a32008-02-29 17:27:03 +0000158 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart72b751c2008-03-01 13:44:24 +0000159 p = &clientobj_get(barrier, ClientBarrier)->barrier;
bart28230a32008-02-29 17:27:03 +0000160 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000161 {
bart0268dfa2008-03-11 20:10:21 +0000162 p = &clientobj_add(barrier, ClientBarrier)->barrier;
163 barrier_initialize(p, barrier, barrier_type, count);
sewardj85642922008-01-14 11:54:56 +0000164 }
bart28230a32008-02-29 17:27:03 +0000165 return p;
166}
167
168/** Look up the address of the information associated with the client-side
169 * barrier object. */
bart72b751c2008-03-01 13:44:24 +0000170static struct barrier_info* barrier_get(const Addr barrier)
bart28230a32008-02-29 17:27:03 +0000171{
172 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart72b751c2008-03-01 13:44:24 +0000173 return &clientobj_get(barrier, ClientBarrier)->barrier;
sewardj85642922008-01-14 11:54:56 +0000174}
175
bartbebc5d62008-02-24 18:42:53 +0000176/** Initialize a barrier with client address barrier, client size size, and
bart28230a32008-02-29 17:27:03 +0000177 * where count threads participate in each barrier.
178 * Called before pthread_barrier_init().
179 */
bart0268dfa2008-03-11 20:10:21 +0000180void barrier_init(const Addr barrier,
181 const BarrierT barrier_type, const Word count,
182 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000183{
bart306527d2008-03-12 17:23:07 +0000184 struct barrier_info* p;
185
186 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
187
188 p = barrier_get_or_allocate(barrier, barrier_type, count);
189
bart3b1ee452008-02-29 19:28:15 +0000190 if (s_trace_barrier)
191 {
bart306527d2008-03-12 17:23:07 +0000192 if (reinitialization)
193 {
194 VG_(message)(Vg_UserMsg,
195 "[%d/%d] barrier_reinit %s 0x%lx count %d -> %d",
196 VG_(get_running_tid)(),
197 thread_get_running_tid(),
198 barrier_get_typename(p),
199 barrier,
200 p->count,
201 count);
202 }
203 else
204 {
205 VG_(message)(Vg_UserMsg,
206 "[%d/%d] barrier_init %s 0x%lx",
207 VG_(get_running_tid)(),
208 thread_get_running_tid(),
209 barrier_get_typename(p),
210 barrier);
211 }
bart3b1ee452008-02-29 19:28:15 +0000212 }
bart306527d2008-03-12 17:23:07 +0000213
214 if (reinitialization && p->count != count)
215 {
216 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
217 {
218 VG_(message)(Vg_UserMsg, "Error: reinitialization with active waiters");
219 }
220 p->count = count;
221 }
sewardj85642922008-01-14 11:54:56 +0000222}
223
bart28230a32008-02-29 17:27:03 +0000224/** Called after pthread_barrier_destroy(). */
bart0268dfa2008-03-11 20:10:21 +0000225void barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000226{
bart72b751c2008-03-01 13:44:24 +0000227 struct barrier_info* p;
228
bart306527d2008-03-12 17:23:07 +0000229 p = barrier_get(barrier);
230
bart3b1ee452008-02-29 19:28:15 +0000231 if (s_trace_barrier)
232 {
233 VG_(message)(Vg_UserMsg,
bart306527d2008-03-12 17:23:07 +0000234 "[%d/%d] barrier_destroy %s 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000235 VG_(get_running_tid)(),
236 thread_get_running_tid(),
bart306527d2008-03-12 17:23:07 +0000237 barrier_get_typename(p),
bart72b751c2008-03-01 13:44:24 +0000238 barrier);
bart3b1ee452008-02-29 19:28:15 +0000239 }
bart72b751c2008-03-01 13:44:24 +0000240
bart72b751c2008-03-01 13:44:24 +0000241 if (p == 0)
242 {
243 GenericErrInfo GEI;
244 VG_(maybe_record_error)(VG_(get_running_tid)(),
245 GenericErr,
246 VG_(get_IP)(VG_(get_running_tid)()),
247 "Not a barrier",
248 &GEI);
249 return;
250 }
251
252 clientobj_remove(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000253}
254
bart28230a32008-02-29 17:27:03 +0000255/** Called before pthread_barrier_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000256void barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
257 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000258{
259 struct barrier_info* p;
260 struct barrier_thread_info* q;
261 const UWord word_tid = tid;
262
263 p = barrier_get(barrier);
bartc68bd602008-03-16 10:04:58 +0000264 if (p == 0 && barrier_type == gomp_barrier)
265 {
266 VG_(message)(Vg_UserMsg, "");
267 VG_(message)(Vg_UserMsg,
268 "Please verify whether gcc has been configured"
269 " with option --disable-linux-futex.");
270 VG_(message)(Vg_UserMsg,
271 "See also the section about OpenMP in the DRD manual.");
272 VG_(message)(Vg_UserMsg, "");
273 }
sewardj85642922008-01-14 11:54:56 +0000274 tl_assert(p);
275
276 if (s_trace_barrier)
277 {
bart3b1ee452008-02-29 19:28:15 +0000278 VG_(message)(Vg_UserMsg,
bart306527d2008-03-12 17:23:07 +0000279 "[%d/%d] barrier_pre_wait %s 0x%lx iteration %d",
bart3b1ee452008-02-29 19:28:15 +0000280 VG_(get_running_tid)(),
281 thread_get_running_tid(),
bart306527d2008-03-12 17:23:07 +0000282 barrier_get_typename(p),
bart3b1ee452008-02-29 19:28:15 +0000283 barrier,
284 p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000285 }
286
sewardj85642922008-01-14 11:54:56 +0000287 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
288 if (q == 0)
289 {
290 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
bartbebc5d62008-02-24 18:42:53 +0000291 barrier_thread_initialize(q, tid, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000292 VG_(OSetGen_Insert)(p->oset, q);
293 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
294 }
bartbebc5d62008-02-24 18:42:53 +0000295 vc_assign(&q->vc[p->pre_iteration], &thread_get_segment(tid)->vc);
296 tl_assert(q->vc[p->pre_iteration].size > 0);
297
298 if (--p->pre_waiters_left <= 0)
299 {
300 p->pre_iteration = 1 - p->pre_iteration;
301 p->pre_waiters_left = p->count;
302 }
sewardj85642922008-01-14 11:54:56 +0000303}
304
bart28230a32008-02-29 17:27:03 +0000305/** Called after pthread_barrier_wait(). */
sewardj85642922008-01-14 11:54:56 +0000306void barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000307 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000308{
bart3b1ee452008-02-29 19:28:15 +0000309 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000310
bart3b1ee452008-02-29 19:28:15 +0000311 p = barrier_get(barrier);
bartbebc5d62008-02-24 18:42:53 +0000312
sewardj85642922008-01-14 11:54:56 +0000313 if (s_trace_barrier)
314 {
bart3b1ee452008-02-29 19:28:15 +0000315 VG_(message)(Vg_UserMsg,
bart306527d2008-03-12 17:23:07 +0000316 "[%d/%d] barrier_post_wait %s 0x%lx iteration %d",
bart3b1ee452008-02-29 19:28:15 +0000317 VG_(get_running_tid)(),
318 tid,
bart306527d2008-03-12 17:23:07 +0000319 p ? barrier_get_typename(p) : "(?)",
bart3b1ee452008-02-29 19:28:15 +0000320 barrier,
bart306527d2008-03-12 17:23:07 +0000321 p ? p->post_iteration : -1);
sewardj85642922008-01-14 11:54:56 +0000322 }
323
bart306527d2008-03-12 17:23:07 +0000324 /* If p == 0, this means that the barrier has been destroyed after */
325 /* *_barrier_wait() returned and before this function was called. Just */
326 /* return in that case. */
327 if (p == 0)
328 return;
329
sewardj85642922008-01-14 11:54:56 +0000330 if (waited)
331 {
332 const UWord word_tid = tid;
333 struct barrier_thread_info* q;
334 struct barrier_thread_info* r;
335
sewardj85642922008-01-14 11:54:56 +0000336 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
337 tl_assert(q);
338 VG_(OSetGen_ResetIter)(p->oset);
339 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
340 {
341 if (r != q)
342 {
bartbebc5d62008-02-24 18:42:53 +0000343 thread_combine_vc2(tid, &r->vc[p->post_iteration]);
sewardj85642922008-01-14 11:54:56 +0000344 }
345 }
bart8ddef882008-03-09 08:48:01 +0000346 thread_combine_vc2(tid, &p->finished_threads_vc);
bartbebc5d62008-02-24 18:42:53 +0000347
348 thread_new_segment(tid);
349
350 if (--p->post_waiters_left <= 0)
351 {
352 p->post_iteration = 1 - p->post_iteration;
353 p->post_waiters_left = p->count;
354 }
sewardj85642922008-01-14 11:54:56 +0000355 }
356}
357
bartbebc5d62008-02-24 18:42:53 +0000358/** Call this function when thread tid stops to exist. */
sewardj85642922008-01-14 11:54:56 +0000359void barrier_thread_delete(const DrdThreadId tid)
360{
bart28230a32008-02-29 17:27:03 +0000361 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000362
bart72b751c2008-03-01 13:44:24 +0000363 clientobj_resetiter();
364 for ( ; (p = &clientobj_next(ClientBarrier)->barrier) != 0; )
sewardj85642922008-01-14 11:54:56 +0000365 {
bart28230a32008-02-29 17:27:03 +0000366 struct barrier_thread_info* q;
367 const UWord word_tid = tid;
368 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
bart8ddef882008-03-09 08:48:01 +0000369 vc_combine(&p->finished_threads_vc, &q->vc[p->post_iteration]);
bart28230a32008-02-29 17:27:03 +0000370 barrier_thread_destroy(q);
371 VG_(OSetGen_FreeNode)(p->oset, q);
sewardj85642922008-01-14 11:54:56 +0000372 }
373}
bart306527d2008-03-12 17:23:07 +0000374
375static const char* barrier_get_typename(struct barrier_info* const p)
376{
377 tl_assert(p);
378
379 return barrier_type_name(p->barrier_type);
380}
381
382static const char* barrier_type_name(const BarrierT bt)
383{
384 switch (bt)
385 {
386 case pthread_barrier:
387 return "pthread barrier";
388 case gomp_barrier:
389 return "gomp barrier";
390 }
391 return "?";
392}