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