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