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