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 | { |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame^] | 79 | if (s_trace_mutex) |
| 80 | { |
| 81 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 82 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 83 | VG_(message)(Vg_DebugMsg, |
| 84 | "drd_pre_mutex_destroy tid = %d/%d, %s 0x%lx", |
| 85 | vg_tid, drd_tid, |
| 86 | mutex_get_typename(p), |
| 87 | p->a1); |
| 88 | } |
| 89 | |
bart | 46d5f17 | 2008-02-28 19:49:37 +0000 | [diff] [blame] | 90 | if (mutex_is_locked(p)) |
| 91 | { |
| 92 | MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner }; |
| 93 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 94 | MutexErr, |
| 95 | VG_(get_IP)(VG_(get_running_tid)()), |
| 96 | "Destroying locked mutex", |
| 97 | &MEI); |
| 98 | } |
| 99 | |
| 100 | vc_cleanup(&p->vc); |
| 101 | } |
| 102 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 103 | static |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 104 | struct mutex_info* |
| 105 | mutex_get_or_allocate(const Addr mutex, |
| 106 | const SizeT size, |
| 107 | const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 108 | { |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 109 | struct mutex_info* p; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 110 | |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 111 | tl_assert(offsetof(DrdClientobj, mutex) == 0); |
| 112 | p = &drd_clientobj_get(mutex, ClientMutex)->mutex; |
| 113 | if (p) |
| 114 | { |
| 115 | tl_assert(p->mutex_type == mutex_type); |
| 116 | tl_assert(p->a2 - p->a1 == size); |
| 117 | return p; |
| 118 | } |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 119 | |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 120 | if (drd_clientobj_present(mutex, mutex + size)) |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 121 | { |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 122 | GenericErrInfo GEI; |
| 123 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 124 | GenericErr, |
| 125 | VG_(get_IP)(VG_(get_running_tid)()), |
| 126 | "Not a mutex", |
| 127 | &GEI); |
| 128 | return 0; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 129 | } |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 130 | |
| 131 | p = &drd_clientobj_add(mutex, mutex + size, ClientMutex)->mutex; |
| 132 | mutex_initialize(p, mutex, size, mutex_type); |
| 133 | return p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 134 | } |
| 135 | |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 136 | struct mutex_info* |
| 137 | mutex_init(const Addr mutex, const SizeT size, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 138 | { |
| 139 | struct mutex_info* mutex_p; |
| 140 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 141 | if (s_trace_mutex) |
| 142 | { |
| 143 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 144 | const DrdThreadId drd_tid = VgThreadIdToDrdThreadId(vg_tid); |
| 145 | VG_(message)(Vg_DebugMsg, |
| 146 | "drd_post_mutex_init tid = %d/%d, %s 0x%lx", |
| 147 | vg_tid, drd_tid, |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 148 | mutex_type_name(mutex_type), |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 149 | mutex); |
| 150 | } |
| 151 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 152 | mutex_p = mutex_get(mutex); |
| 153 | if (mutex_p) |
| 154 | { |
| 155 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 156 | MutexErrInfo MEI |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 157 | = { mutex_p->a1, mutex_p->recursion_count, mutex_p->owner }; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 158 | VG_(maybe_record_error)(vg_tid, |
| 159 | MutexErr, |
| 160 | VG_(get_IP)(vg_tid), |
| 161 | "Mutex reinitialization", |
| 162 | &MEI); |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame^] | 163 | return mutex_p; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 164 | } |
| 165 | mutex_p = mutex_get_or_allocate(mutex, size, mutex_type); |
| 166 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 167 | return mutex_p; |
| 168 | } |
| 169 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 170 | static void mutex_destroy(struct mutex_info* const p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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 ! |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame^] | 309 | * @return New value of the mutex recursion count. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 310 | * @param mutex Pointer to pthread_mutex_t data structure in the client space. |
| 311 | * @param tid ThreadId of the thread calling pthread_mutex_unlock(). |
| 312 | * @param vc Pointer to the current vector clock of thread tid. |
| 313 | */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 314 | int mutex_unlock(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 315 | { |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame^] | 316 | const DrdThreadId drd_tid = thread_get_running_tid(); |
| 317 | const ThreadId vg_tid = VG_(get_running_tid)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 318 | const VectorClock* const vc = thread_get_vc(drd_tid); |
| 319 | struct mutex_info* const p = mutex_get(mutex); |
| 320 | |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame^] | 321 | if (s_trace_mutex && p != 0) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 322 | { |
| 323 | VG_(message)(Vg_DebugMsg, |
| 324 | "drd_pre_mutex_unlock tid = %d/%d, %s 0x%lx rc %d", |
| 325 | vg_tid, drd_tid, |
| 326 | mutex_get_typename(p), |
| 327 | mutex, |
| 328 | p->recursion_count, |
| 329 | p->owner); |
| 330 | } |
| 331 | |
bart | 635cb16 | 2008-02-28 08:30:43 +0000 | [diff] [blame] | 332 | if (mutex_type == mutex_type_invalid_mutex) |
| 333 | { |
| 334 | GenericErrInfo GEI; |
| 335 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 336 | GenericErr, |
| 337 | VG_(get_IP)(VG_(get_running_tid)()), |
| 338 | "Invalid mutex", |
| 339 | &GEI); |
| 340 | } |
| 341 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 342 | if (p == 0) |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 343 | { |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 344 | GenericErrInfo GEI; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 345 | VG_(maybe_record_error)(vg_tid, |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 346 | GenericErr, |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 347 | VG_(get_IP)(vg_tid), |
| 348 | "Not a mutex", |
bart | e883bc8 | 2008-02-26 19:13:04 +0000 | [diff] [blame] | 349 | &GEI); |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 353 | if (p->owner == DRD_INVALID_THREADID) |
| 354 | { |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 355 | MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner }; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 356 | VG_(maybe_record_error)(vg_tid, |
| 357 | MutexErr, |
| 358 | VG_(get_IP)(vg_tid), |
| 359 | "Mutex not locked", |
| 360 | &MEI); |
| 361 | return 0; |
| 362 | } |
| 363 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 364 | tl_assert(p); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 365 | if (p->mutex_type != mutex_type) |
| 366 | { |
| 367 | VG_(message)(Vg_DebugMsg, "??? mutex %p: type changed from %d into %d", |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 368 | p->a1, p->mutex_type, mutex_type); |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 369 | } |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 370 | tl_assert(p->mutex_type == mutex_type); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 371 | tl_assert(p->owner != DRD_INVALID_THREADID); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 372 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 373 | if (p->owner != drd_tid) |
| 374 | { |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 375 | MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner }; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 376 | VG_(maybe_record_error)(vg_tid, |
| 377 | MutexErr, |
| 378 | VG_(get_IP)(vg_tid), |
| 379 | "Mutex not unlocked by owner thread", |
| 380 | &MEI); |
| 381 | } |
| 382 | p->recursion_count--; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 383 | if (p->recursion_count < 0) |
| 384 | { |
| 385 | MutexErrInfo MEI |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 386 | = { p->a1, p->recursion_count, p->owner }; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 387 | VG_(maybe_record_error)(vg_tid, |
| 388 | MutexErr, |
| 389 | VG_(get_IP)(vg_tid), |
| 390 | "Attempt to unlock a mutex that is not locked", |
| 391 | &MEI); |
| 392 | p->recursion_count = 0; |
| 393 | } |
| 394 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 395 | if (p->recursion_count == 0) |
| 396 | { |
| 397 | /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */ |
| 398 | /* current vector clock of the thread such that it is available when */ |
| 399 | /* this mutex is locked again. */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 400 | vc_assign(&p->vc, vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 401 | |
| 402 | thread_new_segment(drd_tid); |
| 403 | } |
| 404 | return p->recursion_count; |
| 405 | } |
| 406 | |
| 407 | const char* mutex_get_typename(struct mutex_info* const p) |
| 408 | { |
| 409 | tl_assert(p); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 410 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 411 | return mutex_type_name(p->mutex_type); |
| 412 | } |
| 413 | |
| 414 | const char* mutex_type_name(const MutexT mt) |
| 415 | { |
| 416 | switch (mt) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 417 | { |
bart | 635cb16 | 2008-02-28 08:30:43 +0000 | [diff] [blame] | 418 | case mutex_type_invalid_mutex: |
| 419 | return "invalid mutex"; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 420 | case mutex_type_recursive_mutex: |
| 421 | return "recursive mutex"; |
| 422 | case mutex_type_errorcheck_mutex: |
| 423 | return "error checking mutex"; |
| 424 | case mutex_type_default_mutex: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 425 | return "mutex"; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 426 | case mutex_type_spinlock: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 427 | return "spinlock"; |
| 428 | default: |
| 429 | tl_assert(0); |
| 430 | } |
| 431 | return "?"; |
| 432 | } |
| 433 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 434 | /** Return true if the specified mutex is locked by any thread. */ |
| 435 | static Bool mutex_is_locked(struct mutex_info* const p) |
| 436 | { |
| 437 | tl_assert(p); |
| 438 | return (p->recursion_count > 0); |
| 439 | } |
| 440 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 441 | Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid) |
| 442 | { |
| 443 | struct mutex_info* const p = mutex_get(mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 444 | if (p) |
| 445 | { |
| 446 | return (p->recursion_count > 0 && p->owner == tid); |
| 447 | } |
| 448 | return False; |
| 449 | } |
| 450 | |
| 451 | const VectorClock* mutex_get_last_vc(const Addr mutex) |
| 452 | { |
| 453 | struct mutex_info* const p = mutex_get(mutex); |
| 454 | return p ? &p->vc : 0; |
| 455 | } |
| 456 | |
| 457 | int mutex_get_recursion_count(const Addr mutex) |
| 458 | { |
| 459 | struct mutex_info* const p = mutex_get(mutex); |
| 460 | tl_assert(p); |
| 461 | return p->recursion_count; |
| 462 | } |
| 463 | |
| 464 | /** |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 465 | * Call this function when thread tid stops to exist, such that the |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 466 | * "last owner" field can be cleared if it still refers to that thread. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 467 | */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 468 | void mutex_thread_delete(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 469 | { |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 470 | struct mutex_info* p; |
| 471 | |
| 472 | drd_clientobj_resetiter(); |
| 473 | for ( ; (p = &drd_clientobj_next(ClientMutex)->mutex) != 0; ) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 474 | { |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 475 | if (p->owner == tid && p->recursion_count > 0) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 476 | { |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 477 | MutexErrInfo MEI |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 478 | = { p->a1, p->recursion_count, p->owner }; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 479 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 480 | MutexErr, |
| 481 | VG_(get_IP)(VG_(get_running_tid)()), |
| 482 | "Mutex still locked at thread exit", |
| 483 | &MEI); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 484 | p->owner = VG_INVALID_THREADID; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 489 | ULong get_mutex_lock_count(void) |
| 490 | { |
| 491 | return s_mutex_lock_count; |
| 492 | } |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 493 | |
| 494 | |
| 495 | /* |
| 496 | * Local variables: |
| 497 | * c-basic-offset: 3 |
| 498 | * End: |
| 499 | */ |