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 | |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 140 | /** Called before pthread_mutex_init(). */ |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 141 | struct mutex_info* |
| 142 | mutex_init(const Addr mutex, const SizeT size, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 143 | { |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 144 | struct mutex_info* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 145 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 146 | if (s_trace_mutex) |
| 147 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 148 | VG_(message)(Vg_UserMsg, |
| 149 | "[%d/%d] mutex_init %s 0x%lx", |
| 150 | VG_(get_running_tid)(), |
| 151 | thread_get_running_tid(), |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 152 | mutex_type_name(mutex_type), |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 153 | mutex); |
| 154 | } |
| 155 | |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 156 | if (mutex_type == mutex_type_invalid_mutex) |
| 157 | { |
| 158 | GenericErrInfo GEI; |
| 159 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 160 | GenericErr, |
| 161 | VG_(get_IP)(VG_(get_running_tid)()), |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 162 | "Not a mutex", |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 163 | &GEI); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | p = mutex_get(mutex); |
| 168 | if (p) |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 169 | { |
| 170 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 171 | MutexErrInfo MEI |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 172 | = { p->a1, p->recursion_count, p->owner }; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 173 | VG_(maybe_record_error)(vg_tid, |
| 174 | MutexErr, |
| 175 | VG_(get_IP)(vg_tid), |
| 176 | "Mutex reinitialization", |
| 177 | &MEI); |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 178 | return p; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 179 | } |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 180 | p = mutex_get_or_allocate(mutex, size, mutex_type); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 181 | |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 182 | return p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 183 | } |
| 184 | |
bart | 46d5f17 | 2008-02-28 19:49:37 +0000 | [diff] [blame] | 185 | /** Called after pthread_mutex_destroy(). */ |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 186 | void mutex_post_destroy(const Addr mutex) |
| 187 | { |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 188 | struct mutex_info* p; |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 189 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 190 | p = mutex_get(mutex); |
| 191 | if (p == 0) |
| 192 | { |
| 193 | GenericErrInfo GEI; |
| 194 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 195 | GenericErr, |
| 196 | VG_(get_IP)(VG_(get_running_tid)()), |
| 197 | "Not a mutex", |
| 198 | &GEI); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | clientobj_remove(mutex, ClientMutex); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 203 | } |
| 204 | |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 205 | /** Called before pthread_mutex_lock() is invoked. If a data structure for |
| 206 | * the client-side object was not yet created, do this now. Also check whether |
| 207 | * an attempt is made to lock recursively a synchronization object that must |
| 208 | * not be locked recursively. |
| 209 | */ |
| 210 | void mutex_pre_lock(const Addr mutex, const SizeT size, MutexT mutex_type) |
| 211 | { |
bart | 635cb16 | 2008-02-28 08:30:43 +0000 | [diff] [blame] | 212 | struct mutex_info* p; |
| 213 | |
| 214 | p = mutex_get(mutex); |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 215 | |
| 216 | if (s_trace_mutex) |
| 217 | { |
| 218 | VG_(message)(Vg_UserMsg, |
| 219 | "[%d/%d] pre_mutex_lock %s 0x%lx rc %d owner %d", |
| 220 | VG_(get_running_tid)(), |
| 221 | thread_get_running_tid(), |
| 222 | p ? mutex_get_typename(p) : "(?)", |
| 223 | mutex, |
| 224 | p ? p->recursion_count : 0, |
| 225 | p ? p->owner : VG_INVALID_THREADID); |
| 226 | } |
| 227 | |
| 228 | if (mutex_type == mutex_type_invalid_mutex) |
| 229 | { |
| 230 | GenericErrInfo GEI; |
| 231 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 232 | GenericErr, |
| 233 | VG_(get_IP)(VG_(get_running_tid)()), |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 234 | "Not a mutex", |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 235 | &GEI); |
| 236 | return; |
| 237 | } |
| 238 | |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 239 | if (p == 0) |
| 240 | { |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 241 | p = mutex_init(mutex, size, mutex_type); |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 242 | } |
bart | 635cb16 | 2008-02-28 08:30:43 +0000 | [diff] [blame] | 243 | |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 244 | tl_assert(p); |
| 245 | |
| 246 | if (p->owner == thread_get_running_tid() |
| 247 | && p->recursion_count >= 1 |
| 248 | && mutex_type != mutex_type_recursive_mutex) |
| 249 | { |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 250 | MutexErrInfo MEI = { p->a1, p->recursion_count, p->owner }; |
bart | 8bba1f7 | 2008-02-27 16:13:05 +0000 | [diff] [blame] | 251 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 252 | MutexErr, |
| 253 | VG_(get_IP)(VG_(get_running_tid)()), |
| 254 | "Recursive locking not allowed", |
| 255 | &MEI); |
| 256 | } |
| 257 | } |
| 258 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 259 | /** |
| 260 | * Update mutex_info state when locking the pthread_mutex_t mutex. |
| 261 | * Note: this function must be called after pthread_mutex_lock() has been |
| 262 | * called, or a race condition is triggered ! |
| 263 | */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 264 | void mutex_post_lock(const Addr mutex, const Bool took_lock) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 265 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 266 | const DrdThreadId drd_tid = thread_get_running_tid(); |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 267 | struct mutex_info* p; |
| 268 | |
| 269 | p = mutex_get(mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 270 | |
| 271 | if (s_trace_mutex) |
| 272 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 273 | VG_(message)(Vg_UserMsg, |
| 274 | "[%d/%d] post_mutex_lock %s 0x%lx rc %d owner %d", |
| 275 | VG_(get_running_tid)(), |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 276 | drd_tid, |
bart | 0034464 | 2008-03-01 15:27:41 +0000 | [diff] [blame] | 277 | p ? mutex_get_typename(p) : "(?)", |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 278 | mutex, |
| 279 | p ? p->recursion_count : 0, |
| 280 | p ? p->owner : VG_INVALID_THREADID); |
| 281 | } |
| 282 | |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 283 | if (! p || ! took_lock) |
| 284 | return; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 285 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 286 | if (p->recursion_count == 0) |
| 287 | { |
| 288 | p->owner = drd_tid; |
| 289 | s_mutex_lock_count++; |
| 290 | } |
| 291 | else if (p->owner != drd_tid) |
| 292 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 293 | VG_(message)(Vg_UserMsg, |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 294 | "The impossible happened: mutex 0x%lx is locked" |
| 295 | " simultaneously by two threads (recursion count %d," |
| 296 | " owners %d and %d) !", |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 297 | p->a1, p->recursion_count, p->owner, drd_tid); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 298 | p->owner = drd_tid; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 299 | } |
| 300 | p->recursion_count++; |
| 301 | |
| 302 | if (p->recursion_count == 1) |
| 303 | { |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 304 | const DrdThreadId last_owner = p->owner; |
| 305 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 306 | if (last_owner != drd_tid && last_owner != DRD_INVALID_THREADID) |
| 307 | thread_combine_vc2(drd_tid, mutex_get_last_vc(mutex)); |
| 308 | thread_new_segment(drd_tid); |
| 309 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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 ! |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 316 | * @return New value of the mutex recursion count. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 317 | * @param mutex Pointer to pthread_mutex_t data structure in the client space. |
| 318 | * @param tid ThreadId of the thread calling pthread_mutex_unlock(). |
| 319 | * @param vc Pointer to the current vector clock of thread tid. |
| 320 | */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 321 | void mutex_unlock(const Addr mutex, const MutexT mutex_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 322 | { |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 323 | const DrdThreadId drd_tid = thread_get_running_tid(); |
| 324 | const ThreadId vg_tid = VG_(get_running_tid)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 325 | const VectorClock* const vc = thread_get_vc(drd_tid); |
| 326 | struct mutex_info* const p = mutex_get(mutex); |
| 327 | |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 328 | if (s_trace_mutex) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 329 | { |
bart | 3b1ee45 | 2008-02-29 19:28:15 +0000 | [diff] [blame] | 330 | VG_(message)(Vg_UserMsg, |
| 331 | "[%d/%d] mutex_unlock %s 0x%lx rc %d", |
| 332 | vg_tid, |
| 333 | drd_tid, |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 334 | p ? mutex_get_typename(p) : "?", |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 335 | mutex, |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 336 | p ? p->recursion_count : 0, |
| 337 | p ? p->owner : 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 338 | } |
| 339 | |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 340 | if (p == 0 || mutex_type == mutex_type_invalid_mutex) |
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 | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 348 | return; |
bart | ab7a644 | 2008-02-25 19:46:14 +0000 | [diff] [blame] | 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); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 359 | return; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 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 | |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 371 | if (p->owner != drd_tid || p->recursion_count <= 0) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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), |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 377 | "Mutex not locked by calling thread", |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 378 | &MEI); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 379 | return; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 380 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 381 | tl_assert(p->recursion_count > 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 382 | p->recursion_count--; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame^] | 383 | tl_assert(p->recursion_count >= 0); |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 384 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 385 | if (p->recursion_count == 0) |
| 386 | { |
| 387 | /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */ |
| 388 | /* current vector clock of the thread such that it is available when */ |
| 389 | /* this mutex is locked again. */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 390 | vc_assign(&p->vc, vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 391 | |
| 392 | thread_new_segment(drd_tid); |
| 393 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | const char* mutex_get_typename(struct mutex_info* const p) |
| 397 | { |
| 398 | tl_assert(p); |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 399 | |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 400 | return mutex_type_name(p->mutex_type); |
| 401 | } |
| 402 | |
| 403 | const char* mutex_type_name(const MutexT mt) |
| 404 | { |
| 405 | switch (mt) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 406 | { |
bart | 635cb16 | 2008-02-28 08:30:43 +0000 | [diff] [blame] | 407 | case mutex_type_invalid_mutex: |
| 408 | return "invalid mutex"; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 409 | case mutex_type_recursive_mutex: |
| 410 | return "recursive mutex"; |
| 411 | case mutex_type_errorcheck_mutex: |
| 412 | return "error checking mutex"; |
| 413 | case mutex_type_default_mutex: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 414 | return "mutex"; |
sewardj | 721ad7b | 2007-11-30 08:30:29 +0000 | [diff] [blame] | 415 | case mutex_type_spinlock: |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 416 | return "spinlock"; |
| 417 | default: |
| 418 | tl_assert(0); |
| 419 | } |
| 420 | return "?"; |
| 421 | } |
| 422 | |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 423 | /** Return true if the specified mutex is locked by any thread. */ |
| 424 | static Bool mutex_is_locked(struct mutex_info* const p) |
| 425 | { |
| 426 | tl_assert(p); |
| 427 | return (p->recursion_count > 0); |
| 428 | } |
| 429 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 430 | Bool mutex_is_locked_by(const Addr mutex, const DrdThreadId tid) |
| 431 | { |
| 432 | struct mutex_info* const p = mutex_get(mutex); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 433 | if (p) |
| 434 | { |
| 435 | return (p->recursion_count > 0 && p->owner == tid); |
| 436 | } |
| 437 | return False; |
| 438 | } |
| 439 | |
| 440 | const VectorClock* mutex_get_last_vc(const Addr mutex) |
| 441 | { |
| 442 | struct mutex_info* const p = mutex_get(mutex); |
| 443 | return p ? &p->vc : 0; |
| 444 | } |
| 445 | |
| 446 | int mutex_get_recursion_count(const Addr mutex) |
| 447 | { |
| 448 | struct mutex_info* const p = mutex_get(mutex); |
| 449 | tl_assert(p); |
| 450 | return p->recursion_count; |
| 451 | } |
| 452 | |
| 453 | /** |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 454 | * Call this function when thread tid stops to exist, such that the |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 455 | * "last owner" field can be cleared if it still refers to that thread. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 456 | */ |
bart | 301c311 | 2008-02-24 18:22:37 +0000 | [diff] [blame] | 457 | void mutex_thread_delete(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 458 | { |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 459 | struct mutex_info* p; |
| 460 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 461 | clientobj_resetiter(); |
| 462 | for ( ; (p = &clientobj_next(ClientMutex)->mutex) != 0; ) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 463 | { |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 464 | if (p->owner == tid && p->recursion_count > 0) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 465 | { |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 466 | MutexErrInfo MEI |
bart | 4bb53d8 | 2008-02-28 19:06:34 +0000 | [diff] [blame] | 467 | = { p->a1, p->recursion_count, p->owner }; |
bart | 5357fcb | 2008-02-27 15:46:00 +0000 | [diff] [blame] | 468 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 469 | MutexErr, |
| 470 | VG_(get_IP)(VG_(get_running_tid)()), |
| 471 | "Mutex still locked at thread exit", |
| 472 | &MEI); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 473 | p->owner = VG_INVALID_THREADID; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 478 | ULong get_mutex_lock_count(void) |
| 479 | { |
| 480 | return s_mutex_lock_count; |
| 481 | } |
sewardj | 347eeba | 2008-01-21 14:19:07 +0000 | [diff] [blame] | 482 | |
| 483 | |
| 484 | /* |
| 485 | * Local variables: |
| 486 | * c-basic-offset: 3 |
| 487 | * End: |
| 488 | */ |