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