blob: 8e13c66112a8740239e54c42b70ab4da03df824d [file] [log] [blame]
bart922304f2011-03-13 12:02:44 +00001/* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
bart777f7fe2008-03-02 17:43:18 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
bart777f7fe2008-03-02 17:43:18 +00004
bart922304f2011-03-13 12:02:44 +00005 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
bart777f7fe2008-03-02 17:43:18 +00006
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"
bart9d5b7962008-05-14 12:25:00 +000029#include "pub_tool_vki.h"
bart777f7fe2008-03-02 17:43:18 +000030#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)()
bart9d5b7962008-05-14 12:25:00 +000033#include "pub_tool_libcproc.h" // VG_(read_millisecond_timer)()
bart777f7fe2008-03-02 17:43:18 +000034#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
bartdc1ef032009-02-15 14:18:02 +000039/* Local type definitions. */
bart777f7fe2008-03-02 17:43:18 +000040
41struct rwlock_thread_info
42{
bartbedfd232009-03-26 19:07:15 +000043 UWord tid; // DrdThreadId.
44 UInt reader_nesting_count;
45 UInt writer_nesting_count;
bartb3f33402009-07-25 11:15:03 +000046 // 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;
bart777f7fe2008-03-02 17:43:18 +000050};
51
52
bartdc1ef032009-02-15 14:18:02 +000053/* Local functions. */
bart777f7fe2008-03-02 17:43:18 +000054
bartd2c5eae2009-02-21 15:27:04 +000055static void rwlock_cleanup(struct rwlock_info* p);
56static void rwlock_delete_thread(struct rwlock_info* const p,
57 const DrdThreadId tid);
bart777f7fe2008-03-02 17:43:18 +000058
59
bartdc1ef032009-02-15 14:18:02 +000060/* Local variables. */
bart777f7fe2008-03-02 17:43:18 +000061
bartdc1ef032009-02-15 14:18:02 +000062static Bool DRD_(s_trace_rwlock);
63static UInt DRD_(s_exclusive_threshold_ms);
64static UInt DRD_(s_shared_threshold_ms);
65static ULong DRD_(s_rwlock_segment_creation_count);
bart777f7fe2008-03-02 17:43:18 +000066
67
bartdc1ef032009-02-15 14:18:02 +000068/* Function definitions. */
bart777f7fe2008-03-02 17:43:18 +000069
bartdc1ef032009-02-15 14:18:02 +000070void DRD_(rwlock_set_trace)(const Bool trace_rwlock)
bart777f7fe2008-03-02 17:43:18 +000071{
bartbedfd232009-03-26 19:07:15 +000072 tl_assert(trace_rwlock == False || trace_rwlock == True);
73 DRD_(s_trace_rwlock) = trace_rwlock;
bart777f7fe2008-03-02 17:43:18 +000074}
75
bartdc1ef032009-02-15 14:18:02 +000076void DRD_(rwlock_set_exclusive_threshold)(const UInt exclusive_threshold_ms)
bart9d5b7962008-05-14 12:25:00 +000077{
bartbedfd232009-03-26 19:07:15 +000078 DRD_(s_exclusive_threshold_ms) = exclusive_threshold_ms;
bart9d5b7962008-05-14 12:25:00 +000079}
80
bartdc1ef032009-02-15 14:18:02 +000081void DRD_(rwlock_set_shared_threshold)(const UInt shared_threshold_ms)
bart9d5b7962008-05-14 12:25:00 +000082{
bartbedfd232009-03-26 19:07:15 +000083 DRD_(s_shared_threshold_ms) = shared_threshold_ms;
bart9d5b7962008-05-14 12:25:00 +000084}
85
bartdc1ef032009-02-15 14:18:02 +000086static Bool DRD_(rwlock_is_rdlocked)(struct rwlock_info* p)
bart777f7fe2008-03-02 17:43:18 +000087{
bartbedfd232009-03-26 19:07:15 +000088 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +000089
bartbedfd232009-03-26 19:07:15 +000090 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;
bart777f7fe2008-03-02 17:43:18 +000096}
97
bartdc1ef032009-02-15 14:18:02 +000098static Bool DRD_(rwlock_is_wrlocked)(struct rwlock_info* p)
bart777f7fe2008-03-02 17:43:18 +000099{
bartbedfd232009-03-26 19:07:15 +0000100 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000101
bartbedfd232009-03-26 19:07:15 +0000102 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;
bart777f7fe2008-03-02 17:43:18 +0000108}
109
bartdc1ef032009-02-15 14:18:02 +0000110static Bool DRD_(rwlock_is_locked)(struct rwlock_info* p)
bart777f7fe2008-03-02 17:43:18 +0000111{
bartbedfd232009-03-26 19:07:15 +0000112 return DRD_(rwlock_is_rdlocked)(p) || DRD_(rwlock_is_wrlocked)(p);
bart777f7fe2008-03-02 17:43:18 +0000113}
114
bartdc1ef032009-02-15 14:18:02 +0000115static Bool DRD_(rwlock_is_rdlocked_by)(struct rwlock_info* p,
116 const DrdThreadId tid)
bart777f7fe2008-03-02 17:43:18 +0000117{
bartbedfd232009-03-26 19:07:15 +0000118 const UWord uword_tid = tid;
119 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000120
bartbedfd232009-03-26 19:07:15 +0000121 q = VG_(OSetGen_Lookup)(p->thread_info, &uword_tid);
122 return q && q->reader_nesting_count > 0;
bart777f7fe2008-03-02 17:43:18 +0000123}
124
bartdc1ef032009-02-15 14:18:02 +0000125static Bool DRD_(rwlock_is_wrlocked_by)(struct rwlock_info* p,
126 const DrdThreadId tid)
bart777f7fe2008-03-02 17:43:18 +0000127{
bartbedfd232009-03-26 19:07:15 +0000128 const UWord uword_tid = tid;
129 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000130
bartbedfd232009-03-26 19:07:15 +0000131 q = VG_(OSetGen_Lookup)(p->thread_info, &uword_tid);
132 return q && q->writer_nesting_count > 0;
bart777f7fe2008-03-02 17:43:18 +0000133}
134
bartdc1ef032009-02-15 14:18:02 +0000135static Bool DRD_(rwlock_is_locked_by)(struct rwlock_info* p,
136 const DrdThreadId tid)
bart777f7fe2008-03-02 17:43:18 +0000137{
bartbedfd232009-03-26 19:07:15 +0000138 return (DRD_(rwlock_is_rdlocked_by)(p, tid)
139 || DRD_(rwlock_is_wrlocked_by)(p, tid));
bart777f7fe2008-03-02 17:43:18 +0000140}
141
bart165b90f2008-05-10 12:54:27 +0000142/** Either look up or insert a node corresponding to DRD thread id 'tid'. */
bart777f7fe2008-03-02 17:43:18 +0000143static
bartdc1ef032009-02-15 14:18:02 +0000144struct rwlock_thread_info*
145DRD_(lookup_or_insert_node)(OSet* oset, const UWord tid)
bart777f7fe2008-03-02 17:43:18 +0000146{
bartbedfd232009-03-26 19:07:15 +0000147 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000148
bartbedfd232009-03-26 19:07:15 +0000149 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;
bartb3f33402009-07-25 11:15:03 +0000156 q->latest_wrlocked_segment = 0;
157 q->latest_rdlocked_segment = 0;
bartbedfd232009-03-26 19:07:15 +0000158 VG_(OSetGen_Insert)(oset, q);
159 }
160 tl_assert(q);
161 return q;
bart777f7fe2008-03-02 17:43:18 +0000162}
163
bartdc1ef032009-02-15 14:18:02 +0000164/**
165 * Combine the vector clock corresponding to the last unlock operation of
166 * reader-writer lock p into the vector clock of thread 'tid'.
bart165b90f2008-05-10 12:54:27 +0000167 */
bartdc1ef032009-02-15 14:18:02 +0000168static void DRD_(rwlock_combine_other_vc)(struct rwlock_info* const p,
169 const DrdThreadId tid,
170 const Bool readers_too)
bart777f7fe2008-03-02 17:43:18 +0000171{
bartbedfd232009-03-26 19:07:15 +0000172 struct rwlock_thread_info* q;
bart8f822af2009-06-08 18:20:42 +0000173 VectorClock old_vc;
bart777f7fe2008-03-02 17:43:18 +0000174
bart8f822af2009-06-08 18:20:42 +0000175 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
bartbedfd232009-03-26 19:07:15 +0000176 VG_(OSetGen_ResetIter)(p->thread_info);
177 for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; )
178 {
bartb3f33402009-07-25 11:15:03 +0000179 if (q->tid != tid)
bartbedfd232009-03-26 19:07:15 +0000180 {
bartb3f33402009-07-25 11:15:03 +0000181 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 }
bartbedfd232009-03-26 19:07:15 +0000191 }
192 }
bart8f822af2009-06-08 18:20:42 +0000193 DRD_(thread_update_conflict_set)(tid, &old_vc);
194 DRD_(vc_cleanup)(&old_vc);
bart777f7fe2008-03-02 17:43:18 +0000195}
196
bartc8441502009-07-27 16:03:51 +0000197/**
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 */
202static 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
bart165b90f2008-05-10 12:54:27 +0000227/** Initialize the rwlock_info data structure *p. */
bart777f7fe2008-03-02 17:43:18 +0000228static
bartc8441502009-07-27 16:03:51 +0000229void DRD_(rwlock_initialize)(struct rwlock_info* const p, const Addr rwlock,
230 const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000231{
bartbedfd232009-03-26 19:07:15 +0000232 tl_assert(rwlock != 0);
233 tl_assert(p->a1 == rwlock);
234 tl_assert(p->type == ClientRwlock);
bart777f7fe2008-03-02 17:43:18 +0000235
bartbedfd232009-03-26 19:07:15 +0000236 p->cleanup = (void(*)(DrdClientobj*))rwlock_cleanup;
237 p->delete_thread
238 = (void(*)(DrdClientobj*, DrdThreadId))rwlock_delete_thread;
bartc8441502009-07-27 16:03:51 +0000239 p->rwlock_type = rwlock_type;
bartbedfd232009-03-26 19:07:15 +0000240 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;
bart777f7fe2008-03-02 17:43:18 +0000244}
245
246/** Deallocate the memory that was allocated by rwlock_initialize(). */
bartd2c5eae2009-02-21 15:27:04 +0000247static void rwlock_cleanup(struct rwlock_info* p)
bart777f7fe2008-03-02 17:43:18 +0000248{
bartbedfd232009-03-26 19:07:15 +0000249 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000250
bartbedfd232009-03-26 19:07:15 +0000251 tl_assert(p);
bart777f7fe2008-03-02 17:43:18 +0000252
bartbedfd232009-03-26 19:07:15 +0000253 if (DRD_(s_trace_rwlock))
bartad994e82011-10-13 18:04:30 +0000254 DRD_(trace_msg)("[%d] rwlock_destroy 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000255 DRD_(thread_get_running_tid)(), p->a1);
bart777f7fe2008-03-02 17:43:18 +0000256
bartbedfd232009-03-26 19:07:15 +0000257 if (DRD_(rwlock_is_locked)(p))
258 {
bartd45d9952009-05-31 18:53:54 +0000259 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000260 VG_(maybe_record_error)(VG_(get_running_tid)(),
261 RwlockErr,
262 VG_(get_IP)(VG_(get_running_tid)()),
263 "Destroying locked rwlock",
264 &REI);
265 }
bart777f7fe2008-03-02 17:43:18 +0000266
bartbedfd232009-03-26 19:07:15 +0000267 VG_(OSetGen_ResetIter)(p->thread_info);
268 for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; )
269 {
bartb3f33402009-07-25 11:15:03 +0000270 DRD_(sg_put)(q->latest_wrlocked_segment);
271 DRD_(sg_put)(q->latest_rdlocked_segment);
bartbedfd232009-03-26 19:07:15 +0000272 }
bartb3f33402009-07-25 11:15:03 +0000273
bartbedfd232009-03-26 19:07:15 +0000274 VG_(OSetGen_Destroy)(p->thread_info);
bart777f7fe2008-03-02 17:43:18 +0000275}
276
277static
278struct rwlock_info*
bartc8441502009-07-27 16:03:51 +0000279DRD_(rwlock_get_or_allocate)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000280{
bartbedfd232009-03-26 19:07:15 +0000281 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000282
bartbedfd232009-03-26 19:07:15 +0000283 tl_assert(offsetof(DrdClientobj, rwlock) == 0);
284 p = &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock);
285 if (p)
bartc8441502009-07-27 16:03:51 +0000286 {
287 drd_rwlock_check_type(p, rwlock_type);
bartbedfd232009-03-26 19:07:15 +0000288 return p;
bartc8441502009-07-27 16:03:51 +0000289 }
bart777f7fe2008-03-02 17:43:18 +0000290
bartbedfd232009-03-26 19:07:15 +0000291 if (DRD_(clientobj_present)(rwlock, rwlock + 1))
292 {
bart62cc2322010-03-07 10:54:21 +0000293 GenericErrInfo GEI = {
294 .tid = DRD_(thread_get_running_tid)(),
295 .addr = rwlock,
296 };
bartbedfd232009-03-26 19:07:15 +0000297 VG_(maybe_record_error)(VG_(get_running_tid)(),
298 GenericErr,
299 VG_(get_IP)(VG_(get_running_tid)()),
300 "Not a reader-writer lock",
301 &GEI);
302 return 0;
303 }
bart777f7fe2008-03-02 17:43:18 +0000304
bartbedfd232009-03-26 19:07:15 +0000305 p = &(DRD_(clientobj_add)(rwlock, ClientRwlock)->rwlock);
bartc8441502009-07-27 16:03:51 +0000306 DRD_(rwlock_initialize)(p, rwlock, rwlock_type);
bartbedfd232009-03-26 19:07:15 +0000307 return p;
bart777f7fe2008-03-02 17:43:18 +0000308}
309
bartdc1ef032009-02-15 14:18:02 +0000310static struct rwlock_info* DRD_(rwlock_get)(const Addr rwlock)
bart777f7fe2008-03-02 17:43:18 +0000311{
bartbedfd232009-03-26 19:07:15 +0000312 tl_assert(offsetof(DrdClientobj, rwlock) == 0);
313 return &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock);
bart777f7fe2008-03-02 17:43:18 +0000314}
315
316/** Called before pthread_rwlock_init(). */
bartd45d9952009-05-31 18:53:54 +0000317struct rwlock_info* DRD_(rwlock_pre_init)(const Addr rwlock,
318 const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000319{
bartbedfd232009-03-26 19:07:15 +0000320 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000321
bartbedfd232009-03-26 19:07:15 +0000322 if (DRD_(s_trace_rwlock))
bartad994e82011-10-13 18:04:30 +0000323 DRD_(trace_msg)("[%d] rwlock_init 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000324 DRD_(thread_get_running_tid)(), rwlock);
bart777f7fe2008-03-02 17:43:18 +0000325
bartbedfd232009-03-26 19:07:15 +0000326 p = DRD_(rwlock_get)(rwlock);
bart777f7fe2008-03-02 17:43:18 +0000327
bartbedfd232009-03-26 19:07:15 +0000328 if (p)
bart67707ec2009-07-27 17:02:52 +0000329 drd_rwlock_check_type(p, rwlock_type);
330
331 if (p)
bartbedfd232009-03-26 19:07:15 +0000332 {
333 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000334 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000335 VG_(maybe_record_error)(vg_tid,
336 RwlockErr,
337 VG_(get_IP)(vg_tid),
338 "Reader-writer lock reinitialization",
339 &REI);
340 return p;
341 }
bart777f7fe2008-03-02 17:43:18 +0000342
bartc8441502009-07-27 16:03:51 +0000343 p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type);
bart777f7fe2008-03-02 17:43:18 +0000344
bartbedfd232009-03-26 19:07:15 +0000345 return p;
bart777f7fe2008-03-02 17:43:18 +0000346}
347
348/** Called after pthread_rwlock_destroy(). */
bartd45d9952009-05-31 18:53:54 +0000349void DRD_(rwlock_post_destroy)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000350{
bartbedfd232009-03-26 19:07:15 +0000351 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000352
bartbedfd232009-03-26 19:07:15 +0000353 p = DRD_(rwlock_get)(rwlock);
354 if (p == 0)
355 {
bart62cc2322010-03-07 10:54:21 +0000356 GenericErrInfo GEI = {
357 .tid = DRD_(thread_get_running_tid)(),
358 .addr = rwlock,
359 };
bartbedfd232009-03-26 19:07:15 +0000360 VG_(maybe_record_error)(VG_(get_running_tid)(),
361 GenericErr,
362 VG_(get_IP)(VG_(get_running_tid)()),
363 "Not a reader-writer lock",
364 &GEI);
365 return;
366 }
bart777f7fe2008-03-02 17:43:18 +0000367
bart67707ec2009-07-27 17:02:52 +0000368 drd_rwlock_check_type(p, rwlock_type);
369
bartbedfd232009-03-26 19:07:15 +0000370 DRD_(clientobj_remove)(rwlock, ClientRwlock);
bart777f7fe2008-03-02 17:43:18 +0000371}
372
bartdc1ef032009-02-15 14:18:02 +0000373/**
374 * Called before pthread_rwlock_rdlock() is invoked. If a data structure for
375 * the client-side object was not yet created, do this now. Also check whether
376 * an attempt is made to lock recursively a synchronization object that must
377 * not be locked recursively.
bart777f7fe2008-03-02 17:43:18 +0000378 */
bartd45d9952009-05-31 18:53:54 +0000379void DRD_(rwlock_pre_rdlock)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000380{
bartbedfd232009-03-26 19:07:15 +0000381 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000382
bartbedfd232009-03-26 19:07:15 +0000383 if (DRD_(s_trace_rwlock))
bartad994e82011-10-13 18:04:30 +0000384 DRD_(trace_msg)("[%d] pre_rwlock_rdlock 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000385 DRD_(thread_get_running_tid)(), rwlock);
bart777f7fe2008-03-02 17:43:18 +0000386
bartc8441502009-07-27 16:03:51 +0000387 p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type);
bartbedfd232009-03-26 19:07:15 +0000388 tl_assert(p);
bart165b90f2008-05-10 12:54:27 +0000389
bart74b2d972011-10-08 08:54:57 +0000390 if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)())) {
391 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
392 VG_(maybe_record_error)(VG_(get_running_tid)(),
393 RwlockErr,
394 VG_(get_IP)(VG_(get_running_tid)()),
395 "Already locked for writing by calling thread",
396 &REI);
bartbedfd232009-03-26 19:07:15 +0000397 }
bart777f7fe2008-03-02 17:43:18 +0000398}
399
bartdc1ef032009-02-15 14:18:02 +0000400/**
401 * Update rwlock_info state when locking the pthread_rwlock_t mutex.
402 * Note: this function must be called after pthread_rwlock_rdlock() has been
403 * called, or a race condition is triggered !
bart777f7fe2008-03-02 17:43:18 +0000404 */
bartd45d9952009-05-31 18:53:54 +0000405void DRD_(rwlock_post_rdlock)(const Addr rwlock, const RwLockT rwlock_type,
406 const Bool took_lock)
bart777f7fe2008-03-02 17:43:18 +0000407{
bartbedfd232009-03-26 19:07:15 +0000408 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
409 struct rwlock_info* p;
410 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000411
bartbedfd232009-03-26 19:07:15 +0000412 if (DRD_(s_trace_rwlock))
bartad994e82011-10-13 18:04:30 +0000413 DRD_(trace_msg)("[%d] post_rwlock_rdlock 0x%lx", drd_tid, rwlock);
bart777f7fe2008-03-02 17:43:18 +0000414
bartbedfd232009-03-26 19:07:15 +0000415 p = DRD_(rwlock_get)(rwlock);
bart165b90f2008-05-10 12:54:27 +0000416
bartbedfd232009-03-26 19:07:15 +0000417 if (! p || ! took_lock)
418 return;
bart777f7fe2008-03-02 17:43:18 +0000419
bartbedfd232009-03-26 19:07:15 +0000420 tl_assert(! DRD_(rwlock_is_wrlocked)(p));
bart777f7fe2008-03-02 17:43:18 +0000421
bartbedfd232009-03-26 19:07:15 +0000422 q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid);
423 if (++q->reader_nesting_count == 1)
424 {
bartbedfd232009-03-26 19:07:15 +0000425 DRD_(thread_new_segment)(drd_tid);
426 DRD_(s_rwlock_segment_creation_count)++;
bart7627be32009-06-06 12:26:05 +0000427 DRD_(rwlock_combine_other_vc)(p, drd_tid, False);
bart9d5b7962008-05-14 12:25:00 +0000428
bartbedfd232009-03-26 19:07:15 +0000429 p->acquiry_time_ms = VG_(read_millisecond_timer)();
430 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
431 }
bart777f7fe2008-03-02 17:43:18 +0000432}
433
bartdc1ef032009-02-15 14:18:02 +0000434/**
435 * Called before pthread_rwlock_wrlock() is invoked. If a data structure for
436 * the client-side object was not yet created, do this now. Also check whether
437 * an attempt is made to lock recursively a synchronization object that must
438 * not be locked recursively.
bart777f7fe2008-03-02 17:43:18 +0000439 */
bartd45d9952009-05-31 18:53:54 +0000440void DRD_(rwlock_pre_wrlock)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000441{
bartbedfd232009-03-26 19:07:15 +0000442 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000443
bartbedfd232009-03-26 19:07:15 +0000444 p = DRD_(rwlock_get)(rwlock);
bart777f7fe2008-03-02 17:43:18 +0000445
bartbedfd232009-03-26 19:07:15 +0000446 if (DRD_(s_trace_rwlock))
bartad994e82011-10-13 18:04:30 +0000447 DRD_(trace_msg)("[%d] pre_rwlock_wrlock 0x%lx",
bartb92ff0f2011-10-08 08:29:29 +0000448 DRD_(thread_get_running_tid)(), rwlock);
bart777f7fe2008-03-02 17:43:18 +0000449
bartbedfd232009-03-26 19:07:15 +0000450 if (p == 0)
bartc8441502009-07-27 16:03:51 +0000451 p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type);
bart777f7fe2008-03-02 17:43:18 +0000452
bartbedfd232009-03-26 19:07:15 +0000453 tl_assert(p);
bart777f7fe2008-03-02 17:43:18 +0000454
bartbedfd232009-03-26 19:07:15 +0000455 if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)()))
456 {
bartd45d9952009-05-31 18:53:54 +0000457 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000458 VG_(maybe_record_error)(VG_(get_running_tid)(),
459 RwlockErr,
460 VG_(get_IP)(VG_(get_running_tid)()),
461 "Recursive writer locking not allowed",
462 &REI);
463 }
bart777f7fe2008-03-02 17:43:18 +0000464}
465
466/**
467 * Update rwlock_info state when locking the pthread_rwlock_t rwlock.
bart165b90f2008-05-10 12:54:27 +0000468 * Note: this function must be called after pthread_rwlock_wrlock() has
469 * finished, or a race condition is triggered !
bart777f7fe2008-03-02 17:43:18 +0000470 */
bartd45d9952009-05-31 18:53:54 +0000471void DRD_(rwlock_post_wrlock)(const Addr rwlock, const RwLockT rwlock_type,
472 const Bool took_lock)
bart777f7fe2008-03-02 17:43:18 +0000473{
bartbedfd232009-03-26 19:07:15 +0000474 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
475 struct rwlock_info* p;
476 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000477
bartbedfd232009-03-26 19:07:15 +0000478 p = DRD_(rwlock_get)(rwlock);
bart777f7fe2008-03-02 17:43:18 +0000479
bartbedfd232009-03-26 19:07:15 +0000480 if (DRD_(s_trace_rwlock))
bartad994e82011-10-13 18:04:30 +0000481 DRD_(trace_msg)("[%d] post_rwlock_wrlock 0x%lx", drd_tid, rwlock);
bart777f7fe2008-03-02 17:43:18 +0000482
bartbedfd232009-03-26 19:07:15 +0000483 if (! p || ! took_lock)
484 return;
bart777f7fe2008-03-02 17:43:18 +0000485
bartbedfd232009-03-26 19:07:15 +0000486 q = DRD_(lookup_or_insert_node)(p->thread_info,
487 DRD_(thread_get_running_tid)());
488 tl_assert(q->writer_nesting_count == 0);
489 q->writer_nesting_count++;
bartbedfd232009-03-26 19:07:15 +0000490 tl_assert(q->writer_nesting_count == 1);
bartbedfd232009-03-26 19:07:15 +0000491 DRD_(thread_new_segment)(drd_tid);
492 DRD_(s_rwlock_segment_creation_count)++;
bart7627be32009-06-06 12:26:05 +0000493 DRD_(rwlock_combine_other_vc)(p, drd_tid, True);
bartbedfd232009-03-26 19:07:15 +0000494 p->acquiry_time_ms = VG_(read_millisecond_timer)();
495 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
bart777f7fe2008-03-02 17:43:18 +0000496}
497
498/**
499 * Update rwlock_info state when unlocking the pthread_rwlock_t rwlock.
bart7e6de962009-02-21 09:39:09 +0000500 *
bart777f7fe2008-03-02 17:43:18 +0000501 * @param rwlock Pointer to pthread_rwlock_t data structure in the client space.
bart7e6de962009-02-21 09:39:09 +0000502 *
503 * @return New value of the rwlock recursion count.
504 *
505 * @note This function must be called before pthread_rwlock_unlock() is called,
506 * or a race condition is triggered !
bart777f7fe2008-03-02 17:43:18 +0000507 */
bartd45d9952009-05-31 18:53:54 +0000508void DRD_(rwlock_pre_unlock)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000509{
bartbedfd232009-03-26 19:07:15 +0000510 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
511 const ThreadId vg_tid = VG_(get_running_tid)();
512 struct rwlock_info* p;
513 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000514
bartbedfd232009-03-26 19:07:15 +0000515 if (DRD_(s_trace_rwlock))
bartad994e82011-10-13 18:04:30 +0000516 DRD_(trace_msg)("[%d] rwlock_unlock 0x%lx", drd_tid, rwlock);
bart777f7fe2008-03-02 17:43:18 +0000517
bartbedfd232009-03-26 19:07:15 +0000518 p = DRD_(rwlock_get)(rwlock);
519 if (p == 0)
520 {
bart62cc2322010-03-07 10:54:21 +0000521 GenericErrInfo GEI = {
522 .tid = DRD_(thread_get_running_tid)(),
523 .addr = rwlock,
524 };
bartbedfd232009-03-26 19:07:15 +0000525 VG_(maybe_record_error)(VG_(get_running_tid)(),
526 GenericErr,
527 VG_(get_IP)(VG_(get_running_tid)()),
528 "Not a reader-writer lock",
529 &GEI);
530 return;
531 }
bart67707ec2009-07-27 17:02:52 +0000532
533 drd_rwlock_check_type(p, rwlock_type);
534
bartbedfd232009-03-26 19:07:15 +0000535 if (! DRD_(rwlock_is_locked_by)(p, drd_tid))
536 {
bartd45d9952009-05-31 18:53:54 +0000537 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000538 VG_(maybe_record_error)(vg_tid,
539 RwlockErr,
540 VG_(get_IP)(vg_tid),
541 "Reader-writer lock not locked by calling thread",
542 &REI);
543 return;
544 }
545 q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid);
546 tl_assert(q);
547 if (q->reader_nesting_count > 0)
548 {
549 q->reader_nesting_count--;
550 if (q->reader_nesting_count == 0 && DRD_(s_shared_threshold_ms) > 0)
bart9d5b7962008-05-14 12:25:00 +0000551 {
bart430c45f2009-04-13 08:05:18 +0000552 Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
bartbedfd232009-03-26 19:07:15 +0000553 if (held > DRD_(s_shared_threshold_ms))
554 {
555 HoldtimeErrInfo HEI
bartd45d9952009-05-31 18:53:54 +0000556 = { DRD_(thread_get_running_tid)(),
557 rwlock, p->acquired_at, held, DRD_(s_shared_threshold_ms) };
bartbedfd232009-03-26 19:07:15 +0000558 VG_(maybe_record_error)(vg_tid,
559 HoldtimeErr,
560 VG_(get_IP)(vg_tid),
561 "rwlock",
562 &HEI);
563 }
bart9d5b7962008-05-14 12:25:00 +0000564 }
bartb3f33402009-07-25 11:15:03 +0000565 if (q->reader_nesting_count == 0 && q->writer_nesting_count == 0)
566 {
567 /*
568 * This pthread_rwlock_unlock() call really unlocks the rwlock. Save
569 * the current vector clock of the thread such that it is available
570 * when this rwlock is locked again.
571 */
572 DRD_(thread_get_latest_segment)(&q->latest_rdlocked_segment, drd_tid);
573 DRD_(thread_new_segment)(drd_tid);
574 DRD_(s_rwlock_segment_creation_count)++;
575 }
bartbedfd232009-03-26 19:07:15 +0000576 }
577 else if (q->writer_nesting_count > 0)
578 {
579 q->writer_nesting_count--;
580 if (q->writer_nesting_count == 0 && DRD_(s_exclusive_threshold_ms) > 0)
bart9d5b7962008-05-14 12:25:00 +0000581 {
bart430c45f2009-04-13 08:05:18 +0000582 Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
bartbedfd232009-03-26 19:07:15 +0000583 if (held > DRD_(s_exclusive_threshold_ms))
584 {
585 HoldtimeErrInfo HEI
bartd45d9952009-05-31 18:53:54 +0000586 = { DRD_(thread_get_running_tid)(),
587 rwlock, p->acquired_at, held,
bartbedfd232009-03-26 19:07:15 +0000588 DRD_(s_exclusive_threshold_ms) };
589 VG_(maybe_record_error)(vg_tid,
590 HoldtimeErr,
591 VG_(get_IP)(vg_tid),
592 "rwlock",
593 &HEI);
594 }
bart9d5b7962008-05-14 12:25:00 +0000595 }
bartb3f33402009-07-25 11:15:03 +0000596 if (q->reader_nesting_count == 0 && q->writer_nesting_count == 0)
597 {
598 /*
599 * This pthread_rwlock_unlock() call really unlocks the rwlock. Save
600 * the current vector clock of the thread such that it is available
601 * when this rwlock is locked again.
602 */
603 DRD_(thread_get_latest_segment)(&q->latest_wrlocked_segment, drd_tid);
604 DRD_(thread_new_segment)(drd_tid);
605 DRD_(s_rwlock_segment_creation_count)++;
606 }
bartbedfd232009-03-26 19:07:15 +0000607 }
608 else
609 {
610 tl_assert(False);
611 }
bart777f7fe2008-03-02 17:43:18 +0000612}
613
bartb3f33402009-07-25 11:15:03 +0000614/** Called when thread tid stops to exist. */
bartd2c5eae2009-02-21 15:27:04 +0000615static void rwlock_delete_thread(struct rwlock_info* const p,
616 const DrdThreadId tid)
bart777f7fe2008-03-02 17:43:18 +0000617{
bartbedfd232009-03-26 19:07:15 +0000618 struct rwlock_thread_info* q;
bartb3f33402009-07-25 11:15:03 +0000619
bartbedfd232009-03-26 19:07:15 +0000620 if (DRD_(rwlock_is_locked_by)(p, tid))
621 {
bartd45d9952009-05-31 18:53:54 +0000622 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000623 VG_(maybe_record_error)(VG_(get_running_tid)(),
624 RwlockErr,
625 VG_(get_IP)(VG_(get_running_tid)()),
626 "Reader-writer lock still locked at thread exit",
627 &REI);
628 q = DRD_(lookup_or_insert_node)(p->thread_info, tid);
629 q->reader_nesting_count = 0;
630 q->writer_nesting_count = 0;
631 }
bart777f7fe2008-03-02 17:43:18 +0000632}
bart6bbefaf2008-04-19 15:16:45 +0000633
bartdc1ef032009-02-15 14:18:02 +0000634ULong DRD_(get_rwlock_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000635{
bartbedfd232009-03-26 19:07:15 +0000636 return DRD_(s_rwlock_segment_creation_count);
bart6bbefaf2008-04-19 15:16:45 +0000637}