blob: 7a8f335886f98990dc3664872569fa6c4b7a9e3b [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
26#include "drd_error.h"
bart09dc13f2009-02-14 15:13:31 +000027#include "drd_barrier.h"
28#include "drd_cond.h"
29#include "drd_mutex.h"
sewardjaf44c822007-11-25 14:01:38 +000030#include "drd_segment.h"
bart09dc13f2009-02-14 15:13:31 +000031#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000032#include "drd_suppression.h"
33#include "drd_thread.h"
bart82195c12008-04-13 17:35:08 +000034#include "pub_tool_vki.h"
sewardjaf44c822007-11-25 14:01:38 +000035#include "pub_tool_basics.h" // Addr, SizeT
36#include "pub_tool_errormgr.h" // VG_(unique_error)()
37#include "pub_tool_libcassert.h" // tl_assert()
38#include "pub_tool_libcbase.h" // VG_(strlen)()
39#include "pub_tool_libcprint.h" // VG_(printf)()
bart82195c12008-04-13 17:35:08 +000040#include "pub_tool_libcproc.h" // VG_(getenv)()
sewardjaf44c822007-11-25 14:01:38 +000041#include "pub_tool_machine.h"
42#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardj85642922008-01-14 11:54:56 +000043#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000044#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
45
bart32ba2082008-06-05 08:53:42 +000046
sewardjaf44c822007-11-25 14:01:38 +000047
bart324a23b2009-02-15 12:14:52 +000048/* Local functions. */
sewardjaf44c822007-11-25 14:01:38 +000049
bart62a784c2009-02-15 13:11:14 +000050static void DRD_(thread_append_segment)(const DrdThreadId tid,
51 Segment* const sg);
52static void DRD_(thread_discard_segment)(const DrdThreadId tid,
53 Segment* const sg);
54static Bool DRD_(thread_conflict_set_up_to_date)(const DrdThreadId tid);
55static void DRD_(thread_compute_conflict_set)(struct bitmap** conflict_set,
56 const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +000057
58
bart324a23b2009-02-15 12:14:52 +000059/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000060
bart324a23b2009-02-15 12:14:52 +000061static ULong DRD_(s_context_switch_count);
62static ULong DRD_(s_discard_ordered_segments_count);
63static ULong DRD_(s_update_conflict_set_count);
64static ULong DRD_(s_conflict_set_new_segment_count);
65static ULong DRD_(s_conflict_set_combine_vc_count);
66static ULong DRD_(s_conflict_set_bitmap_creation_count);
67static ULong DRD_(s_conflict_set_bitmap2_creation_count);
68static ThreadId DRD_(s_vg_running_tid) = VG_INVALID_THREADID;
69DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID;
70ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS];
71struct bitmap* DRD_(g_conflict_set);
72static Bool DRD_(s_trace_context_switches) = False;
73static Bool DRD_(s_trace_conflict_set) = False;
74static Bool DRD_(s_trace_fork_join) = False;
75static Bool DRD_(s_segment_merging) = True;
sewardjaf44c822007-11-25 14:01:38 +000076
77
bart324a23b2009-02-15 12:14:52 +000078/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000079
bart62a784c2009-02-15 13:11:14 +000080void DRD_(thread_trace_context_switches)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000081{
bart09dc13f2009-02-14 15:13:31 +000082 tl_assert(t == False || t == True);
bart324a23b2009-02-15 12:14:52 +000083 DRD_(s_trace_context_switches) = t;
bart26f73e12008-02-24 18:37:08 +000084}
85
bart62a784c2009-02-15 13:11:14 +000086void DRD_(thread_trace_conflict_set)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000087{
bart09dc13f2009-02-14 15:13:31 +000088 tl_assert(t == False || t == True);
bart324a23b2009-02-15 12:14:52 +000089 DRD_(s_trace_conflict_set) = t;
bart26f73e12008-02-24 18:37:08 +000090}
91
bart09dc13f2009-02-14 15:13:31 +000092Bool DRD_(thread_get_trace_fork_join)(void)
93{
bart324a23b2009-02-15 12:14:52 +000094 return DRD_(s_trace_fork_join);
bart09dc13f2009-02-14 15:13:31 +000095}
96
97void DRD_(thread_set_trace_fork_join)(const Bool t)
98{
99 tl_assert(t == False || t == True);
bart324a23b2009-02-15 12:14:52 +0000100 DRD_(s_trace_fork_join) = t;
bart09dc13f2009-02-14 15:13:31 +0000101}
102
bart62a784c2009-02-15 13:11:14 +0000103void DRD_(thread_set_segment_merging)(const Bool m)
barta9c37392008-03-22 09:38:48 +0000104{
bart09dc13f2009-02-14 15:13:31 +0000105 tl_assert(m == False || m == True);
bart324a23b2009-02-15 12:14:52 +0000106 DRD_(s_segment_merging) = m;
barta9c37392008-03-22 09:38:48 +0000107}
108
sewardjaf44c822007-11-25 14:01:38 +0000109/**
110 * Convert Valgrind's ThreadId into a DrdThreadId. Report failure if
111 * Valgrind's ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000112 */
bart62a784c2009-02-15 13:11:14 +0000113DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000114{
bart3772a982008-03-15 08:11:03 +0000115 int i;
sewardjaf44c822007-11-25 14:01:38 +0000116
bart3772a982008-03-15 08:11:03 +0000117 if (tid == VG_INVALID_THREADID)
118 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000119
bart3772a982008-03-15 08:11:03 +0000120 for (i = 1; i < DRD_N_THREADS; i++)
121 {
bart324a23b2009-02-15 12:14:52 +0000122 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
123 && DRD_(g_threadinfo)[i].vg_threadid == tid)
bart3772a982008-03-15 08:11:03 +0000124 {
125 return i;
126 }
127 }
sewardjaf44c822007-11-25 14:01:38 +0000128
bart3772a982008-03-15 08:11:03 +0000129 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000130}
131
bart62a784c2009-02-15 13:11:14 +0000132static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000133{
bart3772a982008-03-15 08:11:03 +0000134 int i;
sewardjaf44c822007-11-25 14:01:38 +0000135
bart62a784c2009-02-15 13:11:14 +0000136 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000137
bart3772a982008-03-15 08:11:03 +0000138 for (i = 1; i < DRD_N_THREADS; i++)
139 {
bart324a23b2009-02-15 12:14:52 +0000140 if (DRD_(g_threadinfo)[i].vg_thread_exists == False
141 && DRD_(g_threadinfo)[i].posix_thread_exists == False
142 && DRD_(g_threadinfo)[i].detached_posix_thread == False)
bart3772a982008-03-15 08:11:03 +0000143 {
bart324a23b2009-02-15 12:14:52 +0000144 DRD_(g_threadinfo)[i].vg_thread_exists = True;
145 DRD_(g_threadinfo)[i].vg_threadid = tid;
146 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
147 DRD_(g_threadinfo)[i].stack_min = 0;
148 DRD_(g_threadinfo)[i].stack_min_min = 0;
149 DRD_(g_threadinfo)[i].stack_startup = 0;
150 DRD_(g_threadinfo)[i].stack_max = 0;
151 DRD_(g_threadinfo)[i].is_recording = True;
152 DRD_(g_threadinfo)[i].synchr_nesting = 0;
153 if (DRD_(g_threadinfo)[i].first != 0)
bart3772a982008-03-15 08:11:03 +0000154 VG_(printf)("drd thread id = %d\n", i);
bart324a23b2009-02-15 12:14:52 +0000155 tl_assert(DRD_(g_threadinfo)[i].first == 0);
156 tl_assert(DRD_(g_threadinfo)[i].last == 0);
bart3772a982008-03-15 08:11:03 +0000157 return i;
158 }
159 }
sewardjaf44c822007-11-25 14:01:38 +0000160
bart3772a982008-03-15 08:11:03 +0000161 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000162
bart3772a982008-03-15 08:11:03 +0000163 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000164}
165
bart62a784c2009-02-15 13:11:14 +0000166DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000167{
bart3772a982008-03-15 08:11:03 +0000168 int i;
sewardjaf44c822007-11-25 14:01:38 +0000169
bart3772a982008-03-15 08:11:03 +0000170 tl_assert(tid != INVALID_POSIX_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000171
bart3772a982008-03-15 08:11:03 +0000172 for (i = 1; i < DRD_N_THREADS; i++)
173 {
bart324a23b2009-02-15 12:14:52 +0000174 if (DRD_(g_threadinfo)[i].posix_thread_exists
175 && DRD_(g_threadinfo)[i].pt_threadid == tid)
bart3772a982008-03-15 08:11:03 +0000176 {
177 return i;
178 }
179 }
180 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000181}
182
bart62a784c2009-02-15 13:11:14 +0000183ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000184{
bart74a5f212008-05-11 06:43:07 +0000185 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
186 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000187 return (DRD_(g_threadinfo)[tid].vg_thread_exists
188 ? DRD_(g_threadinfo)[tid].vg_threadid
bart3772a982008-03-15 08:11:03 +0000189 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000190}
191
bart23d3a4e2008-04-05 12:53:00 +0000192#if 0
bart324a23b2009-02-15 12:14:52 +0000193/**
194 * Sanity check of the doubly linked list of segments referenced by a
195 * ThreadInfo struct.
196 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000197 */
bart62a784c2009-02-15 13:11:14 +0000198static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000199{
bart3772a982008-03-15 08:11:03 +0000200 Segment* p;
201 for (p = ti->first; p; p = p->next) {
202 if (p->next && p->next->prev != p)
203 return False;
204 if (p->next == 0 && p != ti->last)
205 return False;
206 }
207 for (p = ti->last; p; p = p->prev) {
208 if (p->prev && p->prev->next != p)
209 return False;
210 if (p->prev == 0 && p != ti->first)
211 return False;
212 }
213 return True;
sewardjaf44c822007-11-25 14:01:38 +0000214}
bart23d3a4e2008-04-05 12:53:00 +0000215#endif
sewardjaf44c822007-11-25 14:01:38 +0000216
bart439c55f2009-02-15 10:38:37 +0000217/**
218 * Create the first segment for a newly started thread.
219 *
220 * This function is called from the handler installed via
221 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
222 * from the context of the creator thread, before the new thread has been
223 * created.
224 */
bart62a784c2009-02-15 13:11:14 +0000225DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
226 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000227{
bart3772a982008-03-15 08:11:03 +0000228 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000229
bart62a784c2009-02-15 13:11:14 +0000230 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
231 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
bart74a5f212008-05-11 06:43:07 +0000232 tl_assert(0 <= (int)created && created < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000233 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000234
bart324a23b2009-02-15 12:14:52 +0000235 tl_assert(DRD_(g_threadinfo)[created].first == 0);
236 tl_assert(DRD_(g_threadinfo)[created].last == 0);
bart62a784c2009-02-15 13:11:14 +0000237 DRD_(thread_append_segment)(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000238
bart3772a982008-03-15 08:11:03 +0000239 return created;
sewardjaf44c822007-11-25 14:01:38 +0000240}
241
bart439c55f2009-02-15 10:38:37 +0000242/**
bart324a23b2009-02-15 12:14:52 +0000243 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called after
bart439c55f2009-02-15 10:38:37 +0000244 * the thread has been created and before any client instructioins are run
245 * on the newly created thread, e.g. from the handler installed via
246 * VG_(track_pre_thread_first_insn)().
247 */
bart62a784c2009-02-15 13:11:14 +0000248DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000249{
bart62a784c2009-02-15 13:11:14 +0000250 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000251
252 tl_assert(0 <= (int)created && created < DRD_N_THREADS
253 && created != DRD_INVALID_THREADID);
254
bart324a23b2009-02-15 12:14:52 +0000255 DRD_(g_threadinfo)[created].stack_max = VG_(thread_get_stack_max)(vg_created);
256 DRD_(g_threadinfo)[created].stack_startup = DRD_(g_threadinfo)[created].stack_max;
257 DRD_(g_threadinfo)[created].stack_min = DRD_(g_threadinfo)[created].stack_max;
258 DRD_(g_threadinfo)[created].stack_min_min = DRD_(g_threadinfo)[created].stack_max;
259 DRD_(g_threadinfo)[created].stack_size = VG_(thread_get_stack_size)(vg_created);
260 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000261
262 return created;
263}
bart09dc13f2009-02-14 15:13:31 +0000264
bart324a23b2009-02-15 12:14:52 +0000265/**
266 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
267 * after thread drd_joiner joined thread drd_joinee.
268 */
bart09dc13f2009-02-14 15:13:31 +0000269void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
270{
bart62a784c2009-02-15 13:11:14 +0000271 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
272 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
273 DRD_(thread_new_segment)(drd_joinee);
274 DRD_(thread_combine_vc)(drd_joiner, drd_joinee);
275 DRD_(thread_new_segment)(drd_joiner);
bart09dc13f2009-02-14 15:13:31 +0000276
bart324a23b2009-02-15 12:14:52 +0000277 if (DRD_(s_trace_fork_join))
bart09dc13f2009-02-14 15:13:31 +0000278 {
bart62a784c2009-02-15 13:11:14 +0000279 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
280 const ThreadId joinee = DRD_(DrdThreadIdToVgThreadId)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000281 const unsigned msg_size = 256;
282 char* msg;
283
284 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
285 tl_assert(msg);
286 VG_(snprintf)(msg, msg_size,
287 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
288 joiner, drd_joiner, joinee, drd_joinee);
289 if (joiner)
290 {
291 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
292 ", new vc: ");
bart41b226c2009-02-14 16:55:19 +0000293 DRD_(vc_snprint)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart62a784c2009-02-15 13:11:14 +0000294 DRD_(thread_get_vc)(drd_joiner));
bart09dc13f2009-02-14 15:13:31 +0000295 }
296 VG_(message)(Vg_DebugMsg, "%s", msg);
297 VG_(free)(msg);
298 }
299
300 if (! DRD_(get_check_stack_accesses)())
301 {
bart62a784c2009-02-15 13:11:14 +0000302 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
303 - DRD_(thread_get_stack_size)(drd_joinee),
304 DRD_(thread_get_stack_max)(drd_joinee));
bart09dc13f2009-02-14 15:13:31 +0000305 }
bart62a784c2009-02-15 13:11:14 +0000306 DRD_(thread_delete)(drd_joinee);
bartdc1ef032009-02-15 14:18:02 +0000307 DRD_(mutex_thread_delete)(drd_joinee);
308 DRD_(cond_thread_delete)(drd_joinee);
309 DRD_(semaphore_thread_delete)(drd_joinee);
barta8cf7652009-02-15 11:00:29 +0000310 DRD_(barrier_thread_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000311}
312
bart324a23b2009-02-15 12:14:52 +0000313/**
314 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
315 * and accesses this data structure from multiple threads without locking.
316 * Any conflicting accesses in the range stack_startup..stack_max will be
317 * ignored.
318 */
bart62a784c2009-02-15 13:11:14 +0000319void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
320 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000321{
bart74a5f212008-05-11 06:43:07 +0000322 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
323 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000324 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
325 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
326 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000327}
328
bart62a784c2009-02-15 13:11:14 +0000329Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000330{
bart74a5f212008-05-11 06:43:07 +0000331 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000332 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000333 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000334}
335
bart62a784c2009-02-15 13:11:14 +0000336Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000337{
bart74a5f212008-05-11 06:43:07 +0000338 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bartcac53462008-03-29 09:27:08 +0000339 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000340 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000341}
342
bart62a784c2009-02-15 13:11:14 +0000343Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000344{
bart74a5f212008-05-11 06:43:07 +0000345 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bartd43f8d32008-03-16 17:29:20 +0000346 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000347 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000348}
349
bart62a784c2009-02-15 13:11:14 +0000350SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000351{
bart74a5f212008-05-11 06:43:07 +0000352 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bartcac53462008-03-29 09:27:08 +0000353 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000354 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000355}
356
bart09dc13f2009-02-14 15:13:31 +0000357/**
358 * Clean up thread-specific data structures. Call this just after
359 * pthread_join().
sewardjaf44c822007-11-25 14:01:38 +0000360 */
bart62a784c2009-02-15 13:11:14 +0000361void DRD_(thread_delete)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000362{
bart3772a982008-03-15 08:11:03 +0000363 Segment* sg;
364 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000365
bart74a5f212008-05-11 06:43:07 +0000366 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000367 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000368 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
369 for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev)
bart3772a982008-03-15 08:11:03 +0000370 {
371 sg_prev = sg->prev;
barta2b6e1b2008-03-17 18:32:39 +0000372 sg->prev = 0;
373 sg->next = 0;
bart62ada3f2009-02-14 17:19:58 +0000374 DRD_(sg_put)(sg);
bart3772a982008-03-15 08:11:03 +0000375 }
bart324a23b2009-02-15 12:14:52 +0000376 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
377 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
378 tl_assert(DRD_(g_threadinfo)[tid].detached_posix_thread == False);
379 DRD_(g_threadinfo)[tid].first = 0;
380 DRD_(g_threadinfo)[tid].last = 0;
sewardjaf44c822007-11-25 14:01:38 +0000381}
382
bart324a23b2009-02-15 12:14:52 +0000383/**
384 * Called after a thread performed its last memory access and before
385 * thread_delete() is called. Note: thread_delete() is only called for
386 * joinable threads, not for detached threads.
387 */
bart62a784c2009-02-15 13:11:14 +0000388void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000389{
bart74a5f212008-05-11 06:43:07 +0000390 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000391 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000392
bart324a23b2009-02-15 12:14:52 +0000393 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000394
bart324a23b2009-02-15 12:14:52 +0000395 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
bart3772a982008-03-15 08:11:03 +0000396 {
397 /* Once a detached thread has finished, its stack is deallocated and */
barte73b0aa2008-06-28 07:19:56 +0000398 /* should no longer be taken into account when computing the conflict set*/
bart324a23b2009-02-15 12:14:52 +0000399 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000400
bart3772a982008-03-15 08:11:03 +0000401 /* For a detached thread, calling pthread_exit() invalidates the */
402 /* POSIX thread ID associated with the detached thread. For joinable */
403 /* POSIX threads however, the POSIX thread ID remains live after the */
404 /* pthread_exit() call until pthread_join() is called. */
bart324a23b2009-02-15 12:14:52 +0000405 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
bart3772a982008-03-15 08:11:03 +0000406 }
sewardjaf44c822007-11-25 14:01:38 +0000407}
408
bart9b2974a2008-09-27 12:35:31 +0000409/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000410void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000411{
412 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
413 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000414 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000415
bart324a23b2009-02-15 12:14:52 +0000416 DRD_(g_threadinfo)[tid].synchr_nesting = 0;
bartaf0691b2008-09-27 12:26:50 +0000417}
418
bart62a784c2009-02-15 13:11:14 +0000419void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000420{
bart74a5f212008-05-11 06:43:07 +0000421 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000422 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000423 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID);
bart3772a982008-03-15 08:11:03 +0000424 tl_assert(ptid != INVALID_POSIX_THREADID);
bart324a23b2009-02-15 12:14:52 +0000425 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
426 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000427}
428
bart62a784c2009-02-15 13:11:14 +0000429Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000430{
bart74a5f212008-05-11 06:43:07 +0000431 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000432 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000433 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000434}
435
bart62a784c2009-02-15 13:11:14 +0000436void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000437{
bart74a5f212008-05-11 06:43:07 +0000438 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000439 && tid != DRD_INVALID_THREADID);
440 tl_assert(!! joinable == joinable);
bart324a23b2009-02-15 12:14:52 +0000441 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000442#if 0
bart3772a982008-03-15 08:11:03 +0000443 VG_(message)(Vg_DebugMsg,
444 "thread_set_joinable(%d/%d, %s)",
445 tid,
bart324a23b2009-02-15 12:14:52 +0000446 DRD_(g_threadinfo)[tid].vg_threadid,
bart3772a982008-03-15 08:11:03 +0000447 joinable ? "joinable" : "detached");
sewardjaf44c822007-11-25 14:01:38 +0000448#endif
bart324a23b2009-02-15 12:14:52 +0000449 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000450}
451
bart62a784c2009-02-15 13:11:14 +0000452void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000453{
bart3772a982008-03-15 08:11:03 +0000454 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000455
bart324a23b2009-02-15 12:14:52 +0000456 if (vg_tid != DRD_(s_vg_running_tid))
bart3772a982008-03-15 08:11:03 +0000457 {
bart62a784c2009-02-15 13:11:14 +0000458 DRD_(thread_set_running_tid)(vg_tid,
459 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
bart3772a982008-03-15 08:11:03 +0000460 }
sewardj8b09d4f2007-12-04 21:27:18 +0000461
bart324a23b2009-02-15 12:14:52 +0000462 tl_assert(DRD_(s_vg_running_tid) != VG_INVALID_THREADID);
463 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000464}
465
bart62a784c2009-02-15 13:11:14 +0000466void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
467 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000468{
bart3772a982008-03-15 08:11:03 +0000469 tl_assert(vg_tid != VG_INVALID_THREADID);
470 tl_assert(drd_tid != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000471
bart324a23b2009-02-15 12:14:52 +0000472 if (vg_tid != DRD_(s_vg_running_tid))
bart3772a982008-03-15 08:11:03 +0000473 {
bart324a23b2009-02-15 12:14:52 +0000474 if (DRD_(s_trace_context_switches)
475 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
bart3772a982008-03-15 08:11:03 +0000476 {
477 VG_(message)(Vg_DebugMsg,
barta2b6e1b2008-03-17 18:32:39 +0000478 "Context switch from thread %d/%d to thread %d/%d;"
479 " segments: %llu",
bart324a23b2009-02-15 12:14:52 +0000480 DRD_(s_vg_running_tid), DRD_(g_drd_running_tid),
bart62a784c2009-02-15 13:11:14 +0000481 DRD_(DrdThreadIdToVgThreadId)(drd_tid), drd_tid,
bart62ada3f2009-02-14 17:19:58 +0000482 DRD_(sg_get_segments_alive_count)());
bart3772a982008-03-15 08:11:03 +0000483 }
bart324a23b2009-02-15 12:14:52 +0000484 DRD_(s_vg_running_tid) = vg_tid;
485 DRD_(g_drd_running_tid) = drd_tid;
bart62a784c2009-02-15 13:11:14 +0000486 DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set), drd_tid);
bart324a23b2009-02-15 12:14:52 +0000487 DRD_(s_context_switch_count)++;
bart3772a982008-03-15 08:11:03 +0000488 }
sewardj8b09d4f2007-12-04 21:27:18 +0000489
bart324a23b2009-02-15 12:14:52 +0000490 tl_assert(DRD_(s_vg_running_tid) != VG_INVALID_THREADID);
491 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000492}
493
bart62a784c2009-02-15 13:11:14 +0000494int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000495{
bart62a784c2009-02-15 13:11:14 +0000496 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart324a23b2009-02-15 12:14:52 +0000497 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000498}
499
bart62a784c2009-02-15 13:11:14 +0000500int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000501{
bart62a784c2009-02-15 13:11:14 +0000502 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart324a23b2009-02-15 12:14:52 +0000503 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
504 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000505}
506
bart62a784c2009-02-15 13:11:14 +0000507int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000508{
bart62a784c2009-02-15 13:11:14 +0000509 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart324a23b2009-02-15 12:14:52 +0000510 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000511}
512
bart1a473c72008-03-13 19:03:38 +0000513/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000514static
515void DRD_(thread_append_segment)(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000516{
bart74a5f212008-05-11 06:43:07 +0000517 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000518 && tid != DRD_INVALID_THREADID);
bart62a784c2009-02-15 13:11:14 +0000519 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
bart324a23b2009-02-15 12:14:52 +0000520 sg->prev = DRD_(g_threadinfo)[tid].last;
bart3772a982008-03-15 08:11:03 +0000521 sg->next = 0;
bart324a23b2009-02-15 12:14:52 +0000522 if (DRD_(g_threadinfo)[tid].last)
523 DRD_(g_threadinfo)[tid].last->next = sg;
524 DRD_(g_threadinfo)[tid].last = sg;
525 if (DRD_(g_threadinfo)[tid].first == 0)
526 DRD_(g_threadinfo)[tid].first = sg;
bart62a784c2009-02-15 13:11:14 +0000527 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
sewardjaf44c822007-11-25 14:01:38 +0000528}
529
bart324a23b2009-02-15 12:14:52 +0000530/**
531 * Remove a segment from the segment list of thread threadid, and free the
532 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000533 */
bart62a784c2009-02-15 13:11:14 +0000534static
535void DRD_(thread_discard_segment)(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000536{
bart74a5f212008-05-11 06:43:07 +0000537 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000538 && tid != DRD_INVALID_THREADID);
bart62a784c2009-02-15 13:11:14 +0000539 //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
bart26f73e12008-02-24 18:37:08 +0000540
bart3772a982008-03-15 08:11:03 +0000541 if (sg->prev)
542 sg->prev->next = sg->next;
543 if (sg->next)
544 sg->next->prev = sg->prev;
bart324a23b2009-02-15 12:14:52 +0000545 if (sg == DRD_(g_threadinfo)[tid].first)
546 DRD_(g_threadinfo)[tid].first = sg->next;
547 if (sg == DRD_(g_threadinfo)[tid].last)
548 DRD_(g_threadinfo)[tid].last = sg->prev;
bart62ada3f2009-02-14 17:19:58 +0000549 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000550
bart62a784c2009-02-15 13:11:14 +0000551 //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
sewardjaf44c822007-11-25 14:01:38 +0000552}
553
bart62a784c2009-02-15 13:11:14 +0000554VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000555{
bart74a5f212008-05-11 06:43:07 +0000556 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
557 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000558 tl_assert(DRD_(g_threadinfo)[tid].last);
559 return &DRD_(g_threadinfo)[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000560}
561
bart324a23b2009-02-15 12:14:52 +0000562/**
563 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000564 */
bart62a784c2009-02-15 13:11:14 +0000565void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000566{
567 tl_assert(sg);
bart74a5f212008-05-11 06:43:07 +0000568 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
569 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000570 tl_assert(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000571
bart62ada3f2009-02-14 17:19:58 +0000572 DRD_(sg_put)(*sg);
bart324a23b2009-02-15 12:14:52 +0000573 *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000574}
575
sewardjaf44c822007-11-25 14:01:38 +0000576/**
577 * Compute the minimum of all latest vector clocks of all threads
578 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
579 * @param vc pointer to a vectorclock, holds result upon return.
580 */
bart62a784c2009-02-15 13:11:14 +0000581static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000582{
bart3772a982008-03-15 08:11:03 +0000583 unsigned i;
584 Bool first;
585 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000586
bart3772a982008-03-15 08:11:03 +0000587 first = True;
bart62a784c2009-02-15 13:11:14 +0000588 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
589 i++)
bart3772a982008-03-15 08:11:03 +0000590 {
bart324a23b2009-02-15 12:14:52 +0000591 latest_sg = DRD_(g_threadinfo)[i].last;
bart3772a982008-03-15 08:11:03 +0000592 if (latest_sg)
593 {
594 if (first)
bart41b226c2009-02-14 16:55:19 +0000595 DRD_(vc_assign)(vc, &latest_sg->vc);
bart3772a982008-03-15 08:11:03 +0000596 else
bart41b226c2009-02-14 16:55:19 +0000597 DRD_(vc_min)(vc, &latest_sg->vc);
bart3772a982008-03-15 08:11:03 +0000598 first = False;
599 }
600 }
sewardjaf44c822007-11-25 14:01:38 +0000601}
602
bart62a784c2009-02-15 13:11:14 +0000603static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000604{
bart3772a982008-03-15 08:11:03 +0000605 unsigned i;
606 Bool first;
607 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000608
bart3772a982008-03-15 08:11:03 +0000609 first = True;
bart62a784c2009-02-15 13:11:14 +0000610 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
611 i++)
bart3772a982008-03-15 08:11:03 +0000612 {
bart324a23b2009-02-15 12:14:52 +0000613 latest_sg = DRD_(g_threadinfo)[i].last;
bart3772a982008-03-15 08:11:03 +0000614 if (latest_sg)
615 {
616 if (first)
bart41b226c2009-02-14 16:55:19 +0000617 DRD_(vc_assign)(vc, &latest_sg->vc);
bart3772a982008-03-15 08:11:03 +0000618 else
bart41b226c2009-02-14 16:55:19 +0000619 DRD_(vc_combine)(vc, &latest_sg->vc);
bart3772a982008-03-15 08:11:03 +0000620 first = False;
621 }
622 }
sewardjaf44c822007-11-25 14:01:38 +0000623}
624
625/**
bart5bd9f2d2008-03-03 20:31:58 +0000626 * Discard all segments that have a defined order against the latest vector
sewardjaf44c822007-11-25 14:01:38 +0000627 * clock of every thread -- these segments can no longer be involved in a
628 * data race.
629 */
bart62a784c2009-02-15 13:11:14 +0000630static void DRD_(thread_discard_ordered_segments)(void)
sewardjaf44c822007-11-25 14:01:38 +0000631{
bart3772a982008-03-15 08:11:03 +0000632 unsigned i;
633 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000634
bart324a23b2009-02-15 12:14:52 +0000635 DRD_(s_discard_ordered_segments_count)++;
sewardjaf44c822007-11-25 14:01:38 +0000636
bart41b226c2009-02-14 16:55:19 +0000637 DRD_(vc_init)(&thread_vc_min, 0, 0);
bart62a784c2009-02-15 13:11:14 +0000638 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
bart62ada3f2009-02-14 17:19:58 +0000639 if (DRD_(sg_get_trace)())
bart3772a982008-03-15 08:11:03 +0000640 {
641 char msg[256];
642 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000643
bart41b226c2009-02-14 16:55:19 +0000644 DRD_(vc_init)(&thread_vc_max, 0, 0);
bart62a784c2009-02-15 13:11:14 +0000645 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart3772a982008-03-15 08:11:03 +0000646 VG_(snprintf)(msg, sizeof(msg),
647 "Discarding ordered segments -- min vc is ");
bart41b226c2009-02-14 16:55:19 +0000648 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
649 &thread_vc_min);
bart3772a982008-03-15 08:11:03 +0000650 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
651 ", max vc is ");
bart41b226c2009-02-14 16:55:19 +0000652 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
653 &thread_vc_max);
barta2b6e1b2008-03-17 18:32:39 +0000654 VG_(message)(Vg_UserMsg, "%s", msg);
bart41b226c2009-02-14 16:55:19 +0000655 DRD_(vc_cleanup)(&thread_vc_max);
bart3772a982008-03-15 08:11:03 +0000656 }
sewardjaf44c822007-11-25 14:01:38 +0000657
bart62a784c2009-02-15 13:11:14 +0000658 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
659 i++)
bart3772a982008-03-15 08:11:03 +0000660 {
661 Segment* sg;
662 Segment* sg_next;
bart324a23b2009-02-15 12:14:52 +0000663 for (sg = DRD_(g_threadinfo)[i].first;
bart41b226c2009-02-14 16:55:19 +0000664 sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
bart3772a982008-03-15 08:11:03 +0000665 sg = sg_next)
666 {
bart62a784c2009-02-15 13:11:14 +0000667 DRD_(thread_discard_segment)(i, sg);
bart3772a982008-03-15 08:11:03 +0000668 }
669 }
bart41b226c2009-02-14 16:55:19 +0000670 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000671}
672
bart324a23b2009-02-15 12:14:52 +0000673/**
674 * Merge all segments that may be merged without triggering false positives
675 * or discarding real data races. For the theoretical background of segment
676 * merging, see also the following paper:
677 * Mark Christiaens, Michiel Ronsse and Koen De Bosschere.
678 * Bounding the number of segment histories during data race detection.
679 * Parallel Computing archive, Volume 28, Issue 9, pp 1221-1238,
680 * September 2002.
barta9c37392008-03-22 09:38:48 +0000681 */
682static void thread_merge_segments(void)
683{
684 unsigned i;
685
bart62a784c2009-02-15 13:11:14 +0000686 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
687 i++)
barta9c37392008-03-22 09:38:48 +0000688 {
689 Segment* sg;
690
bart62a784c2009-02-15 13:11:14 +0000691 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
barta9c37392008-03-22 09:38:48 +0000692
bart324a23b2009-02-15 12:14:52 +0000693 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
barta9c37392008-03-22 09:38:48 +0000694 {
bart62ada3f2009-02-14 17:19:58 +0000695 if (DRD_(sg_get_refcnt)(sg) == 1
barta9c37392008-03-22 09:38:48 +0000696 && sg->next
bart62ada3f2009-02-14 17:19:58 +0000697 && DRD_(sg_get_refcnt)(sg->next) == 1
barta9c37392008-03-22 09:38:48 +0000698 && sg->next->next)
699 {
700 /* Merge sg and sg->next into sg. */
bart62ada3f2009-02-14 17:19:58 +0000701 DRD_(sg_merge)(sg, sg->next);
bart62a784c2009-02-15 13:11:14 +0000702 DRD_(thread_discard_segment)(i, sg->next);
barta9c37392008-03-22 09:38:48 +0000703 }
704 }
705
bart62a784c2009-02-15 13:11:14 +0000706 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
barta9c37392008-03-22 09:38:48 +0000707 }
708}
709
bart324a23b2009-02-15 12:14:52 +0000710/**
711 * Every change in the vector clock of a thread may cause segments that
712 * were previously ordered to this thread to become unordered. Hence,
713 * it may be necessary to recalculate the conflict set if the vector clock
714 * of the current thread is updated. This function check whether such a
715 * recalculation is necessary.
bartd66e3a82008-04-06 15:02:17 +0000716 *
bart324a23b2009-02-15 12:14:52 +0000717 * @param tid Thread ID of the thread to which a new segment has been
718 * appended.
719 * @param new_sg Pointer to the most recent segment of thread tid.
bartd66e3a82008-04-06 15:02:17 +0000720 */
barte73b0aa2008-06-28 07:19:56 +0000721static Bool conflict_set_update_needed(const DrdThreadId tid,
bart41b226c2009-02-14 16:55:19 +0000722 const Segment* const new_sg)
bartd66e3a82008-04-06 15:02:17 +0000723{
bart5d421ba2008-04-19 15:15:12 +0000724#if 0
bartd66e3a82008-04-06 15:02:17 +0000725 unsigned i;
726 const Segment* old_sg;
727
728 tl_assert(new_sg);
729
730 /* If a new segment was added to another thread than the running thread, */
barte73b0aa2008-06-28 07:19:56 +0000731 /* just tell the caller to update the conflict set. */
bart324a23b2009-02-15 12:14:52 +0000732 if (tid != DRD_(g_drd_running_tid))
bartd66e3a82008-04-06 15:02:17 +0000733 return True;
734
barte73b0aa2008-06-28 07:19:56 +0000735 /* Always let the caller update the conflict set after creation of the */
bartd66e3a82008-04-06 15:02:17 +0000736 /* first segment. */
737 old_sg = new_sg->prev;
738 if (old_sg == 0)
739 return True;
740
bart62a784c2009-02-15 13:11:14 +0000741 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
742 i++)
bartd66e3a82008-04-06 15:02:17 +0000743 {
744 Segment* q;
745
bart324a23b2009-02-15 12:14:52 +0000746 if (i == DRD_(g_drd_running_tid))
bartd66e3a82008-04-06 15:02:17 +0000747 continue;
748
bart324a23b2009-02-15 12:14:52 +0000749 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
bartd66e3a82008-04-06 15:02:17 +0000750 {
751 /* If the expression below evaluates to false, this expression will */
752 /* also evaluate to false for all subsequent iterations. So stop */
753 /* iterating. */
bart41b226c2009-02-14 16:55:19 +0000754 if (DRD_(vc_lte)(&q->vc, &old_sg->vc))
bartd66e3a82008-04-06 15:02:17 +0000755 break;
756 /* If the vector clock of the 2nd the last segment is not ordered */
757 /* to the vector clock of segment q, and the last segment is, ask */
barte73b0aa2008-06-28 07:19:56 +0000758 /* the caller to update the conflict set. */
bart41b226c2009-02-14 16:55:19 +0000759 if (! DRD_(vc_lte)(&old_sg->vc, &q->vc))
bartd66e3a82008-04-06 15:02:17 +0000760 {
761 return True;
762 }
763 /* If the vector clock of the last segment is not ordered to the */
barte73b0aa2008-06-28 07:19:56 +0000764 /* vector clock of segment q, ask the caller to update the conflict */
bartd66e3a82008-04-06 15:02:17 +0000765 /* set. */
bart41b226c2009-02-14 16:55:19 +0000766 if (! DRD_(vc_lte)(&q->vc, &new_sg->vc) && ! DRD_(vc_lte)(&new_sg->vc, &q->vc))
bartd66e3a82008-04-06 15:02:17 +0000767 {
768 return True;
769 }
770 }
771 }
772
773 return False;
bart5d421ba2008-04-19 15:15:12 +0000774#else
775 return True;
776#endif
bartd66e3a82008-04-06 15:02:17 +0000777}
778
bart324a23b2009-02-15 12:14:52 +0000779/**
780 * Create a new segment for the specified thread, and discard any segments
781 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +0000782 */
bart62a784c2009-02-15 13:11:14 +0000783void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000784{
bartd66e3a82008-04-06 15:02:17 +0000785 Segment* new_sg;
786
bart74a5f212008-05-11 06:43:07 +0000787 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
788 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000789
bart62ada3f2009-02-14 17:19:58 +0000790 new_sg = DRD_(sg_new)(tid, tid);
bart62a784c2009-02-15 13:11:14 +0000791 DRD_(thread_append_segment)(tid, new_sg);
bartd66e3a82008-04-06 15:02:17 +0000792
barte73b0aa2008-06-28 07:19:56 +0000793 if (conflict_set_update_needed(tid, new_sg))
bartd66e3a82008-04-06 15:02:17 +0000794 {
bart62a784c2009-02-15 13:11:14 +0000795 DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set),
796 DRD_(g_drd_running_tid));
bart324a23b2009-02-15 12:14:52 +0000797 DRD_(s_conflict_set_new_segment_count)++;
bartd66e3a82008-04-06 15:02:17 +0000798 }
bart324a23b2009-02-15 12:14:52 +0000799 else if (tid == DRD_(g_drd_running_tid))
bart82195c12008-04-13 17:35:08 +0000800 {
bart62a784c2009-02-15 13:11:14 +0000801 tl_assert(DRD_(thread_conflict_set_up_to_date)(DRD_(g_drd_running_tid)));
bart82195c12008-04-13 17:35:08 +0000802 }
sewardjaf44c822007-11-25 14:01:38 +0000803
bart62a784c2009-02-15 13:11:14 +0000804 DRD_(thread_discard_ordered_segments)();
bart26f73e12008-02-24 18:37:08 +0000805
bart324a23b2009-02-15 12:14:52 +0000806 if (DRD_(s_segment_merging))
807 {
barta9c37392008-03-22 09:38:48 +0000808 thread_merge_segments();
bart324a23b2009-02-15 12:14:52 +0000809 }
sewardjaf44c822007-11-25 14:01:38 +0000810}
811
bart26f73e12008-02-24 18:37:08 +0000812/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart62a784c2009-02-15 13:11:14 +0000813void DRD_(thread_combine_vc)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +0000814{
bart3772a982008-03-15 08:11:03 +0000815 tl_assert(joiner != joinee);
bart74a5f212008-05-11 06:43:07 +0000816 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000817 && joiner != DRD_INVALID_THREADID);
bart74a5f212008-05-11 06:43:07 +0000818 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000819 && joinee != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000820 tl_assert(DRD_(g_threadinfo)[joiner].last);
821 tl_assert(DRD_(g_threadinfo)[joinee].last);
822 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
823 &DRD_(g_threadinfo)[joinee].last->vc);
bart62a784c2009-02-15 13:11:14 +0000824 DRD_(thread_discard_ordered_segments)();
sewardjaf44c822007-11-25 14:01:38 +0000825
bart324a23b2009-02-15 12:14:52 +0000826 if (joiner == DRD_(g_drd_running_tid))
bart3772a982008-03-15 08:11:03 +0000827 {
bart62a784c2009-02-15 13:11:14 +0000828 DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set), joiner);
bart3772a982008-03-15 08:11:03 +0000829 }
sewardjaf44c822007-11-25 14:01:38 +0000830}
831
bart324a23b2009-02-15 12:14:52 +0000832/**
833 * Call this function after thread 'tid' had to wait because of thread
834 * synchronization until the memory accesses in the segment with vector clock
835 * 'vc' finished.
bart26f73e12008-02-24 18:37:08 +0000836 */
bart62a784c2009-02-15 13:11:14 +0000837void DRD_(thread_combine_vc2)(DrdThreadId tid, const VectorClock* const vc)
sewardjaf44c822007-11-25 14:01:38 +0000838{
bart74a5f212008-05-11 06:43:07 +0000839 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
840 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000841 tl_assert(DRD_(g_threadinfo)[tid].last);
bart3772a982008-03-15 08:11:03 +0000842 tl_assert(vc);
bart324a23b2009-02-15 12:14:52 +0000843 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
bart62a784c2009-02-15 13:11:14 +0000844 DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set), tid);
845 DRD_(thread_discard_ordered_segments)();
bart324a23b2009-02-15 12:14:52 +0000846 DRD_(s_conflict_set_combine_vc_count)++;
sewardjaf44c822007-11-25 14:01:38 +0000847}
848
bart324a23b2009-02-15 12:14:52 +0000849/**
850 * Call this function whenever a thread is no longer using the memory
851 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
852 * increase.
bart26f73e12008-02-24 18:37:08 +0000853 */
bart62a784c2009-02-15 13:11:14 +0000854void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +0000855{
bartd43f8d32008-03-16 17:29:20 +0000856 DrdThreadId other_user;
857 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +0000858
bart3772a982008-03-15 08:11:03 +0000859 /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
bartd43f8d32008-03-16 17:29:20 +0000860 other_user = DRD_INVALID_THREADID;
bart62a784c2009-02-15 13:11:14 +0000861 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
862 i++)
bart3772a982008-03-15 08:11:03 +0000863 {
864 Segment* p;
bart324a23b2009-02-15 12:14:52 +0000865 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
bart3772a982008-03-15 08:11:03 +0000866 {
867 if (other_user == DRD_INVALID_THREADID
bart324a23b2009-02-15 12:14:52 +0000868 && i != DRD_(g_drd_running_tid))
sewardjaf44c822007-11-25 14:01:38 +0000869 {
bart8bf2f8b2008-03-30 17:56:43 +0000870 if (UNLIKELY(bm_test_and_clear(p->bm, a1, a2)))
871 {
872 other_user = i;
873 }
874 continue;
sewardjaf44c822007-11-25 14:01:38 +0000875 }
bart3772a982008-03-15 08:11:03 +0000876 bm_clear(p->bm, a1, a2);
877 }
878 }
sewardjaf44c822007-11-25 14:01:38 +0000879
bart324a23b2009-02-15 12:14:52 +0000880 /*
881 * If any other thread had accessed memory in [ a1, a2 [, update the
882 * conflict set.
883 */
bart3772a982008-03-15 08:11:03 +0000884 if (other_user != DRD_INVALID_THREADID
bart324a23b2009-02-15 12:14:52 +0000885 && bm_has_any_access(DRD_(g_conflict_set), a1, a2))
bart3772a982008-03-15 08:11:03 +0000886 {
bart62a784c2009-02-15 13:11:14 +0000887 DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set),
888 DRD_(thread_get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000889 }
sewardjaf44c822007-11-25 14:01:38 +0000890}
891
bart62a784c2009-02-15 13:11:14 +0000892void DRD_(thread_start_recording)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000893{
bart74a5f212008-05-11 06:43:07 +0000894 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
895 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000896 tl_assert(! DRD_(g_threadinfo)[tid].is_recording);
897 DRD_(g_threadinfo)[tid].is_recording = True;
bart0268dfa2008-03-11 20:10:21 +0000898}
899
bart62a784c2009-02-15 13:11:14 +0000900void DRD_(thread_stop_recording)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000901{
bart74a5f212008-05-11 06:43:07 +0000902 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
903 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000904 tl_assert(DRD_(g_threadinfo)[tid].is_recording);
905 DRD_(g_threadinfo)[tid].is_recording = False;
bart0268dfa2008-03-11 20:10:21 +0000906}
907
bart62a784c2009-02-15 13:11:14 +0000908void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +0000909{
bart3772a982008-03-15 08:11:03 +0000910 unsigned i;
911 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +0000912
bart62a784c2009-02-15 13:11:14 +0000913 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
914 i++)
bart3772a982008-03-15 08:11:03 +0000915 {
bart324a23b2009-02-15 12:14:52 +0000916 if (DRD_(g_threadinfo)[i].first)
bart3772a982008-03-15 08:11:03 +0000917 {
918 VG_(printf)("**************\n"
barta2b6e1b2008-03-17 18:32:39 +0000919 "* thread %3d (%d/%d/%d/0x%lx/%d) *\n"
bart3772a982008-03-15 08:11:03 +0000920 "**************\n",
921 i,
bart324a23b2009-02-15 12:14:52 +0000922 DRD_(g_threadinfo)[i].vg_thread_exists,
923 DRD_(g_threadinfo)[i].vg_threadid,
924 DRD_(g_threadinfo)[i].posix_thread_exists,
925 DRD_(g_threadinfo)[i].pt_threadid,
926 DRD_(g_threadinfo)[i].detached_posix_thread);
927 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
sewardjaf44c822007-11-25 14:01:38 +0000928 {
bart62ada3f2009-02-14 17:19:58 +0000929 DRD_(sg_print)(p);
sewardjaf44c822007-11-25 14:01:38 +0000930 }
bart3772a982008-03-15 08:11:03 +0000931 }
932 }
sewardjaf44c822007-11-25 14:01:38 +0000933}
934
935static void show_call_stack(const DrdThreadId tid,
936 const Char* const msg,
937 ExeContext* const callstack)
938{
bart62a784c2009-02-15 13:11:14 +0000939 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +0000940
bartaa97a542008-03-16 17:57:01 +0000941 VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid);
sewardjaf44c822007-11-25 14:01:38 +0000942
bart3772a982008-03-15 08:11:03 +0000943 if (vg_tid != VG_INVALID_THREADID)
944 {
945 if (callstack)
946 {
947 VG_(pp_ExeContext)(callstack);
948 }
949 else
950 {
951 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
952 }
953 }
954 else
955 {
956 VG_(message)(Vg_UserMsg,
957 " (thread finished, call stack no longer available)");
958 }
sewardjaf44c822007-11-25 14:01:38 +0000959}
960
sewardjaf44c822007-11-25 14:01:38 +0000961static void
962thread_report_conflicting_segments_segment(const DrdThreadId tid,
963 const Addr addr,
964 const SizeT size,
965 const BmAccessTypeT access_type,
966 const Segment* const p)
967{
bart3772a982008-03-15 08:11:03 +0000968 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +0000969
bart74a5f212008-05-11 06:43:07 +0000970 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000971 && tid != DRD_INVALID_THREADID);
972 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +0000973
bart62a784c2009-02-15 13:11:14 +0000974 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
975 i++)
bart3772a982008-03-15 08:11:03 +0000976 {
977 if (i != tid)
978 {
979 Segment* q;
bart324a23b2009-02-15 12:14:52 +0000980 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
sewardjaf44c822007-11-25 14:01:38 +0000981 {
bart324a23b2009-02-15 12:14:52 +0000982 /*
983 * Since q iterates over the segments of thread i in order of
984 * decreasing vector clocks, if q->vc <= p->vc, then
985 * q->next->vc <= p->vc will also hold. Hence, break out of the
986 * loop once this condition is met.
987 */
bart41b226c2009-02-14 16:55:19 +0000988 if (DRD_(vc_lte)(&q->vc, &p->vc))
bart3772a982008-03-15 08:11:03 +0000989 break;
bart41b226c2009-02-14 16:55:19 +0000990 if (! DRD_(vc_lte)(&p->vc, &q->vc))
bart3772a982008-03-15 08:11:03 +0000991 {
992 if (bm_has_conflict_with(q->bm, addr, addr + size, access_type))
993 {
994 tl_assert(q->stacktrace);
995 show_call_stack(i, "Other segment start",
996 q->stacktrace);
997 show_call_stack(i, "Other segment end",
998 q->next ? q->next->stacktrace : 0);
999 }
1000 }
sewardjaf44c822007-11-25 14:01:38 +00001001 }
bart3772a982008-03-15 08:11:03 +00001002 }
1003 }
sewardjaf44c822007-11-25 14:01:38 +00001004}
1005
bart62a784c2009-02-15 13:11:14 +00001006void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1007 const Addr addr,
1008 const SizeT size,
1009 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001010{
bart3772a982008-03-15 08:11:03 +00001011 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001012
bart74a5f212008-05-11 06:43:07 +00001013 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +00001014 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001015
bart324a23b2009-02-15 12:14:52 +00001016 for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next)
bart3772a982008-03-15 08:11:03 +00001017 {
1018 if (bm_has(p->bm, addr, addr + size, access_type))
1019 {
1020 thread_report_conflicting_segments_segment(tid, addr, size,
1021 access_type, p);
1022 }
1023 }
sewardjaf44c822007-11-25 14:01:38 +00001024}
sewardjaf44c822007-11-25 14:01:38 +00001025
bart324a23b2009-02-15 12:14:52 +00001026/**
1027 * Verify whether the conflict set for thread tid is up to date. Only perform
1028 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
bart82195c12008-04-13 17:35:08 +00001029 */
bart62a784c2009-02-15 13:11:14 +00001030static Bool DRD_(thread_conflict_set_up_to_date)(const DrdThreadId tid)
bart82195c12008-04-13 17:35:08 +00001031{
barte73b0aa2008-06-28 07:19:56 +00001032 static int do_verify_conflict_set = -1;
bart82195c12008-04-13 17:35:08 +00001033 Bool result;
barte73b0aa2008-06-28 07:19:56 +00001034 struct bitmap* computed_conflict_set = 0;
bart82195c12008-04-13 17:35:08 +00001035
barte73b0aa2008-06-28 07:19:56 +00001036 if (do_verify_conflict_set < 0)
bart82195c12008-04-13 17:35:08 +00001037 {
barte73b0aa2008-06-28 07:19:56 +00001038 //VG_(message)(Vg_DebugMsg, "%s", VG_(getenv)("DRD_VERIFY_CONFLICT_SET"));
1039 do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0;
bart82195c12008-04-13 17:35:08 +00001040 }
barte73b0aa2008-06-28 07:19:56 +00001041 if (do_verify_conflict_set == 0)
bart82195c12008-04-13 17:35:08 +00001042 return True;
1043
bart62a784c2009-02-15 13:11:14 +00001044 DRD_(thread_compute_conflict_set)(&computed_conflict_set, tid);
bart324a23b2009-02-15 12:14:52 +00001045 result = bm_equal(DRD_(g_conflict_set), computed_conflict_set);
barte73b0aa2008-06-28 07:19:56 +00001046 bm_delete(computed_conflict_set);
bart82195c12008-04-13 17:35:08 +00001047 return result;
1048}
1049
bart324a23b2009-02-15 12:14:52 +00001050/**
1051 * Compute a bitmap that represents the union of all memory accesses of all
1052 * segments that are unordered to the current segment of the thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001053 */
bart62a784c2009-02-15 13:11:14 +00001054static void DRD_(thread_compute_conflict_set)(struct bitmap** conflict_set,
1055 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001056{
bart3772a982008-03-15 08:11:03 +00001057 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001058
bart74a5f212008-05-11 06:43:07 +00001059 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1060 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +00001061 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001062
bart324a23b2009-02-15 12:14:52 +00001063 DRD_(s_update_conflict_set_count)++;
1064 DRD_(s_conflict_set_bitmap_creation_count) -= bm_get_bitmap_creation_count();
1065 DRD_(s_conflict_set_bitmap2_creation_count) -= bm_get_bitmap2_creation_count();
sewardjaf44c822007-11-25 14:01:38 +00001066
barte73b0aa2008-06-28 07:19:56 +00001067 if (*conflict_set)
bart3772a982008-03-15 08:11:03 +00001068 {
barte73b0aa2008-06-28 07:19:56 +00001069 bm_delete(*conflict_set);
bart3772a982008-03-15 08:11:03 +00001070 }
barte73b0aa2008-06-28 07:19:56 +00001071 *conflict_set = bm_new();
bart26f73e12008-02-24 18:37:08 +00001072
bart324a23b2009-02-15 12:14:52 +00001073 if (DRD_(s_trace_conflict_set))
bart3772a982008-03-15 08:11:03 +00001074 {
1075 char msg[256];
1076
1077 VG_(snprintf)(msg, sizeof(msg),
barte73b0aa2008-06-28 07:19:56 +00001078 "computing conflict set for thread %d/%d with vc ",
bart62a784c2009-02-15 13:11:14 +00001079 DRD_(DrdThreadIdToVgThreadId)(tid), tid);
bart41b226c2009-02-14 16:55:19 +00001080 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1081 sizeof(msg) - VG_(strlen)(msg),
bart324a23b2009-02-15 12:14:52 +00001082 &DRD_(g_threadinfo)[tid].last->vc);
barta2b6e1b2008-03-17 18:32:39 +00001083 VG_(message)(Vg_UserMsg, "%s", msg);
bart3772a982008-03-15 08:11:03 +00001084 }
1085
bart324a23b2009-02-15 12:14:52 +00001086 p = DRD_(g_threadinfo)[tid].last;
bart3772a982008-03-15 08:11:03 +00001087 {
1088 unsigned j;
1089
bart324a23b2009-02-15 12:14:52 +00001090 if (DRD_(s_trace_conflict_set))
bart3772a982008-03-15 08:11:03 +00001091 {
bart26f73e12008-02-24 18:37:08 +00001092 char msg[256];
1093
1094 VG_(snprintf)(msg, sizeof(msg),
barte73b0aa2008-06-28 07:19:56 +00001095 "conflict set: thread [%d] at vc ",
bart26f73e12008-02-24 18:37:08 +00001096 tid);
bart41b226c2009-02-14 16:55:19 +00001097 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1098 sizeof(msg) - VG_(strlen)(msg),
1099 &p->vc);
barta2b6e1b2008-03-17 18:32:39 +00001100 VG_(message)(Vg_UserMsg, "%s", msg);
bart3772a982008-03-15 08:11:03 +00001101 }
sewardjaf44c822007-11-25 14:01:38 +00001102
bart324a23b2009-02-15 12:14:52 +00001103 for (j = 0; j < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); j++)
bart3772a982008-03-15 08:11:03 +00001104 {
bart62a784c2009-02-15 13:11:14 +00001105 if (j != tid && DRD_(IsValidDrdThreadId)(j))
bart26f73e12008-02-24 18:37:08 +00001106 {
bart3772a982008-03-15 08:11:03 +00001107 const Segment* q;
bart324a23b2009-02-15 12:14:52 +00001108 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
bartd66e3a82008-04-06 15:02:17 +00001109 {
bart41b226c2009-02-14 16:55:19 +00001110 if (! DRD_(vc_lte)(&q->vc, &p->vc) && ! DRD_(vc_lte)(&p->vc, &q->vc))
bart3772a982008-03-15 08:11:03 +00001111 {
bart324a23b2009-02-15 12:14:52 +00001112 if (DRD_(s_trace_conflict_set))
bart3772a982008-03-15 08:11:03 +00001113 {
1114 char msg[256];
1115 VG_(snprintf)(msg, sizeof(msg),
barte73b0aa2008-06-28 07:19:56 +00001116 "conflict set: [%d] merging segment ", j);
bart41b226c2009-02-14 16:55:19 +00001117 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1118 sizeof(msg) - VG_(strlen)(msg),
1119 &q->vc);
barta2b6e1b2008-03-17 18:32:39 +00001120 VG_(message)(Vg_UserMsg, "%s", msg);
bart3772a982008-03-15 08:11:03 +00001121 }
barte73b0aa2008-06-28 07:19:56 +00001122 bm_merge2(*conflict_set, q->bm);
bart3772a982008-03-15 08:11:03 +00001123 }
1124 else
1125 {
bart324a23b2009-02-15 12:14:52 +00001126 if (DRD_(s_trace_conflict_set))
bart3772a982008-03-15 08:11:03 +00001127 {
1128 char msg[256];
1129 VG_(snprintf)(msg, sizeof(msg),
barte73b0aa2008-06-28 07:19:56 +00001130 "conflict set: [%d] ignoring segment ", j);
bart41b226c2009-02-14 16:55:19 +00001131 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1132 sizeof(msg) - VG_(strlen)(msg),
1133 &q->vc);
barta2b6e1b2008-03-17 18:32:39 +00001134 VG_(message)(Vg_UserMsg, "%s", msg);
bart3772a982008-03-15 08:11:03 +00001135 }
1136 }
bartd66e3a82008-04-06 15:02:17 +00001137 }
bart26f73e12008-02-24 18:37:08 +00001138 }
bart3772a982008-03-15 08:11:03 +00001139 }
bart3772a982008-03-15 08:11:03 +00001140 }
sewardjaf44c822007-11-25 14:01:38 +00001141
bart324a23b2009-02-15 12:14:52 +00001142 DRD_(s_conflict_set_bitmap_creation_count) += bm_get_bitmap_creation_count();
1143 DRD_(s_conflict_set_bitmap2_creation_count) += bm_get_bitmap2_creation_count();
sewardjaf44c822007-11-25 14:01:38 +00001144
bart324a23b2009-02-15 12:14:52 +00001145 if (0 && DRD_(s_trace_conflict_set))
bart3772a982008-03-15 08:11:03 +00001146 {
barte73b0aa2008-06-28 07:19:56 +00001147 VG_(message)(Vg_UserMsg, "[%d] new conflict set:", tid);
1148 bm_print(*conflict_set);
1149 VG_(message)(Vg_UserMsg, "[%d] end of new conflict set.", tid);
bart3772a982008-03-15 08:11:03 +00001150 }
sewardjaf44c822007-11-25 14:01:38 +00001151}
1152
bart62a784c2009-02-15 13:11:14 +00001153ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001154{
bart324a23b2009-02-15 12:14:52 +00001155 return DRD_(s_context_switch_count);
sewardjaf44c822007-11-25 14:01:38 +00001156}
1157
bart62a784c2009-02-15 13:11:14 +00001158ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001159{
bart324a23b2009-02-15 12:14:52 +00001160 return DRD_(s_discard_ordered_segments_count);
sewardjaf44c822007-11-25 14:01:38 +00001161}
1162
bart62a784c2009-02-15 13:11:14 +00001163ULong DRD_(thread_get_update_conflict_set_count)(ULong* dsnsc, ULong* dscvc)
sewardjaf44c822007-11-25 14:01:38 +00001164{
bartd66e3a82008-04-06 15:02:17 +00001165 tl_assert(dsnsc);
1166 tl_assert(dscvc);
bart324a23b2009-02-15 12:14:52 +00001167 *dsnsc = DRD_(s_conflict_set_new_segment_count);
1168 *dscvc = DRD_(s_conflict_set_combine_vc_count);
1169 return DRD_(s_update_conflict_set_count);
sewardjaf44c822007-11-25 14:01:38 +00001170}
1171
bart62a784c2009-02-15 13:11:14 +00001172ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001173{
bart324a23b2009-02-15 12:14:52 +00001174 return DRD_(s_conflict_set_bitmap_creation_count);
sewardjaf44c822007-11-25 14:01:38 +00001175}
1176
bart62a784c2009-02-15 13:11:14 +00001177ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001178{
bart324a23b2009-02-15 12:14:52 +00001179 return DRD_(s_conflict_set_bitmap2_creation_count);
sewardjaf44c822007-11-25 14:01:38 +00001180}