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