blob: 3a1228f923eb120e982288b5c0bf6aa1398bf154 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 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
bart28230a32008-02-29 17:27:03 +000026#include "drd_clientobj.h"
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_cond.h"
28#include "drd_error.h"
29#include "drd_mutex.h"
30#include "drd_suppression.h"
sewardjaf44c822007-11-25 14:01:38 +000031#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)()
sewardj85642922008-01-14 11:54:56 +000035#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000036#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
sewardjaf44c822007-11-25 14:01:38 +000037
38
bart28230a32008-02-29 17:27:03 +000039// Local functions.
40
41static void cond_cleanup(struct cond_info* p);
42
43
44// Local variables.
45
sewardjaf44c822007-11-25 14:01:38 +000046static Bool s_trace_cond;
47
48
bart28230a32008-02-29 17:27:03 +000049// Function definitions.
50
sewardjaf44c822007-11-25 14:01:38 +000051void cond_set_trace(const Bool trace_cond)
52{
53 s_trace_cond = trace_cond;
54}
55
56static
sewardj721ad7b2007-11-30 08:30:29 +000057void cond_initialize(struct cond_info* const p, const Addr cond,
58 const SizeT size)
sewardjaf44c822007-11-25 14:01:38 +000059{
60 tl_assert(cond != 0);
bart28230a32008-02-29 17:27:03 +000061 tl_assert(p->a1 == cond);
62 tl_assert(p->a2 - p->a1 == size);
63 tl_assert(p->type == ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +000064
bart28230a32008-02-29 17:27:03 +000065 p->cleanup = (void(*)(DrdClientobj*))cond_cleanup;
sewardjaf44c822007-11-25 14:01:38 +000066 p->waiter_count = 0;
67 p->mutex = 0;
68}
69
bart28230a32008-02-29 17:27:03 +000070/** Free the memory that was allocated by cond_initialize(). Called by
bart72b751c2008-03-01 13:44:24 +000071 * clientobj_remove().
bart28230a32008-02-29 17:27:03 +000072 */
73static void cond_cleanup(struct cond_info* p)
74{
75 tl_assert(p);
76 if (p->mutex)
77 {
78 struct mutex_info* q;
bart72b751c2008-03-01 13:44:24 +000079 q = &clientobj_get(p->mutex, ClientMutex)->mutex;
bart3b1ee452008-02-29 19:28:15 +000080 tl_assert(q);
81 {
82 CondDestrErrInfo cde = { p->a1, q->a1, q->owner };
83 VG_(maybe_record_error)(VG_(get_running_tid)(),
84 CondDestrErr,
85 VG_(get_IP)(VG_(get_running_tid)()),
86 "Destroying condition variable that is being"
87 " waited upon",
88 &cde);
89 }
bart28230a32008-02-29 17:27:03 +000090 }
91}
92
sewardj721ad7b2007-11-30 08:30:29 +000093static struct cond_info*
94cond_get_or_allocate(const Addr cond, const SizeT size)
sewardjaf44c822007-11-25 14:01:38 +000095{
bart28230a32008-02-29 17:27:03 +000096 struct cond_info *p;
97
98 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart72b751c2008-03-01 13:44:24 +000099 p = &clientobj_get(cond, ClientCondvar)->cond;
bart28230a32008-02-29 17:27:03 +0000100 if (p == 0)
sewardj721ad7b2007-11-30 08:30:29 +0000101 {
bart72b751c2008-03-01 13:44:24 +0000102 p = &clientobj_add(cond, cond + size, ClientCondvar)->cond;
bart28230a32008-02-29 17:27:03 +0000103 cond_initialize(p, cond, size);
sewardj721ad7b2007-11-30 08:30:29 +0000104 }
bart28230a32008-02-29 17:27:03 +0000105 return p;
sewardjaf44c822007-11-25 14:01:38 +0000106}
107
bart72b751c2008-03-01 13:44:24 +0000108static struct cond_info* cond_get(const Addr cond)
bart28230a32008-02-29 17:27:03 +0000109{
110 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart72b751c2008-03-01 13:44:24 +0000111 return &clientobj_get(cond, ClientCondvar)->cond;
bart28230a32008-02-29 17:27:03 +0000112}
113
114/** Called before pthread_cond_init(). */
bart72b751c2008-03-01 13:44:24 +0000115void cond_pre_init(const Addr cond, const SizeT size)
sewardjaf44c822007-11-25 14:01:38 +0000116{
bart72b751c2008-03-01 13:44:24 +0000117 struct cond_info* p;
118
sewardjaf44c822007-11-25 14:01:38 +0000119 if (s_trace_cond)
120 {
bart3b1ee452008-02-29 19:28:15 +0000121 VG_(message)(Vg_UserMsg,
122 "[%d/%d] cond_init 0x%lx",
123 VG_(get_running_tid)(),
124 thread_get_running_tid(),
125 cond);
sewardjaf44c822007-11-25 14:01:38 +0000126 }
bart72b751c2008-03-01 13:44:24 +0000127
sewardj721ad7b2007-11-30 08:30:29 +0000128 tl_assert(size > 0);
bart72b751c2008-03-01 13:44:24 +0000129
130 p = cond_get(cond);
131
132 if (p)
133 {
134 CondErrInfo cei = { .cond = cond };
135 VG_(maybe_record_error)(VG_(get_running_tid)(),
136 CondErr,
137 VG_(get_IP)(VG_(get_running_tid)()),
138 "initialized twice",
139 &cei);
140 }
141
142 p = cond_get_or_allocate(cond, size);
sewardjaf44c822007-11-25 14:01:38 +0000143}
144
bart28230a32008-02-29 17:27:03 +0000145/** Called after pthread_cond_destroy(). */
bart72b751c2008-03-01 13:44:24 +0000146void cond_post_destroy(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000147{
bart72b751c2008-03-01 13:44:24 +0000148 struct cond_info* p;
149
sewardjaf44c822007-11-25 14:01:38 +0000150 if (s_trace_cond)
151 {
bart3b1ee452008-02-29 19:28:15 +0000152 VG_(message)(Vg_UserMsg,
153 "[%d/%d] cond_destroy 0x%lx",
154 VG_(get_running_tid)(),
155 thread_get_running_tid(),
bart72b751c2008-03-01 13:44:24 +0000156 cond);
sewardjaf44c822007-11-25 14:01:38 +0000157 }
158
bart72b751c2008-03-01 13:44:24 +0000159 p = cond_get(cond);
160 if (p == 0)
161 {
162 CondErrInfo cei = { .cond = cond };
163 VG_(maybe_record_error)(VG_(get_running_tid)(),
164 CondErr,
165 VG_(get_IP)(VG_(get_running_tid)()),
166 "not a condition variable",
167 &cei);
168 return;
169 }
sewardjaf44c822007-11-25 14:01:38 +0000170
bart72b751c2008-03-01 13:44:24 +0000171 if (p->waiter_count != 0)
172 {
173 CondErrInfo cei = { .cond = cond };
174 VG_(maybe_record_error)(VG_(get_running_tid)(),
175 CondErr,
176 VG_(get_IP)(VG_(get_running_tid)()),
177 "destruction of condition variable being waited"
178 " upon",
179 &cei);
180 }
181
182 clientobj_remove(p->a1, ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +0000183}
184
bart28230a32008-02-29 17:27:03 +0000185/** Called before pthread_cond_wait(). */
sewardj721ad7b2007-11-30 08:30:29 +0000186int cond_pre_wait(const Addr cond, const SizeT cond_size, const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000187{
188 struct cond_info* p;
189
bart3b1ee452008-02-29 19:28:15 +0000190 if (s_trace_cond)
191 {
192 VG_(message)(Vg_UserMsg,
193 "[%d/%d] cond_pre_wait 0x%lx",
194 VG_(get_running_tid)(),
195 thread_get_running_tid(),
196 cond);
197 }
198
sewardj721ad7b2007-11-30 08:30:29 +0000199 p = cond_get_or_allocate(cond, cond_size);
bart3b1ee452008-02-29 19:28:15 +0000200 tl_assert(p);
201
sewardjaf44c822007-11-25 14:01:38 +0000202 if (p->waiter_count == 0)
203 {
204 p->mutex = mutex;
205 }
206 else
207 {
208 // TO DO: print a proper error message if two different threads call
209 // pthread_cond_*wait() on the same condition variable but with a different
210 // mutex argument.
211 tl_assert(p->mutex == mutex);
212 }
213 return ++p->waiter_count;
214}
215
bart28230a32008-02-29 17:27:03 +0000216/** Called after pthread_cond_wait(). */
sewardjaf44c822007-11-25 14:01:38 +0000217int cond_post_wait(const Addr cond)
218{
219 struct cond_info* p;
220
bart3b1ee452008-02-29 19:28:15 +0000221 if (s_trace_cond)
222 {
223 VG_(message)(Vg_UserMsg,
224 "[%d/%d] cond_post_wait 0x%lx",
225 VG_(get_running_tid)(),
226 thread_get_running_tid(),
227 cond);
228 }
229
sewardjaf44c822007-11-25 14:01:38 +0000230 p = cond_get(cond);
231 tl_assert(p);
232 tl_assert(p->waiter_count > 0);
233 tl_assert(p->mutex);
234 if (--p->waiter_count == 0)
235 {
236 p->mutex = 0;
237 }
238 return p->waiter_count;
239}
240
bart3b1ee452008-02-29 19:28:15 +0000241static void cond_signal(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000242{
243 const ThreadId vg_tid = VG_(get_running_tid)();
244 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
245 struct cond_info* const cond_p = cond_get(cond);
bart3b1ee452008-02-29 19:28:15 +0000246
sewardjaf44c822007-11-25 14:01:38 +0000247 if (cond_p && cond_p->waiter_count > 0)
248 {
249 if (! mutex_is_locked_by(cond_p->mutex, drd_tid))
250 {
251 CondRaceErrInfo cei;
252 cei.cond = cond;
253 cei.mutex = cond_p->mutex;
254 VG_(maybe_record_error)(vg_tid,
255 CondRaceErr,
256 VG_(get_IP)(vg_tid),
257 "CondErr",
258 &cei);
259 }
260 }
261 else
262 {
263 /* No other thread is waiting for the signal, hence the signal will be */
264 /* lost. This is normal in a POSIX threads application. */
265 }
266}
267
bart3b1ee452008-02-29 19:28:15 +0000268/** Called before pthread_cond_signal(). */
269void cond_pre_signal(Addr const cond)
270{
271 if (s_trace_cond)
272 {
273 VG_(message)(Vg_UserMsg,
274 "[%d/%d] cond_signal 0x%lx",
275 VG_(get_running_tid)(),
276 thread_get_running_tid(),
277 cond);
278 }
279
280 cond_signal(cond);
281}
282
bart28230a32008-02-29 17:27:03 +0000283/** Called before pthread_cond_broadcast(). */
sewardjaf44c822007-11-25 14:01:38 +0000284void cond_pre_broadcast(Addr const cond)
285{
bart3b1ee452008-02-29 19:28:15 +0000286 if (s_trace_cond)
287 {
288 VG_(message)(Vg_UserMsg,
289 "[%d/%d] cond_broadcast 0x%lx",
290 VG_(get_running_tid)(),
291 thread_get_running_tid(),
292 cond);
293 }
294
295 cond_signal(cond);
sewardjaf44c822007-11-25 14:01:38 +0000296}
297
bart28230a32008-02-29 17:27:03 +0000298/** Called after pthread_cond_destroy(). */
sewardj85642922008-01-14 11:54:56 +0000299void cond_thread_delete(const DrdThreadId tid)
300{ }