blob: 30c801e20549a7ff7fce923edd68c7a79e80ddeb [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
bartbebc5d62008-02-24 18:42:53 +000042/* Information associated with one thread participating in a barrier. */
43struct 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));
113}
114
bart28230a32008-02-29 17:27:03 +0000115/** Deallocate the memory allocated by barrier_initialize() and in p->oset.
116 * Called by drd_clientobj_destroy().
117 */
118void barrier_cleanup(struct barrier_info* p)
sewardj85642922008-01-14 11:54:56 +0000119{
120 struct barrier_thread_info* q;
121
122 tl_assert(p);
123
bart28230a32008-02-29 17:27:03 +0000124 if (p->pre_waiters_left != p->count || p->post_waiters_left != p->count)
125 {
bart3b1ee452008-02-29 19:28:15 +0000126 BarrierErrInfo bei = { p->a1 };
127 VG_(maybe_record_error)(VG_(get_running_tid)(),
128 BarrierErr,
129 VG_(get_IP)(VG_(get_running_tid)()),
130 "Destruction of barrier that is being waited"
131 " upon",
132 &bei);
bart28230a32008-02-29 17:27:03 +0000133 }
sewardj85642922008-01-14 11:54:56 +0000134
135 VG_(OSetGen_ResetIter)(p->oset);
136 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
137 {
138 barrier_thread_destroy(q);
139 }
140 VG_(OSetGen_Destroy)(p->oset);
sewardj85642922008-01-14 11:54:56 +0000141}
142
bartbebc5d62008-02-24 18:42:53 +0000143/** Look up the client-side barrier address barrier in s_barrier[]. If not
144 * found, add it. */
sewardj85642922008-01-14 11:54:56 +0000145static
146struct barrier_info*
147barrier_get_or_allocate(const Addr barrier, const SizeT size, const Word count)
148{
bart28230a32008-02-29 17:27:03 +0000149 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000150
bart28230a32008-02-29 17:27:03 +0000151 tl_assert(offsetof(DrdClientobj, barrier) == 0);
152 p = &drd_clientobj_get(barrier, ClientBarrier)->barrier;
153 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000154 {
bart28230a32008-02-29 17:27:03 +0000155 p = &drd_clientobj_add(barrier, barrier + size, ClientBarrier)->barrier;
156 barrier_initialize(p, barrier, size, count);
sewardj85642922008-01-14 11:54:56 +0000157 }
bart28230a32008-02-29 17:27:03 +0000158 return p;
159}
160
161/** Look up the address of the information associated with the client-side
162 * barrier object. */
163struct barrier_info* barrier_get(const Addr barrier)
164{
165 tl_assert(offsetof(DrdClientobj, barrier) == 0);
166 return &drd_clientobj_get(barrier, ClientBarrier)->barrier;
sewardj85642922008-01-14 11:54:56 +0000167}
168
bartbebc5d62008-02-24 18:42:53 +0000169/** Initialize a barrier with client address barrier, client size size, and
bart28230a32008-02-29 17:27:03 +0000170 * where count threads participate in each barrier.
171 * Called before pthread_barrier_init().
172 */
sewardj85642922008-01-14 11:54:56 +0000173struct barrier_info*
174barrier_init(const Addr barrier, const SizeT size, const Word count)
175{
bart3b1ee452008-02-29 19:28:15 +0000176 if (s_trace_barrier)
177 {
178 VG_(message)(Vg_UserMsg,
179 "[%d/%d] barrier_init 0x%lx",
180 VG_(get_running_tid)(),
181 thread_get_running_tid(),
182 barrier);
183 }
sewardj85642922008-01-14 11:54:56 +0000184 tl_assert(barrier_get(barrier) == 0);
185 return barrier_get_or_allocate(barrier, size, count);
186}
187
bart28230a32008-02-29 17:27:03 +0000188/** Called after pthread_barrier_destroy(). */
189void barrier_destroy(struct barrier_info* const p)
sewardj85642922008-01-14 11:54:56 +0000190{
bart3b1ee452008-02-29 19:28:15 +0000191 if (s_trace_barrier)
192 {
193 VG_(message)(Vg_UserMsg,
194 "[%d/%d] barrier_destroy 0x%lx",
195 VG_(get_running_tid)(),
196 thread_get_running_tid(),
197 p->a1);
198 }
bart28230a32008-02-29 17:27:03 +0000199 tl_assert(p);
200 drd_clientobj_remove(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000201}
202
bart28230a32008-02-29 17:27:03 +0000203/** Called before pthread_barrier_wait(). */
sewardj85642922008-01-14 11:54:56 +0000204void barrier_pre_wait(const DrdThreadId tid, const Addr barrier)
205{
206 struct barrier_info* p;
207 struct barrier_thread_info* q;
208 const UWord word_tid = tid;
209
210 p = barrier_get(barrier);
211 tl_assert(p);
212
213 if (s_trace_barrier)
214 {
bart3b1ee452008-02-29 19:28:15 +0000215 VG_(message)(Vg_UserMsg,
216 "[%d/%d] barrier_pre_wait 0x%lx iteration %d",
217 VG_(get_running_tid)(),
218 thread_get_running_tid(),
219 barrier,
220 p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000221 }
222
sewardj85642922008-01-14 11:54:56 +0000223 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
224 if (q == 0)
225 {
226 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
bartbebc5d62008-02-24 18:42:53 +0000227 barrier_thread_initialize(q, tid, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000228 VG_(OSetGen_Insert)(p->oset, q);
229 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
230 }
bartbebc5d62008-02-24 18:42:53 +0000231 vc_assign(&q->vc[p->pre_iteration], &thread_get_segment(tid)->vc);
232 tl_assert(q->vc[p->pre_iteration].size > 0);
233
234 if (--p->pre_waiters_left <= 0)
235 {
236 p->pre_iteration = 1 - p->pre_iteration;
237 p->pre_waiters_left = p->count;
238 }
sewardj85642922008-01-14 11:54:56 +0000239}
240
bart28230a32008-02-29 17:27:03 +0000241/** Called after pthread_barrier_wait(). */
sewardj85642922008-01-14 11:54:56 +0000242void barrier_post_wait(const DrdThreadId tid, const Addr barrier,
243 const Bool waited)
244{
bart3b1ee452008-02-29 19:28:15 +0000245 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000246
bart3b1ee452008-02-29 19:28:15 +0000247 p = barrier_get(barrier);
bartbebc5d62008-02-24 18:42:53 +0000248 tl_assert(p);
249
sewardj85642922008-01-14 11:54:56 +0000250 if (s_trace_barrier)
251 {
bart3b1ee452008-02-29 19:28:15 +0000252 VG_(message)(Vg_UserMsg,
253 "[%d/%d] barrier_post_wait 0x%lx iteration %d",
254 VG_(get_running_tid)(),
255 tid,
256 barrier,
257 p->post_iteration);
sewardj85642922008-01-14 11:54:56 +0000258 }
259
260 if (waited)
261 {
262 const UWord word_tid = tid;
263 struct barrier_thread_info* q;
264 struct barrier_thread_info* r;
265
sewardj85642922008-01-14 11:54:56 +0000266 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
267 tl_assert(q);
268 VG_(OSetGen_ResetIter)(p->oset);
269 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
270 {
271 if (r != q)
272 {
bartbebc5d62008-02-24 18:42:53 +0000273 thread_combine_vc2(tid, &r->vc[p->post_iteration]);
sewardj85642922008-01-14 11:54:56 +0000274 }
275 }
bartbebc5d62008-02-24 18:42:53 +0000276
277 thread_new_segment(tid);
278
279 if (--p->post_waiters_left <= 0)
280 {
281 p->post_iteration = 1 - p->post_iteration;
282 p->post_waiters_left = p->count;
283 }
sewardj85642922008-01-14 11:54:56 +0000284 }
285}
286
bartbebc5d62008-02-24 18:42:53 +0000287/** Call this function when thread tid stops to exist. */
sewardj85642922008-01-14 11:54:56 +0000288void barrier_thread_delete(const DrdThreadId tid)
289{
bart28230a32008-02-29 17:27:03 +0000290 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000291
bart28230a32008-02-29 17:27:03 +0000292 drd_clientobj_resetiter();
293 for ( ; (p = &drd_clientobj_next(ClientBarrier)->barrier) != 0; )
sewardj85642922008-01-14 11:54:56 +0000294 {
bart28230a32008-02-29 17:27:03 +0000295 struct barrier_thread_info* q;
296 const UWord word_tid = tid;
297 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
298 barrier_thread_destroy(q);
299 VG_(OSetGen_FreeNode)(p->oset, q);
sewardj85642922008-01-14 11:54:56 +0000300 }
301}