bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 4 | |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2010 Bart Van Assche <bart.vanassche@gmail.com>. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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" |
bart | 46b5fce | 2008-06-28 13:01:30 +0000 | [diff] [blame] | 30 | #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */ |
| 31 | #include "pub_tool_libcassert.h" /* tl_assert() */ |
bart | 850f199 | 2010-05-29 18:43:21 +0000 | [diff] [blame] | 32 | #include "pub_tool_libcbase.h" /* VG_(memcmp)() */ |
bart | 46b5fce | 2008-06-28 13:01:30 +0000 | [diff] [blame] | 33 | #include "pub_tool_libcprint.h" /* VG_(printf)() */ |
| 34 | #include "pub_tool_machine.h" /* VG_(get_IP)() */ |
bart | 46b5fce | 2008-06-28 13:01:30 +0000 | [diff] [blame] | 35 | #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 36 | |
| 37 | |
bart | 46b5fce | 2008-06-28 13:01:30 +0000 | [diff] [blame] | 38 | /* Local functions. */ |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 39 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 40 | static void DRD_(cond_cleanup)(struct cond_info* p); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 41 | |
| 42 | |
bart | 46b5fce | 2008-06-28 13:01:30 +0000 | [diff] [blame] | 43 | /* Local variables. */ |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 44 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 45 | static Bool DRD_(s_report_signal_unlocked) = True; |
| 46 | static Bool DRD_(s_trace_cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 47 | |
| 48 | |
bart | 850f199 | 2010-05-29 18:43:21 +0000 | [diff] [blame] | 49 | /* Global variables. */ |
| 50 | |
| 51 | Addr DRD_(pthread_cond_initializer); |
| 52 | int DRD_(pthread_cond_initializer_size); |
| 53 | |
| 54 | |
bart | 46b5fce | 2008-06-28 13:01:30 +0000 | [diff] [blame] | 55 | /* Function definitions. */ |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 56 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 57 | void DRD_(cond_set_report_signal_unlocked)(const Bool r) |
bart | 764dea2 | 2009-02-15 13:16:52 +0000 | [diff] [blame] | 58 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 59 | DRD_(s_report_signal_unlocked) = r; |
bart | 764dea2 | 2009-02-15 13:16:52 +0000 | [diff] [blame] | 60 | } |
| 61 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 62 | void DRD_(cond_set_trace)(const Bool trace_cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 63 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 64 | DRD_(s_trace_cond) = trace_cond; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 68 | void DRD_(cond_initialize)(struct cond_info* const p, const Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 69 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 70 | tl_assert(cond != 0); |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 71 | tl_assert(p->a1 == cond); |
| 72 | tl_assert(p->type == ClientCondvar); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 73 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 74 | p->cleanup = (void(*)(DrdClientobj*))(DRD_(cond_cleanup)); |
| 75 | p->delete_thread = 0; |
| 76 | p->waiter_count = 0; |
| 77 | p->mutex = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 78 | } |
| 79 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 80 | /** |
| 81 | * Free the memory that was allocated by cond_initialize(). Called by |
| 82 | * DRD_(clientobj_remove)(). |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 83 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 84 | static void DRD_(cond_cleanup)(struct cond_info* p) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 85 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 86 | tl_assert(p); |
| 87 | if (p->mutex) |
| 88 | { |
| 89 | struct mutex_info* q; |
| 90 | q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 91 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 92 | CondDestrErrInfo cde = { |
| 93 | DRD_(thread_get_running_tid)(), |
| 94 | p->a1, |
| 95 | q ? q->a1 : 0, |
| 96 | q ? q->owner : DRD_INVALID_THREADID |
| 97 | }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 98 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 99 | CondDestrErr, |
| 100 | VG_(get_IP)(VG_(get_running_tid)()), |
| 101 | "Destroying condition variable that is being" |
| 102 | " waited upon", |
| 103 | &cde); |
| 104 | } |
| 105 | } |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 106 | } |
| 107 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Report that the synchronization object at address 'addr' is of the |
| 110 | * wrong type. |
| 111 | */ |
| 112 | static void wrong_type(const Addr addr) |
| 113 | { |
| 114 | GenericErrInfo gei = { |
| 115 | .tid = DRD_(thread_get_running_tid)(), |
| 116 | .addr = addr, |
| 117 | }; |
| 118 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 119 | GenericErr, |
| 120 | VG_(get_IP)(VG_(get_running_tid)()), |
| 121 | "wrong type of synchronization object", |
| 122 | &gei); |
| 123 | } |
| 124 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 125 | static struct cond_info* cond_get_or_allocate(const Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 126 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 127 | struct cond_info *p; |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 128 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 129 | tl_assert(offsetof(DrdClientobj, cond) == 0); |
| 130 | p = &(DRD_(clientobj_get)(cond, ClientCondvar)->cond); |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 131 | if (p) |
| 132 | return p; |
| 133 | |
| 134 | if (DRD_(clientobj_present)(cond, cond + 1)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 135 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 136 | wrong_type(cond); |
| 137 | return 0; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 138 | } |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 139 | |
| 140 | p = &(DRD_(clientobj_add)(cond, ClientCondvar)->cond); |
| 141 | DRD_(cond_initialize)(p, cond); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 142 | return p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 143 | } |
| 144 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 145 | struct cond_info* DRD_(cond_get)(const Addr cond) |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 146 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 147 | tl_assert(offsetof(DrdClientobj, cond) == 0); |
| 148 | return &(DRD_(clientobj_get)(cond, ClientCondvar)->cond); |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** Called before pthread_cond_init(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 152 | void DRD_(cond_pre_init)(const Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 153 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 154 | struct cond_info* p; |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 155 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 156 | if (DRD_(s_trace_cond)) |
| 157 | { |
| 158 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 159 | "[%d] cond_init cond 0x%lx\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 160 | DRD_(thread_get_running_tid)(), |
| 161 | cond); |
| 162 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 163 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 164 | p = DRD_(cond_get)(cond); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 165 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 166 | if (p) |
| 167 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 168 | CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 169 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 170 | CondErr, |
| 171 | VG_(get_IP)(VG_(get_running_tid)()), |
| 172 | "initialized twice", |
| 173 | &cei); |
| 174 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 175 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 176 | p = cond_get_or_allocate(cond); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 177 | } |
| 178 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 179 | /** Called after pthread_cond_destroy(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 180 | void DRD_(cond_post_destroy)(const Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 181 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 182 | struct cond_info* p; |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 183 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 184 | if (DRD_(s_trace_cond)) |
| 185 | { |
| 186 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 187 | "[%d] cond_destroy cond 0x%lx\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 188 | DRD_(thread_get_running_tid)(), |
| 189 | cond); |
| 190 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 191 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 192 | p = DRD_(cond_get)(cond); |
| 193 | if (p == 0) |
| 194 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 195 | CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 196 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 197 | CondErr, |
| 198 | VG_(get_IP)(VG_(get_running_tid)()), |
| 199 | "not a condition variable", |
| 200 | &cei); |
| 201 | return; |
| 202 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 203 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 204 | if (p->waiter_count != 0) |
| 205 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 206 | CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 207 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 208 | CondErr, |
| 209 | VG_(get_IP)(VG_(get_running_tid)()), |
| 210 | "destruction of condition variable being waited" |
| 211 | " upon", |
| 212 | &cei); |
| 213 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 214 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 215 | DRD_(clientobj_remove)(p->a1, ClientCondvar); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 216 | } |
| 217 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 218 | /** |
| 219 | * Called before pthread_cond_wait(). Note: before this function is called, |
| 220 | * mutex_unlock() has already been called from drd_clientreq.c. |
bart | 08e6d6a | 2008-06-28 16:28:49 +0000 | [diff] [blame] | 221 | */ |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 222 | void DRD_(cond_pre_wait)(const Addr cond, const Addr mutex) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 223 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 224 | struct cond_info* p; |
| 225 | struct mutex_info* q; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 226 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 227 | if (DRD_(s_trace_cond)) |
| 228 | { |
| 229 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 230 | "[%d] cond_pre_wait cond 0x%lx\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 231 | DRD_(thread_get_running_tid)(), |
| 232 | cond); |
| 233 | } |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 234 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 235 | p = cond_get_or_allocate(cond); |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 236 | if (!p) |
| 237 | { |
| 238 | CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond }; |
| 239 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 240 | CondErr, |
| 241 | VG_(get_IP)(VG_(get_running_tid)()), |
| 242 | "not a condition variable", |
| 243 | &cei); |
| 244 | return; |
| 245 | } |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 246 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 247 | if (p->waiter_count == 0) |
| 248 | { |
| 249 | p->mutex = mutex; |
| 250 | } |
| 251 | else if (p->mutex != mutex) |
| 252 | { |
| 253 | CondWaitErrInfo cwei |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 254 | = { .tid = DRD_(thread_get_running_tid)(), |
| 255 | .cond = cond, .mutex1 = p->mutex, .mutex2 = mutex }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 256 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 257 | CondWaitErr, |
| 258 | VG_(get_IP)(VG_(get_running_tid)()), |
| 259 | "Inconsistent association of condition variable" |
| 260 | " and mutex", |
| 261 | &cwei); |
| 262 | } |
| 263 | tl_assert(p->mutex); |
| 264 | q = DRD_(mutex_get)(p->mutex); |
| 265 | if (q |
| 266 | && q->owner == DRD_(thread_get_running_tid)() && q->recursion_count > 0) |
| 267 | { |
| 268 | const ThreadId vg_tid = VG_(get_running_tid)(); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 269 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 270 | q->a1, q->recursion_count, q->owner }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 271 | VG_(maybe_record_error)(vg_tid, |
| 272 | MutexErr, |
| 273 | VG_(get_IP)(vg_tid), |
| 274 | "Mutex locked recursively", |
| 275 | &MEI); |
| 276 | } |
| 277 | else if (q == 0) |
| 278 | { |
| 279 | DRD_(not_a_mutex)(p->mutex); |
| 280 | } |
bart | 08e6d6a | 2008-06-28 16:28:49 +0000 | [diff] [blame] | 281 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 282 | ++p->waiter_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 283 | } |
| 284 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 285 | /** |
| 286 | * Called after pthread_cond_wait(). |
| 287 | */ |
| 288 | void DRD_(cond_post_wait)(const Addr cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 289 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 290 | struct cond_info* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 291 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 292 | if (DRD_(s_trace_cond)) |
| 293 | { |
| 294 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 295 | "[%d] cond_post_wait cond 0x%lx\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 296 | DRD_(thread_get_running_tid)(), |
| 297 | cond); |
| 298 | } |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 299 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 300 | p = DRD_(cond_get)(cond); |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 301 | if (!p) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 302 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 303 | struct mutex_info* q; |
| 304 | q = &(DRD_(clientobj_get)(p->mutex, ClientMutex)->mutex); |
bart | a9c5508 | 2008-06-28 16:55:35 +0000 | [diff] [blame] | 305 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 306 | CondDestrErrInfo cde = { |
| 307 | DRD_(thread_get_running_tid)(), |
| 308 | p->a1, |
| 309 | q ? q->a1 : 0, |
| 310 | q ? q->owner : DRD_INVALID_THREADID |
| 311 | }; |
| 312 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 313 | CondDestrErr, |
| 314 | VG_(get_IP)(VG_(get_running_tid)()), |
| 315 | "condition variable has been destroyed while" |
| 316 | " being waited upon", |
| 317 | &cde); |
bart | a9c5508 | 2008-06-28 16:55:35 +0000 | [diff] [blame] | 318 | } |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 319 | return; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 320 | } |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 321 | |
| 322 | if (p->waiter_count > 0) |
| 323 | { |
| 324 | --p->waiter_count; |
| 325 | if (p->waiter_count == 0) |
| 326 | { |
| 327 | p->mutex = 0; |
| 328 | } |
| 329 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 330 | } |
| 331 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 332 | static void cond_signal(const DrdThreadId tid, struct cond_info* const cond_p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 333 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 334 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 335 | const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 336 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 337 | tl_assert(cond_p); |
| 338 | |
| 339 | if (cond_p->waiter_count > 0) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 340 | { |
| 341 | if (DRD_(s_report_signal_unlocked) |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 342 | && ! DRD_(mutex_is_locked_by)(cond_p->mutex, drd_tid)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 343 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 344 | /* |
| 345 | * A signal is sent while the associated mutex has not been locked. |
| 346 | * This can indicate but is not necessarily a race condition. |
| 347 | */ |
| 348 | CondRaceErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), |
| 349 | .cond = cond_p->a1, |
| 350 | .mutex = cond_p->mutex, |
| 351 | }; |
| 352 | VG_(maybe_record_error)(vg_tid, |
| 353 | CondRaceErr, |
| 354 | VG_(get_IP)(vg_tid), |
| 355 | "CondErr", |
| 356 | &cei); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | else |
| 360 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 361 | /* |
| 362 | * No other thread is waiting for the signal, hence the signal will |
| 363 | * be lost. This is normal in a POSIX threads application. |
| 364 | */ |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 365 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 366 | } |
| 367 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 368 | static void not_initialized(Addr const cond) |
| 369 | { |
| 370 | CondErrInfo cei = { .tid = DRD_(thread_get_running_tid)(), .cond = cond }; |
| 371 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 372 | CondErr, |
| 373 | VG_(get_IP)(VG_(get_running_tid)()), |
| 374 | "condition variable has not been initialized", |
| 375 | &cei); |
| 376 | } |
| 377 | |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 378 | /** Called before pthread_cond_signal(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 379 | void DRD_(cond_pre_signal)(Addr const cond) |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 380 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 381 | struct cond_info* p; |
| 382 | |
| 383 | p = DRD_(cond_get)(cond); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 384 | if (DRD_(s_trace_cond)) |
| 385 | { |
| 386 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 387 | "[%d] cond_signal cond 0x%lx\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 388 | DRD_(thread_get_running_tid)(), |
| 389 | cond); |
| 390 | } |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 391 | |
bart | 850f199 | 2010-05-29 18:43:21 +0000 | [diff] [blame] | 392 | tl_assert(DRD_(pthread_cond_initializer)); |
| 393 | if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer), |
| 394 | DRD_(pthread_cond_initializer_size)) != 0) |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 395 | { |
| 396 | not_initialized(cond); |
| 397 | return; |
| 398 | } |
| 399 | |
bart | 850f199 | 2010-05-29 18:43:21 +0000 | [diff] [blame] | 400 | if (!p) |
| 401 | p = cond_get_or_allocate(cond); |
| 402 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 403 | cond_signal(DRD_(thread_get_running_tid)(), p); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 404 | } |
| 405 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 406 | /** Called before pthread_cond_broadcast(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 407 | void DRD_(cond_pre_broadcast)(Addr const cond) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 408 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 409 | struct cond_info* p; |
| 410 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 411 | if (DRD_(s_trace_cond)) |
| 412 | { |
| 413 | VG_(message)(Vg_UserMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 414 | "[%d] cond_broadcast cond 0x%lx\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 415 | DRD_(thread_get_running_tid)(), |
| 416 | cond); |
| 417 | } |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 418 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 419 | p = DRD_(cond_get)(cond); |
bart | 850f199 | 2010-05-29 18:43:21 +0000 | [diff] [blame] | 420 | tl_assert(DRD_(pthread_cond_initializer)); |
| 421 | if (!p && VG_(memcmp)((void*)cond, (void*)DRD_(pthread_cond_initializer), |
| 422 | DRD_(pthread_cond_initializer_size)) != 0) |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 423 | { |
| 424 | not_initialized(cond); |
| 425 | return; |
| 426 | } |
| 427 | |
bart | 850f199 | 2010-05-29 18:43:21 +0000 | [diff] [blame] | 428 | if (!p) |
| 429 | p = cond_get_or_allocate(cond); |
| 430 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 431 | cond_signal(DRD_(thread_get_running_tid)(), p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 432 | } |