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