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