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