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 | |
| 247 | /** |
| 248 | * Update mutex_info state when locking the pthread_mutex_t mutex. |
| 249 | * Note: this function must be called after pthread_mutex_lock() has been |
| 250 | * called, or a race condition is triggered ! |
| 251 | */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 252 | int mutex_lock(const Addr mutex, const SizeT size, MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 253 | { |
| 254 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)()); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 255 | struct mutex_info* const p = mutex_get_or_allocate(mutex, size, mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 256 | |
| 257 | if (s_trace_mutex) |
| 258 | { |
| 259 | const ThreadId tid = DrdThreadIdToVgThreadId(drd_tid); |
| 260 | VG_(message)(Vg_DebugMsg, |
| 261 | "drd_post_mutex_lock tid = %d/%d, %s 0x%lx rc %d owner %d", |
| 262 | tid, |
| 263 | drd_tid, |
| 264 | mutex_get_typename(p), |
| 265 | mutex, |
| 266 | p ? p->recursion_count : 0, |
| 267 | p ? p->owner : VG_INVALID_THREADID); |
| 268 | } |
| 269 | |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 270 | if (p == 0) |
| 271 | { |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 272 | GenericErrInfo GEI; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 273 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 274 | GenericErr, |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 275 | VG_(get_IP)(VG_(get_running_tid)()), |
| 276 | "Not a mutex", |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 277 | &GEI); |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 281 | #if 0 |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 282 | tl_assert(mutex_type == mutex_type_mutex |
| 283 | || mutex_type == mutex_type_spinlock); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 284 | #endif |
| 285 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 286 | tl_assert(p->mutex_type == mutex_type); |
| 287 | tl_assert(p->size == size); |
| 288 | |
| 289 | if (p->recursion_count >= 1 && mutex_type == mutex_type_spinlock) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 290 | { |
| 291 | // TO DO: tell the user in a more friendly way that it is not allowed to |
| 292 | // lock spinlocks recursively. |
| 293 | tl_assert(0); |
| 294 | } |
| 295 | |
| 296 | if (p->recursion_count == 0) |
| 297 | { |
| 298 | p->owner = drd_tid; |
| 299 | s_mutex_lock_count++; |
| 300 | } |
| 301 | else if (p->owner != drd_tid) |
| 302 | { |
| 303 | VG_(message)(Vg_DebugMsg, |
| 304 | "The impossible happened: mutex 0x%lx is locked" |
| 305 | " simultaneously by two threads (recursion count %d," |
| 306 | " owners %d and %d) !", |
| 307 | p->mutex, p->recursion_count, p->owner, drd_tid); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 308 | p->owner = drd_tid; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 309 | } |
| 310 | p->recursion_count++; |
| 311 | |
| 312 | if (p->recursion_count == 1) |
| 313 | { |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 314 | const DrdThreadId last_owner = p->owner; |
| 315 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 316 | if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID) |
| 317 | thread_combine_vc2(drd_tid, mutex_get_last_vc(mutex)); |
| 318 | thread_new_segment(drd_tid); |
| 319 | } |
| 320 | |
| 321 | return p->recursion_count; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Update mutex_info state when unlocking the pthread_mutex_t mutex. |
| 326 | * Note: this function must be called before pthread_mutex_unlock() is called, |
| 327 | * or a race condition is triggered ! |
| 328 | * @param mutex Pointer to pthread_mutex_t data structure in the client space. |
| 329 | * @param tid ThreadId of the thread calling pthread_mutex_unlock(). |
| 330 | * @param vc Pointer to the current vector clock of thread tid. |
| 331 | */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 332 | int mutex_unlock(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 333 | { |
| 334 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(VG_(get_running_tid)()); |
| 335 | const ThreadId vg_tid = DrdThreadIdToVgThreadId(drd_tid); |
| 336 | const VectorClock* const vc = thread_get_vc(drd_tid); |
| 337 | struct mutex_info* const p = mutex_get(mutex); |
| 338 | |
| 339 | if (s_trace_mutex) |
| 340 | { |
| 341 | VG_(message)(Vg_DebugMsg, |
| 342 | "drd_pre_mutex_unlock tid = %d/%d, %s 0x%lx rc %d", |
| 343 | vg_tid, drd_tid, |
| 344 | mutex_get_typename(p), |
| 345 | mutex, |
| 346 | p->recursion_count, |
| 347 | p->owner); |
| 348 | } |
| 349 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 350 | if (p == 0) |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 351 | { |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 352 | GenericErrInfo GEI; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 353 | VG_(maybe_record_error)(vg_tid, |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 354 | GenericErr, |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 355 | VG_(get_IP)(vg_tid), |
| 356 | "Not a mutex", |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 357 | &GEI); |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 361 | if (p->owner == DRD_INVALID_THREADID) |
| 362 | { |
| 363 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 364 | VG_(maybe_record_error)(vg_tid, |
| 365 | MutexErr, |
| 366 | VG_(get_IP)(vg_tid), |
| 367 | "Mutex not locked", |
| 368 | &MEI); |
| 369 | return 0; |
| 370 | } |
| 371 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 372 | tl_assert(p); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 373 | if (p->mutex_type != mutex_type) |
| 374 | { |
| 375 | VG_(message)(Vg_DebugMsg, "??? mutex %p: type changed from %d into %d", |
| 376 | p->mutex, p->mutex_type, mutex_type); |
| 377 | } |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 378 | tl_assert(p->mutex_type == mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 379 | tl_assert(p->owner != DRD_INVALID_THREADID); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 380 | #if 0 |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 381 | tl_assert(mutex_type == mutex_type_mutex |
| 382 | || mutex_type == mutex_type_spinlock); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 383 | #endif |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 384 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 385 | if (p->owner != drd_tid) |
| 386 | { |
| 387 | MutexErrInfo MEI = { p->mutex, p->recursion_count, p->owner }; |
| 388 | VG_(maybe_record_error)(vg_tid, |
| 389 | MutexErr, |
| 390 | VG_(get_IP)(vg_tid), |
| 391 | "Mutex not unlocked by owner thread", |
| 392 | &MEI); |
| 393 | } |
| 394 | p->recursion_count--; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 395 | if (p->recursion_count < 0) |
| 396 | { |
| 397 | MutexErrInfo MEI |
| 398 | = { p->mutex, p->recursion_count, p->owner }; |
| 399 | VG_(maybe_record_error)(vg_tid, |
| 400 | MutexErr, |
| 401 | VG_(get_IP)(vg_tid), |
| 402 | "Attempt to unlock a mutex that is not locked", |
| 403 | &MEI); |
| 404 | p->recursion_count = 0; |
| 405 | } |
| 406 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 407 | if (p->recursion_count == 0) |
| 408 | { |
| 409 | /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */ |
| 410 | /* current vector clock of the thread such that it is available when */ |
| 411 | /* this mutex is locked again. */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 412 | vc_assign(&p->vc, vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 413 | |
| 414 | thread_new_segment(drd_tid); |
| 415 | } |
| 416 | return p->recursion_count; |
| 417 | } |
| 418 | |
| 419 | const char* mutex_get_typename(struct mutex_info* const p) |
| 420 | { |
| 421 | tl_assert(p); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 422 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 423 | return mutex_type_name(p->mutex_type); |
| 424 | } |
| 425 | |
| 426 | const char* mutex_type_name(const MutexT mt) |
| 427 | { |
| 428 | switch (mt) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 429 | { |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 430 | case mutex_type_recursive_mutex: |
| 431 | return "recursive mutex"; |
| 432 | case mutex_type_errorcheck_mutex: |
| 433 | return "error checking mutex"; |
| 434 | case mutex_type_default_mutex: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 435 | return "mutex"; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 436 | case mutex_type_spinlock: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 437 | return "spinlock"; |
| 438 | default: |
| 439 | tl_assert(0); |
| 440 | } |
| 441 | return "?"; |
| 442 | } |
| 443 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 444 | /** Return true if the specified mutex is locked by any thread. */ |
| 445 | static Bool mutex_is_locked(struct mutex_info* const p) |
| 446 | { |
| 447 | tl_assert(p); |
| 448 | return (p->recursion_count > 0); |
| 449 | } |
| 450 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 451 | Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid) |
| 452 | { |
| 453 | struct mutex_info* const p = mutex_get(mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 454 | if (p) |
| 455 | { |
| 456 | return (p->recursion_count > 0 && p->owner == tid); |
| 457 | } |
| 458 | return False; |
| 459 | } |
| 460 | |
| 461 | const VectorClock* mutex_get_last_vc(const Addr mutex) |
| 462 | { |
| 463 | struct mutex_info* const p = mutex_get(mutex); |
| 464 | return p ? &p->vc : 0; |
| 465 | } |
| 466 | |
| 467 | int mutex_get_recursion_count(const Addr mutex) |
| 468 | { |
| 469 | struct mutex_info* const p = mutex_get(mutex); |
| 470 | tl_assert(p); |
| 471 | return p->recursion_count; |
| 472 | } |
| 473 | |
| 474 | /** |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 475 | * Call this function when thread tid stops to exist, such that the |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 476 | * "last owner" field can be cleared if it still refers to that thread. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 477 | */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 478 | void mutex_thread_delete(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 479 | { |
| 480 | int i; |
| 481 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 482 | { |
| 483 | struct mutex_info* const p = &s_mutex[i]; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 484 | if (p->mutex && p->owner == tid && p->recursion_count > 0) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 485 | { |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame^] | 486 | MutexErrInfo MEI |
| 487 | = { p->mutex, p->recursion_count, p->owner }; |
| 488 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 489 | MutexErr, |
| 490 | VG_(get_IP)(VG_(get_running_tid)()), |
| 491 | "Mutex still locked at thread exit", |
| 492 | &MEI); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 493 | p->owner = VG_INVALID_THREADID; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void mutex_stop_using_mem(const Addr a1, const Addr a2) |
| 499 | { |
| 500 | unsigned i; |
| 501 | for (i = 0; i < sizeof(s_mutex)/sizeof(s_mutex[0]); i++) |
| 502 | { |
| 503 | if (a1 <= s_mutex[i].mutex && s_mutex[i].mutex < a2) |
| 504 | { |
| 505 | tl_assert(s_mutex[i].mutex + s_mutex[i].size <= a2); |
| 506 | mutex_destroy(&s_mutex[i]); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | ULong get_mutex_lock_count(void) |
| 512 | { |
| 513 | return s_mutex_lock_count; |
| 514 | } |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 515 | |
| 516 | |
| 517 | /* |
| 518 | * Local variables: |
| 519 | * c-basic-offset: 3 |
| 520 | * End: |
| 521 | */ |