blob: 088872ca01bb5284365283e389bae930d1cbefd4 [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"
27#include "drd_error.h"
28#include "drd_suppression.h"
29#include "priv_drd_clientreq.h"
30#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
bartbebc5d62008-02-24 18:42:53 +000041/* Information associated with a client-side pthread_barrier_t object. */
sewardj85642922008-01-14 11:54:56 +000042struct barrier_info
43{
44 Addr barrier; // Client address of barrier.
45 SizeT size; // Size in bytes of client-side object.
46 Word count; // Participant count in a barrier wait.
bartbebc5d62008-02-24 18:42:53 +000047 Word pre_iteration; // pthread_barrier_wait() call count modulo two.
48 Word post_iteration; // pthread_barrier_wait() call count modulo two.
49 Word pre_waiters_left; // number of waiters left for a complete barrier.
50 Word post_waiters_left; // number of waiters left for a complete barrier.
51 OSet* oset; // Thread-specific barrier information.
52};
53
54/* Information associated with one thread participating in a barrier. */
55struct barrier_thread_info
56{
57 UWord tid; // A DrdThreadId
58 Word iteration; // iteration of last pthread_barrier_wait()
59 // call thread tid participated in.
60 VectorClock vc[2]; // vector clocks corresponding to the last two
61 // pthread_barrier() calls by thread tid.
sewardj85642922008-01-14 11:54:56 +000062};
63
64
65// Local variables.
66
67static Bool s_trace_barrier = False;
bartbebc5d62008-02-24 18:42:53 +000068/* To do: eliminate the upper limit on the number of barriers (4). */
sewardj85642922008-01-14 11:54:56 +000069struct barrier_info s_barrier[4];
70
71
72// Function definitions.
73
74void barrier_set_trace(const Bool trace_barrier)
75{
76 s_trace_barrier = trace_barrier;
77}
78
bartbebc5d62008-02-24 18:42:53 +000079/** Initialize the structure *p with the specified thread ID and iteration
80 * information. */
sewardj85642922008-01-14 11:54:56 +000081static void barrier_thread_initialize(struct barrier_thread_info* const p,
82 const DrdThreadId tid,
83 const Word iteration)
84{
85 p->tid = tid;
86 p->iteration = iteration;
87 vc_init(&p->vc[0], 0, 0);
88 vc_init(&p->vc[1], 0, 0);
89}
90
bartbebc5d62008-02-24 18:42:53 +000091/** Deallocate the memory that was allocated in barrier_thread_initialize(). */
sewardj85642922008-01-14 11:54:56 +000092static void barrier_thread_destroy(struct barrier_thread_info* const p)
93{
94 vc_cleanup(&p->vc[0]);
95 vc_cleanup(&p->vc[1]);
96}
97
bartbebc5d62008-02-24 18:42:53 +000098/** Initialize the structure *p with the specified client-side barrier address,
99 * barrier object size and number of participants in each barrier. */
sewardj85642922008-01-14 11:54:56 +0000100static
101void barrier_initialize(struct barrier_info* const p,
102 const Addr barrier,
103 const SizeT size,
104 const Word count)
105{
106 tl_assert(barrier != 0);
107 tl_assert(size > 0);
108 tl_assert(count > 0);
109
bartbebc5d62008-02-24 18:42:53 +0000110 p->barrier = barrier;
111 p->size = size;
112 p->count = count;
113 p->pre_iteration = 0;
114 p->post_iteration = 0;
115 p->pre_waiters_left = count;
116 p->post_waiters_left = count;
sewardj85642922008-01-14 11:54:56 +0000117 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word));
118 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid)
119 >= sizeof(DrdThreadId));
120 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free));
121}
122
bartbebc5d62008-02-24 18:42:53 +0000123/** Deallocate the memory allocated by barrier_initialize() and in p->oset. */
sewardj85642922008-01-14 11:54:56 +0000124void barrier_destroy(struct barrier_info* const p)
125{
126 struct barrier_thread_info* q;
127
128 tl_assert(p);
129
130 drd_finish_suppression(p->barrier, p->barrier + p->size);
131
132 VG_(OSetGen_ResetIter)(p->oset);
133 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
134 {
135 barrier_thread_destroy(q);
136 }
137 VG_(OSetGen_Destroy)(p->oset);
bartbebc5d62008-02-24 18:42:53 +0000138 p->barrier = 0;
139 p->size = 0;
140 p->count = 0;
141 p->pre_iteration = 0;
142 p->post_iteration = 0;
143 p->pre_waiters_left = 0;
144 p->post_waiters_left = 0;
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*
151barrier_get_or_allocate(const Addr barrier, const SizeT size, const Word count)
152{
153 int i;
154
155 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
156 {
157 if (s_barrier[i].barrier == barrier)
158 {
159 tl_assert(s_barrier[i].size == size);
160 return &s_barrier[i];
161 }
162 }
163 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
164 {
165 if (s_barrier[i].barrier == 0)
166 {
167 barrier_initialize(&s_barrier[i], barrier, size, count);
168 drd_start_suppression(barrier, barrier + size, "barrier");
169 return &s_barrier[i];
170 }
171 }
172 tl_assert(0);
173 return 0;
174}
175
bartbebc5d62008-02-24 18:42:53 +0000176/** Initialize a barrier with client address barrier, client size size, and
177 * where count threads participate in each barrier. */
sewardj85642922008-01-14 11:54:56 +0000178struct barrier_info*
179barrier_init(const Addr barrier, const SizeT size, const Word count)
180{
181 tl_assert(barrier_get(barrier) == 0);
182 return barrier_get_or_allocate(barrier, size, count);
183}
184
bartbebc5d62008-02-24 18:42:53 +0000185/** Look up the address of the information associated with the client-side
186 * barrier object. */
sewardj85642922008-01-14 11:54:56 +0000187struct barrier_info* barrier_get(const Addr barrier)
188{
189 int i;
190 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
191 if (s_barrier[i].barrier == barrier)
192 return &s_barrier[i];
193 return 0;
194}
195
196void barrier_pre_wait(const DrdThreadId tid, const Addr barrier)
197{
198 struct barrier_info* p;
199 struct barrier_thread_info* q;
200 const UWord word_tid = tid;
201
202 p = barrier_get(barrier);
203 tl_assert(p);
204
205 if (s_trace_barrier)
206 {
207 VG_(message)(Vg_DebugMsg,
bartbebc5d62008-02-24 18:42:53 +0000208 "[%d] barrier_pre_wait(%p) iteration %d",
209 tid, barrier, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000210 }
211
sewardj85642922008-01-14 11:54:56 +0000212 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
213 if (q == 0)
214 {
215 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
bartbebc5d62008-02-24 18:42:53 +0000216 barrier_thread_initialize(q, tid, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000217 VG_(OSetGen_Insert)(p->oset, q);
218 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
219 }
bartbebc5d62008-02-24 18:42:53 +0000220 vc_assign(&q->vc[p->pre_iteration], &thread_get_segment(tid)->vc);
221 tl_assert(q->vc[p->pre_iteration].size > 0);
222
223 if (--p->pre_waiters_left <= 0)
224 {
225 p->pre_iteration = 1 - p->pre_iteration;
226 p->pre_waiters_left = p->count;
227 }
sewardj85642922008-01-14 11:54:56 +0000228}
229
230void barrier_post_wait(const DrdThreadId tid, const Addr barrier,
231 const Bool waited)
232{
233 struct barrier_info* const p = barrier_get(barrier);
234
bartbebc5d62008-02-24 18:42:53 +0000235 tl_assert(p);
236
sewardj85642922008-01-14 11:54:56 +0000237 if (s_trace_barrier)
238 {
239 VG_(message)(Vg_DebugMsg, "[%d] barrier_post_wait(%p) iteration %d",
bartbebc5d62008-02-24 18:42:53 +0000240 tid, barrier, p->post_iteration);
sewardj85642922008-01-14 11:54:56 +0000241 }
242
243 if (waited)
244 {
245 const UWord word_tid = tid;
246 struct barrier_thread_info* q;
247 struct barrier_thread_info* r;
248
sewardj85642922008-01-14 11:54:56 +0000249 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
250 tl_assert(q);
251 VG_(OSetGen_ResetIter)(p->oset);
252 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
253 {
254 if (r != q)
255 {
256 if (s_trace_barrier)
257 {
258 VG_(message)(Vg_DebugMsg,
bartbebc5d62008-02-24 18:42:53 +0000259 "[%d] barrier_post_wait: combining vc of thread %d, "
260 "iteration %d",
261 tid, r->tid, p->post_iteration);
262 vc_print(&thread_get_segment(tid)->vc);
263 VG_(printf)(", ");
264 vc_print(&r->vc[p->post_iteration]);
265 VG_(printf)(" -> ");
sewardj85642922008-01-14 11:54:56 +0000266 }
bartbebc5d62008-02-24 18:42:53 +0000267 thread_combine_vc2(tid, &r->vc[p->post_iteration]);
268 if (s_trace_barrier)
269 {
270 vc_print(&thread_get_segment(tid)->vc);
271 VG_(printf)("\n");
272 }
sewardj85642922008-01-14 11:54:56 +0000273 }
274 }
bartbebc5d62008-02-24 18:42:53 +0000275
276 thread_new_segment(tid);
277
278 if (--p->post_waiters_left <= 0)
279 {
280 p->post_iteration = 1 - p->post_iteration;
281 p->post_waiters_left = p->count;
282 }
sewardj85642922008-01-14 11:54:56 +0000283 }
284}
285
bartbebc5d62008-02-24 18:42:53 +0000286/** Call this function when thread tid stops to exist. */
sewardj85642922008-01-14 11:54:56 +0000287void barrier_thread_delete(const DrdThreadId tid)
288{
289 int i;
sewardj85642922008-01-14 11:54:56 +0000290
291 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
292 {
293 struct barrier_info* const p = &s_barrier[i];
294 if (p->barrier)
295 {
bartbebc5d62008-02-24 18:42:53 +0000296 struct barrier_thread_info* q;
297 const UWord word_tid = tid;
298 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
299 barrier_thread_destroy(q);
300 VG_(OSetGen_FreeNode)(p->oset, q);
sewardj85642922008-01-14 11:54:56 +0000301 }
302 }
303}
304
305void barrier_stop_using_mem(const Addr a1, const Addr a2)
306{
307 unsigned i;
308 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
309 {
310 if (a1 <= s_barrier[i].barrier && s_barrier[i].barrier < a2)
311 {
312 tl_assert(s_barrier[i].barrier + s_barrier[i].size <= a2);
313 barrier_destroy(&s_barrier[i]);
314 }
315 }
316}