blob: 3a0c601a971ebbe7b86d96e3fc19cec1837a8eb5 [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))
254 {
255 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000256 "[%d] rwlock_destroy 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000257 DRD_(thread_get_running_tid)(),
258 p->a1);
259 }
bart777f7fe2008-03-02 17:43:18 +0000260
bartbedfd232009-03-26 19:07:15 +0000261 if (DRD_(rwlock_is_locked)(p))
262 {
bartd45d9952009-05-31 18:53:54 +0000263 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000264 VG_(maybe_record_error)(VG_(get_running_tid)(),
265 RwlockErr,
266 VG_(get_IP)(VG_(get_running_tid)()),
267 "Destroying locked rwlock",
268 &REI);
269 }
bart777f7fe2008-03-02 17:43:18 +0000270
bartbedfd232009-03-26 19:07:15 +0000271 VG_(OSetGen_ResetIter)(p->thread_info);
272 for ( ; (q = VG_(OSetGen_Next)(p->thread_info)) != 0; )
273 {
bartb3f33402009-07-25 11:15:03 +0000274 DRD_(sg_put)(q->latest_wrlocked_segment);
275 DRD_(sg_put)(q->latest_rdlocked_segment);
bartbedfd232009-03-26 19:07:15 +0000276 }
bartb3f33402009-07-25 11:15:03 +0000277
bartbedfd232009-03-26 19:07:15 +0000278 VG_(OSetGen_Destroy)(p->thread_info);
bart777f7fe2008-03-02 17:43:18 +0000279}
280
281static
282struct rwlock_info*
bartc8441502009-07-27 16:03:51 +0000283DRD_(rwlock_get_or_allocate)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000284{
bartbedfd232009-03-26 19:07:15 +0000285 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000286
bartbedfd232009-03-26 19:07:15 +0000287 tl_assert(offsetof(DrdClientobj, rwlock) == 0);
288 p = &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock);
289 if (p)
bartc8441502009-07-27 16:03:51 +0000290 {
291 drd_rwlock_check_type(p, rwlock_type);
bartbedfd232009-03-26 19:07:15 +0000292 return p;
bartc8441502009-07-27 16:03:51 +0000293 }
bart777f7fe2008-03-02 17:43:18 +0000294
bartbedfd232009-03-26 19:07:15 +0000295 if (DRD_(clientobj_present)(rwlock, rwlock + 1))
296 {
bart62cc2322010-03-07 10:54:21 +0000297 GenericErrInfo GEI = {
298 .tid = DRD_(thread_get_running_tid)(),
299 .addr = rwlock,
300 };
bartbedfd232009-03-26 19:07:15 +0000301 VG_(maybe_record_error)(VG_(get_running_tid)(),
302 GenericErr,
303 VG_(get_IP)(VG_(get_running_tid)()),
304 "Not a reader-writer lock",
305 &GEI);
306 return 0;
307 }
bart777f7fe2008-03-02 17:43:18 +0000308
bartbedfd232009-03-26 19:07:15 +0000309 p = &(DRD_(clientobj_add)(rwlock, ClientRwlock)->rwlock);
bartc8441502009-07-27 16:03:51 +0000310 DRD_(rwlock_initialize)(p, rwlock, rwlock_type);
bartbedfd232009-03-26 19:07:15 +0000311 return p;
bart777f7fe2008-03-02 17:43:18 +0000312}
313
bartdc1ef032009-02-15 14:18:02 +0000314static struct rwlock_info* DRD_(rwlock_get)(const Addr rwlock)
bart777f7fe2008-03-02 17:43:18 +0000315{
bartbedfd232009-03-26 19:07:15 +0000316 tl_assert(offsetof(DrdClientobj, rwlock) == 0);
317 return &(DRD_(clientobj_get)(rwlock, ClientRwlock)->rwlock);
bart777f7fe2008-03-02 17:43:18 +0000318}
319
320/** Called before pthread_rwlock_init(). */
bartd45d9952009-05-31 18:53:54 +0000321struct rwlock_info* DRD_(rwlock_pre_init)(const Addr rwlock,
322 const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000323{
bartbedfd232009-03-26 19:07:15 +0000324 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000325
bartbedfd232009-03-26 19:07:15 +0000326 if (DRD_(s_trace_rwlock))
327 {
328 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000329 "[%d] rwlock_init 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000330 DRD_(thread_get_running_tid)(),
331 rwlock);
332 }
bart777f7fe2008-03-02 17:43:18 +0000333
bartbedfd232009-03-26 19:07:15 +0000334 p = DRD_(rwlock_get)(rwlock);
bart777f7fe2008-03-02 17:43:18 +0000335
bartbedfd232009-03-26 19:07:15 +0000336 if (p)
bart67707ec2009-07-27 17:02:52 +0000337 drd_rwlock_check_type(p, rwlock_type);
338
339 if (p)
bartbedfd232009-03-26 19:07:15 +0000340 {
341 const ThreadId vg_tid = VG_(get_running_tid)();
bartd45d9952009-05-31 18:53:54 +0000342 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000343 VG_(maybe_record_error)(vg_tid,
344 RwlockErr,
345 VG_(get_IP)(vg_tid),
346 "Reader-writer lock reinitialization",
347 &REI);
348 return p;
349 }
bart777f7fe2008-03-02 17:43:18 +0000350
bartc8441502009-07-27 16:03:51 +0000351 p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type);
bart777f7fe2008-03-02 17:43:18 +0000352
bartbedfd232009-03-26 19:07:15 +0000353 return p;
bart777f7fe2008-03-02 17:43:18 +0000354}
355
356/** Called after pthread_rwlock_destroy(). */
bartd45d9952009-05-31 18:53:54 +0000357void DRD_(rwlock_post_destroy)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000358{
bartbedfd232009-03-26 19:07:15 +0000359 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000360
bartbedfd232009-03-26 19:07:15 +0000361 p = DRD_(rwlock_get)(rwlock);
362 if (p == 0)
363 {
bart62cc2322010-03-07 10:54:21 +0000364 GenericErrInfo GEI = {
365 .tid = DRD_(thread_get_running_tid)(),
366 .addr = rwlock,
367 };
bartbedfd232009-03-26 19:07:15 +0000368 VG_(maybe_record_error)(VG_(get_running_tid)(),
369 GenericErr,
370 VG_(get_IP)(VG_(get_running_tid)()),
371 "Not a reader-writer lock",
372 &GEI);
373 return;
374 }
bart777f7fe2008-03-02 17:43:18 +0000375
bart67707ec2009-07-27 17:02:52 +0000376 drd_rwlock_check_type(p, rwlock_type);
377
bartbedfd232009-03-26 19:07:15 +0000378 DRD_(clientobj_remove)(rwlock, ClientRwlock);
bart777f7fe2008-03-02 17:43:18 +0000379}
380
bartdc1ef032009-02-15 14:18:02 +0000381/**
382 * Called before pthread_rwlock_rdlock() is invoked. If a data structure for
383 * the client-side object was not yet created, do this now. Also check whether
384 * an attempt is made to lock recursively a synchronization object that must
385 * not be locked recursively.
bart777f7fe2008-03-02 17:43:18 +0000386 */
bartd45d9952009-05-31 18:53:54 +0000387void DRD_(rwlock_pre_rdlock)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000388{
bartbedfd232009-03-26 19:07:15 +0000389 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000390
bartbedfd232009-03-26 19:07:15 +0000391 if (DRD_(s_trace_rwlock))
392 {
393 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000394 "[%d] pre_rwlock_rdlock 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000395 DRD_(thread_get_running_tid)(),
396 rwlock);
397 }
bart777f7fe2008-03-02 17:43:18 +0000398
bartc8441502009-07-27 16:03:51 +0000399 p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type);
bartbedfd232009-03-26 19:07:15 +0000400 tl_assert(p);
bart165b90f2008-05-10 12:54:27 +0000401
bartbedfd232009-03-26 19:07:15 +0000402 if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)()))
403 {
404 VG_(message)(Vg_UserMsg,
405 "reader-writer lock 0x%lx is already locked for"
sewardj1e29ebc2009-07-15 14:49:17 +0000406 " writing by calling thread\n",
bartbedfd232009-03-26 19:07:15 +0000407 p->a1);
408 }
bart777f7fe2008-03-02 17:43:18 +0000409}
410
bartdc1ef032009-02-15 14:18:02 +0000411/**
412 * Update rwlock_info state when locking the pthread_rwlock_t mutex.
413 * Note: this function must be called after pthread_rwlock_rdlock() has been
414 * called, or a race condition is triggered !
bart777f7fe2008-03-02 17:43:18 +0000415 */
bartd45d9952009-05-31 18:53:54 +0000416void DRD_(rwlock_post_rdlock)(const Addr rwlock, const RwLockT rwlock_type,
417 const Bool took_lock)
bart777f7fe2008-03-02 17:43:18 +0000418{
bartbedfd232009-03-26 19:07:15 +0000419 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
420 struct rwlock_info* p;
421 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000422
bartbedfd232009-03-26 19:07:15 +0000423 if (DRD_(s_trace_rwlock))
424 {
425 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000426 "[%d] post_rwlock_rdlock 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000427 drd_tid,
428 rwlock);
429 }
bart777f7fe2008-03-02 17:43:18 +0000430
bartbedfd232009-03-26 19:07:15 +0000431 p = DRD_(rwlock_get)(rwlock);
bart165b90f2008-05-10 12:54:27 +0000432
bartbedfd232009-03-26 19:07:15 +0000433 if (! p || ! took_lock)
434 return;
bart777f7fe2008-03-02 17:43:18 +0000435
bartbedfd232009-03-26 19:07:15 +0000436 tl_assert(! DRD_(rwlock_is_wrlocked)(p));
bart777f7fe2008-03-02 17:43:18 +0000437
bartbedfd232009-03-26 19:07:15 +0000438 q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid);
439 if (++q->reader_nesting_count == 1)
440 {
bartbedfd232009-03-26 19:07:15 +0000441 DRD_(thread_new_segment)(drd_tid);
442 DRD_(s_rwlock_segment_creation_count)++;
bart7627be32009-06-06 12:26:05 +0000443 DRD_(rwlock_combine_other_vc)(p, drd_tid, False);
bart9d5b7962008-05-14 12:25:00 +0000444
bartbedfd232009-03-26 19:07:15 +0000445 p->acquiry_time_ms = VG_(read_millisecond_timer)();
446 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
447 }
bart777f7fe2008-03-02 17:43:18 +0000448}
449
bartdc1ef032009-02-15 14:18:02 +0000450/**
451 * Called before pthread_rwlock_wrlock() is invoked. If a data structure for
452 * the client-side object was not yet created, do this now. Also check whether
453 * an attempt is made to lock recursively a synchronization object that must
454 * not be locked recursively.
bart777f7fe2008-03-02 17:43:18 +0000455 */
bartd45d9952009-05-31 18:53:54 +0000456void DRD_(rwlock_pre_wrlock)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000457{
bartbedfd232009-03-26 19:07:15 +0000458 struct rwlock_info* p;
bart777f7fe2008-03-02 17:43:18 +0000459
bartbedfd232009-03-26 19:07:15 +0000460 p = DRD_(rwlock_get)(rwlock);
bart777f7fe2008-03-02 17:43:18 +0000461
bartbedfd232009-03-26 19:07:15 +0000462 if (DRD_(s_trace_rwlock))
463 {
464 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000465 "[%d] pre_rwlock_wrlock 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000466 DRD_(thread_get_running_tid)(),
467 rwlock);
468 }
bart777f7fe2008-03-02 17:43:18 +0000469
bartbedfd232009-03-26 19:07:15 +0000470 if (p == 0)
bartc8441502009-07-27 16:03:51 +0000471 p = DRD_(rwlock_get_or_allocate)(rwlock, rwlock_type);
bart777f7fe2008-03-02 17:43:18 +0000472
bartbedfd232009-03-26 19:07:15 +0000473 tl_assert(p);
bart777f7fe2008-03-02 17:43:18 +0000474
bartbedfd232009-03-26 19:07:15 +0000475 if (DRD_(rwlock_is_wrlocked_by)(p, DRD_(thread_get_running_tid)()))
476 {
bartd45d9952009-05-31 18:53:54 +0000477 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000478 VG_(maybe_record_error)(VG_(get_running_tid)(),
479 RwlockErr,
480 VG_(get_IP)(VG_(get_running_tid)()),
481 "Recursive writer locking not allowed",
482 &REI);
483 }
bart777f7fe2008-03-02 17:43:18 +0000484}
485
486/**
487 * Update rwlock_info state when locking the pthread_rwlock_t rwlock.
bart165b90f2008-05-10 12:54:27 +0000488 * Note: this function must be called after pthread_rwlock_wrlock() has
489 * finished, or a race condition is triggered !
bart777f7fe2008-03-02 17:43:18 +0000490 */
bartd45d9952009-05-31 18:53:54 +0000491void DRD_(rwlock_post_wrlock)(const Addr rwlock, const RwLockT rwlock_type,
492 const Bool took_lock)
bart777f7fe2008-03-02 17:43:18 +0000493{
bartbedfd232009-03-26 19:07:15 +0000494 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
495 struct rwlock_info* p;
496 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000497
bartbedfd232009-03-26 19:07:15 +0000498 p = DRD_(rwlock_get)(rwlock);
bart777f7fe2008-03-02 17:43:18 +0000499
bartbedfd232009-03-26 19:07:15 +0000500 if (DRD_(s_trace_rwlock))
501 {
502 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000503 "[%d] post_rwlock_wrlock 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000504 drd_tid,
505 rwlock);
506 }
bart777f7fe2008-03-02 17:43:18 +0000507
bartbedfd232009-03-26 19:07:15 +0000508 if (! p || ! took_lock)
509 return;
bart777f7fe2008-03-02 17:43:18 +0000510
bartbedfd232009-03-26 19:07:15 +0000511 q = DRD_(lookup_or_insert_node)(p->thread_info,
512 DRD_(thread_get_running_tid)());
513 tl_assert(q->writer_nesting_count == 0);
514 q->writer_nesting_count++;
bartbedfd232009-03-26 19:07:15 +0000515 tl_assert(q->writer_nesting_count == 1);
bartbedfd232009-03-26 19:07:15 +0000516 DRD_(thread_new_segment)(drd_tid);
517 DRD_(s_rwlock_segment_creation_count)++;
bart7627be32009-06-06 12:26:05 +0000518 DRD_(rwlock_combine_other_vc)(p, drd_tid, True);
bartbedfd232009-03-26 19:07:15 +0000519 p->acquiry_time_ms = VG_(read_millisecond_timer)();
520 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
bart777f7fe2008-03-02 17:43:18 +0000521}
522
523/**
524 * Update rwlock_info state when unlocking the pthread_rwlock_t rwlock.
bart7e6de962009-02-21 09:39:09 +0000525 *
bart777f7fe2008-03-02 17:43:18 +0000526 * @param rwlock Pointer to pthread_rwlock_t data structure in the client space.
bart7e6de962009-02-21 09:39:09 +0000527 *
528 * @return New value of the rwlock recursion count.
529 *
530 * @note This function must be called before pthread_rwlock_unlock() is called,
531 * or a race condition is triggered !
bart777f7fe2008-03-02 17:43:18 +0000532 */
bartd45d9952009-05-31 18:53:54 +0000533void DRD_(rwlock_pre_unlock)(const Addr rwlock, const RwLockT rwlock_type)
bart777f7fe2008-03-02 17:43:18 +0000534{
bartbedfd232009-03-26 19:07:15 +0000535 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
536 const ThreadId vg_tid = VG_(get_running_tid)();
537 struct rwlock_info* p;
538 struct rwlock_thread_info* q;
bart777f7fe2008-03-02 17:43:18 +0000539
bartbedfd232009-03-26 19:07:15 +0000540 if (DRD_(s_trace_rwlock))
541 {
542 VG_(message)(Vg_UserMsg,
bart63c92ea2009-07-19 17:53:56 +0000543 "[%d] rwlock_unlock 0x%lx\n",
bartbedfd232009-03-26 19:07:15 +0000544 drd_tid,
545 rwlock);
546 }
bart777f7fe2008-03-02 17:43:18 +0000547
bartbedfd232009-03-26 19:07:15 +0000548 p = DRD_(rwlock_get)(rwlock);
549 if (p == 0)
550 {
bart62cc2322010-03-07 10:54:21 +0000551 GenericErrInfo GEI = {
552 .tid = DRD_(thread_get_running_tid)(),
553 .addr = rwlock,
554 };
bartbedfd232009-03-26 19:07:15 +0000555 VG_(maybe_record_error)(VG_(get_running_tid)(),
556 GenericErr,
557 VG_(get_IP)(VG_(get_running_tid)()),
558 "Not a reader-writer lock",
559 &GEI);
560 return;
561 }
bart67707ec2009-07-27 17:02:52 +0000562
563 drd_rwlock_check_type(p, rwlock_type);
564
bartbedfd232009-03-26 19:07:15 +0000565 if (! DRD_(rwlock_is_locked_by)(p, drd_tid))
566 {
bartd45d9952009-05-31 18:53:54 +0000567 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000568 VG_(maybe_record_error)(vg_tid,
569 RwlockErr,
570 VG_(get_IP)(vg_tid),
571 "Reader-writer lock not locked by calling thread",
572 &REI);
573 return;
574 }
575 q = DRD_(lookup_or_insert_node)(p->thread_info, drd_tid);
576 tl_assert(q);
577 if (q->reader_nesting_count > 0)
578 {
579 q->reader_nesting_count--;
580 if (q->reader_nesting_count == 0 && DRD_(s_shared_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_shared_threshold_ms))
584 {
585 HoldtimeErrInfo HEI
bartd45d9952009-05-31 18:53:54 +0000586 = { DRD_(thread_get_running_tid)(),
587 rwlock, p->acquired_at, held, DRD_(s_shared_threshold_ms) };
bartbedfd232009-03-26 19:07:15 +0000588 VG_(maybe_record_error)(vg_tid,
589 HoldtimeErr,
590 VG_(get_IP)(vg_tid),
591 "rwlock",
592 &HEI);
593 }
bart9d5b7962008-05-14 12:25:00 +0000594 }
bartb3f33402009-07-25 11:15:03 +0000595 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_rdlocked_segment, drd_tid);
603 DRD_(thread_new_segment)(drd_tid);
604 DRD_(s_rwlock_segment_creation_count)++;
605 }
bartbedfd232009-03-26 19:07:15 +0000606 }
607 else if (q->writer_nesting_count > 0)
608 {
609 q->writer_nesting_count--;
610 if (q->writer_nesting_count == 0 && DRD_(s_exclusive_threshold_ms) > 0)
bart9d5b7962008-05-14 12:25:00 +0000611 {
bart430c45f2009-04-13 08:05:18 +0000612 Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
bartbedfd232009-03-26 19:07:15 +0000613 if (held > DRD_(s_exclusive_threshold_ms))
614 {
615 HoldtimeErrInfo HEI
bartd45d9952009-05-31 18:53:54 +0000616 = { DRD_(thread_get_running_tid)(),
617 rwlock, p->acquired_at, held,
bartbedfd232009-03-26 19:07:15 +0000618 DRD_(s_exclusive_threshold_ms) };
619 VG_(maybe_record_error)(vg_tid,
620 HoldtimeErr,
621 VG_(get_IP)(vg_tid),
622 "rwlock",
623 &HEI);
624 }
bart9d5b7962008-05-14 12:25:00 +0000625 }
bartb3f33402009-07-25 11:15:03 +0000626 if (q->reader_nesting_count == 0 && q->writer_nesting_count == 0)
627 {
628 /*
629 * This pthread_rwlock_unlock() call really unlocks the rwlock. Save
630 * the current vector clock of the thread such that it is available
631 * when this rwlock is locked again.
632 */
633 DRD_(thread_get_latest_segment)(&q->latest_wrlocked_segment, drd_tid);
634 DRD_(thread_new_segment)(drd_tid);
635 DRD_(s_rwlock_segment_creation_count)++;
636 }
bartbedfd232009-03-26 19:07:15 +0000637 }
638 else
639 {
640 tl_assert(False);
641 }
bart777f7fe2008-03-02 17:43:18 +0000642}
643
bartb3f33402009-07-25 11:15:03 +0000644/** Called when thread tid stops to exist. */
bartd2c5eae2009-02-21 15:27:04 +0000645static void rwlock_delete_thread(struct rwlock_info* const p,
646 const DrdThreadId tid)
bart777f7fe2008-03-02 17:43:18 +0000647{
bartbedfd232009-03-26 19:07:15 +0000648 struct rwlock_thread_info* q;
bartb3f33402009-07-25 11:15:03 +0000649
bartbedfd232009-03-26 19:07:15 +0000650 if (DRD_(rwlock_is_locked_by)(p, tid))
651 {
bartd45d9952009-05-31 18:53:54 +0000652 RwlockErrInfo REI = { DRD_(thread_get_running_tid)(), p->a1 };
bartbedfd232009-03-26 19:07:15 +0000653 VG_(maybe_record_error)(VG_(get_running_tid)(),
654 RwlockErr,
655 VG_(get_IP)(VG_(get_running_tid)()),
656 "Reader-writer lock still locked at thread exit",
657 &REI);
658 q = DRD_(lookup_or_insert_node)(p->thread_info, tid);
659 q->reader_nesting_count = 0;
660 q->writer_nesting_count = 0;
661 }
bart777f7fe2008-03-02 17:43:18 +0000662}
bart6bbefaf2008-04-19 15:16:45 +0000663
bartdc1ef032009-02-15 14:18:02 +0000664ULong DRD_(get_rwlock_segment_creation_count)(void)
bart6bbefaf2008-04-19 15:16:45 +0000665{
bartbedfd232009-03-26 19:07:15 +0000666 return DRD_(s_rwlock_segment_creation_count);
bart6bbefaf2008-04-19 15:16:45 +0000667}