Fix some spelling errors found by Lintian. Patch from Alessandro Ghedini <ghedo...
[valgrind.git] / drd / drd_hb.c
bloba681a509dca3d88ab803ecbc843f744672b2dec3
1 /*
2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2017 Bart Van Assche <bvanassche@acm.org>.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
19 The GNU General Public License is contained in the file COPYING.
23 #include "drd_clientobj.h"
24 #include "drd_hb.h"
25 #include "drd_error.h"
26 #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
27 #include "pub_tool_libcassert.h" /* tl_assert() */
28 #include "pub_tool_libcprint.h" /* VG_(printf)() */
29 #include "pub_tool_machine.h" /* VG_(get_IP)() */
30 #include "pub_tool_mallocfree.h" /* VG_(malloc)(), VG_(free)()*/
31 #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
34 /* Type definitions. */
36 /** Per-thread hb information. */
37 struct hb_thread_info
39 UWord tid; // A DrdThreadId declared as UWord because
40 // this member variable is the key of an OSet.
41 Segment* sg; // Segment created before most recent
42 // ANNOTATE_HAPPENS_BEFORE().
46 /* Local functions. */
48 static void DRD_(hb_cleanup)(struct hb_info* p);
51 /* Local variables. */
53 static Bool DRD_(s_trace_hb);
56 /* Function definitions. */
58 void DRD_(hb_set_trace)(const Bool trace_hb)
60 DRD_(s_trace_hb) = trace_hb;
63 /**
64 * Initialize the structure *p with the specified thread ID.
66 static
67 void DRD_(hb_thread_initialize)(struct hb_thread_info* const p,
68 const DrdThreadId tid)
70 p->tid = tid;
71 p->sg = 0;
74 /**
75 * Deallocate the memory that is owned by members of struct hb_thread_info.
77 static void DRD_(hb_thread_destroy)(struct hb_thread_info* const p)
79 tl_assert(p);
80 DRD_(sg_put)(p->sg);
83 static
84 void DRD_(hb_initialize)(struct hb_info* const p, const Addr hb)
86 tl_assert(hb != 0);
87 tl_assert(p->a1 == hb);
88 tl_assert(p->type == ClientHbvar);
90 p->cleanup = (void(*)(DrdClientobj*))(DRD_(hb_cleanup));
91 p->delete_thread = 0;
92 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.hb",
93 VG_(free));
96 /**
97 * Free the memory that was allocated by hb_initialize(). Called by
98 * DRD_(clientobj_remove)().
100 static void DRD_(hb_cleanup)(struct hb_info* p)
102 struct hb_thread_info* r;
104 tl_assert(p);
105 VG_(OSetGen_ResetIter)(p->oset);
106 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
107 DRD_(hb_thread_destroy)(r);
108 VG_(OSetGen_Destroy)(p->oset);
112 * Report that the synchronization object at address 'addr' is of the
113 * wrong type.
115 static void wrong_type(const Addr addr)
117 GenericErrInfo gei = {
118 .tid = DRD_(thread_get_running_tid)(),
119 .addr = addr,
121 VG_(maybe_record_error)(VG_(get_running_tid)(),
122 GenericErr,
123 VG_(get_IP)(VG_(get_running_tid)()),
124 "wrong type of synchronization object",
125 &gei);
128 struct hb_info* DRD_(hb_get_or_allocate)(const Addr hb)
130 struct hb_info *p;
132 tl_assert(offsetof(DrdClientobj, hb) == 0);
133 p = &(DRD_(clientobj_get)(hb, ClientHbvar)->hb);
134 if (p)
135 return p;
137 if (DRD_(clientobj_present)(hb, hb + 1))
139 wrong_type(hb);
140 return 0;
143 p = &(DRD_(clientobj_add)(hb, ClientHbvar)->hb);
144 DRD_(hb_initialize)(p, hb);
145 return p;
148 struct hb_info* DRD_(hb_get)(const Addr hb)
150 tl_assert(offsetof(DrdClientobj, hb) == 0);
151 return &(DRD_(clientobj_get)(hb, ClientHbvar)->hb);
154 /** Called because of a happens-before annotation. */
155 void DRD_(hb_happens_before)(const DrdThreadId tid, Addr const hb)
157 const ThreadId vg_tid = VG_(get_running_tid)();
158 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
159 const UWord word_tid = tid;
160 struct hb_info* p;
161 struct hb_thread_info* q;
163 p = DRD_(hb_get_or_allocate)(hb);
164 if (DRD_(s_trace_hb))
165 DRD_(trace_msg)("[%u] happens_before 0x%lx",
166 DRD_(thread_get_running_tid)(), hb);
168 if (!p)
169 return;
171 /* Allocate the per-thread data structure if necessary. */
172 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
173 if (!q)
175 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
176 DRD_(hb_thread_initialize)(q, tid);
177 VG_(OSetGen_Insert)(p->oset, q);
178 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
182 * Store a pointer to the latest segment of the current thread in the
183 * per-thread data structure.
185 DRD_(thread_get_latest_segment)(&q->sg, tid);
186 DRD_(thread_new_segment)(drd_tid);
189 /** Called because of a happens-after annotation. */
190 void DRD_(hb_happens_after)(const DrdThreadId tid, const Addr hb)
192 struct hb_info* p;
193 struct hb_thread_info* q;
194 VectorClock old_vc;
196 p = DRD_(hb_get_or_allocate)(hb);
198 if (DRD_(s_trace_hb))
199 DRD_(trace_msg)("[%u] happens_after 0x%lx",
200 DRD_(thread_get_running_tid)(), hb);
202 if (!p)
203 return;
205 DRD_(thread_new_segment)(tid);
208 * Combine all vector clocks that were stored because of happens-before
209 * annotations with the vector clock of the current thread.
211 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(tid));
212 VG_(OSetGen_ResetIter)(p->oset);
213 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
215 if (q->tid != tid)
217 tl_assert(q->sg);
218 DRD_(vc_combine)(DRD_(thread_get_vc)(tid), &q->sg->vc);
221 DRD_(thread_update_conflict_set)(tid, &old_vc);
222 DRD_(vc_cleanup)(&old_vc);
225 /** Called because of a happens-done annotation. */
226 void DRD_(hb_happens_done)(const DrdThreadId tid, const Addr hb)
228 struct hb_info* p;
230 if (DRD_(s_trace_hb))
231 DRD_(trace_msg)("[%u] happens_done 0x%lx",
232 DRD_(thread_get_running_tid)(), hb);
234 p = DRD_(hb_get)(hb);
235 if (!p)
237 GenericErrInfo gei = {
238 .tid = DRD_(thread_get_running_tid)(),
239 .addr = hb,
241 VG_(maybe_record_error)(VG_(get_running_tid)(),
242 GenericErr,
243 VG_(get_IP)(VG_(get_running_tid)()),
244 "missing happens-before annotation",
245 &gei);
246 return;
249 DRD_(clientobj_remove)(p->a1, ClientHbvar);