sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | bart.vanassche@gmail.com |
| 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 | |
| 26 | #include "drd_error.h" |
| 27 | #include "drd_mutex.h" |
| 28 | #include "drd_suppression.h" |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 29 | #include "priv_drd_clientreq.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +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_libcprint.h" // VG_(printf)() |
| 33 | #include "pub_tool_machine.h" // VG_(get_IP)() |
| 34 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
| 35 | |
| 36 | |
| 37 | // Type definitions. |
| 38 | |
| 39 | struct mutex_info |
| 40 | { |
| 41 | Addr mutex; // Pointer to client mutex. |
| 42 | SizeT size; // Size in bytes of client-side object. |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 43 | MutexT mutex_type; // pthread_mutex_t or pthread_spinlock_t. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 44 | int recursion_count; // 0 if free, >= 1 if locked. |
| 45 | DrdThreadId owner; // owner if locked, last owner if free. |
| 46 | VectorClock vc; // vector clock associated with last unlock. |
| 47 | }; |
| 48 | |
| 49 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 50 | // Local functions. |
| 51 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 52 | static Bool mutex_is_locked(struct mutex_info* const p); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 53 | static void mutex_destroy(struct mutex_info* const p); |
| 54 | |
| 55 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | // Local variables. |
| 57 | |
| 58 | static Bool s_trace_mutex; |
| 59 | static ULong s_mutex_lock_count; |
| 60 | struct mutex_info s_mutex[256]; |
| 61 | |
| 62 | |
| 63 | // Function definitions. |
| 64 | |
| 65 | void mutex_set_trace(const Bool trace_mutex) |
| 66 | { |
| 67 | tl_assert(!! trace_mutex == trace_mutex); |
| 68 | s_trace_mutex = trace_mutex; |
| 69 | } |
| 70 | |
| 71 | static |
| 72 | void mutex_initialize(struct mutex_info* const p, |
| 73 | const Addr mutex, |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 74 | const SizeT size, |
| 75 | const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 76 | { |
| 77 | tl_assert(mutex != 0); |
| 78 | tl_assert(size > 0); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 79 | #if 0 |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 80 | tl_assert(mutex_type == mutex_type_mutex |
| 81 | || mutex_type == mutex_type_spinlock); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 82 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 83 | |
| 84 | p->mutex = mutex; |
| 85 | p->size = size; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 86 | p->mutex_type = mutex_type; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 87 | p->recursion_count = 0; |
| 88 | p->owner = DRD_INVALID_THREADID; |
| 89 | vc_init(&p->vc, 0, 0); |
| 90 | } |
| 91 | |
| 92 | static |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 93 | struct mutex_info* |
| 94 | mutex_get_or_allocate(const Addr mutex, |
| 95 | const SizeT size, |
| 96 | const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 97 | { |
| 98 | int i; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 99 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 100 | #if 0 |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 101 | tl_assert(mutex_type == mutex_type_mutex |
| 102 | || mutex_type == mutex_type_spinlock); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 103 | #endif |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 104 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 105 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 106 | { |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 107 | if (s_mutex[i].mutex == mutex) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 108 | { |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 109 | if (s_mutex[i].mutex_type != mutex_type) |
| 110 | { |
| 111 | VG_(message)(Vg_DebugMsg, "??? mutex %p: type changed from %d into %d", |
| 112 | s_mutex[i].mutex, s_mutex[i].mutex_type, mutex_type); |
| 113 | } |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 114 | tl_assert(s_mutex[i].mutex_type == mutex_type); |
| 115 | tl_assert(s_mutex[i].size == size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 116 | return &s_mutex[i]; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 119 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 120 | { |
| 121 | if (s_mutex[i].mutex == 0) |
| 122 | { |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 123 | if (drd_is_any_suppressed(mutex, mutex + size)) |
| 124 | { |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 125 | GenericErrInfo GEI; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 126 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 127 | GenericErr, |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 128 | VG_(get_IP)(VG_(get_running_tid)()), |
| 129 | "Not a mutex", |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 130 | &GEI); |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 131 | return 0; |
| 132 | } |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 133 | mutex_initialize(&s_mutex[i], mutex, size, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 134 | drd_start_suppression(mutex, mutex + size, |
| 135 | mutex_get_typename(&s_mutex[i])); |
| 136 | return &s_mutex[i]; |
| 137 | } |
| 138 | } |
| 139 | tl_assert(0); |
| 140 | return 0; |
| 141 | } |
| 142 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 143 | struct mutex_info* |
| 144 | mutex_init(const Addr mutex, const SizeT size, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 145 | { |
| 146 | struct mutex_info* mutex_p; |
| 147 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 148 | if (s_trace_mutex) |
| 149 | { |
| 150 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 151 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 152 | VG_(message)(Vg_DebugMsg, |
| 153 | "drd_post_mutex_init tid = %d/%d, %s 0x%lx", |
| 154 | vg_tid, drd_tid, |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 155 | mutex_type_name(mutex_type), |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 156 | mutex); |
| 157 | } |
| 158 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 159 | #if 0 |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 160 | tl_assert(mutex_type == mutex_type_mutex |
| 161 | || mutex_type == mutex_type_spinlock); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 162 | #endif |
| 163 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 164 | mutex_p = mutex_get(mutex); |
| 165 | if (mutex_p) |
| 166 | { |
| 167 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 168 | MutexErrInfo MEI |
| 169 | = { mutex_p->mutex, mutex_p->recursion_count, mutex_p->owner }; |
| 170 | VG_(maybe_record_error)(vg_tid, |
| 171 | MutexErr, |
| 172 | VG_(get_IP)(vg_tid), |
| 173 | "Mutex reinitialization", |
| 174 | &MEI); |
| 175 | mutex_destroy(mutex_p); |
| 176 | } |
| 177 | mutex_p = mutex_get_or_allocate(mutex, size, mutex_type); |
| 178 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 179 | return mutex_p; |
| 180 | } |
| 181 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 182 | static void mutex_destroy(struct mutex_info* const p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 183 | { |
| 184 | if (s_trace_mutex) |
| 185 | { |
| 186 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 187 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 188 | VG_(message)(Vg_DebugMsg, |
| 189 | "drd_pre_mutex_destroy tid = %d/%d, %s 0x%lx", |
| 190 | vg_tid, drd_tid, |
| 191 | mutex_get_typename(p), |
| 192 | p->mutex); |
| 193 | } |
| 194 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 195 | if (mutex_is_locked(p)) |
| 196 | { |
| 197 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 198 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 199 | MutexErr, |
| 200 | VG_(get_IP)(VG_(get_running_tid)()), |
| 201 | "Destroying locked mutex", |
| 202 | &MEI); |
| 203 | } |
| 204 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 205 | drd_finish_suppression(p->mutex, p->mutex + p->size); |
| 206 | |
| 207 | vc_cleanup(&p->vc); |
| 208 | p->mutex = 0; |
| 209 | } |
| 210 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 211 | void mutex_pre_destroy(struct mutex_info* const p) |
| 212 | { |
| 213 | return mutex_destroy(p); |
| 214 | } |
| 215 | |
| 216 | void mutex_post_destroy(const Addr mutex) |
| 217 | { |
| 218 | struct mutex_info* p; |
| 219 | |
| 220 | p = mutex_get(mutex); |
| 221 | tl_assert(p); |
| 222 | if (p) |
| 223 | { |
| 224 | if (mutex_get_recursion_count(mutex) > 0) |
| 225 | { |
| 226 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 227 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 228 | VG_(maybe_record_error)(vg_tid, |
| 229 | MutexErr, |
| 230 | VG_(get_IP)(vg_tid), |
| 231 | "Destroying locked mutex", |
| 232 | &MEI); |
| 233 | } |
| 234 | mutex_pre_destroy(p); |
| 235 | } |
| 236 | } |
| 237 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 238 | struct mutex_info* mutex_get(const Addr mutex) |
| 239 | { |
| 240 | int i; |
| 241 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 242 | if (s_mutex[i].mutex == mutex) |
| 243 | return &s_mutex[i]; |
| 244 | return 0; |
| 245 | } |
| 246 | |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 247 | /** Called before pthread_mutex_lock() is invoked. If a data structure for |
| 248 | * the client-side object was not yet created, do this now. Also check whether |
| 249 | * an attempt is made to lock recursively a synchronization object that must |
| 250 | * not be locked recursively. |
| 251 | */ |
| 252 | void mutex_pre_lock(const Addr mutex, const SizeT size, MutexT mutex_type) |
| 253 | { |
| 254 | struct mutex_info* p = mutex_get(mutex); |
| 255 | if (p == 0) |
| 256 | { |
| 257 | mutex_init(mutex, size, mutex_type); |
| 258 | p = mutex_get(mutex); |
| 259 | } |
| 260 | tl_assert(p); |
| 261 | |
| 262 | if (p->owner == thread_get_running_tid() |
| 263 | && p->recursion_count >= 1 |
| 264 | && mutex_type != mutex_type_recursive_mutex) |
| 265 | { |
| 266 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 267 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 268 | MutexErr, |
| 269 | VG_(get_IP)(VG_(get_running_tid)()), |
| 270 | "Recursive locking not allowed", |
| 271 | &MEI); |
| 272 | } |
| 273 | } |
| 274 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 275 | /** |
| 276 | * Update mutex_info state when locking the pthread_mutex_t mutex. |
| 277 | * Note: this function must be called after pthread_mutex_lock() has been |
| 278 | * called, or a race condition is triggered ! |
| 279 | */ |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 280 | int mutex_post_lock(const Addr mutex, const SizeT size, MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 281 | { |
| 282 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)()); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 283 | struct mutex_info* const p = mutex_get_or_allocate(mutex, size, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 284 | |
| 285 | if (s_trace_mutex) |
| 286 | { |
| 287 | const ThreadId tid = DrdThreadIdToVgThreadId(drd_tid); |
| 288 | VG_(message)(Vg_DebugMsg, |
| 289 | "drd_post_mutex_lock tid = %d/%d, %s 0x%lx rc %d owner %d", |
| 290 | tid, |
| 291 | drd_tid, |
| 292 | mutex_get_typename(p), |
| 293 | mutex, |
| 294 | p ? p->recursion_count : 0, |
| 295 | p ? p->owner : VG_INVALID_THREADID); |
| 296 | } |
| 297 | |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 298 | if (p == 0) |
| 299 | { |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 300 | GenericErrInfo GEI; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 301 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 302 | GenericErr, |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 303 | VG_(get_IP)(VG_(get_running_tid)()), |
| 304 | "Not a mutex", |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 305 | &GEI); |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 309 | #if 0 |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 310 | tl_assert(mutex_type == mutex_type_mutex |
| 311 | || mutex_type == mutex_type_spinlock); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 312 | #endif |
| 313 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 314 | tl_assert(p->mutex_type == mutex_type); |
| 315 | tl_assert(p->size == size); |
| 316 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 317 | if (p->recursion_count == 0) |
| 318 | { |
| 319 | p->owner = drd_tid; |
| 320 | s_mutex_lock_count++; |
| 321 | } |
| 322 | else if (p->owner != drd_tid) |
| 323 | { |
| 324 | VG_(message)(Vg_DebugMsg, |
| 325 | "The impossible happened: mutex 0x%lx is locked" |
| 326 | " simultaneously by two threads (recursion count %d," |
| 327 | " owners %d and %d) !", |
| 328 | p->mutex, p->recursion_count, p->owner, drd_tid); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 329 | p->owner = drd_tid; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 330 | } |
| 331 | p->recursion_count++; |
| 332 | |
| 333 | if (p->recursion_count == 1) |
| 334 | { |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 335 | const DrdThreadId last_owner = p->owner; |
| 336 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 337 | if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID) |
| 338 | thread_combine_vc2(drd_tid, mutex_get_last_vc(mutex)); |
| 339 | thread_new_segment(drd_tid); |
| 340 | } |
| 341 | |
| 342 | return p->recursion_count; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Update mutex_info state when unlocking the pthread_mutex_t mutex. |
| 347 | * Note: this function must be called before pthread_mutex_unlock() is called, |
| 348 | * or a race condition is triggered ! |
| 349 | * @param mutex Pointer to pthread_mutex_t data structure in the client space. |
| 350 | * @param tid ThreadId of the thread calling pthread_mutex_unlock(). |
| 351 | * @param vc Pointer to the current vector clock of thread tid. |
| 352 | */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 353 | int mutex_unlock(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 354 | { |
| 355 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)()); |
| 356 | const ThreadId vg_tid = DrdThreadIdToVgThreadId(drd_tid); |
| 357 | const VectorClock* const vc = thread_get_vc(drd_tid); |
| 358 | struct mutex_info* const p = mutex_get(mutex); |
| 359 | |
| 360 | if (s_trace_mutex) |
| 361 | { |
| 362 | VG_(message)(Vg_DebugMsg, |
| 363 | "drd_pre_mutex_unlock tid = %d/%d, %s 0x%lx rc %d", |
| 364 | vg_tid, drd_tid, |
| 365 | mutex_get_typename(p), |
| 366 | mutex, |
| 367 | p->recursion_count, |
| 368 | p->owner); |
| 369 | } |
| 370 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 371 | if (p == 0) |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 372 | { |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 373 | GenericErrInfo GEI; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 374 | VG_(maybe_record_error)(vg_tid, |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 375 | GenericErr, |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 376 | VG_(get_IP)(vg_tid), |
| 377 | "Not a mutex", |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 378 | &GEI); |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 382 | if (p->owner == DRD_INVALID_THREADID) |
| 383 | { |
| 384 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 385 | VG_(maybe_record_error)(vg_tid, |
| 386 | MutexErr, |
| 387 | VG_(get_IP)(vg_tid), |
| 388 | "Mutex not locked", |
| 389 | &MEI); |
| 390 | return 0; |
| 391 | } |
| 392 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 393 | tl_assert(p); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 394 | if (p->mutex_type != mutex_type) |
| 395 | { |
| 396 | VG_(message)(Vg_DebugMsg, "??? mutex %p: type changed from %d into %d", |
| 397 | p->mutex, p->mutex_type, mutex_type); |
| 398 | } |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 399 | tl_assert(p->mutex_type == mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 400 | tl_assert(p->owner != DRD_INVALID_THREADID); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 401 | #if 0 |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 402 | tl_assert(mutex_type == mutex_type_mutex |
| 403 | || mutex_type == mutex_type_spinlock); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 404 | #endif |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 405 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 406 | if (p->owner != drd_tid) |
| 407 | { |
| 408 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 409 | VG_(maybe_record_error)(vg_tid, |
| 410 | MutexErr, |
| 411 | VG_(get_IP)(vg_tid), |
| 412 | "Mutex not unlocked by owner thread", |
| 413 | &MEI); |
| 414 | } |
| 415 | p->recursion_count--; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 416 | if (p->recursion_count < 0) |
| 417 | { |
| 418 | MutexErrInfo MEI |
| 419 | = { p->mutex, p->recursion_count, p->owner }; |
| 420 | VG_(maybe_record_error)(vg_tid, |
| 421 | MutexErr, |
| 422 | VG_(get_IP)(vg_tid), |
| 423 | "Attempt to unlock a mutex that is not locked", |
| 424 | &MEI); |
| 425 | p->recursion_count = 0; |
| 426 | } |
| 427 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 428 | if (p->recursion_count == 0) |
| 429 | { |
| 430 | /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */ |
| 431 | /* current vector clock of the thread such that it is available when */ |
| 432 | /* this mutex is locked again. */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 433 | vc_assign(&p->vc, vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 434 | |
| 435 | thread_new_segment(drd_tid); |
| 436 | } |
| 437 | return p->recursion_count; |
| 438 | } |
| 439 | |
| 440 | const char* mutex_get_typename(struct mutex_info* const p) |
| 441 | { |
| 442 | tl_assert(p); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 443 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 444 | return mutex_type_name(p->mutex_type); |
| 445 | } |
| 446 | |
| 447 | const char* mutex_type_name(const MutexT mt) |
| 448 | { |
| 449 | switch (mt) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 450 | { |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 451 | case mutex_type_recursive_mutex: |
| 452 | return "recursive mutex"; |
| 453 | case mutex_type_errorcheck_mutex: |
| 454 | return "error checking mutex"; |
| 455 | case mutex_type_default_mutex: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 456 | return "mutex"; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 457 | case mutex_type_spinlock: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 458 | return "spinlock"; |
| 459 | default: |
| 460 | tl_assert(0); |
| 461 | } |
| 462 | return "?"; |
| 463 | } |
| 464 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 465 | /** Return true if the specified mutex is locked by any thread. */ |
| 466 | static Bool mutex_is_locked(struct mutex_info* const p) |
| 467 | { |
| 468 | tl_assert(p); |
| 469 | return (p->recursion_count > 0); |
| 470 | } |
| 471 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 472 | Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid) |
| 473 | { |
| 474 | struct mutex_info* const p = mutex_get(mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 475 | if (p) |
| 476 | { |
| 477 | return (p->recursion_count > 0 && p->owner == tid); |
| 478 | } |
| 479 | return False; |
| 480 | } |
| 481 | |
| 482 | const VectorClock* mutex_get_last_vc(const Addr mutex) |
| 483 | { |
| 484 | struct mutex_info* const p = mutex_get(mutex); |
| 485 | return p ? &p->vc : 0; |
| 486 | } |
| 487 | |
| 488 | int mutex_get_recursion_count(const Addr mutex) |
| 489 | { |
| 490 | struct mutex_info* const p = mutex_get(mutex); |
| 491 | tl_assert(p); |
| 492 | return p->recursion_count; |
| 493 | } |
| 494 | |
| 495 | /** |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 496 | * Call this function when thread tid stops to exist, such that the |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 497 | * "last owner" field can be cleared if it still refers to that thread. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 498 | */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 499 | void mutex_thread_delete(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 500 | { |
| 501 | int i; |
| 502 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 503 | { |
| 504 | struct mutex_info* const p = &s_mutex[i]; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 505 | if (p->mutex && p->owner == tid && p->recursion_count > 0) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 506 | { |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 507 | MutexErrInfo MEI |
| 508 | = { p->mutex, p->recursion_count, p->owner }; |
| 509 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 510 | MutexErr, |
| 511 | VG_(get_IP)(VG_(get_running_tid)()), |
| 512 | "Mutex still locked at thread exit", |
| 513 | &MEI); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 514 | p->owner = VG_INVALID_THREADID; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | void mutex_stop_using_mem(const Addr a1, const Addr a2) |
| 520 | { |
| 521 | unsigned i; |
| 522 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 523 | { |
| 524 | if (a1 <= s_mutex[i].mutex && s_mutex[i].mutex < a2) |
| 525 | { |
| 526 | tl_assert(s_mutex[i].mutex + s_mutex[i].size <= a2); |
| 527 | mutex_destroy(&s_mutex[i]); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | ULong get_mutex_lock_count(void) |
| 533 | { |
| 534 | return s_mutex_lock_count; |
| 535 | } |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 536 | |
| 537 | |
| 538 | /* |
| 539 | * Local variables: |
| 540 | * c-basic-offset: 3 |
| 541 | * End: |
| 542 | */ |