bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
bart | 777f7fe | 2008-03-02 17:43:18 +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. |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 4 | |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
bart | 777f7fe | 2008-03-02 17:43:18 +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 | |
| 26 | #include "drd_clientobj.h" |
| 27 | #include "drd_error.h" |
| 28 | #include "drd_rwlock.h" |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 29 | #include "pub_tool_vki.h" |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 30 | #include "pub_tool_errormgr.h" // VG_(maybe_record_error)() |
| 31 | #include "pub_tool_libcassert.h" // tl_assert() |
| 32 | #include "pub_tool_libcprint.h" // VG_(message)() |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 33 | #include "pub_tool_libcproc.h" // VG_(read_millisecond_timer)() |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 34 | #include "pub_tool_machine.h" // VG_(get_IP)() |
| 35 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
| 36 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
| 37 | |
| 38 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 39 | /* Local type definitions. */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 40 | |
| 41 | struct rwlock_thread_info |
| 42 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 43 | UWord tid; // DrdThreadId. |
| 44 | UInt reader_nesting_count; |
| 45 | UInt writer_nesting_count; |
| 46 | Segment* last_unlock_segment; // Segment of last unlock call by this thread. |
| 47 | Bool last_lock_was_writer_lock; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 51 | /* Local functions. */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 52 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 53 | static void rwlock_cleanup(struct rwlock_info* p); |
| 54 | static void rwlock_delete_thread(struct rwlock_info* const p, |
| 55 | const DrdThreadId tid); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 56 | |
| 57 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 58 | /* Local variables. */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 59 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 60 | static Bool DRD_(s_trace_rwlock); |
| 61 | static UInt DRD_(s_exclusive_threshold_ms); |
| 62 | static UInt DRD_(s_shared_threshold_ms); |
| 63 | static ULong DRD_(s_rwlock_segment_creation_count); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 64 | |
| 65 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 66 | /* Function definitions. */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 67 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 68 | void DRD_(rwlock_set_trace)(const Bool trace_rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 69 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 70 | tl_assert(trace_rwlock == False || trace_rwlock == True); |
| 71 | DRD_(s_trace_rwlock) = trace_rwlock; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 72 | } |
| 73 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 74 | void DRD_(rwlock_set_exclusive_threshold)(const UInt exclusive_threshold_ms) |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 75 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 76 | DRD_(s_exclusive_threshold_ms) = exclusive_threshold_ms; |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 77 | } |
| 78 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 79 | void DRD_(rwlock_set_shared_threshold)(const UInt shared_threshold_ms) |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 80 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 81 | DRD_(s_shared_threshold_ms) = shared_threshold_ms; |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 82 | } |
| 83 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 84 | static Bool DRD_(rwlock_is_rdlocked)(struct rwlock_info* p) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 85 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 86 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 87 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 88 | VG_(OSetGen_ResetIter)(p->thread_info); |
| 89 | for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; ) |
| 90 | { |
| 91 | return q->reader_nesting_count > 0; |
| 92 | } |
| 93 | return False; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 94 | } |
| 95 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 96 | static Bool DRD_(rwlock_is_wrlocked)(struct rwlock_info* p) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 97 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 98 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 99 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 100 | VG_(OSetGen_ResetIter)(p->thread_info); |
| 101 | for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; ) |
| 102 | { |
| 103 | return q->writer_nesting_count > 0; |
| 104 | } |
| 105 | return False; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 106 | } |
| 107 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 108 | static Bool DRD_(rwlock_is_locked)(struct rwlock_info* p) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 109 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 110 | return DRD_(rwlock_is_rdlocked)(p) || DRD_(rwlock_is_wrlocked)(p); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 111 | } |
| 112 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 113 | static Bool DRD_(rwlock_is_rdlocked_by)(struct rwlock_info* p, |
| 114 | const DrdThreadId tid) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 115 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 116 | const UWord uword_tid = tid; |
| 117 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 118 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 119 | q = VG_(OSetGen_Lookup)(p->thread_info, &uword_tid); |
| 120 | return q && q->reader_nesting_count > 0; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 121 | } |
| 122 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 123 | static Bool DRD_(rwlock_is_wrlocked_by)(struct rwlock_info* p, |
| 124 | const DrdThreadId tid) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 125 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 126 | const UWord uword_tid = tid; |
| 127 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 128 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 129 | q = VG_(OSetGen_Lookup)(p->thread_info, &uword_tid); |
| 130 | return q && q->writer_nesting_count > 0; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 131 | } |
| 132 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 133 | static Bool DRD_(rwlock_is_locked_by)(struct rwlock_info* p, |
| 134 | const DrdThreadId tid) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 135 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 136 | return (DRD_(rwlock_is_rdlocked_by)(p, tid) |
| 137 | || DRD_(rwlock_is_wrlocked_by)(p, tid)); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 138 | } |
| 139 | |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 140 | /** Either look up or insert a node corresponding to DRD thread id 'tid'. */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 141 | static |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 142 | struct rwlock_thread_info* |
| 143 | DRD_(lookup_or_insert_node)(OSet* oset, const UWord tid) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 144 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 145 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 146 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 147 | q = VG_(OSetGen_Lookup)(oset, &tid); |
| 148 | if (q == 0) |
| 149 | { |
| 150 | q = VG_(OSetGen_AllocNode)(oset, sizeof(*q)); |
| 151 | q->tid = tid; |
| 152 | q->reader_nesting_count = 0; |
| 153 | q->writer_nesting_count = 0; |
| 154 | q->last_unlock_segment = 0; |
| 155 | q->last_lock_was_writer_lock = False; |
| 156 | VG_(OSetGen_Insert)(oset, q); |
| 157 | } |
| 158 | tl_assert(q); |
| 159 | return q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 160 | } |
| 161 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 162 | /** |
| 163 | * Combine the vector clock corresponding to the last unlock operation of |
| 164 | * reader-writer lock p into the vector clock of thread 'tid'. |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 165 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 166 | static void DRD_(rwlock_combine_other_vc)(struct rwlock_info* const p, |
| 167 | const DrdThreadId tid, |
| 168 | const Bool readers_too) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 169 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 170 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 171 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 172 | VG_(OSetGen_ResetIter)(p->thread_info); |
| 173 | for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; ) |
| 174 | { |
| 175 | if (q->tid != tid && (readers_too || q->last_lock_was_writer_lock)) |
| 176 | { |
| 177 | DRD_(thread_combine_vc2)(tid, &q->last_unlock_segment->vc); |
| 178 | } |
| 179 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 180 | } |
| 181 | |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 182 | /** Initialize the rwlock_info data structure *p. */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 183 | static |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 184 | void DRD_(rwlock_initialize)(struct rwlock_info* const p, const Addr rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 185 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 186 | tl_assert(rwlock != 0); |
| 187 | tl_assert(p->a1 == rwlock); |
| 188 | tl_assert(p->type == ClientRwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 189 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 190 | p->cleanup = (void(*)(DrdClientobj*))rwlock_cleanup; |
| 191 | p->delete_thread |
| 192 | = (void(*)(DrdClientobj*, DrdThreadId))rwlock_delete_thread; |
| 193 | p->thread_info = VG_(OSetGen_Create)( |
| 194 | 0, 0, VG_(malloc), "drd.rwlock.ri.1", VG_(free)); |
| 195 | p->acquiry_time_ms = 0; |
| 196 | p->acquired_at = 0; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** Deallocate the memory that was allocated by rwlock_initialize(). */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 200 | static void rwlock_cleanup(struct rwlock_info* p) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 201 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 202 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 203 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 204 | tl_assert(p); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 205 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 206 | if (DRD_(s_trace_rwlock)) |
| 207 | { |
| 208 | VG_(message)(Vg_UserMsg, |
| 209 | "[%d/%d] rwlock_destroy 0x%lx", |
| 210 | VG_(get_running_tid)(), |
| 211 | DRD_(thread_get_running_tid)(), |
| 212 | p->a1); |
| 213 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 214 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 215 | if (DRD_(rwlock_is_locked)(p)) |
| 216 | { |
| 217 | RwlockErrInfo REI = { p->a1 }; |
| 218 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 219 | RwlockErr, |
| 220 | VG_(get_IP)(VG_(get_running_tid)()), |
| 221 | "Destroying locked rwlock", |
| 222 | &REI); |
| 223 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 224 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 225 | VG_(OSetGen_ResetIter)(p->thread_info); |
| 226 | for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; ) |
| 227 | { |
| 228 | DRD_(sg_put)(q->last_unlock_segment); |
| 229 | } |
| 230 | VG_(OSetGen_Destroy)(p->thread_info); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static |
| 234 | struct rwlock_info* |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 235 | DRD_(rwlock_get_or_allocate)(const Addr rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 236 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 237 | struct rwlock_info* p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 238 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 239 | tl_assert(offsetof(DrdClientobj, rwlock) == 0); |
| 240 | p = &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock); |
| 241 | if (p) |
| 242 | { |
| 243 | return p; |
| 244 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 245 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 246 | if (DRD_(clientobj_present)(rwlock, rwlock + 1)) |
| 247 | { |
| 248 | GenericErrInfo GEI; |
| 249 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 250 | GenericErr, |
| 251 | VG_(get_IP)(VG_(get_running_tid)()), |
| 252 | "Not a reader-writer lock", |
| 253 | &GEI); |
| 254 | return 0; |
| 255 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 256 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 257 | p = &(DRD_(clientobj_add)(rwlock, ClientRwlock)->rwlock); |
| 258 | DRD_(rwlock_initialize)(p, rwlock); |
| 259 | return p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 260 | } |
| 261 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 262 | static struct rwlock_info* DRD_(rwlock_get)(const Addr rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 263 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 264 | tl_assert(offsetof(DrdClientobj, rwlock) == 0); |
| 265 | return &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** Called before pthread_rwlock_init(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 269 | struct rwlock_info* DRD_(rwlock_pre_init)(const Addr rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 270 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 271 | struct rwlock_info* p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 272 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 273 | if (DRD_(s_trace_rwlock)) |
| 274 | { |
| 275 | VG_(message)(Vg_UserMsg, |
| 276 | "[%d/%d] rwlock_init 0x%lx", |
| 277 | VG_(get_running_tid)(), |
| 278 | DRD_(thread_get_running_tid)(), |
| 279 | rwlock); |
| 280 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 281 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 282 | p = DRD_(rwlock_get)(rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 283 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 284 | if (p) |
| 285 | { |
| 286 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 287 | RwlockErrInfo REI |
| 288 | = { p->a1 }; |
| 289 | VG_(maybe_record_error)(vg_tid, |
| 290 | RwlockErr, |
| 291 | VG_(get_IP)(vg_tid), |
| 292 | "Reader-writer lock reinitialization", |
| 293 | &REI); |
| 294 | return p; |
| 295 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 296 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 297 | p = DRD_(rwlock_get_or_allocate)(rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 298 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 299 | return p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** Called after pthread_rwlock_destroy(). */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 303 | void DRD_(rwlock_post_destroy)(const Addr rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 304 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 305 | struct rwlock_info* p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 306 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 307 | p = DRD_(rwlock_get)(rwlock); |
| 308 | if (p == 0) |
| 309 | { |
| 310 | GenericErrInfo GEI; |
| 311 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 312 | GenericErr, |
| 313 | VG_(get_IP)(VG_(get_running_tid)()), |
| 314 | "Not a reader-writer lock", |
| 315 | &GEI); |
| 316 | return; |
| 317 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 318 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 319 | DRD_(clientobj_remove)(rwlock, ClientRwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 320 | } |
| 321 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 322 | /** |
| 323 | * Called before pthread_rwlock_rdlock() is invoked. If a data structure for |
| 324 | * the client-side object was not yet created, do this now. Also check whether |
| 325 | * an attempt is made to lock recursively a synchronization object that must |
| 326 | * not be locked recursively. |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 327 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 328 | void DRD_(rwlock_pre_rdlock)(const Addr rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 329 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 330 | struct rwlock_info* p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 331 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 332 | if (DRD_(s_trace_rwlock)) |
| 333 | { |
| 334 | VG_(message)(Vg_UserMsg, |
| 335 | "[%d/%d] pre_rwlock_rdlock 0x%lx", |
| 336 | VG_(get_running_tid)(), |
| 337 | DRD_(thread_get_running_tid)(), |
| 338 | rwlock); |
| 339 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 340 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 341 | p = DRD_(rwlock_get_or_allocate)(rwlock); |
| 342 | tl_assert(p); |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 343 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 344 | if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)())) |
| 345 | { |
| 346 | VG_(message)(Vg_UserMsg, |
| 347 | "reader-writer lock 0x%lx is already locked for" |
| 348 | " writing by calling thread", |
| 349 | p->a1); |
| 350 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 351 | } |
| 352 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 353 | /** |
| 354 | * Update rwlock_info state when locking the pthread_rwlock_t mutex. |
| 355 | * Note: this function must be called after pthread_rwlock_rdlock() has been |
| 356 | * called, or a race condition is triggered ! |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 357 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 358 | void DRD_(rwlock_post_rdlock)(const Addr rwlock, const Bool took_lock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 359 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 360 | const DrdThreadId drd_tid = DRD_(thread_get_running_tid)(); |
| 361 | struct rwlock_info* p; |
| 362 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 363 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 364 | if (DRD_(s_trace_rwlock)) |
| 365 | { |
| 366 | VG_(message)(Vg_UserMsg, |
| 367 | "[%d/%d] post_rwlock_rdlock 0x%lx", |
| 368 | VG_(get_running_tid)(), |
| 369 | drd_tid, |
| 370 | rwlock); |
| 371 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 372 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 373 | p = DRD_(rwlock_get)(rwlock); |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 374 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 375 | if (! p || ! took_lock) |
| 376 | return; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 377 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 378 | tl_assert(! DRD_(rwlock_is_wrlocked)(p)); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 379 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 380 | q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid); |
| 381 | if (++q->reader_nesting_count == 1) |
| 382 | { |
| 383 | DRD_(rwlock_combine_other_vc)(p, drd_tid, False); |
| 384 | q->last_lock_was_writer_lock = False; |
| 385 | DRD_(thread_new_segment)(drd_tid); |
| 386 | DRD_(s_rwlock_segment_creation_count)++; |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 387 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 388 | p->acquiry_time_ms = VG_(read_millisecond_timer)(); |
| 389 | p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0); |
| 390 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 391 | } |
| 392 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 393 | /** |
| 394 | * Called before pthread_rwlock_wrlock() is invoked. If a data structure for |
| 395 | * the client-side object was not yet created, do this now. Also check whether |
| 396 | * an attempt is made to lock recursively a synchronization object that must |
| 397 | * not be locked recursively. |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 398 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 399 | void DRD_(rwlock_pre_wrlock)(const Addr rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 400 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 401 | struct rwlock_info* p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 402 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 403 | p = DRD_(rwlock_get)(rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 404 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 405 | if (DRD_(s_trace_rwlock)) |
| 406 | { |
| 407 | VG_(message)(Vg_UserMsg, |
| 408 | "[%d/%d] pre_rwlock_wrlock 0x%lx", |
| 409 | VG_(get_running_tid)(), |
| 410 | DRD_(thread_get_running_tid)(), |
| 411 | rwlock); |
| 412 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 413 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 414 | if (p == 0) |
| 415 | { |
| 416 | p = DRD_(rwlock_get_or_allocate)(rwlock); |
| 417 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 418 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 419 | tl_assert(p); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 420 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 421 | if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)())) |
| 422 | { |
| 423 | RwlockErrInfo REI = { p->a1 }; |
| 424 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 425 | RwlockErr, |
| 426 | VG_(get_IP)(VG_(get_running_tid)()), |
| 427 | "Recursive writer locking not allowed", |
| 428 | &REI); |
| 429 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Update rwlock_info state when locking the pthread_rwlock_t rwlock. |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 434 | * Note: this function must be called after pthread_rwlock_wrlock() has |
| 435 | * finished, or a race condition is triggered ! |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 436 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 437 | void DRD_(rwlock_post_wrlock)(const Addr rwlock, const Bool took_lock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 438 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 439 | const DrdThreadId drd_tid = DRD_(thread_get_running_tid)(); |
| 440 | struct rwlock_info* p; |
| 441 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 442 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 443 | p = DRD_(rwlock_get)(rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 444 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 445 | if (DRD_(s_trace_rwlock)) |
| 446 | { |
| 447 | VG_(message)(Vg_UserMsg, |
| 448 | "[%d/%d] post_rwlock_wrlock 0x%lx", |
| 449 | VG_(get_running_tid)(), |
| 450 | drd_tid, |
| 451 | rwlock); |
| 452 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 453 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 454 | if (! p || ! took_lock) |
| 455 | return; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 456 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 457 | q = DRD_(lookup_or_insert_node)(p->thread_info, |
| 458 | DRD_(thread_get_running_tid)()); |
| 459 | tl_assert(q->writer_nesting_count == 0); |
| 460 | q->writer_nesting_count++; |
| 461 | q->last_lock_was_writer_lock = True; |
| 462 | tl_assert(q->writer_nesting_count == 1); |
| 463 | DRD_(rwlock_combine_other_vc)(p, drd_tid, True); |
| 464 | DRD_(thread_new_segment)(drd_tid); |
| 465 | DRD_(s_rwlock_segment_creation_count)++; |
| 466 | p->acquiry_time_ms = VG_(read_millisecond_timer)(); |
| 467 | p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Update rwlock_info state when unlocking the pthread_rwlock_t rwlock. |
bart | 7e6de96 | 2009-02-21 09:39:09 +0000 | [diff] [blame] | 472 | * |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 473 | * @param rwlock Pointer to pthread_rwlock_t data structure in the client space. |
bart | 7e6de96 | 2009-02-21 09:39:09 +0000 | [diff] [blame] | 474 | * |
| 475 | * @return New value of the rwlock recursion count. |
| 476 | * |
| 477 | * @note This function must be called before pthread_rwlock_unlock() is called, |
| 478 | * or a race condition is triggered ! |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 479 | */ |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 480 | void DRD_(rwlock_pre_unlock)(const Addr rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 481 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 482 | const DrdThreadId drd_tid = DRD_(thread_get_running_tid)(); |
| 483 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 484 | struct rwlock_info* p; |
| 485 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 486 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 487 | if (DRD_(s_trace_rwlock)) |
| 488 | { |
| 489 | VG_(message)(Vg_UserMsg, |
| 490 | "[%d/%d] rwlock_unlock 0x%lx", |
| 491 | vg_tid, |
| 492 | drd_tid, |
| 493 | rwlock); |
| 494 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 495 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 496 | p = DRD_(rwlock_get)(rwlock); |
| 497 | if (p == 0) |
| 498 | { |
| 499 | GenericErrInfo GEI; |
| 500 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 501 | GenericErr, |
| 502 | VG_(get_IP)(VG_(get_running_tid)()), |
| 503 | "Not a reader-writer lock", |
| 504 | &GEI); |
| 505 | return; |
| 506 | } |
| 507 | if (! DRD_(rwlock_is_locked_by)(p, drd_tid)) |
| 508 | { |
| 509 | RwlockErrInfo REI = { p->a1 }; |
| 510 | VG_(maybe_record_error)(vg_tid, |
| 511 | RwlockErr, |
| 512 | VG_(get_IP)(vg_tid), |
| 513 | "Reader-writer lock not locked by calling thread", |
| 514 | &REI); |
| 515 | return; |
| 516 | } |
| 517 | q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid); |
| 518 | tl_assert(q); |
| 519 | if (q->reader_nesting_count > 0) |
| 520 | { |
| 521 | q->reader_nesting_count--; |
| 522 | if (q->reader_nesting_count == 0 && DRD_(s_shared_threshold_ms) > 0) |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 523 | { |
bart | 430c45f | 2009-04-13 08:05:18 +0000 | [diff] [blame] | 524 | Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 525 | if (held > DRD_(s_shared_threshold_ms)) |
| 526 | { |
| 527 | HoldtimeErrInfo HEI |
| 528 | = { rwlock, p->acquired_at, held, DRD_(s_shared_threshold_ms) }; |
| 529 | VG_(maybe_record_error)(vg_tid, |
| 530 | HoldtimeErr, |
| 531 | VG_(get_IP)(vg_tid), |
| 532 | "rwlock", |
| 533 | &HEI); |
| 534 | } |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 535 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 536 | } |
| 537 | else if (q->writer_nesting_count > 0) |
| 538 | { |
| 539 | q->writer_nesting_count--; |
| 540 | if (q->writer_nesting_count == 0 && DRD_(s_exclusive_threshold_ms) > 0) |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 541 | { |
bart | 430c45f | 2009-04-13 08:05:18 +0000 | [diff] [blame] | 542 | Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 543 | if (held > DRD_(s_exclusive_threshold_ms)) |
| 544 | { |
| 545 | HoldtimeErrInfo HEI |
| 546 | = { rwlock, p->acquired_at, held, |
| 547 | DRD_(s_exclusive_threshold_ms) }; |
| 548 | VG_(maybe_record_error)(vg_tid, |
| 549 | HoldtimeErr, |
| 550 | VG_(get_IP)(vg_tid), |
| 551 | "rwlock", |
| 552 | &HEI); |
| 553 | } |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 554 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 555 | } |
| 556 | else |
| 557 | { |
| 558 | tl_assert(False); |
| 559 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 560 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 561 | if (q->reader_nesting_count == 0 && q->writer_nesting_count == 0) |
| 562 | { |
| 563 | /* This pthread_rwlock_unlock() call really unlocks the rwlock. Save */ |
| 564 | /* the current vector clock of the thread such that it is available */ |
| 565 | /* when this rwlock is locked again. */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 566 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 567 | DRD_(thread_get_latest_segment)(&q->last_unlock_segment, drd_tid); |
| 568 | DRD_(thread_new_segment)(drd_tid); |
| 569 | DRD_(s_rwlock_segment_creation_count)++; |
| 570 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Call this function when thread tid stops to exist, such that the |
| 575 | * "last owner" field can be cleared if it still refers to that thread. |
| 576 | */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 577 | static void rwlock_delete_thread(struct rwlock_info* const p, |
| 578 | const DrdThreadId tid) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 579 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 580 | struct rwlock_thread_info* q; |
| 581 | if (DRD_(rwlock_is_locked_by)(p, tid)) |
| 582 | { |
| 583 | RwlockErrInfo REI = { p->a1 }; |
| 584 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 585 | RwlockErr, |
| 586 | VG_(get_IP)(VG_(get_running_tid)()), |
| 587 | "Reader-writer lock still locked at thread exit", |
| 588 | &REI); |
| 589 | q = DRD_(lookup_or_insert_node)(p->thread_info, tid); |
| 590 | q->reader_nesting_count = 0; |
| 591 | q->writer_nesting_count = 0; |
| 592 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 593 | } |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 594 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 595 | ULong DRD_(get_rwlock_segment_creation_count)(void) |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 596 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 597 | return DRD_(s_rwlock_segment_creation_count); |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 598 | } |