sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 26 | #include "drd_clientobj.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 27 | #include "drd_cond.h" |
| 28 | #include "drd_error.h" |
| 29 | #include "drd_mutex.h" |
| 30 | #include "drd_suppression.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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)() |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 35 | #include "pub_tool_options.h" // VG_(clo_backtrace_size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 36 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 37 | |
| 38 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 39 | // Local functions. |
| 40 | |
| 41 | static void cond_cleanup(struct cond_info* p); |
| 42 | |
| 43 | |
| 44 | // Local variables. |
| 45 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 46 | static Bool s_trace_cond; |
| 47 | |
| 48 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 49 | // Function definitions. |
| 50 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 51 | void cond_set_trace(const Bool trace_cond) |
| 52 | { |
| 53 | s_trace_cond = trace_cond; |
| 54 | } |
| 55 | |
| 56 | static |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame^] | 57 | void cond_initialize(struct cond_info* const p, const Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 58 | { |
| 59 | tl_assert(cond != 0); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 60 | tl_assert(p->a1 == cond); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 61 | tl_assert(p->type == ClientCondvar); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 62 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 63 | p->cleanup = (void(*)(DrdClientobj*))cond_cleanup; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 64 | p->waiter_count = 0; |
| 65 | p->mutex = 0; |
| 66 | } |
| 67 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 68 | /** Free the memory that was allocated by cond_initialize(). Called by |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 69 | * clientobj_remove(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 70 | */ |
| 71 | static void cond_cleanup(struct cond_info* p) |
| 72 | { |
| 73 | tl_assert(p); |
| 74 | if (p->mutex) |
| 75 | { |
| 76 | struct mutex_info* q; |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 77 | q = &clientobj_get(p->mutex, ClientMutex)->mutex; |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 78 | tl_assert(q); |
| 79 | { |
| 80 | CondDestrErrInfo cde = { p->a1, q->a1, q->owner }; |
| 81 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 82 | CondDestrErr, |
| 83 | VG_(get_IP)(VG_(get_running_tid)()), |
| 84 | "Destroying condition variable that is being" |
| 85 | " waited upon", |
| 86 | &cde); |
| 87 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame^] | 91 | static struct cond_info* cond_get_or_allocate(const Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 92 | { |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 93 | struct cond_info *p; |
| 94 | |
| 95 | tl_assert(offsetof(DrdClientobj, cond) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 96 | p = &clientobj_get(cond, ClientCondvar)->cond; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 97 | if (p == 0) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 98 | { |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame^] | 99 | p = &clientobj_add(cond, ClientCondvar)->cond; |
| 100 | cond_initialize(p, cond); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 101 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 102 | return p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 103 | } |
| 104 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 105 | static struct cond_info* cond_get(const Addr cond) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 106 | { |
| 107 | tl_assert(offsetof(DrdClientobj, cond) == 0); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 108 | return &clientobj_get(cond, ClientCondvar)->cond; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** Called before pthread_cond_init(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame^] | 112 | void cond_pre_init(const Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 113 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 114 | struct cond_info* p; |
| 115 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 116 | if (s_trace_cond) |
| 117 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 118 | VG_(message)(Vg_UserMsg, |
| 119 | "[%d/%d] cond_init 0x%lx", |
| 120 | VG_(get_running_tid)(), |
| 121 | thread_get_running_tid(), |
| 122 | cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 123 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 124 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 125 | p = cond_get(cond); |
| 126 | |
| 127 | if (p) |
| 128 | { |
| 129 | CondErrInfo cei = { .cond = cond }; |
| 130 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 131 | CondErr, |
| 132 | VG_(get_IP)(VG_(get_running_tid)()), |
| 133 | "initialized twice", |
| 134 | &cei); |
| 135 | } |
| 136 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame^] | 137 | p = cond_get_or_allocate(cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 138 | } |
| 139 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 140 | /** Called after pthread_cond_destroy(). */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 141 | void cond_post_destroy(const Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 142 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 143 | struct cond_info* p; |
| 144 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 145 | if (s_trace_cond) |
| 146 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 147 | VG_(message)(Vg_UserMsg, |
| 148 | "[%d/%d] cond_destroy 0x%lx", |
| 149 | VG_(get_running_tid)(), |
| 150 | thread_get_running_tid(), |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 151 | cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 152 | } |
| 153 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 154 | p = cond_get(cond); |
| 155 | if (p == 0) |
| 156 | { |
| 157 | CondErrInfo cei = { .cond = cond }; |
| 158 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 159 | CondErr, |
| 160 | VG_(get_IP)(VG_(get_running_tid)()), |
| 161 | "not a condition variable", |
| 162 | &cei); |
| 163 | return; |
| 164 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 165 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 166 | if (p->waiter_count != 0) |
| 167 | { |
| 168 | CondErrInfo cei = { .cond = cond }; |
| 169 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 170 | CondErr, |
| 171 | VG_(get_IP)(VG_(get_running_tid)()), |
| 172 | "destruction of condition variable being waited" |
| 173 | " upon", |
| 174 | &cei); |
| 175 | } |
| 176 | |
| 177 | clientobj_remove(p->a1, ClientCondvar); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 178 | } |
| 179 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 180 | /** Called before pthread_cond_wait(). */ |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame^] | 181 | int cond_pre_wait(const Addr cond, const Addr mutex) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 182 | { |
| 183 | struct cond_info* p; |
| 184 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 185 | if (s_trace_cond) |
| 186 | { |
| 187 | VG_(message)(Vg_UserMsg, |
| 188 | "[%d/%d] cond_pre_wait 0x%lx", |
| 189 | VG_(get_running_tid)(), |
| 190 | thread_get_running_tid(), |
| 191 | cond); |
| 192 | } |
| 193 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame^] | 194 | p = cond_get_or_allocate(cond); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 195 | tl_assert(p); |
| 196 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 197 | if (p->waiter_count == 0) |
| 198 | { |
| 199 | p->mutex = mutex; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | // TO DO: print a proper error message if two different threads call |
| 204 | // pthread_cond_*wait() on the same condition variable but with a different |
| 205 | // mutex argument. |
| 206 | tl_assert(p->mutex == mutex); |
| 207 | } |
| 208 | return ++p->waiter_count; |
| 209 | } |
| 210 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 211 | /** Called after pthread_cond_wait(). */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 212 | int cond_post_wait(const Addr cond) |
| 213 | { |
| 214 | struct cond_info* p; |
| 215 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 216 | if (s_trace_cond) |
| 217 | { |
| 218 | VG_(message)(Vg_UserMsg, |
| 219 | "[%d/%d] cond_post_wait 0x%lx", |
| 220 | VG_(get_running_tid)(), |
| 221 | thread_get_running_tid(), |
| 222 | cond); |
| 223 | } |
| 224 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 225 | p = cond_get(cond); |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 226 | // To do: print a proper error message if the assert below fails. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 227 | tl_assert(p); |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 228 | // To do: print a proper error message if the assert below fails. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 229 | tl_assert(p->waiter_count > 0); |
| 230 | tl_assert(p->mutex); |
| 231 | if (--p->waiter_count == 0) |
| 232 | { |
| 233 | p->mutex = 0; |
| 234 | } |
| 235 | return p->waiter_count; |
| 236 | } |
| 237 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 238 | static void cond_signal(Addr const cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 239 | { |
| 240 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 241 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 242 | struct cond_info* const cond_p = cond_get(cond); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 243 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 244 | if (cond_p && cond_p->waiter_count > 0) |
| 245 | { |
| 246 | if (! mutex_is_locked_by(cond_p->mutex, drd_tid)) |
| 247 | { |
| 248 | CondRaceErrInfo cei; |
| 249 | cei.cond = cond; |
| 250 | cei.mutex = cond_p->mutex; |
| 251 | VG_(maybe_record_error)(vg_tid, |
| 252 | CondRaceErr, |
| 253 | VG_(get_IP)(vg_tid), |
| 254 | "CondErr", |
| 255 | &cei); |
| 256 | } |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | /* No other thread is waiting for the signal, hence the signal will be */ |
| 261 | /* lost. This is normal in a POSIX threads application. */ |
| 262 | } |
| 263 | } |
| 264 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 265 | /** Called before pthread_cond_signal(). */ |
| 266 | void cond_pre_signal(Addr const cond) |
| 267 | { |
| 268 | if (s_trace_cond) |
| 269 | { |
| 270 | VG_(message)(Vg_UserMsg, |
| 271 | "[%d/%d] cond_signal 0x%lx", |
| 272 | VG_(get_running_tid)(), |
| 273 | thread_get_running_tid(), |
| 274 | cond); |
| 275 | } |
| 276 | |
| 277 | cond_signal(cond); |
| 278 | } |
| 279 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 280 | /** Called before pthread_cond_broadcast(). */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 281 | void cond_pre_broadcast(Addr const cond) |
| 282 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 283 | if (s_trace_cond) |
| 284 | { |
| 285 | VG_(message)(Vg_UserMsg, |
| 286 | "[%d/%d] cond_broadcast 0x%lx", |
| 287 | VG_(get_running_tid)(), |
| 288 | thread_get_running_tid(), |
| 289 | cond); |
| 290 | } |
| 291 | |
| 292 | cond_signal(cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 293 | } |
| 294 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 295 | /** Called after pthread_cond_destroy(). */ |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 296 | void cond_thread_delete(const DrdThreadId tid) |
| 297 | { } |