blob: 4e2dbd02948ec40dece7607e3df206f6d138ecd0 [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
71 * drd_clientobj_remove().
72 */
73static void cond_cleanup(struct cond_info* p)
74{
75 tl_assert(p);
76 if (p->mutex)
77 {
78 struct mutex_info* q;
79 q = &drd_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);
99 p = &drd_clientobj_get(cond, ClientCondvar)->cond;
100 if (p == 0)
sewardj721ad7b2007-11-30 08:30:29 +0000101 {
bart28230a32008-02-29 17:27:03 +0000102 p = &drd_clientobj_add(cond, cond + size, ClientCondvar)->cond;
103 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
bart28230a32008-02-29 17:27:03 +0000108struct cond_info* cond_get(const Addr cond)
109{
110 tl_assert(offsetof(DrdClientobj, cond) == 0);
111 return &drd_clientobj_get(cond, ClientCondvar)->cond;
112}
113
114/** Called before pthread_cond_init(). */
sewardj721ad7b2007-11-30 08:30:29 +0000115void cond_init(const Addr cond, const SizeT size)
sewardjaf44c822007-11-25 14:01:38 +0000116{
117 if (s_trace_cond)
118 {
bart3b1ee452008-02-29 19:28:15 +0000119 VG_(message)(Vg_UserMsg,
120 "[%d/%d] cond_init 0x%lx",
121 VG_(get_running_tid)(),
122 thread_get_running_tid(),
123 cond);
sewardjaf44c822007-11-25 14:01:38 +0000124 }
125 tl_assert(cond_get(cond) == 0);
sewardj721ad7b2007-11-30 08:30:29 +0000126 tl_assert(size > 0);
127 cond_get_or_allocate(cond, size);
sewardjaf44c822007-11-25 14:01:38 +0000128}
129
bart28230a32008-02-29 17:27:03 +0000130/** Called after pthread_cond_destroy(). */
sewardjaf44c822007-11-25 14:01:38 +0000131void cond_destroy(struct cond_info* const p)
132{
133 if (s_trace_cond)
134 {
bart3b1ee452008-02-29 19:28:15 +0000135 VG_(message)(Vg_UserMsg,
136 "[%d/%d] cond_destroy 0x%lx",
137 VG_(get_running_tid)(),
138 thread_get_running_tid(),
139 p->a1);
sewardjaf44c822007-11-25 14:01:38 +0000140 }
141
142 // TO DO: print a proper error message if waiter_count != 0.
143 tl_assert(p->waiter_count == 0);
144
bart28230a32008-02-29 17:27:03 +0000145 drd_clientobj_remove(p->a1, ClientCondvar);
sewardjaf44c822007-11-25 14:01:38 +0000146}
147
bart28230a32008-02-29 17:27:03 +0000148/** Called before pthread_cond_wait(). */
sewardj721ad7b2007-11-30 08:30:29 +0000149int cond_pre_wait(const Addr cond, const SizeT cond_size, const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000150{
151 struct cond_info* p;
152
bart3b1ee452008-02-29 19:28:15 +0000153 if (s_trace_cond)
154 {
155 VG_(message)(Vg_UserMsg,
156 "[%d/%d] cond_pre_wait 0x%lx",
157 VG_(get_running_tid)(),
158 thread_get_running_tid(),
159 cond);
160 }
161
sewardj721ad7b2007-11-30 08:30:29 +0000162 p = cond_get_or_allocate(cond, cond_size);
bart3b1ee452008-02-29 19:28:15 +0000163 tl_assert(p);
164
sewardjaf44c822007-11-25 14:01:38 +0000165 if (p->waiter_count == 0)
166 {
167 p->mutex = mutex;
168 }
169 else
170 {
171 // TO DO: print a proper error message if two different threads call
172 // pthread_cond_*wait() on the same condition variable but with a different
173 // mutex argument.
174 tl_assert(p->mutex == mutex);
175 }
176 return ++p->waiter_count;
177}
178
bart28230a32008-02-29 17:27:03 +0000179/** Called after pthread_cond_wait(). */
sewardjaf44c822007-11-25 14:01:38 +0000180int cond_post_wait(const Addr cond)
181{
182 struct cond_info* p;
183
bart3b1ee452008-02-29 19:28:15 +0000184 if (s_trace_cond)
185 {
186 VG_(message)(Vg_UserMsg,
187 "[%d/%d] cond_post_wait 0x%lx",
188 VG_(get_running_tid)(),
189 thread_get_running_tid(),
190 cond);
191 }
192
sewardjaf44c822007-11-25 14:01:38 +0000193 p = cond_get(cond);
194 tl_assert(p);
195 tl_assert(p->waiter_count > 0);
196 tl_assert(p->mutex);
197 if (--p->waiter_count == 0)
198 {
199 p->mutex = 0;
200 }
201 return p->waiter_count;
202}
203
bart3b1ee452008-02-29 19:28:15 +0000204static void cond_signal(Addr const cond)
sewardjaf44c822007-11-25 14:01:38 +0000205{
206 const ThreadId vg_tid = VG_(get_running_tid)();
207 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
208 struct cond_info* const cond_p = cond_get(cond);
bart3b1ee452008-02-29 19:28:15 +0000209
sewardjaf44c822007-11-25 14:01:38 +0000210 if (cond_p && cond_p->waiter_count > 0)
211 {
212 if (! mutex_is_locked_by(cond_p->mutex, drd_tid))
213 {
214 CondRaceErrInfo cei;
215 cei.cond = cond;
216 cei.mutex = cond_p->mutex;
217 VG_(maybe_record_error)(vg_tid,
218 CondRaceErr,
219 VG_(get_IP)(vg_tid),
220 "CondErr",
221 &cei);
222 }
223 }
224 else
225 {
226 /* No other thread is waiting for the signal, hence the signal will be */
227 /* lost. This is normal in a POSIX threads application. */
228 }
229}
230
bart3b1ee452008-02-29 19:28:15 +0000231/** Called before pthread_cond_signal(). */
232void cond_pre_signal(Addr const cond)
233{
234 if (s_trace_cond)
235 {
236 VG_(message)(Vg_UserMsg,
237 "[%d/%d] cond_signal 0x%lx",
238 VG_(get_running_tid)(),
239 thread_get_running_tid(),
240 cond);
241 }
242
243 cond_signal(cond);
244}
245
bart28230a32008-02-29 17:27:03 +0000246/** Called before pthread_cond_broadcast(). */
sewardjaf44c822007-11-25 14:01:38 +0000247void cond_pre_broadcast(Addr const cond)
248{
bart3b1ee452008-02-29 19:28:15 +0000249 if (s_trace_cond)
250 {
251 VG_(message)(Vg_UserMsg,
252 "[%d/%d] cond_broadcast 0x%lx",
253 VG_(get_running_tid)(),
254 thread_get_running_tid(),
255 cond);
256 }
257
258 cond_signal(cond);
sewardjaf44c822007-11-25 14:01:38 +0000259}
260
bart28230a32008-02-29 17:27:03 +0000261/** Called after pthread_cond_destroy(). */
sewardj85642922008-01-14 11:54:56 +0000262void cond_thread_delete(const DrdThreadId tid)
263{ }