blob: 762418039a03935a1a50fdef1d391e7581d8265b [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{
barta2b6e1b2008-03-17 18:32:39 +000087 sg_put(p->sg[0]);
88 sg_put(p->sg[1]);
sewardj85642922008-01-14 11:54:56 +000089}
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));
115}
116
bart28230a32008-02-29 17:27:03 +0000117/** Deallocate the memory allocated by barrier_initialize() and in p->oset.
bart72b751c2008-03-01 13:44:24 +0000118 * Called by clientobj_destroy().
bart28230a32008-02-29 17:27:03 +0000119 */
120void barrier_cleanup(struct barrier_info* p)
sewardj85642922008-01-14 11:54:56 +0000121{
122 struct barrier_thread_info* q;
123
124 tl_assert(p);
125
bart306527d2008-03-12 17:23:07 +0000126 if (p->pre_waiters_left != p->count)
bart28230a32008-02-29 17:27:03 +0000127 {
bart3b1ee452008-02-29 19:28:15 +0000128 BarrierErrInfo bei = { p->a1 };
129 VG_(maybe_record_error)(VG_(get_running_tid)(),
130 BarrierErr,
131 VG_(get_IP)(VG_(get_running_tid)()),
132 "Destruction of barrier that is being waited"
133 " upon",
134 &bei);
bart28230a32008-02-29 17:27:03 +0000135 }
sewardj85642922008-01-14 11:54:56 +0000136
137 VG_(OSetGen_ResetIter)(p->oset);
138 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
139 {
140 barrier_thread_destroy(q);
141 }
142 VG_(OSetGen_Destroy)(p->oset);
sewardj85642922008-01-14 11:54:56 +0000143}
144
bartbebc5d62008-02-24 18:42:53 +0000145/** Look up the client-side barrier address barrier in s_barrier[]. If not
146 * found, add it. */
sewardj85642922008-01-14 11:54:56 +0000147static
148struct barrier_info*
bart0268dfa2008-03-11 20:10:21 +0000149barrier_get_or_allocate(const Addr barrier,
150 const BarrierT barrier_type, const Word count)
sewardj85642922008-01-14 11:54:56 +0000151{
bart28230a32008-02-29 17:27:03 +0000152 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000153
bart0268dfa2008-03-11 20:10:21 +0000154 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
155
bart28230a32008-02-29 17:27:03 +0000156 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart72b751c2008-03-01 13:44:24 +0000157 p = &clientobj_get(barrier, ClientBarrier)->barrier;
bart28230a32008-02-29 17:27:03 +0000158 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000159 {
bart0268dfa2008-03-11 20:10:21 +0000160 p = &clientobj_add(barrier, ClientBarrier)->barrier;
161 barrier_initialize(p, barrier, barrier_type, count);
sewardj85642922008-01-14 11:54:56 +0000162 }
bart28230a32008-02-29 17:27:03 +0000163 return p;
164}
165
166/** Look up the address of the information associated with the client-side
167 * barrier object. */
bart72b751c2008-03-01 13:44:24 +0000168static struct barrier_info* barrier_get(const Addr barrier)
bart28230a32008-02-29 17:27:03 +0000169{
170 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart72b751c2008-03-01 13:44:24 +0000171 return &clientobj_get(barrier, ClientBarrier)->barrier;
sewardj85642922008-01-14 11:54:56 +0000172}
173
bartbebc5d62008-02-24 18:42:53 +0000174/** Initialize a barrier with client address barrier, client size size, and
bart28230a32008-02-29 17:27:03 +0000175 * where count threads participate in each barrier.
176 * Called before pthread_barrier_init().
177 */
bart0268dfa2008-03-11 20:10:21 +0000178void barrier_init(const Addr barrier,
179 const BarrierT barrier_type, const Word count,
180 const Bool reinitialization)
sewardj85642922008-01-14 11:54:56 +0000181{
bart306527d2008-03-12 17:23:07 +0000182 struct barrier_info* p;
183
184 tl_assert(barrier_type == pthread_barrier || barrier_type == gomp_barrier);
185
bartd9e39ec2008-06-28 15:03:26 +0000186 if (! reinitialization && barrier_type == pthread_barrier)
187 {
188 p = barrier_get(barrier);
189 if (p)
190 {
191 BarrierErrInfo bei = { barrier };
192 VG_(maybe_record_error)(VG_(get_running_tid)(),
193 BarrierErr,
194 VG_(get_IP)(VG_(get_running_tid)()),
195 "Barrier reinitializatoin",
196 &bei);
197 }
198 }
bart306527d2008-03-12 17:23:07 +0000199 p = barrier_get_or_allocate(barrier, barrier_type, count);
200
bart3b1ee452008-02-29 19:28:15 +0000201 if (s_trace_barrier)
202 {
bart306527d2008-03-12 17:23:07 +0000203 if (reinitialization)
204 {
205 VG_(message)(Vg_UserMsg,
barta2b6e1b2008-03-17 18:32:39 +0000206 "[%d/%d] barrier_reinit %s 0x%lx count %ld -> %ld",
bart306527d2008-03-12 17:23:07 +0000207 VG_(get_running_tid)(),
208 thread_get_running_tid(),
209 barrier_get_typename(p),
210 barrier,
211 p->count,
212 count);
213 }
214 else
215 {
216 VG_(message)(Vg_UserMsg,
217 "[%d/%d] barrier_init %s 0x%lx",
218 VG_(get_running_tid)(),
219 thread_get_running_tid(),
220 barrier_get_typename(p),
221 barrier);
222 }
bart3b1ee452008-02-29 19:28:15 +0000223 }
bart306527d2008-03-12 17:23:07 +0000224
225 if (reinitialization && p->count != count)
226 {
227 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
228 {
bart195a3982008-07-01 13:15:31 +0000229 BarrierErrInfo bei = { p->a1 };
230 VG_(maybe_record_error)(VG_(get_running_tid)(),
231 BarrierErr,
232 VG_(get_IP)(VG_(get_running_tid)()),
233 "Reinitialization of barrier with active"
234 " waiters",
235 &bei);
bart306527d2008-03-12 17:23:07 +0000236 }
237 p->count = count;
238 }
sewardj85642922008-01-14 11:54:56 +0000239}
240
bart28230a32008-02-29 17:27:03 +0000241/** Called after pthread_barrier_destroy(). */
bart0268dfa2008-03-11 20:10:21 +0000242void barrier_destroy(const Addr barrier, const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000243{
bart72b751c2008-03-01 13:44:24 +0000244 struct barrier_info* p;
245
bart306527d2008-03-12 17:23:07 +0000246 p = barrier_get(barrier);
247
bart3b1ee452008-02-29 19:28:15 +0000248 if (s_trace_barrier)
249 {
250 VG_(message)(Vg_UserMsg,
bart306527d2008-03-12 17:23:07 +0000251 "[%d/%d] barrier_destroy %s 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000252 VG_(get_running_tid)(),
253 thread_get_running_tid(),
bart306527d2008-03-12 17:23:07 +0000254 barrier_get_typename(p),
bart72b751c2008-03-01 13:44:24 +0000255 barrier);
bart3b1ee452008-02-29 19:28:15 +0000256 }
bart72b751c2008-03-01 13:44:24 +0000257
bart72b751c2008-03-01 13:44:24 +0000258 if (p == 0)
259 {
260 GenericErrInfo GEI;
261 VG_(maybe_record_error)(VG_(get_running_tid)(),
262 GenericErr,
263 VG_(get_IP)(VG_(get_running_tid)()),
264 "Not a barrier",
265 &GEI);
266 return;
267 }
268
bart195a3982008-07-01 13:15:31 +0000269 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
270 {
271 BarrierErrInfo bei = { p->a1 };
272 VG_(maybe_record_error)(VG_(get_running_tid)(),
273 BarrierErr,
274 VG_(get_IP)(VG_(get_running_tid)()),
275 "Destruction of a barrier with active waiters",
276 &bei);
277 }
278
bart72b751c2008-03-01 13:44:24 +0000279 clientobj_remove(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000280}
281
bart28230a32008-02-29 17:27:03 +0000282/** Called before pthread_barrier_wait(). */
bart0268dfa2008-03-11 20:10:21 +0000283void barrier_pre_wait(const DrdThreadId tid, const Addr barrier,
284 const BarrierT barrier_type)
sewardj85642922008-01-14 11:54:56 +0000285{
286 struct barrier_info* p;
287 struct barrier_thread_info* q;
288 const UWord word_tid = tid;
289
290 p = barrier_get(barrier);
bartc68bd602008-03-16 10:04:58 +0000291 if (p == 0 && barrier_type == gomp_barrier)
292 {
bart654013c2008-06-23 12:41:00 +0000293 VG_(message)(Vg_UserMsg, "");
bartc68bd602008-03-16 10:04:58 +0000294 VG_(message)(Vg_UserMsg,
295 "Please verify whether gcc has been configured"
296 " with option --disable-linux-futex.");
297 VG_(message)(Vg_UserMsg,
298 "See also the section about OpenMP in the DRD manual.");
bart654013c2008-06-23 12:41:00 +0000299 VG_(message)(Vg_UserMsg, "");
bartc68bd602008-03-16 10:04:58 +0000300 }
sewardj85642922008-01-14 11:54:56 +0000301 tl_assert(p);
302
303 if (s_trace_barrier)
304 {
bart3b1ee452008-02-29 19:28:15 +0000305 VG_(message)(Vg_UserMsg,
barta2b6e1b2008-03-17 18:32:39 +0000306 "[%d/%d] barrier_pre_wait %s 0x%lx iteration %ld",
bart3b1ee452008-02-29 19:28:15 +0000307 VG_(get_running_tid)(),
308 thread_get_running_tid(),
bart306527d2008-03-12 17:23:07 +0000309 barrier_get_typename(p),
bart3b1ee452008-02-29 19:28:15 +0000310 barrier,
311 p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000312 }
313
sewardj85642922008-01-14 11:54:56 +0000314 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
315 if (q == 0)
316 {
317 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
bartbebc5d62008-02-24 18:42:53 +0000318 barrier_thread_initialize(q, tid, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000319 VG_(OSetGen_Insert)(p->oset, q);
320 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
321 }
barta2b6e1b2008-03-17 18:32:39 +0000322 thread_get_latest_segment(&q->sg[p->pre_iteration], tid);
bartbebc5d62008-02-24 18:42:53 +0000323
324 if (--p->pre_waiters_left <= 0)
325 {
326 p->pre_iteration = 1 - p->pre_iteration;
327 p->pre_waiters_left = p->count;
328 }
sewardj85642922008-01-14 11:54:56 +0000329}
330
bart28230a32008-02-29 17:27:03 +0000331/** Called after pthread_barrier_wait(). */
sewardj85642922008-01-14 11:54:56 +0000332void barrier_post_wait(const DrdThreadId tid, const Addr barrier,
bart0268dfa2008-03-11 20:10:21 +0000333 const BarrierT barrier_type, const Bool waited)
sewardj85642922008-01-14 11:54:56 +0000334{
bart3b1ee452008-02-29 19:28:15 +0000335 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000336
bart3b1ee452008-02-29 19:28:15 +0000337 p = barrier_get(barrier);
bartbebc5d62008-02-24 18:42:53 +0000338
sewardj85642922008-01-14 11:54:56 +0000339 if (s_trace_barrier)
340 {
bart3b1ee452008-02-29 19:28:15 +0000341 VG_(message)(Vg_UserMsg,
barta2b6e1b2008-03-17 18:32:39 +0000342 "[%d/%d] barrier_post_wait %s 0x%lx iteration %ld",
bart3b1ee452008-02-29 19:28:15 +0000343 VG_(get_running_tid)(),
344 tid,
bart306527d2008-03-12 17:23:07 +0000345 p ? barrier_get_typename(p) : "(?)",
bart3b1ee452008-02-29 19:28:15 +0000346 barrier,
bart306527d2008-03-12 17:23:07 +0000347 p ? p->post_iteration : -1);
sewardj85642922008-01-14 11:54:56 +0000348 }
349
bart306527d2008-03-12 17:23:07 +0000350 /* If p == 0, this means that the barrier has been destroyed after */
351 /* *_barrier_wait() returned and before this function was called. Just */
352 /* return in that case. */
353 if (p == 0)
354 return;
355
sewardj85642922008-01-14 11:54:56 +0000356 if (waited)
357 {
358 const UWord word_tid = tid;
359 struct barrier_thread_info* q;
360 struct barrier_thread_info* r;
361
sewardj85642922008-01-14 11:54:56 +0000362 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
bart195a3982008-07-01 13:15:31 +0000363 if (q == 0)
364 {
365 BarrierErrInfo bei = { p->a1 };
366 VG_(maybe_record_error)(VG_(get_running_tid)(),
367 BarrierErr,
368 VG_(get_IP)(VG_(get_running_tid)()),
369 "Error in barrier implementation"
370 " -- barrier_wait() started before"
371 " barrier_destroy() and finished after"
372 " barrier_destroy()",
373 &bei);
374
375 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
376 barrier_thread_initialize(q, tid, p->pre_iteration);
377 VG_(OSetGen_Insert)(p->oset, q);
378 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
379 }
sewardj85642922008-01-14 11:54:56 +0000380 VG_(OSetGen_ResetIter)(p->oset);
381 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
382 {
383 if (r != q)
384 {
barta2b6e1b2008-03-17 18:32:39 +0000385 tl_assert(r->sg[p->post_iteration]);
386 thread_combine_vc2(tid, &r->sg[p->post_iteration]->vc);
sewardj85642922008-01-14 11:54:56 +0000387 }
388 }
bartbebc5d62008-02-24 18:42:53 +0000389
390 thread_new_segment(tid);
bart6bbefaf2008-04-19 15:16:45 +0000391 s_barrier_segment_creation_count++;
bartbebc5d62008-02-24 18:42:53 +0000392
393 if (--p->post_waiters_left <= 0)
394 {
395 p->post_iteration = 1 - p->post_iteration;
396 p->post_waiters_left = p->count;
397 }
sewardj85642922008-01-14 11:54:56 +0000398 }
399}
400
bartbebc5d62008-02-24 18:42:53 +0000401/** Call this function when thread tid stops to exist. */
sewardj85642922008-01-14 11:54:56 +0000402void barrier_thread_delete(const DrdThreadId tid)
403{
bart28230a32008-02-29 17:27:03 +0000404 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000405
bart72b751c2008-03-01 13:44:24 +0000406 clientobj_resetiter();
407 for ( ; (p = &clientobj_next(ClientBarrier)->barrier) != 0; )
sewardj85642922008-01-14 11:54:56 +0000408 {
bart28230a32008-02-29 17:27:03 +0000409 struct barrier_thread_info* q;
410 const UWord word_tid = tid;
411 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
412 barrier_thread_destroy(q);
413 VG_(OSetGen_FreeNode)(p->oset, q);
sewardj85642922008-01-14 11:54:56 +0000414 }
415}
bart306527d2008-03-12 17:23:07 +0000416
417static const char* barrier_get_typename(struct barrier_info* const p)
418{
419 tl_assert(p);
420
421 return barrier_type_name(p->barrier_type);
422}
423
424static const char* barrier_type_name(const BarrierT bt)
425{
426 switch (bt)
427 {
428 case pthread_barrier:
429 return "pthread barrier";
430 case gomp_barrier:
431 return "gomp barrier";
432 }
433 return "?";
434}
bart6bbefaf2008-04-19 15:16:45 +0000435
436ULong get_barrier_segment_creation_count(void)
437{
438 return s_barrier_segment_creation_count;
439}