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