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