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 | |
bart | 922304f | 2011-03-13 12:02:44 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2011 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); |
| 176 | for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; ) |
| 177 | { |
bart | b3f3340 | 2009-07-25 11:15:03 +0000 | [diff] [blame] | 178 | if (q->tid != tid) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 179 | { |
bart | b3f3340 | 2009-07-25 11:15:03 +0000 | [diff] [blame] | 180 | if (q->latest_wrlocked_segment) |
| 181 | { |
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_wrlocked_segment->vc); |
| 184 | } |
| 185 | if (readers_too && q->latest_rdlocked_segment) |
| 186 | { |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame^] | 187 | DRD_(vc_combine)(DRD_(thread_get_vc)(tid), |
bart | b3f3340 | 2009-07-25 11:15:03 +0000 | [diff] [blame] | 188 | &q->latest_rdlocked_segment->vc); |
| 189 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 192 | DRD_(thread_update_conflict_set)(tid, &old_vc); |
| 193 | DRD_(vc_cleanup)(&old_vc); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 194 | } |
| 195 | |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 196 | /** |
| 197 | * Compare the type of the rwlock specified at initialization time with |
| 198 | * the type passed as an argument, and complain if these two types do not |
| 199 | * match. |
| 200 | */ |
| 201 | static Bool drd_rwlock_check_type(struct rwlock_info* const p, |
| 202 | const RwLockT rwlock_type) |
| 203 | { |
| 204 | tl_assert(p); |
| 205 | /* The code below has to be updated if additional rwlock types are added. */ |
| 206 | tl_assert(rwlock_type == pthread_rwlock || rwlock_type == user_rwlock); |
| 207 | tl_assert(p->rwlock_type == pthread_rwlock || p->rwlock_type == user_rwlock); |
| 208 | |
| 209 | if (p->rwlock_type == rwlock_type) |
| 210 | return True; |
| 211 | |
| 212 | { |
| 213 | RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 }; |
| 214 | VG_(maybe_record_error) |
| 215 | (VG_(get_running_tid)(), |
| 216 | RwlockErr, |
| 217 | VG_(get_IP)(VG_(get_running_tid)()), |
| 218 | rwlock_type == pthread_rwlock |
| 219 | ? "Attempt to use a user-defined rwlock as a POSIX rwlock" |
| 220 | : "Attempt to use a POSIX rwlock as a user-defined rwlock", |
| 221 | &REI); |
| 222 | } |
| 223 | return False; |
| 224 | } |
| 225 | |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 226 | /** Initialize the rwlock_info data structure *p. */ |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 227 | static |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 228 | void DRD_(rwlock_initialize)(struct rwlock_info* const p, const Addr rwlock, |
| 229 | const RwLockT rwlock_type) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 230 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 231 | tl_assert(rwlock != 0); |
| 232 | tl_assert(p->a1 == rwlock); |
| 233 | tl_assert(p->type == ClientRwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 234 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 235 | p->cleanup = (void(*)(DrdClientobj*))rwlock_cleanup; |
| 236 | p->delete_thread |
| 237 | = (void(*)(DrdClientobj*, DrdThreadId))rwlock_delete_thread; |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 238 | p->rwlock_type = rwlock_type; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 239 | p->thread_info = VG_(OSetGen_Create)( |
| 240 | 0, 0, VG_(malloc), "drd.rwlock.ri.1", VG_(free)); |
| 241 | p->acquiry_time_ms = 0; |
| 242 | p->acquired_at = 0; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** Deallocate the memory that was allocated by rwlock_initialize(). */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 246 | static void rwlock_cleanup(struct rwlock_info* p) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 247 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 248 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 249 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 250 | tl_assert(p); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 251 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 252 | if (DRD_(s_trace_rwlock)) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 253 | DRD_(trace_msg)("[%d] rwlock_destroy 0x%lx", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 254 | DRD_(thread_get_running_tid)(), p->a1); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 255 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 256 | if (DRD_(rwlock_is_locked)(p)) |
| 257 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 258 | RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 259 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 260 | RwlockErr, |
| 261 | VG_(get_IP)(VG_(get_running_tid)()), |
| 262 | "Destroying locked rwlock", |
| 263 | &REI); |
| 264 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 265 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 266 | VG_(OSetGen_ResetIter)(p->thread_info); |
| 267 | for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; ) |
| 268 | { |
bart | b3f3340 | 2009-07-25 11:15:03 +0000 | [diff] [blame] | 269 | DRD_(sg_put)(q->latest_wrlocked_segment); |
| 270 | DRD_(sg_put)(q->latest_rdlocked_segment); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 271 | } |
bart | b3f3340 | 2009-07-25 11:15:03 +0000 | [diff] [blame] | 272 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 273 | VG_(OSetGen_Destroy)(p->thread_info); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static |
| 277 | struct rwlock_info* |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 278 | DRD_(rwlock_get_or_allocate)(const Addr rwlock, const RwLockT rwlock_type) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 279 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 280 | struct rwlock_info* p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 281 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 282 | tl_assert(offsetof(DrdClientobj, rwlock) == 0); |
| 283 | p = &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock); |
| 284 | if (p) |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 285 | { |
| 286 | drd_rwlock_check_type(p, rwlock_type); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 287 | return p; |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 288 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 289 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 290 | if (DRD_(clientobj_present)(rwlock, rwlock + 1)) |
| 291 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 292 | GenericErrInfo GEI = { |
| 293 | .tid = DRD_(thread_get_running_tid)(), |
| 294 | .addr = rwlock, |
| 295 | }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 296 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 297 | GenericErr, |
| 298 | VG_(get_IP)(VG_(get_running_tid)()), |
| 299 | "Not a reader-writer lock", |
| 300 | &GEI); |
| 301 | return 0; |
| 302 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 303 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 304 | p = &(DRD_(clientobj_add)(rwlock, ClientRwlock)->rwlock); |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 305 | DRD_(rwlock_initialize)(p, rwlock, rwlock_type); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 306 | return p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 307 | } |
| 308 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 309 | static struct rwlock_info* DRD_(rwlock_get)(const Addr rwlock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 310 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 311 | tl_assert(offsetof(DrdClientobj, rwlock) == 0); |
| 312 | return &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** Called before pthread_rwlock_init(). */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 316 | struct rwlock_info* DRD_(rwlock_pre_init)(const Addr rwlock, |
| 317 | const RwLockT rwlock_type) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 318 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 319 | struct rwlock_info* p; |
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 (DRD_(s_trace_rwlock)) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 322 | DRD_(trace_msg)("[%d] rwlock_init 0x%lx", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 323 | DRD_(thread_get_running_tid)(), rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 324 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 325 | p = DRD_(rwlock_get)(rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 326 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 327 | if (p) |
bart | 67707ec | 2009-07-27 17:02:52 +0000 | [diff] [blame] | 328 | drd_rwlock_check_type(p, rwlock_type); |
| 329 | |
| 330 | if (p) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 331 | { |
| 332 | const ThreadId vg_tid = VG_(get_running_tid)(); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 333 | RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 334 | VG_(maybe_record_error)(vg_tid, |
| 335 | RwlockErr, |
| 336 | VG_(get_IP)(vg_tid), |
| 337 | "Reader-writer lock reinitialization", |
| 338 | &REI); |
| 339 | return p; |
| 340 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 341 | |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 342 | p = DRD_(rwlock_get_or_allocate)(rwlock, 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 | return p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /** Called after pthread_rwlock_destroy(). */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 348 | void DRD_(rwlock_post_destroy)(const Addr rwlock, const RwLockT 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 | struct rwlock_info* p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 351 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 352 | p = DRD_(rwlock_get)(rwlock); |
| 353 | if (p == 0) |
| 354 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 355 | GenericErrInfo GEI = { |
| 356 | .tid = DRD_(thread_get_running_tid)(), |
| 357 | .addr = rwlock, |
| 358 | }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 359 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 360 | GenericErr, |
| 361 | VG_(get_IP)(VG_(get_running_tid)()), |
| 362 | "Not a reader-writer lock", |
| 363 | &GEI); |
| 364 | return; |
| 365 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 366 | |
bart | 67707ec | 2009-07-27 17:02:52 +0000 | [diff] [blame] | 367 | drd_rwlock_check_type(p, rwlock_type); |
| 368 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 369 | DRD_(clientobj_remove)(rwlock, ClientRwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 370 | } |
| 371 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 372 | /** |
| 373 | * Called before pthread_rwlock_rdlock() is invoked. If a data structure for |
| 374 | * the client-side object was not yet created, do this now. Also check whether |
| 375 | * an attempt is made to lock recursively a synchronization object that must |
| 376 | * not be locked recursively. |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 377 | */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 378 | void DRD_(rwlock_pre_rdlock)(const Addr rwlock, const RwLockT rwlock_type) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 379 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 380 | struct rwlock_info* p; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 381 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 382 | if (DRD_(s_trace_rwlock)) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 383 | DRD_(trace_msg)("[%d] pre_rwlock_rdlock 0x%lx", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 384 | DRD_(thread_get_running_tid)(), rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 385 | |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 386 | p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 387 | tl_assert(p); |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 388 | |
bart | 74b2d97 | 2011-10-08 08:54:57 +0000 | [diff] [blame] | 389 | if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)())) { |
| 390 | RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 }; |
| 391 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 392 | RwlockErr, |
| 393 | VG_(get_IP)(VG_(get_running_tid)()), |
| 394 | "Already locked for writing by calling thread", |
| 395 | &REI); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 396 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 397 | } |
| 398 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 399 | /** |
| 400 | * Update rwlock_info state when locking the pthread_rwlock_t mutex. |
| 401 | * Note: this function must be called after pthread_rwlock_rdlock() has been |
| 402 | * called, or a race condition is triggered ! |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 403 | */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 404 | void DRD_(rwlock_post_rdlock)(const Addr rwlock, const RwLockT rwlock_type, |
| 405 | const Bool took_lock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 406 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 407 | const DrdThreadId drd_tid = DRD_(thread_get_running_tid)(); |
| 408 | struct rwlock_info* p; |
| 409 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 410 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 411 | if (DRD_(s_trace_rwlock)) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 412 | DRD_(trace_msg)("[%d] post_rwlock_rdlock 0x%lx", drd_tid, rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 413 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 414 | p = DRD_(rwlock_get)(rwlock); |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 415 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 416 | if (! p || ! took_lock) |
| 417 | return; |
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(! DRD_(rwlock_is_wrlocked)(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 | q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid); |
| 422 | if (++q->reader_nesting_count == 1) |
| 423 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 424 | DRD_(thread_new_segment)(drd_tid); |
| 425 | DRD_(s_rwlock_segment_creation_count)++; |
bart | 7627be3 | 2009-06-06 12:26:05 +0000 | [diff] [blame] | 426 | DRD_(rwlock_combine_other_vc)(p, drd_tid, False); |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 427 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 428 | p->acquiry_time_ms = VG_(read_millisecond_timer)(); |
| 429 | p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0); |
| 430 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 431 | } |
| 432 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 433 | /** |
| 434 | * Called before pthread_rwlock_wrlock() is invoked. If a data structure for |
| 435 | * the client-side object was not yet created, do this now. Also check whether |
| 436 | * an attempt is made to lock recursively a synchronization object that must |
| 437 | * not be locked recursively. |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 438 | */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 439 | void DRD_(rwlock_pre_wrlock)(const Addr rwlock, const RwLockT rwlock_type) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 440 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 441 | struct rwlock_info* p; |
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)) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 446 | DRD_(trace_msg)("[%d] pre_rwlock_wrlock 0x%lx", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 447 | DRD_(thread_get_running_tid)(), rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 448 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 449 | if (p == 0) |
bart | c844150 | 2009-07-27 16:03:51 +0000 | [diff] [blame] | 450 | p = DRD_(rwlock_get_or_allocate)(rwlock, 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 | tl_assert(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 | if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)())) |
| 455 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 456 | RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 457 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 458 | RwlockErr, |
| 459 | VG_(get_IP)(VG_(get_running_tid)()), |
| 460 | "Recursive writer locking not allowed", |
| 461 | &REI); |
| 462 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Update rwlock_info state when locking the pthread_rwlock_t rwlock. |
bart | 165b90f | 2008-05-10 12:54:27 +0000 | [diff] [blame] | 467 | * Note: this function must be called after pthread_rwlock_wrlock() has |
| 468 | * finished, or a race condition is triggered ! |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 469 | */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 470 | void DRD_(rwlock_post_wrlock)(const Addr rwlock, const RwLockT rwlock_type, |
| 471 | const Bool took_lock) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 472 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 473 | const DrdThreadId drd_tid = DRD_(thread_get_running_tid)(); |
| 474 | struct rwlock_info* p; |
| 475 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 476 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 477 | p = DRD_(rwlock_get)(rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 478 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 479 | if (DRD_(s_trace_rwlock)) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 480 | DRD_(trace_msg)("[%d] post_rwlock_wrlock 0x%lx", drd_tid, 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 | if (! p || ! took_lock) |
| 483 | return; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 484 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 485 | q = DRD_(lookup_or_insert_node)(p->thread_info, |
| 486 | DRD_(thread_get_running_tid)()); |
| 487 | tl_assert(q->writer_nesting_count == 0); |
| 488 | q->writer_nesting_count++; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 489 | tl_assert(q->writer_nesting_count == 1); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 490 | DRD_(thread_new_segment)(drd_tid); |
| 491 | DRD_(s_rwlock_segment_creation_count)++; |
bart | 7627be3 | 2009-06-06 12:26:05 +0000 | [diff] [blame] | 492 | DRD_(rwlock_combine_other_vc)(p, drd_tid, True); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 493 | p->acquiry_time_ms = VG_(read_millisecond_timer)(); |
| 494 | p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Update rwlock_info state when unlocking the pthread_rwlock_t rwlock. |
bart | 7e6de96 | 2009-02-21 09:39:09 +0000 | [diff] [blame] | 499 | * |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 500 | * @param rwlock Pointer to pthread_rwlock_t data structure in the client space. |
bart | 7e6de96 | 2009-02-21 09:39:09 +0000 | [diff] [blame] | 501 | * |
| 502 | * @return New value of the rwlock recursion count. |
| 503 | * |
| 504 | * @note This function must be called before pthread_rwlock_unlock() is called, |
| 505 | * or a race condition is triggered ! |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 506 | */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 507 | void DRD_(rwlock_pre_unlock)(const Addr rwlock, const RwLockT rwlock_type) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 508 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 509 | const DrdThreadId drd_tid = DRD_(thread_get_running_tid)(); |
| 510 | const ThreadId vg_tid = VG_(get_running_tid)(); |
| 511 | struct rwlock_info* p; |
| 512 | struct rwlock_thread_info* q; |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 513 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 514 | if (DRD_(s_trace_rwlock)) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 515 | DRD_(trace_msg)("[%d] rwlock_unlock 0x%lx", drd_tid, rwlock); |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 516 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 517 | p = DRD_(rwlock_get)(rwlock); |
| 518 | if (p == 0) |
| 519 | { |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 520 | GenericErrInfo GEI = { |
| 521 | .tid = DRD_(thread_get_running_tid)(), |
| 522 | .addr = rwlock, |
| 523 | }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 524 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 525 | GenericErr, |
| 526 | VG_(get_IP)(VG_(get_running_tid)()), |
| 527 | "Not a reader-writer lock", |
| 528 | &GEI); |
| 529 | return; |
| 530 | } |
bart | 67707ec | 2009-07-27 17:02:52 +0000 | [diff] [blame] | 531 | |
| 532 | drd_rwlock_check_type(p, rwlock_type); |
| 533 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 534 | if (! DRD_(rwlock_is_locked_by)(p, drd_tid)) |
| 535 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 536 | RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 537 | VG_(maybe_record_error)(vg_tid, |
| 538 | RwlockErr, |
| 539 | VG_(get_IP)(vg_tid), |
| 540 | "Reader-writer lock not locked by calling thread", |
| 541 | &REI); |
| 542 | return; |
| 543 | } |
| 544 | q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid); |
| 545 | tl_assert(q); |
| 546 | if (q->reader_nesting_count > 0) |
| 547 | { |
| 548 | q->reader_nesting_count--; |
| 549 | if (q->reader_nesting_count == 0 && DRD_(s_shared_threshold_ms) > 0) |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 550 | { |
bart | 430c45f | 2009-04-13 08:05:18 +0000 | [diff] [blame] | 551 | Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 552 | if (held > DRD_(s_shared_threshold_ms)) |
| 553 | { |
| 554 | HoldtimeErrInfo HEI |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 555 | = { DRD_(thread_get_running_tid)(), |
| 556 | rwlock, p->acquired_at, held, DRD_(s_shared_threshold_ms) }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 557 | VG_(maybe_record_error)(vg_tid, |
| 558 | HoldtimeErr, |
| 559 | VG_(get_IP)(vg_tid), |
| 560 | "rwlock", |
| 561 | &HEI); |
| 562 | } |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 563 | } |
bart | b3f3340 | 2009-07-25 11:15:03 +0000 | [diff] [blame] | 564 | if (q->reader_nesting_count == 0 && q->writer_nesting_count == 0) |
| 565 | { |
| 566 | /* |
| 567 | * This pthread_rwlock_unlock() call really unlocks the rwlock. Save |
| 568 | * the current vector clock of the thread such that it is available |
| 569 | * when this rwlock is locked again. |
| 570 | */ |
| 571 | DRD_(thread_get_latest_segment)(&q->latest_rdlocked_segment, drd_tid); |
| 572 | DRD_(thread_new_segment)(drd_tid); |
| 573 | DRD_(s_rwlock_segment_creation_count)++; |
| 574 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 575 | } |
| 576 | else if (q->writer_nesting_count > 0) |
| 577 | { |
| 578 | q->writer_nesting_count--; |
| 579 | if (q->writer_nesting_count == 0 && DRD_(s_exclusive_threshold_ms) > 0) |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 580 | { |
bart | 430c45f | 2009-04-13 08:05:18 +0000 | [diff] [blame] | 581 | Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 582 | if (held > DRD_(s_exclusive_threshold_ms)) |
| 583 | { |
| 584 | HoldtimeErrInfo HEI |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 585 | = { DRD_(thread_get_running_tid)(), |
| 586 | rwlock, p->acquired_at, held, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 587 | DRD_(s_exclusive_threshold_ms) }; |
| 588 | VG_(maybe_record_error)(vg_tid, |
| 589 | HoldtimeErr, |
| 590 | VG_(get_IP)(vg_tid), |
| 591 | "rwlock", |
| 592 | &HEI); |
| 593 | } |
bart | 9d5b796 | 2008-05-14 12:25:00 +0000 | [diff] [blame] | 594 | } |
bart | b3f3340 | 2009-07-25 11:15:03 +0000 | [diff] [blame] | 595 | if (q->reader_nesting_count == 0 && q->writer_nesting_count == 0) |
| 596 | { |
| 597 | /* |
| 598 | * This pthread_rwlock_unlock() call really unlocks the rwlock. Save |
| 599 | * the current vector clock of the thread such that it is available |
| 600 | * when this rwlock is locked again. |
| 601 | */ |
| 602 | DRD_(thread_get_latest_segment)(&q->latest_wrlocked_segment, drd_tid); |
| 603 | DRD_(thread_new_segment)(drd_tid); |
| 604 | DRD_(s_rwlock_segment_creation_count)++; |
| 605 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 606 | } |
| 607 | else |
| 608 | { |
| 609 | tl_assert(False); |
| 610 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 611 | } |
| 612 | |
bart | b3f3340 | 2009-07-25 11:15:03 +0000 | [diff] [blame] | 613 | /** Called when thread tid stops to exist. */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 614 | static void rwlock_delete_thread(struct rwlock_info* const p, |
| 615 | const DrdThreadId tid) |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 616 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 617 | struct rwlock_thread_info* q; |
bart | b3f3340 | 2009-07-25 11:15:03 +0000 | [diff] [blame] | 618 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 619 | if (DRD_(rwlock_is_locked_by)(p, tid)) |
| 620 | { |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 621 | RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 }; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 622 | VG_(maybe_record_error)(VG_(get_running_tid)(), |
| 623 | RwlockErr, |
| 624 | VG_(get_IP)(VG_(get_running_tid)()), |
| 625 | "Reader-writer lock still locked at thread exit", |
| 626 | &REI); |
| 627 | q = DRD_(lookup_or_insert_node)(p->thread_info, tid); |
| 628 | q->reader_nesting_count = 0; |
| 629 | q->writer_nesting_count = 0; |
| 630 | } |
bart | 777f7fe | 2008-03-02 17:43:18 +0000 | [diff] [blame] | 631 | } |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 632 | |
bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 633 | ULong DRD_(get_rwlock_segment_creation_count)(void) |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 634 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 635 | return DRD_(s_rwlock_segment_creation_count); |
bart | 6bbefaf | 2008-04-19 15:16:45 +0000 | [diff] [blame] | 636 | } |