blob: 689e582fec287ff21d8eef0d327a5d9055f90916 [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"
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
39// Type definitions.
40
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{
44 UWord tid; // A DrdThreadId
45 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.
sewardj85642922008-01-14 11:54:56 +000049};
50
51
bart28230a32008-02-29 17:27:03 +000052// Local functions.
53
bart306527d2008-03-12 17:23:07 +000054static void barrier_cleanup(struct barrier_info* p);
55static const char* barrier_get_typename(struct barrier_info* const p);
56static const char* barrier_type_name(const BarrierT bt);
bart28230a32008-02-29 17:27:03 +000057
58
sewardj85642922008-01-14 11:54:56 +000059// Local variables.
60
61static Bool s_trace_barrier = False;
bart6bbefaf2008-04-19 15:16:45 +000062static ULong s_barrier_segment_creation_count;
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;
barta2b6e1b2008-03-17 18:32:39 +000080 p->sg[0] = 0;
81 p->sg[1] = 0;
sewardj85642922008-01-14 11:54:56 +000082}
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{
bart22f74572008-07-07 08:17:55 +000087 tl_assert(p);
bart62ada3f2009-02-14 17:19:58 +000088 DRD_(sg_put)(p->sg[0]);
89 DRD_(sg_put)(p->sg[1]);
sewardj85642922008-01-14 11:54:56 +000090}
91
bartbebc5d62008-02-24 18:42:53 +000092/** Initialize the structure *p with the specified client-side barrier address,
93 * barrier object size and number of participants in each barrier. */
sewardj85642922008-01-14 11:54:56 +000094static
95void barrier_initialize(struct barrier_info* const p,
96 const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +000097 const BarrierT barrier_type,
sewardj85642922008-01-14 11:54:56 +000098 const Word count)
99{
100 tl_assert(barrier != 0);
bart0268dfa2008-03-11 20:10:21 +0000101 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
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));
sewardj9c606bd2008-09-18 18:12:50 +0000114 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.barrier.bi.1",
115 VG_(free));
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);
sewardj85642922008-01-14 11:54:56 +0000144}
145
bartbebc5d62008-02-24 18:42:53 +0000146/** Look up the client-side barrier address barrier in s_barrier[]. If not
147 * found, add it. */
sewardj85642922008-01-14 11:54:56 +0000148static
149struct barrier_info*
bart0268dfa2008-03-11 20:10:21 +0000150barrier_get_or_allocate(const Addr barrier,
151 const BarrierT barrier_type, const Word count)
sewardj85642922008-01-14 11:54:56 +0000152{
bart28230a32008-02-29 17:27:03 +0000153 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000154
bart0268dfa2008-03-11 20:10:21 +0000155 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
156
bart28230a32008-02-29 17:27:03 +0000157 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart72b751c2008-03-01 13:44:24 +0000158 p = &clientobj_get(barrier, ClientBarrier)->barrier;
bart28230a32008-02-29 17:27:03 +0000159 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000160 {
bart0268dfa2008-03-11 20:10:21 +0000161 p = &clientobj_add(barrier, ClientBarrier)->barrier;
162 barrier_initialize(p, barrier, barrier_type, count);
sewardj85642922008-01-14 11:54:56 +0000163 }
bart28230a32008-02-29 17:27:03 +0000164 return p;
165}
166
167/** Look up the address of the information associated with the client-side
168 * barrier object. */
bart72b751c2008-03-01 13:44:24 +0000169static struct barrier_info* barrier_get(const Addr barrier)
bart28230a32008-02-29 17:27:03 +0000170{
171 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart72b751c2008-03-01 13:44:24 +0000172 return &clientobj_get(barrier, ClientBarrier)->barrier;
sewardj85642922008-01-14 11:54:56 +0000173}
174
bartbebc5d62008-02-24 18:42:53 +0000175/** Initialize a barrier with client address barrier, client size size, and
bart28230a32008-02-29 17:27:03 +0000176 * where count threads participate in each barrier.
177 * Called before pthread_barrier_init().
178 */
bart0268dfa2008-03-11 20:10:21 +0000179void barrier_init(const Addr barrier,
180 const BarrierT barrier_type, const Word count,
181 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000182{
bart306527d2008-03-12 17:23:07 +0000183 struct barrier_info* p;
184
185 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
186
bartfcff0b92008-11-17 17:35:26 +0000187 if (count == 0)
188 {
189 BarrierErrInfo bei = { barrier };
190 VG_(maybe_record_error)(VG_(get_running_tid)(),
191 BarrierErr,
192 VG_(get_IP)(VG_(get_running_tid)()),
193 "pthread_barrier_init: 'count' argument is zero",
194 &bei);
195 }
196
bartd9e39ec2008-06-28 15:03:26 +0000197 if (! reinitialization && barrier_type == pthread_barrier)
198 {
199 p = barrier_get(barrier);
200 if (p)
201 {
202 BarrierErrInfo bei = { barrier };
203 VG_(maybe_record_error)(VG_(get_running_tid)(),
204 BarrierErr,
205 VG_(get_IP)(VG_(get_running_tid)()),
bartfcff0b92008-11-17 17:35:26 +0000206 "Barrier reinitialization",
bartd9e39ec2008-06-28 15:03:26 +0000207 &bei);
208 }
209 }
bart306527d2008-03-12 17:23:07 +0000210 p = barrier_get_or_allocate(barrier, barrier_type, count);
211
bart3b1ee452008-02-29 19:28:15 +0000212 if (s_trace_barrier)
213 {
bart306527d2008-03-12 17:23:07 +0000214 if (reinitialization)
215 {
216 VG_(message)(Vg_UserMsg,
barta2b6e1b2008-03-17 18:32:39 +0000217 "[%d/%d] barrier_reinit %s 0x%lx count %ld -> %ld",
bart306527d2008-03-12 17:23:07 +0000218 VG_(get_running_tid)(),
219 thread_get_running_tid(),
220 barrier_get_typename(p),
221 barrier,
222 p->count,
223 count);
224 }
225 else
226 {
227 VG_(message)(Vg_UserMsg,
228 "[%d/%d] barrier_init %s 0x%lx",
229 VG_(get_running_tid)(),
230 thread_get_running_tid(),
231 barrier_get_typename(p),
232 barrier);
233 }
bart3b1ee452008-02-29 19:28:15 +0000234 }
bart306527d2008-03-12 17:23:07 +0000235
236 if (reinitialization && p->count != count)
237 {
238 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
239 {
bart195a3982008-07-01 13:15:31 +0000240 BarrierErrInfo bei = { p->a1 };
241 VG_(maybe_record_error)(VG_(get_running_tid)(),
242 BarrierErr,
243 VG_(get_IP)(VG_(get_running_tid)()),
244 "Reinitialization of barrier with active"
245 " waiters",
246 &bei);
bart306527d2008-03-12 17:23:07 +0000247 }
248 p->count = count;
249 }
sewardj85642922008-01-14 11:54:56 +0000250}
251
bart28230a32008-02-29 17:27:03 +0000252/** Called after pthread_barrier_destroy(). */
bart0268dfa2008-03-11 20:10:21 +0000253void barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000254{
bart72b751c2008-03-01 13:44:24 +0000255 struct barrier_info* p;
256
bart306527d2008-03-12 17:23:07 +0000257 p = barrier_get(barrier);
258
bart3b1ee452008-02-29 19:28:15 +0000259 if (s_trace_barrier)
260 {
261 VG_(message)(Vg_UserMsg,
bart306527d2008-03-12 17:23:07 +0000262 "[%d/%d] barrier_destroy %s 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000263 VG_(get_running_tid)(),
264 thread_get_running_tid(),
bart306527d2008-03-12 17:23:07 +0000265 barrier_get_typename(p),
bart72b751c2008-03-01 13:44:24 +0000266 barrier);
bart3b1ee452008-02-29 19:28:15 +0000267 }
bart72b751c2008-03-01 13:44:24 +0000268
bart72b751c2008-03-01 13:44:24 +0000269 if (p == 0)
270 {
271 GenericErrInfo GEI;
272 VG_(maybe_record_error)(VG_(get_running_tid)(),
273 GenericErr,
274 VG_(get_IP)(VG_(get_running_tid)()),
275 "Not a barrier",
276 &GEI);
277 return;
278 }
279
bart195a3982008-07-01 13:15:31 +0000280 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
281 {
282 BarrierErrInfo bei = { p->a1 };
283 VG_(maybe_record_error)(VG_(get_running_tid)(),
284 BarrierErr,
285 VG_(get_IP)(VG_(get_running_tid)()),
286 "Destruction of a barrier with active waiters",
287 &bei);
288 }
289
bart72b751c2008-03-01 13:44:24 +0000290 clientobj_remove(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000291}
292
bart28230a32008-02-29 17:27:03 +0000293/** Called before pthread_barrier_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000294void barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
295 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000296{
297 struct barrier_info* p;
298 struct barrier_thread_info* q;
299 const UWord word_tid = tid;
300
301 p = barrier_get(barrier);
bartc68bd602008-03-16 10:04:58 +0000302 if (p == 0 && barrier_type == gomp_barrier)
303 {
bart654013c2008-06-23 12:41:00 +0000304 VG_(message)(Vg_UserMsg, "");
bartc68bd602008-03-16 10:04:58 +0000305 VG_(message)(Vg_UserMsg,
306 "Please verify whether gcc has been configured"
307 " with option --disable-linux-futex.");
308 VG_(message)(Vg_UserMsg,
309 "See also the section about OpenMP in the DRD manual.");
bart654013c2008-06-23 12:41:00 +0000310 VG_(message)(Vg_UserMsg, "");
bartc68bd602008-03-16 10:04:58 +0000311 }
sewardj85642922008-01-14 11:54:56 +0000312 tl_assert(p);
313
314 if (s_trace_barrier)
315 {
bart3b1ee452008-02-29 19:28:15 +0000316 VG_(message)(Vg_UserMsg,
barta2b6e1b2008-03-17 18:32:39 +0000317 "[%d/%d] barrier_pre_wait %s 0x%lx iteration %ld",
bart3b1ee452008-02-29 19:28:15 +0000318 VG_(get_running_tid)(),
319 thread_get_running_tid(),
bart306527d2008-03-12 17:23:07 +0000320 barrier_get_typename(p),
bart3b1ee452008-02-29 19:28:15 +0000321 barrier,
322 p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000323 }
324
sewardj85642922008-01-14 11:54:56 +0000325 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
326 if (q == 0)
327 {
328 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
bartbebc5d62008-02-24 18:42:53 +0000329 barrier_thread_initialize(q, tid, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000330 VG_(OSetGen_Insert)(p->oset, q);
331 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
332 }
barta2b6e1b2008-03-17 18:32:39 +0000333 thread_get_latest_segment(&q->sg[p->pre_iteration], tid);
bartbebc5d62008-02-24 18:42:53 +0000334
335 if (--p->pre_waiters_left <= 0)
336 {
337 p->pre_iteration = 1 - p->pre_iteration;
338 p->pre_waiters_left = p->count;
339 }
sewardj85642922008-01-14 11:54:56 +0000340}
341
bart28230a32008-02-29 17:27:03 +0000342/** Called after pthread_barrier_wait(). */
sewardj85642922008-01-14 11:54:56 +0000343void barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000344 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000345{
bart3b1ee452008-02-29 19:28:15 +0000346 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000347
bart3b1ee452008-02-29 19:28:15 +0000348 p = barrier_get(barrier);
bartbebc5d62008-02-24 18:42:53 +0000349
sewardj85642922008-01-14 11:54:56 +0000350 if (s_trace_barrier)
351 {
bart3b1ee452008-02-29 19:28:15 +0000352 VG_(message)(Vg_UserMsg,
barta2b6e1b2008-03-17 18:32:39 +0000353 "[%d/%d] barrier_post_wait %s 0x%lx iteration %ld",
bart3b1ee452008-02-29 19:28:15 +0000354 VG_(get_running_tid)(),
355 tid,
bart306527d2008-03-12 17:23:07 +0000356 p ? barrier_get_typename(p) : "(?)",
bart3b1ee452008-02-29 19:28:15 +0000357 barrier,
bart306527d2008-03-12 17:23:07 +0000358 p ? p->post_iteration : -1);
sewardj85642922008-01-14 11:54:56 +0000359 }
360
bart306527d2008-03-12 17:23:07 +0000361 /* If p == 0, this means that the barrier has been destroyed after */
362 /* *_barrier_wait() returned and before this function was called. Just */
363 /* return in that case. */
364 if (p == 0)
365 return;
366
sewardj85642922008-01-14 11:54:56 +0000367 if (waited)
368 {
369 const UWord word_tid = tid;
370 struct barrier_thread_info* q;
371 struct barrier_thread_info* r;
372
sewardj85642922008-01-14 11:54:56 +0000373 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
bart195a3982008-07-01 13:15:31 +0000374 if (q == 0)
375 {
376 BarrierErrInfo bei = { p->a1 };
377 VG_(maybe_record_error)(VG_(get_running_tid)(),
378 BarrierErr,
379 VG_(get_IP)(VG_(get_running_tid)()),
380 "Error in barrier implementation"
381 " -- barrier_wait() started before"
382 " barrier_destroy() and finished after"
383 " barrier_destroy()",
384 &bei);
385
386 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
387 barrier_thread_initialize(q, tid, p->pre_iteration);
388 VG_(OSetGen_Insert)(p->oset, q);
389 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
390 }
sewardj85642922008-01-14 11:54:56 +0000391 VG_(OSetGen_ResetIter)(p->oset);
392 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
393 {
394 if (r != q)
395 {
barta2b6e1b2008-03-17 18:32:39 +0000396 tl_assert(r->sg[p->post_iteration]);
397 thread_combine_vc2(tid, &r->sg[p->post_iteration]->vc);
sewardj85642922008-01-14 11:54:56 +0000398 }
399 }
bartbebc5d62008-02-24 18:42:53 +0000400
401 thread_new_segment(tid);
bart6bbefaf2008-04-19 15:16:45 +0000402 s_barrier_segment_creation_count++;
bartbebc5d62008-02-24 18:42:53 +0000403
404 if (--p->post_waiters_left <= 0)
405 {
406 p->post_iteration = 1 - p->post_iteration;
407 p->post_waiters_left = p->count;
408 }
sewardj85642922008-01-14 11:54:56 +0000409 }
410}
411
bartbebc5d62008-02-24 18:42:53 +0000412/** Call this function when thread tid stops to exist. */
sewardj85642922008-01-14 11:54:56 +0000413void barrier_thread_delete(const DrdThreadId tid)
414{
bart28230a32008-02-29 17:27:03 +0000415 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000416
bart72b751c2008-03-01 13:44:24 +0000417 clientobj_resetiter();
418 for ( ; (p = &clientobj_next(ClientBarrier)->barrier) != 0; )
sewardj85642922008-01-14 11:54:56 +0000419 {
bart28230a32008-02-29 17:27:03 +0000420 struct barrier_thread_info* q;
421 const UWord word_tid = tid;
422 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
bart7f912c02008-07-07 08:45:55 +0000423 /* q is only non-zero if the barrier object has been used by thread tid
424 * after the barrier_init() call and before the thread finished.
425 */
bart22f74572008-07-07 08:17:55 +0000426 if (q)
427 {
428 barrier_thread_destroy(q);
429 VG_(OSetGen_FreeNode)(p->oset, q);
430 }
sewardj85642922008-01-14 11:54:56 +0000431 }
432}
bart306527d2008-03-12 17:23:07 +0000433
434static const char* barrier_get_typename(struct barrier_info* const p)
435{
436 tl_assert(p);
437
438 return barrier_type_name(p->barrier_type);
439}
440
441static const char* barrier_type_name(const BarrierT bt)
442{
443 switch (bt)
444 {
445 case pthread_barrier:
446 return "pthread barrier";
447 case gomp_barrier:
448 return "gomp barrier";
449 }
450 return "?";
451}
bart6bbefaf2008-04-19 15:16:45 +0000452
453ULong get_barrier_segment_creation_count(void)
454{
455 return s_barrier_segment_creation_count;
456}