blob: 134614273c17b494aea911546bbc1c65241f5e1d [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
41struct barrier_thread_info
42{
43 UWord tid; // A DrdThreadId
44 Word iteration; // barrier number corresponding to ongoing
45 // pthread_barrier() call modulo two.
46 VectorClock vc[2]; // vector clocks corresponding to the last two
47 // pthread_barrier() calls.
48};
49
50struct barrier_info
51{
52 Addr barrier; // Client address of barrier.
53 SizeT size; // Size in bytes of client-side object.
54 Word count; // Participant count in a barrier wait.
55 Word iteration; // barrier number corresponding to ongoing
56 // pthread_barrier() call modulo two.
57 Word participants; // Number of participants that still have to join
58 // the most recent barrier.
59 OSet* oset; // Information about specific threads.
60};
61
62
63// Local variables.
64
65static Bool s_trace_barrier = False;
66struct barrier_info s_barrier[4];
67
68
69// Function definitions.
70
71void barrier_set_trace(const Bool trace_barrier)
72{
73 s_trace_barrier = trace_barrier;
74}
75
76static void barrier_thread_initialize(struct barrier_thread_info* const p,
77 const DrdThreadId tid,
78 const Word iteration)
79{
80 p->tid = tid;
81 p->iteration = iteration;
82 vc_init(&p->vc[0], 0, 0);
83 vc_init(&p->vc[1], 0, 0);
84}
85
86static void barrier_thread_destroy(struct barrier_thread_info* const p)
87{
88 vc_cleanup(&p->vc[0]);
89 vc_cleanup(&p->vc[1]);
90}
91
92static
93void barrier_initialize(struct barrier_info* const p,
94 const Addr barrier,
95 const SizeT size,
96 const Word count)
97{
98 tl_assert(barrier != 0);
99 tl_assert(size > 0);
100 tl_assert(count > 0);
101
102 p->barrier = barrier;
103 p->size = size;
104 p->count = count;
105 p->iteration = 0;
106 p->participants = count;
107 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid) == sizeof(Word));
108 tl_assert(sizeof(((struct barrier_thread_info*)0)->tid)
109 >= sizeof(DrdThreadId));
110 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free));
111}
112
113void barrier_destroy(struct barrier_info* const p)
114{
115 struct barrier_thread_info* q;
116
117 tl_assert(p);
118
119 drd_finish_suppression(p->barrier, p->barrier + p->size);
120
121 VG_(OSetGen_ResetIter)(p->oset);
122 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
123 {
124 barrier_thread_destroy(q);
125 }
126 VG_(OSetGen_Destroy)(p->oset);
127 p->barrier = 0;
128 p->size = 0;
129 p->count = 0;
130 p->iteration = 0;
131 p->participants = 0;
132}
133
134static
135struct barrier_info*
136barrier_get_or_allocate(const Addr barrier, const SizeT size, const Word count)
137{
138 int i;
139
140 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
141 {
142 if (s_barrier[i].barrier == barrier)
143 {
144 tl_assert(s_barrier[i].size == size);
145 return &s_barrier[i];
146 }
147 }
148 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
149 {
150 if (s_barrier[i].barrier == 0)
151 {
152 barrier_initialize(&s_barrier[i], barrier, size, count);
153 drd_start_suppression(barrier, barrier + size, "barrier");
154 return &s_barrier[i];
155 }
156 }
157 tl_assert(0);
158 return 0;
159}
160
161struct barrier_info*
162barrier_init(const Addr barrier, const SizeT size, const Word count)
163{
164 tl_assert(barrier_get(barrier) == 0);
165 return barrier_get_or_allocate(barrier, size, count);
166}
167
168struct barrier_info* barrier_get(const Addr barrier)
169{
170 int i;
171 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
172 if (s_barrier[i].barrier == barrier)
173 return &s_barrier[i];
174 return 0;
175}
176
177void barrier_pre_wait(const DrdThreadId tid, const Addr barrier)
178{
179 struct barrier_info* p;
180 struct barrier_thread_info* q;
181 const UWord word_tid = tid;
182
183 p = barrier_get(barrier);
184 tl_assert(p);
185
186 if (s_trace_barrier)
187 {
188 VG_(message)(Vg_DebugMsg,
189 "[%d] barrier_pre_wait(%p) iteration %d / left %d/%d",
190 tid, barrier, p->iteration, p->participants, p->count);
191 }
192
193 if (--p->participants <= 0)
194 {
195 p->iteration = ! p->iteration;
196 p->participants = p->count;
197 }
198 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
199 if (q == 0)
200 {
201 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
202 barrier_thread_initialize(q, tid, p->iteration);
203 tl_assert(q->tid == tid);
204 VG_(OSetGen_Insert)(p->oset, q);
205 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
206 }
207 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
208 vc_copy(&q->vc[p->iteration], &thread_get_segment(tid)->vc);
209}
210
211void barrier_post_wait(const DrdThreadId tid, const Addr barrier,
212 const Bool waited)
213{
214 struct barrier_info* const p = barrier_get(barrier);
215
216 if (s_trace_barrier)
217 {
218 VG_(message)(Vg_DebugMsg, "[%d] barrier_post_wait(%p) iteration %d",
219 tid, barrier, p ? 1 - p->iteration : -1);
220 }
221
222 if (waited)
223 {
224 const UWord word_tid = tid;
225 struct barrier_thread_info* q;
226 struct barrier_thread_info* r;
227
228 tl_assert(p);
229 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
230 tl_assert(q);
231 VG_(OSetGen_ResetIter)(p->oset);
232 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
233 {
234 if (r != q)
235 {
236 if (s_trace_barrier)
237 {
238 VG_(message)(Vg_DebugMsg,
239 "[%d] barrier_post_wait: combining vc of thread %d",
240 tid, r->tid);
241 }
242 thread_combine_vc2(tid, &r->vc[1 - q->iteration]);
243 }
244 }
245 }
246}
247
248/**
249 * Call this function when thread threadid stops to exist, such that the
250 * "last owner" field can be cleared if it still refers to that thread.
251 */
252void barrier_thread_delete(const DrdThreadId tid)
253{
254 int i;
255 struct barrier_thread_info* q;
256
257 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
258 {
259 struct barrier_info* const p = &s_barrier[i];
260 if (p->barrier)
261 {
262 VG_(OSetGen_ResetIter)(p->oset);
263 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
264 {
265 }
266 }
267 }
268}
269
270void barrier_stop_using_mem(const Addr a1, const Addr a2)
271{
272 unsigned i;
273 for (i = 0; i < sizeof(s_barrier)/sizeof(s_barrier[0]); i++)
274 {
275 if (a1 <= s_barrier[i].barrier && s_barrier[i].barrier < a2)
276 {
277 tl_assert(s_barrier[i].barrier + s_barrier[i].size <= a2);
278 barrier_destroy(&s_barrier[i]);
279 }
280 }
281}