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