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