blob: 7cdd581ad8f5f8d9af0c2480e557176cc8bd7701 [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"
bart46b5fce2008-06-28 13:01:30 +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)() */
35#include "pub_tool_options.h" /* VG_(clo_backtrace_size) */
36#include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
sewardjaf44c822007-11-25 14:01:38 +000037
38
bart46b5fce2008-06-28 13:01:30 +000039/* Local functions. */
bart28230a32008-02-29 17:27:03 +000040
41static void cond_cleanup(struct cond_info* p);
42
43
bart46b5fce2008-06-28 13:01:30 +000044/* Global variables. */
45
46Bool s_drd_report_signal_unlocked = True;
47
48
49/* Local variables. */
bart28230a32008-02-29 17:27:03 +000050
sewardjaf44c822007-11-25 14:01:38 +000051static Bool s_trace_cond;
52
53
bart46b5fce2008-06-28 13:01:30 +000054/* Function definitions. */
bart28230a32008-02-29 17:27:03 +000055
sewardjaf44c822007-11-25 14:01:38 +000056void cond_set_trace(const Bool trace_cond)
57{
58 s_trace_cond = trace_cond;
59}
60
61static
bart0268dfa2008-03-11 20:10:21 +000062void cond_initialize(struct cond_info* const p, const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +000063{
64 tl_assert(cond != 0);
bart28230a32008-02-29 17:27:03 +000065 tl_assert(p->a1 == cond);
bart28230a32008-02-29 17:27:03 +000066 tl_assert(p->type == ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +000067
bart28230a32008-02-29 17:27:03 +000068 p->cleanup = (void(*)(DrdClientobj*))cond_cleanup;
sewardjaf44c822007-11-25 14:01:38 +000069 p->waiter_count = 0;
70 p->mutex = 0;
71}
72
bart28230a32008-02-29 17:27:03 +000073/** Free the memory that was allocated by cond_initialize(). Called by
bart72b751c2008-03-01 13:44:24 +000074 * clientobj_remove().
bart28230a32008-02-29 17:27:03 +000075 */
76static void cond_cleanup(struct cond_info* p)
77{
78 tl_assert(p);
79 if (p->mutex)
80 {
81 struct mutex_info* q;
bart72b751c2008-03-01 13:44:24 +000082 q = &clientobj_get(p->mutex, ClientMutex)->mutex;
bart3b1ee452008-02-29 19:28:15 +000083 tl_assert(q);
84 {
85 CondDestrErrInfo cde = { p->a1, q->a1, q->owner };
86 VG_(maybe_record_error)(VG_(get_running_tid)(),
87 CondDestrErr,
88 VG_(get_IP)(VG_(get_running_tid)()),
89 "Destroying condition variable that is being"
90 " waited upon",
91 &cde);
92 }
bart28230a32008-02-29 17:27:03 +000093 }
94}
95
bart0268dfa2008-03-11 20:10:21 +000096static struct cond_info* cond_get_or_allocate(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +000097{
bart28230a32008-02-29 17:27:03 +000098 struct cond_info *p;
99
100 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart72b751c2008-03-01 13:44:24 +0000101 p = &clientobj_get(cond, ClientCondvar)->cond;
bart28230a32008-02-29 17:27:03 +0000102 if (p == 0)
sewardj721ad7b2007-11-30 08:30:29 +0000103 {
bart0268dfa2008-03-11 20:10:21 +0000104 p = &clientobj_add(cond, ClientCondvar)->cond;
105 cond_initialize(p, cond);
sewardj721ad7b2007-11-30 08:30:29 +0000106 }
bart28230a32008-02-29 17:27:03 +0000107 return p;
sewardjaf44c822007-11-25 14:01:38 +0000108}
109
bart72b751c2008-03-01 13:44:24 +0000110static struct cond_info* cond_get(const Addr cond)
bart28230a32008-02-29 17:27:03 +0000111{
112 tl_assert(offsetof(DrdClientobj, cond) == 0);
bart72b751c2008-03-01 13:44:24 +0000113 return &clientobj_get(cond, ClientCondvar)->cond;
bart28230a32008-02-29 17:27:03 +0000114}
115
116/** Called before pthread_cond_init(). */
bart0268dfa2008-03-11 20:10:21 +0000117void cond_pre_init(const Addr cond)
sewardjaf44c822007-11-25 14:01:38 +0000118{
bart72b751c2008-03-01 13:44:24 +0000119 struct cond_info* p;
120
sewardjaf44c822007-11-25 14:01:38 +0000121 if (s_trace_cond)
122 {
bart3b1ee452008-02-29 19:28:15 +0000123 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000124 "[%d/%d] cond_init cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000125 VG_(get_running_tid)(),
126 thread_get_running_tid(),
127 cond);
sewardjaf44c822007-11-25 14:01:38 +0000128 }
bart72b751c2008-03-01 13:44:24 +0000129
bart72b751c2008-03-01 13:44:24 +0000130 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
bart0268dfa2008-03-11 20:10:21 +0000142 p = cond_get_or_allocate(cond);
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,
bart4a975e12008-03-30 13:28:33 +0000153 "[%d/%d] cond_destroy cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000154 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(). */
bart0268dfa2008-03-11 20:10:21 +0000186int cond_pre_wait(const Addr cond, 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,
bart4a975e12008-03-30 13:28:33 +0000193 "[%d/%d] cond_pre_wait cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000194 VG_(get_running_tid)(),
195 thread_get_running_tid(),
196 cond);
197 }
198
bart0268dfa2008-03-11 20:10:21 +0000199 p = cond_get_or_allocate(cond);
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 }
bart3bb1cec2008-06-28 16:01:43 +0000206 else if (p->mutex != mutex)
sewardjaf44c822007-11-25 14:01:38 +0000207 {
bart3bb1cec2008-06-28 16:01:43 +0000208 CondWaitErrInfo cwei
209 = { .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex };
210 VG_(maybe_record_error)(VG_(get_running_tid)(),
211 CondWaitErr,
212 VG_(get_IP)(VG_(get_running_tid)()),
213 "Inconsistent association of condition variable"
214 " and mutex",
215 &cwei);
sewardjaf44c822007-11-25 14:01:38 +0000216 }
217 return ++p->waiter_count;
218}
219
bart28230a32008-02-29 17:27:03 +0000220/** Called after pthread_cond_wait(). */
sewardjaf44c822007-11-25 14:01:38 +0000221int cond_post_wait(const Addr cond)
222{
223 struct cond_info* p;
224
bart3b1ee452008-02-29 19:28:15 +0000225 if (s_trace_cond)
226 {
227 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000228 "[%d/%d] cond_post_wait cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000229 VG_(get_running_tid)(),
230 thread_get_running_tid(),
231 cond);
232 }
233
sewardjaf44c822007-11-25 14:01:38 +0000234 p = cond_get(cond);
bart00344642008-03-01 15:27:41 +0000235 // To do: print a proper error message if the assert below fails.
sewardjaf44c822007-11-25 14:01:38 +0000236 tl_assert(p);
bart00344642008-03-01 15:27:41 +0000237 // To do: print a proper error message if the assert below fails.
sewardjaf44c822007-11-25 14:01:38 +0000238 tl_assert(p->waiter_count > 0);
239 tl_assert(p->mutex);
240 if (--p->waiter_count == 0)
241 {
242 p->mutex = 0;
243 }
244 return p->waiter_count;
245}
246
bart3b1ee452008-02-29 19:28:15 +0000247static void cond_signal(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000248{
249 const ThreadId vg_tid = VG_(get_running_tid)();
250 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
251 struct cond_info* const cond_p = cond_get(cond);
bart3b1ee452008-02-29 19:28:15 +0000252
sewardjaf44c822007-11-25 14:01:38 +0000253 if (cond_p && cond_p->waiter_count > 0)
254 {
bart46b5fce2008-06-28 13:01:30 +0000255 if (s_drd_report_signal_unlocked
256 && ! mutex_is_locked_by(cond_p->mutex, drd_tid))
sewardjaf44c822007-11-25 14:01:38 +0000257 {
bart11608002008-03-13 17:47:01 +0000258 /* A signal is sent while the associated mutex has not been locked. */
259 /* This can indicate but is not necessarily a race condition. */
sewardjaf44c822007-11-25 14:01:38 +0000260 CondRaceErrInfo cei;
261 cei.cond = cond;
262 cei.mutex = cond_p->mutex;
263 VG_(maybe_record_error)(vg_tid,
264 CondRaceErr,
265 VG_(get_IP)(vg_tid),
266 "CondErr",
267 &cei);
268 }
269 }
270 else
271 {
272 /* No other thread is waiting for the signal, hence the signal will be */
273 /* lost. This is normal in a POSIX threads application. */
274 }
275}
276
bart3b1ee452008-02-29 19:28:15 +0000277/** Called before pthread_cond_signal(). */
278void cond_pre_signal(Addr const cond)
279{
280 if (s_trace_cond)
281 {
282 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000283 "[%d/%d] cond_signal cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000284 VG_(get_running_tid)(),
285 thread_get_running_tid(),
286 cond);
287 }
288
289 cond_signal(cond);
290}
291
bart28230a32008-02-29 17:27:03 +0000292/** Called before pthread_cond_broadcast(). */
sewardjaf44c822007-11-25 14:01:38 +0000293void cond_pre_broadcast(Addr const cond)
294{
bart3b1ee452008-02-29 19:28:15 +0000295 if (s_trace_cond)
296 {
297 VG_(message)(Vg_UserMsg,
bart4a975e12008-03-30 13:28:33 +0000298 "[%d/%d] cond_broadcast cond 0x%lx",
bart3b1ee452008-02-29 19:28:15 +0000299 VG_(get_running_tid)(),
300 thread_get_running_tid(),
301 cond);
302 }
303
304 cond_signal(cond);
sewardjaf44c822007-11-25 14:01:38 +0000305}
306
bart28230a32008-02-29 17:27:03 +0000307/** Called after pthread_cond_destroy(). */
sewardj85642922008-01-14 11:54:56 +0000308void cond_thread_delete(const DrdThreadId tid)
309{ }