blob: 0323fa0c4daeb73ed4645622e5b2d684495ff4b7 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
4 Copyright (C) 2006-2007 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_cond.h"
27#include "drd_error.h"
28#include "drd_mutex.h"
29#include "drd_suppression.h"
30#include "pthread_object_size.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_threadstate.h" // VG_(get_running_tid)()
36#include "pub_core_options.h" // VG_(clo_backtrace_size)
37
38
39static struct cond_info s_cond[256];
40static Bool s_trace_cond;
41
42
43void cond_set_trace(const Bool trace_cond)
44{
45 s_trace_cond = trace_cond;
46}
47
48static
49void cond_initialize(struct cond_info* const p, const Addr cond)
50{
51 tl_assert(cond != 0);
52
53 p->cond = cond;
54 p->waiter_count = 0;
55 p->mutex = 0;
56}
57
58static struct cond_info* cond_get_or_allocate(const Addr cond)
59{
60 int i;
61 for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++)
62 if (s_cond[i].cond == cond)
63 return &s_cond[i];
64 for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++)
65 {
66 if (s_cond[i].cond == 0)
67 {
68 cond_initialize(&s_cond[i], cond);
69 /* TO DO: replace the constant below by a symbolic constant referring */
70 /* to sizeof(pthread_cond_t). */
71 drd_start_suppression(cond, cond + PTHREAD_COND_SIZE, "cond");
72 return &s_cond[i];
73 }
74 }
75 tl_assert(0);
76 return 0;
77}
78
79void cond_init(const Addr cond)
80{
81 if (s_trace_cond)
82 {
83 VG_(message)(Vg_UserMsg, "Initializing condition variable 0x%lx", cond);
84 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
85 VG_(clo_backtrace_size));
86 }
87 tl_assert(cond_get(cond) == 0);
88 cond_get_or_allocate(cond);
89}
90
91void cond_destroy(struct cond_info* const p)
92{
93 if (s_trace_cond)
94 {
95 VG_(message)(Vg_UserMsg, "Destroying condition variable 0x%lx", p->cond);
96 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
97 VG_(clo_backtrace_size));
98 }
99
100 // TO DO: print a proper error message if waiter_count != 0.
101 tl_assert(p->waiter_count == 0);
102
103 drd_finish_suppression(p->cond, p->cond + PTHREAD_COND_SIZE);
104
105 p->cond = 0;
106 p->waiter_count = 0;
107 p->mutex = 0;
108}
109
110struct cond_info* cond_get(const Addr cond)
111{
112 int i;
113 for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++)
114 if (s_cond[i].cond == cond)
115 return &s_cond[i];
116 return 0;
117}
118
119int cond_pre_wait(const Addr cond, const Addr mutex)
120{
121 struct cond_info* p;
122
123 p = cond_get_or_allocate(cond);
124 if (p->waiter_count == 0)
125 {
126 p->mutex = mutex;
127 }
128 else
129 {
130 // TO DO: print a proper error message if two different threads call
131 // pthread_cond_*wait() on the same condition variable but with a different
132 // mutex argument.
133 tl_assert(p->mutex == mutex);
134 }
135 return ++p->waiter_count;
136}
137
138int cond_post_wait(const Addr cond)
139{
140 struct cond_info* p;
141
142 p = cond_get(cond);
143 tl_assert(p);
144 tl_assert(p->waiter_count > 0);
145 tl_assert(p->mutex);
146 if (--p->waiter_count == 0)
147 {
148 p->mutex = 0;
149 }
150 return p->waiter_count;
151}
152
153void cond_pre_signal(Addr const cond)
154{
155 const ThreadId vg_tid = VG_(get_running_tid)();
156 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
157 struct cond_info* const cond_p = cond_get(cond);
158#if 0
159 VG_(message)(Vg_DebugMsg, "cond_pre_signal cond %d, w.c. %d, mutex %d",
160 cond,
161 cond_p ? cond_p->waiter_count : 0,
162 cond_p ? cond_p->mutex : 0);
163#endif
164 if (cond_p && cond_p->waiter_count > 0)
165 {
166 if (! mutex_is_locked_by(cond_p->mutex, drd_tid))
167 {
168 CondRaceErrInfo cei;
169 cei.cond = cond;
170 cei.mutex = cond_p->mutex;
171 VG_(maybe_record_error)(vg_tid,
172 CondRaceErr,
173 VG_(get_IP)(vg_tid),
174 "CondErr",
175 &cei);
176 }
177 }
178 else
179 {
180 /* No other thread is waiting for the signal, hence the signal will be */
181 /* lost. This is normal in a POSIX threads application. */
182 }
183}
184
185void cond_pre_broadcast(Addr const cond)
186{
187 cond_pre_signal(cond);
188}
189
190void cond_stop_using_mem(const Addr a1, const Addr a2)
191{
192 unsigned i;
193 for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++)
194 {
195 if (a1 <= s_cond[i].cond && s_cond[i].cond < a2)
196 {
197 tl_assert(s_cond[i].cond + PTHREAD_COND_SIZE <= a2);
198 cond_destroy(&s_cond[i]);
199 }
200 }
201}