blob: b1211053644c6aad6d573b8c9d2ccec0f08ad521 [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
55void barrier_cleanup(struct barrier_info* p);
56
57
sewardj85642922008-01-14 11:54:56 +000058// Local variables.
59
60static Bool s_trace_barrier = False;
sewardj85642922008-01-14 11:54:56 +000061
62
63// Function definitions.
64
65void barrier_set_trace(const Bool trace_barrier)
66{
67 s_trace_barrier = trace_barrier;
68}
69
bartbebc5d62008-02-24 18:42:53 +000070/** Initialize the structure *p with the specified thread ID and iteration
71 * information. */
sewardj85642922008-01-14 11:54:56 +000072static void barrier_thread_initialize(struct barrier_thread_info* const p,
73 const DrdThreadId tid,
74 const Word iteration)
75{
76 p->tid = tid;
77 p->iteration = iteration;
78 vc_init(&p->vc[0], 0, 0);
79 vc_init(&p->vc[1], 0, 0);
80}
81
bartbebc5d62008-02-24 18:42:53 +000082/** Deallocate the memory that was allocated in barrier_thread_initialize(). */
sewardj85642922008-01-14 11:54:56 +000083static void barrier_thread_destroy(struct barrier_thread_info* const p)
84{
85 vc_cleanup(&p->vc[0]);
86 vc_cleanup(&p->vc[1]);
87}
88
bartbebc5d62008-02-24 18:42:53 +000089/** Initialize the structure *p with the specified client-side barrier address,
90 * barrier object size and number of participants in each barrier. */
sewardj85642922008-01-14 11:54:56 +000091static
92void barrier_initialize(struct barrier_info* const p,
93 const Addr barrier,
94 const SizeT size,
95 const Word count)
96{
97 tl_assert(barrier != 0);
98 tl_assert(size > 0);
99 tl_assert(count > 0);
bart28230a32008-02-29 17:27:03 +0000100 tl_assert(p->a1 == barrier);
101 tl_assert(p->a2 - p->a1 == size);
sewardj85642922008-01-14 11:54:56 +0000102
bart28230a32008-02-29 17:27:03 +0000103 p->cleanup = (void(*)(DrdClientobj*))barrier_cleanup;
bartbebc5d62008-02-24 18:42:53 +0000104 p->count = count;
105 p->pre_iteration = 0;
106 p->post_iteration = 0;
107 p->pre_waiters_left = count;
108 p->post_waiters_left = count;
sewardj85642922008-01-14 11:54:56 +0000109 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word));
110 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid)
111 >= sizeof(DrdThreadId));
112 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free));
bart8ddef882008-03-09 08:48:01 +0000113 vc_init(&p->finished_threads_vc, 0, 0);
sewardj85642922008-01-14 11:54:56 +0000114}
115
bart28230a32008-02-29 17:27:03 +0000116/** Deallocate the memory allocated by barrier_initialize() and in p->oset.
bart72b751c2008-03-01 13:44:24 +0000117 * Called by clientobj_destroy().
bart28230a32008-02-29 17:27:03 +0000118 */
119void barrier_cleanup(struct barrier_info* p)
sewardj85642922008-01-14 11:54:56 +0000120{
121 struct barrier_thread_info* q;
122
123 tl_assert(p);
124
bart28230a32008-02-29 17:27:03 +0000125 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
126 {
bart3b1ee452008-02-29 19:28:15 +0000127 BarrierErrInfo bei = { p->a1 };
128 VG_(maybe_record_error)(VG_(get_running_tid)(),
129 BarrierErr,
130 VG_(get_IP)(VG_(get_running_tid)()),
131 "Destruction of barrier that is being waited"
132 " upon",
133 &bei);
bart28230a32008-02-29 17:27:03 +0000134 }
sewardj85642922008-01-14 11:54:56 +0000135
136 VG_(OSetGen_ResetIter)(p->oset);
137 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
138 {
139 barrier_thread_destroy(q);
140 }
141 VG_(OSetGen_Destroy)(p->oset);
bart8ddef882008-03-09 08:48:01 +0000142 vc_cleanup(&p->finished_threads_vc);
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*
149barrier_get_or_allocate(const Addr barrier, const SizeT size, const Word count)
150{
bart28230a32008-02-29 17:27:03 +0000151 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000152
bart28230a32008-02-29 17:27:03 +0000153 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart72b751c2008-03-01 13:44:24 +0000154 p = &clientobj_get(barrier, ClientBarrier)->barrier;
bart28230a32008-02-29 17:27:03 +0000155 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000156 {
bart72b751c2008-03-01 13:44:24 +0000157 p = &clientobj_add(barrier, barrier + size, ClientBarrier)->barrier;
bart28230a32008-02-29 17:27:03 +0000158 barrier_initialize(p, barrier, size, count);
sewardj85642922008-01-14 11:54:56 +0000159 }
bart28230a32008-02-29 17:27:03 +0000160 return p;
161}
162
163/** Look up the address of the information associated with the client-side
164 * barrier object. */
bart72b751c2008-03-01 13:44:24 +0000165static struct barrier_info* barrier_get(const Addr barrier)
bart28230a32008-02-29 17:27:03 +0000166{
167 tl_assert(offsetof(DrdClientobj, barrier) == 0);
bart72b751c2008-03-01 13:44:24 +0000168 return &clientobj_get(barrier, ClientBarrier)->barrier;
sewardj85642922008-01-14 11:54:56 +0000169}
170
bartbebc5d62008-02-24 18:42:53 +0000171/** Initialize a barrier with client address barrier, client size size, and
bart28230a32008-02-29 17:27:03 +0000172 * where count threads participate in each barrier.
173 * Called before pthread_barrier_init().
174 */
sewardj85642922008-01-14 11:54:56 +0000175struct barrier_info*
176barrier_init(const Addr barrier, const SizeT size, const Word count)
177{
bart3b1ee452008-02-29 19:28:15 +0000178 if (s_trace_barrier)
179 {
180 VG_(message)(Vg_UserMsg,
181 "[%d/%d] barrier_init 0x%lx",
182 VG_(get_running_tid)(),
183 thread_get_running_tid(),
184 barrier);
185 }
sewardj85642922008-01-14 11:54:56 +0000186 tl_assert(barrier_get(barrier) == 0);
187 return barrier_get_or_allocate(barrier, size, count);
188}
189
bart28230a32008-02-29 17:27:03 +0000190/** Called after pthread_barrier_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000191void barrier_destroy(const Addr barrier)
sewardj85642922008-01-14 11:54:56 +0000192{
bart72b751c2008-03-01 13:44:24 +0000193 struct barrier_info* p;
194
bart3b1ee452008-02-29 19:28:15 +0000195 if (s_trace_barrier)
196 {
197 VG_(message)(Vg_UserMsg,
198 "[%d/%d] barrier_destroy 0x%lx",
199 VG_(get_running_tid)(),
200 thread_get_running_tid(),
bart72b751c2008-03-01 13:44:24 +0000201 barrier);
bart3b1ee452008-02-29 19:28:15 +0000202 }
bart72b751c2008-03-01 13:44:24 +0000203
204 p = barrier_get(barrier);
205 if (p == 0)
206 {
207 GenericErrInfo GEI;
208 VG_(maybe_record_error)(VG_(get_running_tid)(),
209 GenericErr,
210 VG_(get_IP)(VG_(get_running_tid)()),
211 "Not a barrier",
212 &GEI);
213 return;
214 }
215
216 clientobj_remove(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000217}
218
bart28230a32008-02-29 17:27:03 +0000219/** Called before pthread_barrier_wait(). */
sewardj85642922008-01-14 11:54:56 +0000220void barrier_pre_wait(const DrdThreadId tid, const Addr barrier)
221{
222 struct barrier_info* p;
223 struct barrier_thread_info* q;
224 const UWord word_tid = tid;
225
226 p = barrier_get(barrier);
227 tl_assert(p);
228
229 if (s_trace_barrier)
230 {
bart3b1ee452008-02-29 19:28:15 +0000231 VG_(message)(Vg_UserMsg,
232 "[%d/%d] barrier_pre_wait 0x%lx iteration %d",
233 VG_(get_running_tid)(),
234 thread_get_running_tid(),
235 barrier,
236 p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000237 }
238
sewardj85642922008-01-14 11:54:56 +0000239 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
240 if (q == 0)
241 {
242 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
bartbebc5d62008-02-24 18:42:53 +0000243 barrier_thread_initialize(q, tid, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000244 VG_(OSetGen_Insert)(p->oset, q);
245 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
246 }
bartbebc5d62008-02-24 18:42:53 +0000247 vc_assign(&q->vc[p->pre_iteration], &thread_get_segment(tid)->vc);
248 tl_assert(q->vc[p->pre_iteration].size > 0);
249
250 if (--p->pre_waiters_left <= 0)
251 {
252 p->pre_iteration = 1 - p->pre_iteration;
253 p->pre_waiters_left = p->count;
254 }
sewardj85642922008-01-14 11:54:56 +0000255}
256
bart28230a32008-02-29 17:27:03 +0000257/** Called after pthread_barrier_wait(). */
sewardj85642922008-01-14 11:54:56 +0000258void barrier_post_wait(const DrdThreadId tid, const Addr barrier,
259 const Bool waited)
260{
bart3b1ee452008-02-29 19:28:15 +0000261 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000262
bart3b1ee452008-02-29 19:28:15 +0000263 p = barrier_get(barrier);
bartbebc5d62008-02-24 18:42:53 +0000264 tl_assert(p);
265
sewardj85642922008-01-14 11:54:56 +0000266 if (s_trace_barrier)
267 {
bart3b1ee452008-02-29 19:28:15 +0000268 VG_(message)(Vg_UserMsg,
269 "[%d/%d] barrier_post_wait 0x%lx iteration %d",
270 VG_(get_running_tid)(),
271 tid,
272 barrier,
273 p->post_iteration);
sewardj85642922008-01-14 11:54:56 +0000274 }
275
276 if (waited)
277 {
278 const UWord word_tid = tid;
279 struct barrier_thread_info* q;
280 struct barrier_thread_info* r;
281
sewardj85642922008-01-14 11:54:56 +0000282 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
283 tl_assert(q);
284 VG_(OSetGen_ResetIter)(p->oset);
285 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
286 {
287 if (r != q)
288 {
bartbebc5d62008-02-24 18:42:53 +0000289 thread_combine_vc2(tid, &r->vc[p->post_iteration]);
sewardj85642922008-01-14 11:54:56 +0000290 }
291 }
bart8ddef882008-03-09 08:48:01 +0000292 thread_combine_vc2(tid, &p->finished_threads_vc);
bartbebc5d62008-02-24 18:42:53 +0000293
294 thread_new_segment(tid);
295
296 if (--p->post_waiters_left <= 0)
297 {
298 p->post_iteration = 1 - p->post_iteration;
299 p->post_waiters_left = p->count;
300 }
sewardj85642922008-01-14 11:54:56 +0000301 }
302}
303
bartbebc5d62008-02-24 18:42:53 +0000304/** Call this function when thread tid stops to exist. */
sewardj85642922008-01-14 11:54:56 +0000305void barrier_thread_delete(const DrdThreadId tid)
306{
bart28230a32008-02-29 17:27:03 +0000307 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000308
bart72b751c2008-03-01 13:44:24 +0000309 clientobj_resetiter();
310 for ( ; (p = &clientobj_next(ClientBarrier)->barrier) != 0; )
sewardj85642922008-01-14 11:54:56 +0000311 {
bart28230a32008-02-29 17:27:03 +0000312 struct barrier_thread_info* q;
313 const UWord word_tid = tid;
314 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
bart8ddef882008-03-09 08:48:01 +0000315 vc_combine(&p->finished_threads_vc, &q->vc[p->post_iteration]);
bart28230a32008-02-29 17:27:03 +0000316 barrier_thread_destroy(q);
317 VG_(OSetGen_FreeNode)(p->oset, q);
sewardj85642922008-01-14 11:54:56 +0000318 }
319}