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