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 | |
bart | d4bab99 | 2013-10-04 05:55:30 +0000 | [diff] [blame^] | 4 | Copyright (C) 2006-2013 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 | 3dbdc86 | 2009-02-14 12:14:50 +0000 | [diff] [blame] | 25 | #include "drd_basics.h" |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 26 | #include "drd_clientobj.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 27 | #include "drd_error.h" |
| 28 | #include "drd_mutex.h" |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 29 | #include "pub_tool_vki.h" |
bart | 74b2d97 | 2011-10-08 08:54:57 +0000 | [diff] [blame] | 30 | #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */ |
| 31 | #include "pub_tool_libcassert.h" /* tl_assert() */ |
| 32 | #include "pub_tool_libcbase.h" /* VG_(strlen) */ |
| 33 | #include "pub_tool_libcprint.h" /* VG_(message)() */ |
| 34 | #include "pub_tool_libcproc.h" /* VG_(read_millisecond_timer)() */ |
| 35 | #include "pub_tool_machine.h" /* VG_(get_IP)() */ |
| 36 | #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 37 | |
| 38 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 39 | /* Local functions. */ |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 40 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 41 | static void mutex_cleanup(struct mutex_info* p); |
| 42 | static Bool mutex_is_locked(struct mutex_info* const p); |
| 43 | static void mutex_delete_thread(struct mutex_info* p, const DrdThreadId tid); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 44 | |
| 45 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 46 | /* Local variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 47 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 48 | static Bool s_trace_mutex; |
| 49 | static ULong s_mutex_lock_count; |
| 50 | static ULong s_mutex_segment_creation_count; |
bart | 8600c42 | 2010-10-25 18:18:54 +0000 | [diff] [blame] | 51 | static UInt s_mutex_lock_threshold_ms; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 52 | |
| 53 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 54 | /* Function definitions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 55 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 56 | void DRD_(mutex_set_trace)(const Bool trace_mutex) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 57 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 58 | tl_assert(!! trace_mutex == trace_mutex); |
| 59 | s_trace_mutex = trace_mutex; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 60 | } |
| 61 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 62 | void DRD_(mutex_set_lock_threshold)(const UInt lock_threshold_ms) |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 63 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 64 | s_mutex_lock_threshold_ms = lock_threshold_ms; |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 65 | } |
| 66 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 67 | static |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 68 | void DRD_(mutex_initialize)(struct mutex_info* const p, |
| 69 | const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 70 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 71 | tl_assert(mutex); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 72 | tl_assert(p->a1 == mutex); |
bart | 3f4623e | 2008-07-07 16:53:07 +0000 | [diff] [blame] | 73 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 74 | p->cleanup = (void(*)(DrdClientobj*))mutex_cleanup; |
| 75 | p->delete_thread |
| 76 | = (void(*)(DrdClientobj*, DrdThreadId))mutex_delete_thread; |
| 77 | p->mutex_type = mutex_type; |
| 78 | p->recursion_count = 0; |
| 79 | p->owner = DRD_INVALID_THREADID; |
| 80 | p->last_locked_segment = 0; |
| 81 | p->acquiry_time_ms = 0; |
| 82 | p->acquired_at = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 83 | } |
| 84 | |
bart | 46d5f17 | 2008-02-28 19:49:37 +0000 | [diff] [blame] | 85 | /** Deallocate the memory that was allocated by mutex_initialize(). */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 86 | static void mutex_cleanup(struct mutex_info* p) |
bart | 46d5f17 | 2008-02-28 19:49:37 +0000 | [diff] [blame] | 87 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 88 | tl_assert(p); |
bart | 6b71761 | 2008-03-24 09:29:38 +0000 | [diff] [blame] | 89 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 90 | if (s_trace_mutex) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 91 | DRD_(trace_msg)("[%d] mutex_destroy %s 0x%lx rc %d owner %d", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 92 | DRD_(thread_get_running_tid)(), |
| 93 | DRD_(mutex_get_typename)(p), p->a1, |
| 94 | p ? p->recursion_count : -1, |
| 95 | p ? p->owner : DRD_INVALID_THREADID); |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 96 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 97 | if (mutex_is_locked(p)) |
| 98 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 99 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 100 | p->a1, p->recursion_count, p->owner }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 101 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 102 | MutexErr, |
| 103 | VG_(get_IP)(VG_(get_running_tid)()), |
| 104 | "Destroying locked mutex", |
| 105 | &MEI); |
| 106 | } |
bart | 46d5f17 | 2008-02-28 19:49:37 +0000 | [diff] [blame] | 107 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 108 | DRD_(sg_put)(p->last_locked_segment); |
| 109 | p->last_locked_segment = 0; |
bart | 46d5f17 | 2008-02-28 19:49:37 +0000 | [diff] [blame] | 110 | } |
| 111 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 112 | /** Report that address 'mutex' is not the address of a mutex object. */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 113 | void DRD_(not_a_mutex)(const Addr mutex) |
bart | 6b71761 | 2008-03-24 09:29:38 +0000 | [diff] [blame] | 114 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 115 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 116 | mutex, -1, DRD_INVALID_THREADID }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 117 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 118 | MutexErr, |
| 119 | VG_(get_IP)(VG_(get_running_tid)()), |
| 120 | "Not a mutex", |
| 121 | &MEI); |
bart | 6b71761 | 2008-03-24 09:29:38 +0000 | [diff] [blame] | 122 | } |
| 123 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Report that address 'mutex' is not the address of a mutex object of the |
| 126 | * expected type. |
| 127 | */ |
| 128 | static void wrong_mutex_type(const Addr mutex) |
| 129 | { |
| 130 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 131 | mutex, -1, DRD_INVALID_THREADID }; |
| 132 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 133 | MutexErr, |
| 134 | VG_(get_IP)(VG_(get_running_tid)()), |
| 135 | "Mutex type mismatch", |
| 136 | &MEI); |
| 137 | } |
| 138 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 139 | static |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 140 | struct mutex_info* |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 141 | DRD_(mutex_get_or_allocate)(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 142 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 143 | struct mutex_info* p; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 144 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 145 | tl_assert(offsetof(DrdClientobj, mutex) == 0); |
| 146 | p = &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex); |
| 147 | if (p) |
| 148 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 149 | if (mutex_type == mutex_type_unknown || p->mutex_type == mutex_type) |
| 150 | return p; |
| 151 | else |
| 152 | { |
| 153 | wrong_mutex_type(mutex); |
| 154 | return 0; |
| 155 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 156 | } |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 157 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 158 | if (DRD_(clientobj_present)(mutex, mutex + 1)) |
| 159 | { |
| 160 | DRD_(not_a_mutex)(mutex); |
| 161 | return 0; |
| 162 | } |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 163 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 164 | p = &(DRD_(clientobj_add)(mutex, ClientMutex)->mutex); |
| 165 | DRD_(mutex_initialize)(p, mutex, mutex_type); |
| 166 | return p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 167 | } |
| 168 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 169 | struct mutex_info* DRD_(mutex_get)(const Addr mutex) |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 170 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 171 | tl_assert(offsetof(DrdClientobj, mutex) == 0); |
| 172 | return &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex); |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 173 | } |
| 174 | |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 175 | /** Called before pthread_mutex_init(). */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 176 | struct mutex_info* |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 177 | DRD_(mutex_init)(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 178 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 179 | struct mutex_info* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 180 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 181 | if (s_trace_mutex) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 182 | DRD_(trace_msg)("[%d] mutex_init %s 0x%lx", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 183 | DRD_(thread_get_running_tid)(), |
| 184 | DRD_(mutex_type_name)(mutex_type), |
| 185 | mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 186 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 187 | if (mutex_type == mutex_type_invalid_mutex) |
| 188 | { |
| 189 | DRD_(not_a_mutex)(mutex); |
| 190 | return 0; |
| 191 | } |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 192 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 193 | p = DRD_(mutex_get)(mutex); |
| 194 | if (p) |
| 195 | { |
| 196 | const ThreadId vg_tid = VG_(get_running_tid)(); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 197 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 198 | p->a1, p->recursion_count, p->owner }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 199 | VG_(maybe_record_error)(vg_tid, |
| 200 | MutexErr, |
| 201 | VG_(get_IP)(vg_tid), |
| 202 | "Mutex reinitialization", |
| 203 | &MEI); |
bart | fa35828 | 2009-05-16 06:22:46 +0000 | [diff] [blame] | 204 | p->mutex_type = mutex_type; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 205 | return p; |
| 206 | } |
| 207 | p = DRD_(mutex_get_or_allocate)(mutex, mutex_type); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 208 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 209 | return p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 210 | } |
| 211 | |
bart | 46d5f17 | 2008-02-28 19:49:37 +0000 | [diff] [blame] | 212 | /** Called after pthread_mutex_destroy(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 213 | void DRD_(mutex_post_destroy)(const Addr mutex) |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 214 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 215 | struct mutex_info* p; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 216 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 217 | p = DRD_(mutex_get)(mutex); |
| 218 | if (p == 0) |
| 219 | { |
| 220 | DRD_(not_a_mutex)(mutex); |
| 221 | return; |
| 222 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 223 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 224 | DRD_(clientobj_remove)(mutex, ClientMutex); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 225 | } |
| 226 | |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 227 | /** |
| 228 | * Called before pthread_mutex_lock() is invoked. If a data structure for the |
| 229 | * client-side object was not yet created, do this now. Also check whether an |
| 230 | * attempt is made to lock recursively a synchronization object that must not |
| 231 | * be locked recursively. |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 232 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 233 | void DRD_(mutex_pre_lock)(const Addr mutex, MutexT mutex_type, |
| 234 | const Bool trylock) |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 235 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 236 | struct mutex_info* p; |
bart | 635cb16 | 2008-02-28 08:30:43 +0000 | [diff] [blame] | 237 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 238 | p = DRD_(mutex_get_or_allocate)(mutex, mutex_type); |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 239 | if (p && mutex_type == mutex_type_unknown) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 240 | mutex_type = p->mutex_type; |
bart | 3f4623e | 2008-07-07 16:53:07 +0000 | [diff] [blame] | 241 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 242 | if (s_trace_mutex) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 243 | DRD_(trace_msg)("[%d] %s %s 0x%lx rc %d owner %d", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 244 | DRD_(thread_get_running_tid)(), |
| 245 | trylock ? "pre_mutex_lock " : "mutex_trylock ", |
| 246 | p ? DRD_(mutex_get_typename)(p) : "(?)", |
| 247 | mutex, p ? p->recursion_count : -1, |
| 248 | p ? p->owner : DRD_INVALID_THREADID); |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 249 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 250 | if (p == 0) |
| 251 | { |
| 252 | DRD_(not_a_mutex)(mutex); |
| 253 | return; |
| 254 | } |
bart | 2e3a3c1 | 2008-03-24 08:33:47 +0000 | [diff] [blame] | 255 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 256 | tl_assert(p); |
bart | 2e3a3c1 | 2008-03-24 08:33:47 +0000 | [diff] [blame] | 257 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 258 | if (mutex_type == mutex_type_invalid_mutex) |
| 259 | { |
| 260 | DRD_(not_a_mutex)(mutex); |
| 261 | return; |
| 262 | } |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 263 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 264 | if (! trylock |
| 265 | && p->owner == DRD_(thread_get_running_tid)() |
| 266 | && p->recursion_count >= 1 |
| 267 | && mutex_type != mutex_type_recursive_mutex) |
| 268 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 269 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 270 | p->a1, p->recursion_count, p->owner }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 271 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 272 | MutexErr, |
| 273 | VG_(get_IP)(VG_(get_running_tid)()), |
| 274 | "Recursive locking not allowed", |
| 275 | &MEI); |
| 276 | } |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 277 | } |
| 278 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 279 | /** |
| 280 | * Update mutex_info state when locking the pthread_mutex_t mutex. |
| 281 | * Note: this function must be called after pthread_mutex_lock() has been |
| 282 | * called, or a race condition is triggered ! |
| 283 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 284 | void DRD_(mutex_post_lock)(const Addr mutex, const Bool took_lock, |
| 285 | const Bool post_cond_wait) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 286 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 287 | const DrdThreadId drd_tid = DRD_(thread_get_running_tid)(); |
| 288 | struct mutex_info* p; |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 289 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 290 | p = DRD_(mutex_get)(mutex); |
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 (s_trace_mutex) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 293 | DRD_(trace_msg)("[%d] %s %s 0x%lx rc %d owner %d%s", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 294 | drd_tid, |
| 295 | post_cond_wait ? "cond_post_wait " : "post_mutex_lock", |
| 296 | p ? DRD_(mutex_get_typename)(p) : "(?)", |
| 297 | mutex, p ? p->recursion_count : 0, |
| 298 | p ? p->owner : VG_INVALID_THREADID, |
| 299 | took_lock ? "" : " (locking failed)"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 300 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 301 | if (! p || ! took_lock) |
| 302 | return; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 303 | |
bart | 74b2d97 | 2011-10-08 08:54:57 +0000 | [diff] [blame] | 304 | if (p->recursion_count == 0) { |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 305 | if (p->owner != drd_tid && p->owner != DRD_INVALID_THREADID) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 306 | { |
| 307 | tl_assert(p->last_locked_segment); |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 308 | |
| 309 | DRD_(thread_new_segment_and_combine_vc)(drd_tid, |
| 310 | p->last_locked_segment); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 311 | } |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 312 | else |
| 313 | DRD_(thread_new_segment)(drd_tid); |
| 314 | |
| 315 | s_mutex_segment_creation_count++; |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 316 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 317 | p->owner = drd_tid; |
| 318 | p->acquiry_time_ms = VG_(read_millisecond_timer)(); |
| 319 | p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0); |
| 320 | s_mutex_lock_count++; |
bart | 74b2d97 | 2011-10-08 08:54:57 +0000 | [diff] [blame] | 321 | } else if (p->owner != drd_tid) { |
| 322 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 323 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 324 | p->a1, p->recursion_count, p->owner }; |
| 325 | VG_(maybe_record_error)(vg_tid, |
| 326 | MutexErr, |
| 327 | VG_(get_IP)(vg_tid), |
| 328 | "The impossible happened: mutex is locked" |
| 329 | " simultaneously by two threads", |
| 330 | &MEI); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 331 | p->owner = drd_tid; |
| 332 | } |
| 333 | p->recursion_count++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 334 | } |
| 335 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 336 | /** |
| 337 | * Update mutex_info state when unlocking the pthread_mutex_t mutex. |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 338 | * |
bart | 7e6de96 | 2009-02-21 09:39:09 +0000 | [diff] [blame] | 339 | * @param[in] mutex Address of the client mutex. |
| 340 | * @param[in] mutex_type Mutex type. |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 341 | * |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 342 | * @return New value of the mutex recursion count. |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 343 | * |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 344 | * @note This function must be called before pthread_mutex_unlock() is called, |
| 345 | * or a race condition is triggered ! |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 346 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 347 | void DRD_(mutex_unlock)(const Addr mutex, MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 348 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 349 | const DrdThreadId drd_tid = DRD_(thread_get_running_tid)(); |
| 350 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 351 | struct mutex_info* p; |
bart | 3f4623e | 2008-07-07 16:53:07 +0000 | [diff] [blame] | 352 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 353 | p = DRD_(mutex_get)(mutex); |
bart | adb7a20 | 2009-07-21 12:39:25 +0000 | [diff] [blame] | 354 | if (p && mutex_type == mutex_type_unknown) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 355 | mutex_type = p->mutex_type; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 356 | |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 357 | if (s_trace_mutex) { |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 358 | DRD_(trace_msg)("[%d] mutex_unlock %s 0x%lx rc %d", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 359 | drd_tid, p ? DRD_(mutex_get_typename)(p) : "(?)", |
| 360 | mutex, p ? p->recursion_count : 0); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 361 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 362 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 363 | if (p == 0 || mutex_type == mutex_type_invalid_mutex) |
| 364 | { |
| 365 | DRD_(not_a_mutex)(mutex); |
| 366 | return; |
| 367 | } |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 368 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 369 | if (p->owner == DRD_INVALID_THREADID) |
| 370 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 371 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 372 | p->a1, p->recursion_count, p->owner }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 373 | VG_(maybe_record_error)(vg_tid, |
| 374 | MutexErr, |
| 375 | VG_(get_IP)(vg_tid), |
| 376 | "Mutex not locked", |
| 377 | &MEI); |
| 378 | return; |
| 379 | } |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 380 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 381 | tl_assert(p); |
bart | 74b2d97 | 2011-10-08 08:54:57 +0000 | [diff] [blame] | 382 | if (p->mutex_type != mutex_type) { |
| 383 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 384 | p->a1, p->recursion_count, p->owner }; |
| 385 | VG_(maybe_record_error)(vg_tid, MutexErr, VG_(get_IP)(vg_tid), |
| 386 | "Mutex type changed", &MEI); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 387 | } |
| 388 | tl_assert(p->mutex_type == mutex_type); |
| 389 | tl_assert(p->owner != DRD_INVALID_THREADID); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 390 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 391 | if (p->owner != drd_tid || p->recursion_count <= 0) |
| 392 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 393 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 394 | p->a1, p->recursion_count, p->owner }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 395 | VG_(maybe_record_error)(vg_tid, |
| 396 | MutexErr, |
| 397 | VG_(get_IP)(vg_tid), |
| 398 | "Mutex not locked by calling thread", |
| 399 | &MEI); |
| 400 | return; |
| 401 | } |
| 402 | tl_assert(p->recursion_count > 0); |
| 403 | p->recursion_count--; |
| 404 | tl_assert(p->recursion_count >= 0); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 405 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 406 | if (p->recursion_count == 0) |
| 407 | { |
| 408 | if (s_mutex_lock_threshold_ms > 0) |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 409 | { |
bart | 430c45f | 2009-04-13 08:05:18 +0000 | [diff] [blame] | 410 | Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 411 | if (held > s_mutex_lock_threshold_ms) |
| 412 | { |
| 413 | HoldtimeErrInfo HEI |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 414 | = { DRD_(thread_get_running_tid)(), |
| 415 | mutex, p->acquired_at, held, s_mutex_lock_threshold_ms }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 416 | VG_(maybe_record_error)(vg_tid, |
| 417 | HoldtimeErr, |
| 418 | VG_(get_IP)(vg_tid), |
| 419 | "mutex", |
| 420 | &HEI); |
| 421 | } |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 422 | } |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 423 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 424 | /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */ |
| 425 | /* current vector clock of the thread such that it is available when */ |
| 426 | /* this mutex is locked again. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 427 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 428 | DRD_(thread_get_latest_segment)(&p->last_locked_segment, drd_tid); |
| 429 | DRD_(thread_new_segment)(drd_tid); |
| 430 | p->acquired_at = 0; |
| 431 | s_mutex_segment_creation_count++; |
| 432 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 433 | } |
| 434 | |
bart | 3dbdc86 | 2009-02-14 12:14:50 +0000 | [diff] [blame] | 435 | void DRD_(spinlock_init_or_unlock)(const Addr spinlock) |
| 436 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 437 | struct mutex_info* mutex_p = DRD_(mutex_get)(spinlock); |
| 438 | if (mutex_p) |
| 439 | { |
| 440 | DRD_(mutex_unlock)(spinlock, mutex_type_spinlock); |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | DRD_(mutex_init)(spinlock, mutex_type_spinlock); |
| 445 | } |
bart | 3dbdc86 | 2009-02-14 12:14:50 +0000 | [diff] [blame] | 446 | } |
| 447 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 448 | const HChar* DRD_(mutex_get_typename)(struct mutex_info* const p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 449 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 450 | tl_assert(p); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 451 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 452 | return DRD_(mutex_type_name)(p->mutex_type); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 453 | } |
| 454 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 455 | const HChar* DRD_(mutex_type_name)(const MutexT mt) |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 456 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 457 | switch (mt) |
| 458 | { |
bart | adb7a20 | 2009-07-21 12:39:25 +0000 | [diff] [blame] | 459 | case mutex_type_unknown: |
| 460 | return "mutex"; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 461 | case mutex_type_invalid_mutex: |
| 462 | return "invalid mutex"; |
| 463 | case mutex_type_recursive_mutex: |
| 464 | return "recursive mutex"; |
| 465 | case mutex_type_errorcheck_mutex: |
| 466 | return "error checking mutex"; |
| 467 | case mutex_type_default_mutex: |
| 468 | return "mutex"; |
| 469 | case mutex_type_spinlock: |
| 470 | return "spinlock"; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 471 | } |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 472 | tl_assert(0); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 473 | return "?"; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 474 | } |
| 475 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 476 | /** Return true if the specified mutex is locked by any thread. */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 477 | static Bool mutex_is_locked(struct mutex_info* const p) |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 478 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 479 | tl_assert(p); |
| 480 | return (p->recursion_count > 0); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 481 | } |
| 482 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 483 | Bool DRD_(mutex_is_locked_by)(const Addr mutex, const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 484 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 485 | struct mutex_info* const p = DRD_(mutex_get)(mutex); |
| 486 | if (p) |
| 487 | { |
| 488 | return (p->recursion_count > 0 && p->owner == tid); |
| 489 | } |
| 490 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 491 | } |
| 492 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 493 | int DRD_(mutex_get_recursion_count)(const Addr mutex) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 494 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 495 | struct mutex_info* const p = DRD_(mutex_get)(mutex); |
| 496 | tl_assert(p); |
| 497 | return p->recursion_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 501 | * Call this function when thread tid stops to exist, such that the |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 502 | * "last owner" field can be cleared if it still refers to that thread. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 503 | */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 504 | static void mutex_delete_thread(struct mutex_info* p, const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 505 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 506 | tl_assert(p); |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 507 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 508 | if (p->owner == tid && p->recursion_count > 0) |
| 509 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 510 | MutexErrInfo MEI = { DRD_(thread_get_running_tid)(), |
| 511 | p->a1, p->recursion_count, p->owner }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 512 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 513 | MutexErr, |
| 514 | VG_(get_IP)(VG_(get_running_tid)()), |
| 515 | "Mutex still locked at thread exit", |
| 516 | &MEI); |
| 517 | p->owner = VG_INVALID_THREADID; |
| 518 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 519 | } |
| 520 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 521 | ULong DRD_(get_mutex_lock_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 522 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 523 | return s_mutex_lock_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 524 | } |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 525 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 526 | ULong DRD_(get_mutex_segment_creation_count)(void) |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 527 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 528 | return s_mutex_segment_creation_count; |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 529 | } |