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