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