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