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 | |
| 52 | static void mutex_destroy(struct mutex_info* const p); |
| 53 | |
| 54 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 55 | // Local variables. |
| 56 | |
| 57 | static Bool s_trace_mutex; |
| 58 | static ULong s_mutex_lock_count; |
| 59 | struct mutex_info s_mutex[256]; |
| 60 | |
| 61 | |
| 62 | // Function definitions. |
| 63 | |
| 64 | void mutex_set_trace(const Bool trace_mutex) |
| 65 | { |
| 66 | tl_assert(!! trace_mutex == trace_mutex); |
| 67 | s_trace_mutex = trace_mutex; |
| 68 | } |
| 69 | |
| 70 | static |
| 71 | void mutex_initialize(struct mutex_info* const p, |
| 72 | const Addr mutex, |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 73 | const SizeT size, |
| 74 | const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 75 | { |
| 76 | tl_assert(mutex != 0); |
| 77 | tl_assert(size > 0); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 78 | tl_assert(mutex_type == mutex_type_mutex |
| 79 | || mutex_type == mutex_type_spinlock); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 80 | |
| 81 | p->mutex = mutex; |
| 82 | p->size = size; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 83 | p->mutex_type = mutex_type; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 84 | p->recursion_count = 0; |
| 85 | p->owner = DRD_INVALID_THREADID; |
| 86 | vc_init(&p->vc, 0, 0); |
| 87 | } |
| 88 | |
| 89 | static |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 90 | struct mutex_info* |
| 91 | mutex_get_or_allocate(const Addr mutex, |
| 92 | const SizeT size, |
| 93 | const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 94 | { |
| 95 | int i; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 96 | |
| 97 | tl_assert(mutex_type == mutex_type_mutex |
| 98 | || mutex_type == mutex_type_spinlock); |
| 99 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 100 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 101 | { |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 102 | if (s_mutex[i].mutex == mutex) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 103 | { |
| 104 | tl_assert(s_mutex[i].mutex_type == mutex_type); |
| 105 | tl_assert(s_mutex[i].size == size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 106 | return &s_mutex[i]; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 109 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 110 | { |
| 111 | if (s_mutex[i].mutex == 0) |
| 112 | { |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 113 | if (drd_is_any_suppressed(mutex, mutex + size)) |
| 114 | { |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame^] | 115 | GenericErrInfo GEI; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 116 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame^] | 117 | GenericErr, |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 118 | VG_(get_IP)(VG_(get_running_tid)()), |
| 119 | "Not a mutex", |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame^] | 120 | &GEI); |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 121 | return 0; |
| 122 | } |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 123 | mutex_initialize(&s_mutex[i], mutex, size, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 124 | drd_start_suppression(mutex, mutex + size, |
| 125 | mutex_get_typename(&s_mutex[i])); |
| 126 | return &s_mutex[i]; |
| 127 | } |
| 128 | } |
| 129 | tl_assert(0); |
| 130 | return 0; |
| 131 | } |
| 132 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 133 | struct mutex_info* |
| 134 | mutex_init(const Addr mutex, const SizeT size, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 135 | { |
| 136 | struct mutex_info* mutex_p; |
| 137 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 138 | if (s_trace_mutex) |
| 139 | { |
| 140 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 141 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 142 | VG_(message)(Vg_DebugMsg, |
| 143 | "drd_post_mutex_init tid = %d/%d, %s 0x%lx", |
| 144 | vg_tid, drd_tid, |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 145 | mutex_type_name(mutex_type), |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 146 | mutex); |
| 147 | } |
| 148 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 149 | tl_assert(mutex_type == mutex_type_mutex |
| 150 | || mutex_type == mutex_type_spinlock); |
| 151 | mutex_p = mutex_get(mutex); |
| 152 | if (mutex_p) |
| 153 | { |
| 154 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 155 | MutexErrInfo MEI |
| 156 | = { mutex_p->mutex, mutex_p->recursion_count, mutex_p->owner }; |
| 157 | VG_(maybe_record_error)(vg_tid, |
| 158 | MutexErr, |
| 159 | VG_(get_IP)(vg_tid), |
| 160 | "Mutex reinitialization", |
| 161 | &MEI); |
| 162 | mutex_destroy(mutex_p); |
| 163 | } |
| 164 | mutex_p = mutex_get_or_allocate(mutex, size, mutex_type); |
| 165 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 166 | return mutex_p; |
| 167 | } |
| 168 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 169 | static void mutex_destroy(struct mutex_info* const p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 170 | { |
| 171 | if (s_trace_mutex) |
| 172 | { |
| 173 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 174 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 175 | VG_(message)(Vg_DebugMsg, |
| 176 | "drd_pre_mutex_destroy tid = %d/%d, %s 0x%lx", |
| 177 | vg_tid, drd_tid, |
| 178 | mutex_get_typename(p), |
| 179 | p->mutex); |
| 180 | } |
| 181 | |
| 182 | drd_finish_suppression(p->mutex, p->mutex + p->size); |
| 183 | |
| 184 | vc_cleanup(&p->vc); |
| 185 | p->mutex = 0; |
| 186 | } |
| 187 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 188 | void mutex_pre_destroy(struct mutex_info* const p) |
| 189 | { |
| 190 | return mutex_destroy(p); |
| 191 | } |
| 192 | |
| 193 | void mutex_post_destroy(const Addr mutex) |
| 194 | { |
| 195 | struct mutex_info* p; |
| 196 | |
| 197 | p = mutex_get(mutex); |
| 198 | tl_assert(p); |
| 199 | if (p) |
| 200 | { |
| 201 | if (mutex_get_recursion_count(mutex) > 0) |
| 202 | { |
| 203 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 204 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 205 | VG_(maybe_record_error)(vg_tid, |
| 206 | MutexErr, |
| 207 | VG_(get_IP)(vg_tid), |
| 208 | "Destroying locked mutex", |
| 209 | &MEI); |
| 210 | } |
| 211 | mutex_pre_destroy(p); |
| 212 | } |
| 213 | } |
| 214 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 215 | struct mutex_info* mutex_get(const Addr mutex) |
| 216 | { |
| 217 | int i; |
| 218 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 219 | if (s_mutex[i].mutex == mutex) |
| 220 | return &s_mutex[i]; |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Update mutex_info state when locking the pthread_mutex_t mutex. |
| 226 | * Note: this function must be called after pthread_mutex_lock() has been |
| 227 | * called, or a race condition is triggered ! |
| 228 | */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 229 | int mutex_lock(const Addr mutex, const SizeT size, MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 230 | { |
| 231 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)()); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 232 | struct mutex_info* const p = mutex_get_or_allocate(mutex, size, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 233 | |
| 234 | if (s_trace_mutex) |
| 235 | { |
| 236 | const ThreadId tid = DrdThreadIdToVgThreadId(drd_tid); |
| 237 | VG_(message)(Vg_DebugMsg, |
| 238 | "drd_post_mutex_lock tid = %d/%d, %s 0x%lx rc %d owner %d", |
| 239 | tid, |
| 240 | drd_tid, |
| 241 | mutex_get_typename(p), |
| 242 | mutex, |
| 243 | p ? p->recursion_count : 0, |
| 244 | p ? p->owner : VG_INVALID_THREADID); |
| 245 | } |
| 246 | |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 247 | if (p == 0) |
| 248 | { |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame^] | 249 | GenericErrInfo GEI; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 250 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame^] | 251 | GenericErr, |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 252 | VG_(get_IP)(VG_(get_running_tid)()), |
| 253 | "Not a mutex", |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame^] | 254 | &GEI); |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 258 | tl_assert(mutex_type == mutex_type_mutex |
| 259 | || mutex_type == mutex_type_spinlock); |
| 260 | tl_assert(p->mutex_type == mutex_type); |
| 261 | tl_assert(p->size == size); |
| 262 | |
| 263 | if (p->recursion_count >= 1 && mutex_type == mutex_type_spinlock) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 264 | { |
| 265 | // TO DO: tell the user in a more friendly way that it is not allowed to |
| 266 | // lock spinlocks recursively. |
| 267 | tl_assert(0); |
| 268 | } |
| 269 | |
| 270 | if (p->recursion_count == 0) |
| 271 | { |
| 272 | p->owner = drd_tid; |
| 273 | s_mutex_lock_count++; |
| 274 | } |
| 275 | else if (p->owner != drd_tid) |
| 276 | { |
| 277 | VG_(message)(Vg_DebugMsg, |
| 278 | "The impossible happened: mutex 0x%lx is locked" |
| 279 | " simultaneously by two threads (recursion count %d," |
| 280 | " owners %d and %d) !", |
| 281 | p->mutex, p->recursion_count, p->owner, drd_tid); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 282 | p->owner = drd_tid; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 283 | } |
| 284 | p->recursion_count++; |
| 285 | |
| 286 | if (p->recursion_count == 1) |
| 287 | { |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 288 | const DrdThreadId last_owner = p->owner; |
| 289 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 290 | if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID) |
| 291 | thread_combine_vc2(drd_tid, mutex_get_last_vc(mutex)); |
| 292 | thread_new_segment(drd_tid); |
| 293 | } |
| 294 | |
| 295 | return p->recursion_count; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Update mutex_info state when unlocking the pthread_mutex_t mutex. |
| 300 | * Note: this function must be called before pthread_mutex_unlock() is called, |
| 301 | * or a race condition is triggered ! |
| 302 | * @param mutex Pointer to pthread_mutex_t data structure in the client space. |
| 303 | * @param tid ThreadId of the thread calling pthread_mutex_unlock(). |
| 304 | * @param vc Pointer to the current vector clock of thread tid. |
| 305 | */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 306 | int mutex_unlock(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 307 | { |
| 308 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)()); |
| 309 | const ThreadId vg_tid = DrdThreadIdToVgThreadId(drd_tid); |
| 310 | const VectorClock* const vc = thread_get_vc(drd_tid); |
| 311 | struct mutex_info* const p = mutex_get(mutex); |
| 312 | |
| 313 | if (s_trace_mutex) |
| 314 | { |
| 315 | VG_(message)(Vg_DebugMsg, |
| 316 | "drd_pre_mutex_unlock tid = %d/%d, %s 0x%lx rc %d", |
| 317 | vg_tid, drd_tid, |
| 318 | mutex_get_typename(p), |
| 319 | mutex, |
| 320 | p->recursion_count, |
| 321 | p->owner); |
| 322 | } |
| 323 | |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 324 | if (p == 0 || p->owner == DRD_INVALID_THREADID) |
| 325 | { |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame^] | 326 | GenericErrInfo GEI; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 327 | VG_(maybe_record_error)(vg_tid, |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame^] | 328 | GenericErr, |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 329 | VG_(get_IP)(vg_tid), |
| 330 | "Not a mutex", |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame^] | 331 | &GEI); |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 335 | tl_assert(p); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 336 | tl_assert(p->mutex_type == mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 337 | tl_assert(p->owner != DRD_INVALID_THREADID); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 338 | tl_assert(mutex_type == mutex_type_mutex |
| 339 | || mutex_type == mutex_type_spinlock); |
| 340 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 341 | if (p->owner != drd_tid) |
| 342 | { |
| 343 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 344 | VG_(maybe_record_error)(vg_tid, |
| 345 | MutexErr, |
| 346 | VG_(get_IP)(vg_tid), |
| 347 | "Mutex not unlocked by owner thread", |
| 348 | &MEI); |
| 349 | } |
| 350 | p->recursion_count--; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 351 | if (p->recursion_count < 0) |
| 352 | { |
| 353 | MutexErrInfo MEI |
| 354 | = { p->mutex, p->recursion_count, p->owner }; |
| 355 | VG_(maybe_record_error)(vg_tid, |
| 356 | MutexErr, |
| 357 | VG_(get_IP)(vg_tid), |
| 358 | "Attempt to unlock a mutex that is not locked", |
| 359 | &MEI); |
| 360 | p->recursion_count = 0; |
| 361 | } |
| 362 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 363 | if (p->recursion_count == 0) |
| 364 | { |
| 365 | /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */ |
| 366 | /* current vector clock of the thread such that it is available when */ |
| 367 | /* this mutex is locked again. */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 368 | vc_assign(&p->vc, vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 369 | |
| 370 | thread_new_segment(drd_tid); |
| 371 | } |
| 372 | return p->recursion_count; |
| 373 | } |
| 374 | |
| 375 | const char* mutex_get_typename(struct mutex_info* const p) |
| 376 | { |
| 377 | tl_assert(p); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 378 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 379 | return mutex_type_name(p->mutex_type); |
| 380 | } |
| 381 | |
| 382 | const char* mutex_type_name(const MutexT mt) |
| 383 | { |
| 384 | switch (mt) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 385 | { |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 386 | case mutex_type_mutex: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 387 | return "mutex"; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 388 | case mutex_type_spinlock: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 389 | return "spinlock"; |
| 390 | default: |
| 391 | tl_assert(0); |
| 392 | } |
| 393 | return "?"; |
| 394 | } |
| 395 | |
| 396 | Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid) |
| 397 | { |
| 398 | struct mutex_info* const p = mutex_get(mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 399 | if (p) |
| 400 | { |
| 401 | return (p->recursion_count > 0 && p->owner == tid); |
| 402 | } |
| 403 | return False; |
| 404 | } |
| 405 | |
| 406 | const VectorClock* mutex_get_last_vc(const Addr mutex) |
| 407 | { |
| 408 | struct mutex_info* const p = mutex_get(mutex); |
| 409 | return p ? &p->vc : 0; |
| 410 | } |
| 411 | |
| 412 | int mutex_get_recursion_count(const Addr mutex) |
| 413 | { |
| 414 | struct mutex_info* const p = mutex_get(mutex); |
| 415 | tl_assert(p); |
| 416 | return p->recursion_count; |
| 417 | } |
| 418 | |
| 419 | /** |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 420 | * Call this function when thread tid stops to exist, such that the |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 421 | * "last owner" field can be cleared if it still refers to that thread. |
| 422 | * TO DO: print an error message if a thread exits while it still has some |
| 423 | * mutexes locked. |
| 424 | */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 425 | void mutex_thread_delete(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 426 | { |
| 427 | int i; |
| 428 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 429 | { |
| 430 | struct mutex_info* const p = &s_mutex[i]; |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 431 | if (p->mutex && p->owner == tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 432 | { |
| 433 | p->owner = VG_INVALID_THREADID; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void mutex_stop_using_mem(const Addr a1, const Addr a2) |
| 439 | { |
| 440 | unsigned i; |
| 441 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 442 | { |
| 443 | if (a1 <= s_mutex[i].mutex && s_mutex[i].mutex < a2) |
| 444 | { |
| 445 | tl_assert(s_mutex[i].mutex + s_mutex[i].size <= a2); |
| 446 | mutex_destroy(&s_mutex[i]); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | ULong get_mutex_lock_count(void) |
| 452 | { |
| 453 | return s_mutex_lock_count; |
| 454 | } |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 455 | |
| 456 | |
| 457 | /* |
| 458 | * Local variables: |
| 459 | * c-basic-offset: 3 |
| 460 | * End: |
| 461 | */ |