blob: 83db465b63be98dc58e845808988d38beb3dff97 [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"
sewardjaf44c822007-11-25 14:01:38 +000030#include "pub_tool_errormgr.h" // VG_(maybe_record_error)()
31#include "pub_tool_libcassert.h" // tl_assert()
32#include "pub_tool_libcprint.h" // VG_(printf)()
33#include "pub_tool_machine.h" // VG_(get_IP)()
34#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
35#include "pub_core_options.h" // VG_(clo_backtrace_size)
36
37
38static struct cond_info s_cond[256];
39static Bool s_trace_cond;
40
41
42void cond_set_trace(const Bool trace_cond)
43{
44 s_trace_cond = trace_cond;
45}
46
47static
sewardj721ad7b2007-11-30 08:30:29 +000048void cond_initialize(struct cond_info* const p, const Addr cond,
49 const SizeT size)
sewardjaf44c822007-11-25 14:01:38 +000050{
51 tl_assert(cond != 0);
52
53 p->cond = cond;
sewardj721ad7b2007-11-30 08:30:29 +000054 p->size = size;
sewardjaf44c822007-11-25 14:01:38 +000055 p->waiter_count = 0;
56 p->mutex = 0;
57}
58
sewardj721ad7b2007-11-30 08:30:29 +000059static struct cond_info*
60cond_get_or_allocate(const Addr cond, const SizeT size)
sewardjaf44c822007-11-25 14:01:38 +000061{
62 int i;
63 for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++)
sewardj721ad7b2007-11-30 08:30:29 +000064 {
sewardjaf44c822007-11-25 14:01:38 +000065 if (s_cond[i].cond == cond)
sewardj721ad7b2007-11-30 08:30:29 +000066 {
67 tl_assert(s_cond[i].size == size);
sewardjaf44c822007-11-25 14:01:38 +000068 return &s_cond[i];
sewardj721ad7b2007-11-30 08:30:29 +000069 }
70 }
sewardjaf44c822007-11-25 14:01:38 +000071 for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++)
72 {
73 if (s_cond[i].cond == 0)
74 {
sewardj721ad7b2007-11-30 08:30:29 +000075 cond_initialize(&s_cond[i], cond, size);
sewardjaf44c822007-11-25 14:01:38 +000076 /* TO DO: replace the constant below by a symbolic constant referring */
77 /* to sizeof(pthread_cond_t). */
sewardj721ad7b2007-11-30 08:30:29 +000078 drd_start_suppression(cond, cond + size, "cond");
sewardjaf44c822007-11-25 14:01:38 +000079 return &s_cond[i];
80 }
81 }
82 tl_assert(0);
83 return 0;
84}
85
sewardj721ad7b2007-11-30 08:30:29 +000086void cond_init(const Addr cond, const SizeT size)
sewardjaf44c822007-11-25 14:01:38 +000087{
88 if (s_trace_cond)
89 {
90 VG_(message)(Vg_UserMsg, "Initializing condition variable 0x%lx", cond);
91 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
92 VG_(clo_backtrace_size));
93 }
94 tl_assert(cond_get(cond) == 0);
sewardj721ad7b2007-11-30 08:30:29 +000095 tl_assert(size > 0);
96 cond_get_or_allocate(cond, size);
sewardjaf44c822007-11-25 14:01:38 +000097}
98
99void cond_destroy(struct cond_info* const p)
100{
101 if (s_trace_cond)
102 {
103 VG_(message)(Vg_UserMsg, "Destroying condition variable 0x%lx", p->cond);
104 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
105 VG_(clo_backtrace_size));
106 }
107
108 // TO DO: print a proper error message if waiter_count != 0.
109 tl_assert(p->waiter_count == 0);
110
sewardj721ad7b2007-11-30 08:30:29 +0000111 drd_finish_suppression(p->cond, p->cond + p->size);
sewardjaf44c822007-11-25 14:01:38 +0000112
113 p->cond = 0;
114 p->waiter_count = 0;
115 p->mutex = 0;
116}
117
118struct cond_info* cond_get(const Addr cond)
119{
120 int i;
121 for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++)
122 if (s_cond[i].cond == cond)
123 return &s_cond[i];
124 return 0;
125}
126
sewardj721ad7b2007-11-30 08:30:29 +0000127int cond_pre_wait(const Addr cond, const SizeT cond_size, const Addr mutex)
sewardjaf44c822007-11-25 14:01:38 +0000128{
129 struct cond_info* p;
130
sewardj721ad7b2007-11-30 08:30:29 +0000131 p = cond_get_or_allocate(cond, cond_size);
sewardjaf44c822007-11-25 14:01:38 +0000132 if (p->waiter_count == 0)
133 {
134 p->mutex = mutex;
135 }
136 else
137 {
138 // TO DO: print a proper error message if two different threads call
139 // pthread_cond_*wait() on the same condition variable but with a different
140 // mutex argument.
141 tl_assert(p->mutex == mutex);
142 }
143 return ++p->waiter_count;
144}
145
146int cond_post_wait(const Addr cond)
147{
148 struct cond_info* p;
149
150 p = cond_get(cond);
151 tl_assert(p);
152 tl_assert(p->waiter_count > 0);
153 tl_assert(p->mutex);
154 if (--p->waiter_count == 0)
155 {
156 p->mutex = 0;
157 }
158 return p->waiter_count;
159}
160
161void cond_pre_signal(Addr const cond)
162{
163 const ThreadId vg_tid = VG_(get_running_tid)();
164 const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid);
165 struct cond_info* const cond_p = cond_get(cond);
166#if 0
167 VG_(message)(Vg_DebugMsg, "cond_pre_signal cond %d, w.c. %d, mutex %d",
168 cond,
169 cond_p ? cond_p->waiter_count : 0,
170 cond_p ? cond_p->mutex : 0);
171#endif
172 if (cond_p && cond_p->waiter_count > 0)
173 {
174 if (! mutex_is_locked_by(cond_p->mutex, drd_tid))
175 {
176 CondRaceErrInfo cei;
177 cei.cond = cond;
178 cei.mutex = cond_p->mutex;
179 VG_(maybe_record_error)(vg_tid,
180 CondRaceErr,
181 VG_(get_IP)(vg_tid),
182 "CondErr",
183 &cei);
184 }
185 }
186 else
187 {
188 /* No other thread is waiting for the signal, hence the signal will be */
189 /* lost. This is normal in a POSIX threads application. */
190 }
191}
192
193void cond_pre_broadcast(Addr const cond)
194{
195 cond_pre_signal(cond);
196}
197
198void cond_stop_using_mem(const Addr a1, const Addr a2)
199{
200 unsigned i;
201 for (i = 0; i < sizeof(s_cond)/sizeof(s_cond[0]); i++)
202 {
203 if (a1 <= s_cond[i].cond && s_cond[i].cond < a2)
204 {
sewardj721ad7b2007-11-30 08:30:29 +0000205 tl_assert(s_cond[i].cond + s_cond[i].size <= a2);
sewardjaf44c822007-11-25 14:01:38 +0000206 cond_destroy(&s_cond[i]);
207 }
208 }
209}