blob: b4fca3da4bd8a6432385cd3db5b42342dbde3114 [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 {
126 VG_(message)(Vg_UserMsg, "Destruction of barrier 0x%lx being waited upon",
127 p->a1);
128 }
sewardj85642922008-01-14 11:54:56 +0000129
130 VG_(OSetGen_ResetIter)(p->oset);
131 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
132 {
133 barrier_thread_destroy(q);
134 }
135 VG_(OSetGen_Destroy)(p->oset);
sewardj85642922008-01-14 11:54:56 +0000136}
137
bartbebc5d62008-02-24 18:42:53 +0000138/** Look up the client-side barrier address barrier in s_barrier[]. If not
139 * found, add it. */
sewardj85642922008-01-14 11:54:56 +0000140static
141struct barrier_info*
142barrier_get_or_allocate(const Addr barrier, const SizeT size, const Word count)
143{
bart28230a32008-02-29 17:27:03 +0000144 struct barrier_info *p;
sewardj85642922008-01-14 11:54:56 +0000145
bart28230a32008-02-29 17:27:03 +0000146 tl_assert(offsetof(DrdClientobj, barrier) == 0);
147 p = &drd_clientobj_get(barrier, ClientBarrier)->barrier;
148 if (p == 0)
sewardj85642922008-01-14 11:54:56 +0000149 {
bart28230a32008-02-29 17:27:03 +0000150 p = &drd_clientobj_add(barrier, barrier + size, ClientBarrier)->barrier;
151 barrier_initialize(p, barrier, size, count);
sewardj85642922008-01-14 11:54:56 +0000152 }
bart28230a32008-02-29 17:27:03 +0000153 return p;
154}
155
156/** Look up the address of the information associated with the client-side
157 * barrier object. */
158struct barrier_info* barrier_get(const Addr barrier)
159{
160 tl_assert(offsetof(DrdClientobj, barrier) == 0);
161 return &drd_clientobj_get(barrier, ClientBarrier)->barrier;
sewardj85642922008-01-14 11:54:56 +0000162}
163
bartbebc5d62008-02-24 18:42:53 +0000164/** Initialize a barrier with client address barrier, client size size, and
bart28230a32008-02-29 17:27:03 +0000165 * where count threads participate in each barrier.
166 * Called before pthread_barrier_init().
167 */
sewardj85642922008-01-14 11:54:56 +0000168struct barrier_info*
169barrier_init(const Addr barrier, const SizeT size, const Word count)
170{
171 tl_assert(barrier_get(barrier) == 0);
172 return barrier_get_or_allocate(barrier, size, count);
173}
174
bart28230a32008-02-29 17:27:03 +0000175/** Called after pthread_barrier_destroy(). */
176void barrier_destroy(struct barrier_info* const p)
sewardj85642922008-01-14 11:54:56 +0000177{
bart28230a32008-02-29 17:27:03 +0000178 tl_assert(p);
179 drd_clientobj_remove(p->a1, ClientBarrier);
sewardj85642922008-01-14 11:54:56 +0000180}
181
bart28230a32008-02-29 17:27:03 +0000182/** Called before pthread_barrier_wait(). */
sewardj85642922008-01-14 11:54:56 +0000183void barrier_pre_wait(const DrdThreadId tid, const Addr barrier)
184{
185 struct barrier_info* p;
186 struct barrier_thread_info* q;
187 const UWord word_tid = tid;
188
189 p = barrier_get(barrier);
190 tl_assert(p);
191
192 if (s_trace_barrier)
193 {
194 VG_(message)(Vg_DebugMsg,
bartbebc5d62008-02-24 18:42:53 +0000195 "[%d] barrier_pre_wait(%p) iteration %d",
196 tid, barrier, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000197 }
198
sewardj85642922008-01-14 11:54:56 +0000199 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
200 if (q == 0)
201 {
202 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
bartbebc5d62008-02-24 18:42:53 +0000203 barrier_thread_initialize(q, tid, p->pre_iteration);
sewardj85642922008-01-14 11:54:56 +0000204 VG_(OSetGen_Insert)(p->oset, q);
205 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
206 }
bartbebc5d62008-02-24 18:42:53 +0000207 vc_assign(&q->vc[p->pre_iteration], &thread_get_segment(tid)->vc);
208 tl_assert(q->vc[p->pre_iteration].size > 0);
209
210 if (--p->pre_waiters_left <= 0)
211 {
212 p->pre_iteration = 1 - p->pre_iteration;
213 p->pre_waiters_left = p->count;
214 }
sewardj85642922008-01-14 11:54:56 +0000215}
216
bart28230a32008-02-29 17:27:03 +0000217/** Called after pthread_barrier_wait(). */
sewardj85642922008-01-14 11:54:56 +0000218void barrier_post_wait(const DrdThreadId tid, const Addr barrier,
219 const Bool waited)
220{
221 struct barrier_info* const p = barrier_get(barrier);
222
bartbebc5d62008-02-24 18:42:53 +0000223 tl_assert(p);
224
sewardj85642922008-01-14 11:54:56 +0000225 if (s_trace_barrier)
226 {
227 VG_(message)(Vg_DebugMsg, "[%d] barrier_post_wait(%p) iteration %d",
bartbebc5d62008-02-24 18:42:53 +0000228 tid, barrier, p->post_iteration);
sewardj85642922008-01-14 11:54:56 +0000229 }
230
231 if (waited)
232 {
233 const UWord word_tid = tid;
234 struct barrier_thread_info* q;
235 struct barrier_thread_info* r;
236
sewardj85642922008-01-14 11:54:56 +0000237 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
238 tl_assert(q);
239 VG_(OSetGen_ResetIter)(p->oset);
240 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
241 {
242 if (r != q)
243 {
244 if (s_trace_barrier)
245 {
246 VG_(message)(Vg_DebugMsg,
bartbebc5d62008-02-24 18:42:53 +0000247 "[%d] barrier_post_wait: combining vc of thread %d, "
248 "iteration %d",
249 tid, r->tid, p->post_iteration);
250 vc_print(&thread_get_segment(tid)->vc);
251 VG_(printf)(", ");
252 vc_print(&r->vc[p->post_iteration]);
253 VG_(printf)(" -> ");
sewardj85642922008-01-14 11:54:56 +0000254 }
bartbebc5d62008-02-24 18:42:53 +0000255 thread_combine_vc2(tid, &r->vc[p->post_iteration]);
256 if (s_trace_barrier)
257 {
258 vc_print(&thread_get_segment(tid)->vc);
259 VG_(printf)("\n");
260 }
sewardj85642922008-01-14 11:54:56 +0000261 }
262 }
bartbebc5d62008-02-24 18:42:53 +0000263
264 thread_new_segment(tid);
265
266 if (--p->post_waiters_left <= 0)
267 {
268 p->post_iteration = 1 - p->post_iteration;
269 p->post_waiters_left = p->count;
270 }
sewardj85642922008-01-14 11:54:56 +0000271 }
272}
273
bartbebc5d62008-02-24 18:42:53 +0000274/** Call this function when thread tid stops to exist. */
sewardj85642922008-01-14 11:54:56 +0000275void barrier_thread_delete(const DrdThreadId tid)
276{
bart28230a32008-02-29 17:27:03 +0000277 struct barrier_info* p;
sewardj85642922008-01-14 11:54:56 +0000278
bart28230a32008-02-29 17:27:03 +0000279 drd_clientobj_resetiter();
280 for ( ; (p = &drd_clientobj_next(ClientBarrier)->barrier) != 0; )
sewardj85642922008-01-14 11:54:56 +0000281 {
bart28230a32008-02-29 17:27:03 +0000282 struct barrier_thread_info* q;
283 const UWord word_tid = tid;
284 q = VG_(OSetGen_Remove)(p->oset, &word_tid);
285 barrier_thread_destroy(q);
286 VG_(OSetGen_FreeNode)(p->oset, q);
sewardj85642922008-01-14 11:54:56 +0000287 }
288}