drd_thread.h revision e278ab506b9a73ef1c17a17077546b2de9a11d7c
1af44c8236f7a73e71b16b707bba56f33af4d01cesewardj/*
286562bd89ac23ce795d19c71fabcb9d1c8f956d3bart  This file is part of drd, a thread error detector.
3af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
4922304f9e9d5df1a20a1bb1d4736263c99272aa4bart  Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
5af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
6af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  This program is free software; you can redistribute it and/or
7af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  modify it under the terms of the GNU General Public License as
8af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  published by the Free Software Foundation; either version 2 of the
9af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  License, or (at your option) any later version.
10af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
11af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  This program is distributed in the hope that it will be useful, but
12af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  WITHOUT ANY WARRANTY; without even the implied warranty of
13af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  General Public License for more details.
15af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
16af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  You should have received a copy of the GNU General Public License
17af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  along with this program; if not, write to the Free Software
18af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  02111-1307, USA.
20af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
21af44c8236f7a73e71b16b707bba56f33af4d01cesewardj  The GNU General Public License is contained in the file COPYING.
22af44c8236f7a73e71b16b707bba56f33af4d01cesewardj*/
23af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
24af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
25af44c8236f7a73e71b16b707bba56f33af4d01cesewardj#ifndef __THREAD_H
26af44c8236f7a73e71b16b707bba56f33af4d01cesewardj#define __THREAD_H
27af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
28af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
29324a23bea57e0bfc27d9442ec00a8eee8294f01abart/* Include directives. */
30f00a85bc55a2790c31670dd21e449a335c739068bart
3109dc13f324a19f1bebf58c7f197a581dc8fb528bbart#include "drd_basics.h"
32e278ab506b9a73ef1c17a17077546b2de9a11d7cbart#include "drd_list.h"
33af44c8236f7a73e71b16b707bba56f33af4d01cesewardj#include "drd_segment.h"
34d59bb0f2a79bac77cf0f01b20d3d51102d6c71e3bart#include "pub_drd_bitmap.h"
3586a87df5949beb1f89ebbed923068faed08d048cbart#include "pub_tool_libcassert.h"  /* tl_assert()        */
3686a87df5949beb1f89ebbed923068faed08d048cbart#include "pub_tool_stacktrace.h"  /* typedef StackTrace */
3786a87df5949beb1f89ebbed923068faed08d048cbart#include "pub_tool_threadstate.h" /* VG_N_THREADS       */
38f00a85bc55a2790c31670dd21e449a335c739068bart
39f00a85bc55a2790c31670dd21e449a335c739068bart
40324a23bea57e0bfc27d9442ec00a8eee8294f01abart/* Defines. */
41af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
4286a87df5949beb1f89ebbed923068faed08d048cbart/** Maximum number of threads DRD keeps information about. */
43f00a85bc55a2790c31670dd21e449a335c739068bart#define DRD_N_THREADS VG_N_THREADS
44af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
4586a87df5949beb1f89ebbed923068faed08d048cbart/** A number different from any valid DRD thread ID. */
46af44c8236f7a73e71b16b707bba56f33af4d01cesewardj#define DRD_INVALID_THREADID 0
47af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
4886a87df5949beb1f89ebbed923068faed08d048cbart/**
4986a87df5949beb1f89ebbed923068faed08d048cbart * A number different from any valid POSIX thread ID.
5086a87df5949beb1f89ebbed923068faed08d048cbart *
5186a87df5949beb1f89ebbed923068faed08d048cbart * @note The PThreadId typedef and the INVALID_POSIX_THREADID depend on the
5286a87df5949beb1f89ebbed923068faed08d048cbart * operating system and threading library in use. PThreadId must contain at
5386a87df5949beb1f89ebbed923068faed08d048cbart * least as many bits as pthread_t, and INVALID_POSIX_THREADID
5486a87df5949beb1f89ebbed923068faed08d048cbart * must be a value that will never be returned by pthread_self().
5586a87df5949beb1f89ebbed923068faed08d048cbart */
56af44c8236f7a73e71b16b707bba56f33af4d01cesewardj#define INVALID_POSIX_THREADID ((PThreadId)0)
57af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
58af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
59324a23bea57e0bfc27d9442ec00a8eee8294f01abart/* Type definitions. */
60f00a85bc55a2790c31670dd21e449a335c739068bart
6186a87df5949beb1f89ebbed923068faed08d048cbart/**
6286a87df5949beb1f89ebbed923068faed08d048cbart * POSIX thread ID. The type PThreadId must be at least as wide as
6386a87df5949beb1f89ebbed923068faed08d048cbart * pthread_t.
6486a87df5949beb1f89ebbed923068faed08d048cbart */
65af44c8236f7a73e71b16b707bba56f33af4d01cesewardjtypedef UWord PThreadId;
66af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
6786a87df5949beb1f89ebbed923068faed08d048cbart/** Per-thread information managed by DRD. */
68f00a85bc55a2790c31670dd21e449a335c739068barttypedef struct
69f00a85bc55a2790c31670dd21e449a335c739068bart{
70e278ab506b9a73ef1c17a17077546b2de9a11d7cbart   struct list_head sg_list;/**< Segment list. */
71bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   ThreadId  vg_threadid;   /**< Valgrind thread ID. */
72bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   PThreadId pt_threadid;   /**< POSIX thread ID. */
73bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   Addr      stack_min_min; /**< Lowest value stack pointer ever had. */
74bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   Addr      stack_min;     /**< Current stack pointer. */
75bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   Addr      stack_startup; /**<Stack pointer after pthread_create() finished.*/
76bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   Addr      stack_max;     /**< Top of stack. */
77bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   SizeT     stack_size;    /**< Maximum size of stack. */
78d45d99553c15a361bb797d21ec6afb9bad22d2d4bart   char      name[64];      /**< User-assigned thread name. */
79383d613d286501412f1760c8eaafaeb8c518b344bart   Bool      on_alt_stack;
806d956dc21b6a81f0c67ad18ac61867da0d5922a3bart   /** Whether this structure contains valid information. */
816d956dc21b6a81f0c67ad18ac61867da0d5922a3bart   Bool      valid;
82bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   /** Indicates whether the Valgrind core knows about this thread. */
83bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   Bool      vg_thread_exists;
84bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   /** Indicates whether there is an associated POSIX thread ID. */
85bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   Bool      posix_thread_exists;
86bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   /**
87bedfd237fbdc80d0c917cfcb85a94b5561c92633bart    * If true, indicates that there is a corresponding POSIX thread ID and
88bedfd237fbdc80d0c917cfcb85a94b5561c92633bart    * a corresponding OS thread that is detached.
89bedfd237fbdc80d0c917cfcb85a94b5561c92633bart    */
90bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   Bool      detached_posix_thread;
91d45d99553c15a361bb797d21ec6afb9bad22d2d4bart   /** Wether recording of memory load accesses is currently enabled. */
92d45d99553c15a361bb797d21ec6afb9bad22d2d4bart   Bool      is_recording_loads;
93d45d99553c15a361bb797d21ec6afb9bad22d2d4bart   /** Wether recording of memory load accesses is currently enabled. */
94d45d99553c15a361bb797d21ec6afb9bad22d2d4bart   Bool      is_recording_stores;
95dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart   /** pthread_create() nesting level. */
96dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart   Int       pthread_create_nesting_level;
97bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   /** Nesting level of synchronization functions called by the client. */
98bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   Int       synchr_nesting;
996d956dc21b6a81f0c67ad18ac61867da0d5922a3bart   /** Delayed thread deletion sequence number. */
1006d956dc21b6a81f0c67ad18ac61867da0d5922a3bart   unsigned  deletion_seq;
101f00a85bc55a2790c31670dd21e449a335c739068bart} ThreadInfo;
102f00a85bc55a2790c31670dd21e449a335c739068bart
103f00a85bc55a2790c31670dd21e449a335c739068bart
104324a23bea57e0bfc27d9442ec00a8eee8294f01abart/*
105324a23bea57e0bfc27d9442ec00a8eee8294f01abart * Local variables of drd_thread.c that are declared here such that these
106324a23bea57e0bfc27d9442ec00a8eee8294f01abart * can be accessed by inline functions.
107324a23bea57e0bfc27d9442ec00a8eee8294f01abart */
108f00a85bc55a2790c31670dd21e449a335c739068bart
10986a87df5949beb1f89ebbed923068faed08d048cbart/**
11086a87df5949beb1f89ebbed923068faed08d048cbart * DRD thread ID of the currently running thread. It is crucial for correct
11186a87df5949beb1f89ebbed923068faed08d048cbart * operation of DRD that this number is always in sync with
11286a87df5949beb1f89ebbed923068faed08d048cbart * VG_(get_running_tid)().
11386a87df5949beb1f89ebbed923068faed08d048cbart */
114324a23bea57e0bfc27d9442ec00a8eee8294f01abartextern DrdThreadId    DRD_(g_drd_running_tid);
11586a87df5949beb1f89ebbed923068faed08d048cbart/** Per-thread information managed by DRD. */
116324a23bea57e0bfc27d9442ec00a8eee8294f01abartextern ThreadInfo     DRD_(g_threadinfo)[DRD_N_THREADS];
11786a87df5949beb1f89ebbed923068faed08d048cbart/** Conflict set for the currently running thread. */
118324a23bea57e0bfc27d9442ec00a8eee8294f01abartextern struct bitmap* DRD_(g_conflict_set);
119f00a85bc55a2790c31670dd21e449a335c739068bart
120f00a85bc55a2790c31670dd21e449a335c739068bart
121324a23bea57e0bfc27d9442ec00a8eee8294f01abart/* Function declarations. */
122af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
12362a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_trace_context_switches)(const Bool t);
12462a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_trace_conflict_set)(const Bool t);
1258f822af9b234e7c553c408eba65a641c4773457fbartvoid DRD_(thread_trace_conflict_set_bm)(const Bool t);
12609dc13f324a19f1bebf58c7f197a581dc8fb528bbartBool DRD_(thread_get_trace_fork_join)(void);
12709dc13f324a19f1bebf58c7f197a581dc8fb528bbartvoid DRD_(thread_set_trace_fork_join)(const Bool t);
12862a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_set_segment_merging)(const Bool m);
1298f822af9b234e7c553c408eba65a641c4773457fbartint DRD_(thread_get_segment_merge_interval)(void);
1308f822af9b234e7c553c408eba65a641c4773457fbartvoid DRD_(thread_set_segment_merge_interval)(const int i);
1316d956dc21b6a81f0c67ad18ac61867da0d5922a3bartvoid DRD_(thread_set_join_list_vol)(const int jlv);
132af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
133e278ab506b9a73ef1c17a17077546b2de9a11d7cbartvoid DRD_(thread_init)(void);
13462a784c9382fdf7184065ad76ae8d3b905605f21bartDrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid);
13562a784c9382fdf7184065ad76ae8d3b905605f21bartDrdThreadId DRD_(NewVgThreadIdToDrdThreadId)(const ThreadId tid);
13662a784c9382fdf7184065ad76ae8d3b905605f21bartDrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid);
13762a784c9382fdf7184065ad76ae8d3b905605f21bartThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid);
13862a784c9382fdf7184065ad76ae8d3b905605f21bartDrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
139bedfd237fbdc80d0c917cfcb85a94b5561c92633bart                                    const ThreadId vg_created);
14062a784c9382fdf7184065ad76ae8d3b905605f21bartDrdThreadId DRD_(thread_post_create)(const ThreadId vg_created);
14109dc13f324a19f1bebf58c7f197a581dc8fb528bbartvoid DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee);
1429194e93ab737439d653498ee43df4bae7989ad3dbartvoid DRD_(thread_delete)(const DrdThreadId tid, Bool detached);
14362a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_finished)(const DrdThreadId tid);
1445c7e6b69ccaecc32ff43d7f3cb9573f3901d19f0bartvoid DRD_(drd_thread_atfork_child)(const DrdThreadId tid);
14562a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_pre_cancel)(const DrdThreadId tid);
146bedfd237fbdc80d0c917cfcb85a94b5561c92633bartvoid DRD_(thread_set_stack_startup)(const DrdThreadId tid,
147bedfd237fbdc80d0c917cfcb85a94b5561c92633bart                                    const Addr stack_startup);
14862a784c9382fdf7184065ad76ae8d3b905605f21bartAddr DRD_(thread_get_stack_min)(const DrdThreadId tid);
14962a784c9382fdf7184065ad76ae8d3b905605f21bartAddr DRD_(thread_get_stack_min_min)(const DrdThreadId tid);
15062a784c9382fdf7184065ad76ae8d3b905605f21bartAddr DRD_(thread_get_stack_max)(const DrdThreadId tid);
15162a784c9382fdf7184065ad76ae8d3b905605f21bartSizeT DRD_(thread_get_stack_size)(const DrdThreadId tid);
152383d613d286501412f1760c8eaafaeb8c518b344bartBool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid);
153383d613d286501412f1760c8eaafaeb8c518b344bartvoid DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
154383d613d286501412f1760c8eaafaeb8c518b344bart                                   const Bool on_alt_stack);
155383d613d286501412f1760c8eaafaeb8c518b344bartInt DRD_(thread_get_threads_on_alt_stack)(void);
15662a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid);
15762a784c9382fdf7184065ad76ae8d3b905605f21bartBool DRD_(thread_get_joinable)(const DrdThreadId tid);
15862a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable);
159dd75cdfe7612f58bdba8e3a16d34ee32eef85980bartvoid DRD_(thread_entering_pthread_create)(const DrdThreadId tid);
160dd75cdfe7612f58bdba8e3a16d34ee32eef85980bartvoid DRD_(thread_left_pthread_create)(const DrdThreadId tid);
161d45d99553c15a361bb797d21ec6afb9bad22d2d4bartconst char* DRD_(thread_get_name)(const DrdThreadId tid);
162d45d99553c15a361bb797d21ec6afb9bad22d2d4bartvoid DRD_(thread_set_name)(const DrdThreadId tid, const char* const name);
16362a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid);
16462a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_set_running_tid)(const ThreadId vg_tid,
165bedfd237fbdc80d0c917cfcb85a94b5561c92633bart                                  const DrdThreadId drd_tid);
16662a784c9382fdf7184065ad76ae8d3b905605f21bartint DRD_(thread_enter_synchr)(const DrdThreadId tid);
16762a784c9382fdf7184065ad76ae8d3b905605f21bartint DRD_(thread_leave_synchr)(const DrdThreadId tid);
16862a784c9382fdf7184065ad76ae8d3b905605f21bartint DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid);
16962a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_new_segment)(const DrdThreadId tid);
17062a784c9382fdf7184065ad76ae8d3b905605f21bartVectorClock* DRD_(thread_get_vc)(const DrdThreadId tid);
17162a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid);
1728f822af9b234e7c553c408eba65a641c4773457fbartvoid DRD_(thread_combine_vc_join)(const DrdThreadId joiner,
1738f822af9b234e7c553c408eba65a641c4773457fbart                                  const DrdThreadId joinee);
174f6ec1fe18f1fc1222fb274d7dfd4343c16048fb1bartvoid DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid,
175f6ec1fe18f1fc1222fb274d7dfd4343c16048fb1bart                                             const Segment* sg);
1768f822af9b234e7c553c408eba65a641c4773457fbartvoid DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1778f822af9b234e7c553c408eba65a641c4773457fbart                                      const VectorClock* const old_vc);
17862a784c9382fdf7184065ad76ae8d3b905605f21bart
17923ef19de837854c360809e59833513360dc1cce2bartvoid DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2);
180d45d99553c15a361bb797d21ec6afb9bad22d2d4bartvoid DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled);
181d45d99553c15a361bb797d21ec6afb9bad22d2d4bartvoid DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled);
18262a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_print_all)(void);
18362a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_report_races)(const DrdThreadId tid);
18462a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_report_races_segment)(const DrdThreadId tid,
185bedfd237fbdc80d0c917cfcb85a94b5561c92633bart                                       const Segment* const p);
18662a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_report_all_races)(void);
18762a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
188bedfd237fbdc80d0c917cfcb85a94b5561c92633bart                                              const Addr addr,
189bedfd237fbdc80d0c917cfcb85a94b5561c92633bart                                              const SizeT size,
190bedfd237fbdc80d0c917cfcb85a94b5561c92633bart                                              const BmAccessTypeT access_type);
19162a784c9382fdf7184065ad76ae8d3b905605f21bartULong DRD_(thread_get_context_switch_count)(void);
19262a784c9382fdf7184065ad76ae8d3b905605f21bartULong DRD_(thread_get_report_races_count)(void);
19362a784c9382fdf7184065ad76ae8d3b905605f21bartULong DRD_(thread_get_discard_ordered_segments_count)(void);
19454803feb0b92e4708d3cee92e7449f802be70197bartULong DRD_(thread_get_compute_conflict_set_count)(void);
19554803feb0b92e4708d3cee92e7449f802be70197bartULong DRD_(thread_get_update_conflict_set_count)(void);
196e5214666ead5aebb79ad662deacff0a952cba70dbartULong DRD_(thread_get_update_conflict_set_new_sg_count)(void);
197e5214666ead5aebb79ad662deacff0a952cba70dbartULong DRD_(thread_get_update_conflict_set_sync_count)(void);
198e5214666ead5aebb79ad662deacff0a952cba70dbartULong DRD_(thread_get_update_conflict_set_join_count)(void);
19962a784c9382fdf7184065ad76ae8d3b905605f21bartULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void);
20062a784c9382fdf7184065ad76ae8d3b905605f21bartULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void);
201af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
202af44c8236f7a73e71b16b707bba56f33af4d01cesewardj
203324a23bea57e0bfc27d9442ec00a8eee8294f01abart/* Inline function definitions. */
204324a23bea57e0bfc27d9442ec00a8eee8294f01abart
20586a87df5949beb1f89ebbed923068faed08d048cbart/**
20686a87df5949beb1f89ebbed923068faed08d048cbart * Whether or not the specified DRD thread ID is valid.
20786a87df5949beb1f89ebbed923068faed08d048cbart *
20886a87df5949beb1f89ebbed923068faed08d048cbart * A DRD thread ID is valid if and only if the following conditions are met:
20986a87df5949beb1f89ebbed923068faed08d048cbart * - The ID is a valid index of the DRD_(g_threadinfo)[] array.
21086a87df5949beb1f89ebbed923068faed08d048cbart * - The ID is not equal to DRD_INVALID_THREADID.
21186a87df5949beb1f89ebbed923068faed08d048cbart * - The ID refers either to a thread known by the Valgrind core, a joinable
21286a87df5949beb1f89ebbed923068faed08d048cbart *   thread that has not yet been joined or a detached thread.
21386a87df5949beb1f89ebbed923068faed08d048cbart */
214bf80e12418014298bef23cddea3249dee74e5b96bartstatic __inline__
21562a784c9382fdf7184065ad76ae8d3b905605f21bartBool DRD_(IsValidDrdThreadId)(const DrdThreadId tid)
216bf80e12418014298bef23cddea3249dee74e5b96bart{
217bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
2186d956dc21b6a81f0c67ad18ac61867da0d5922a3bart           && (DRD_(g_threadinfo)[tid].valid));
219bf80e12418014298bef23cddea3249dee74e5b96bart}
220bf80e12418014298bef23cddea3249dee74e5b96bart
22186a87df5949beb1f89ebbed923068faed08d048cbart/** Returns the DRD thread ID of the currently running thread. */
222088656233045bfaa98d73a9a83eab0a412bd3f2bbartstatic __inline__
22362a784c9382fdf7184065ad76ae8d3b905605f21bartDrdThreadId DRD_(thread_get_running_tid)(void)
2241ea5fff9a68ea6d56cb946eb709838bd40562a79bart{
225dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
226bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
227dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart#endif
228bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   return DRD_(g_drd_running_tid);
2291ea5fff9a68ea6d56cb946eb709838bd40562a79bart}
2301ea5fff9a68ea6d56cb946eb709838bd40562a79bart
23186a87df5949beb1f89ebbed923068faed08d048cbart/** Returns a pointer to the conflict set for the currently running thread. */
232088656233045bfaa98d73a9a83eab0a412bd3f2bbartstatic __inline__
23362a784c9382fdf7184065ad76ae8d3b905605f21bartstruct bitmap* DRD_(thread_get_conflict_set)(void)
2341a473c757cae99f11477440d5473be6e5c95e0cebart{
235bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   return DRD_(g_conflict_set);
2361a473c757cae99f11477440d5473be6e5c95e0cebart}
2371a473c757cae99f11477440d5473be6e5c95e0cebart
23886a87df5949beb1f89ebbed923068faed08d048cbart/**
239dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart * Reports whether or not the currently running client thread is executing code
240dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart * inside the pthread_create() function.
241dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart */
242dd75cdfe7612f58bdba8e3a16d34ee32eef85980bartstatic __inline__
243dd75cdfe7612f58bdba8e3a16d34ee32eef85980bartBool DRD_(running_thread_inside_pthread_create)(void)
244dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart{
245dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart   return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)]
246dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart           .pthread_create_nesting_level > 0);
247dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart}
248dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart
249dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart/**
25031b983d29affe6c30a2283be8824c6d75c74d848bart * Reports whether or not recording of memory loads is enabled for the
251dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart * currently running client thread.
25286a87df5949beb1f89ebbed923068faed08d048cbart */
253088656233045bfaa98d73a9a83eab0a412bd3f2bbartstatic __inline__
254d45d99553c15a361bb797d21ec6afb9bad22d2d4bartBool DRD_(running_thread_is_recording_loads)(void)
255f00a85bc55a2790c31670dd21e449a335c739068bart{
2568b4b2eee19af852047cafd270c48c09d8adea33fbart#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
257bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   tl_assert(0 <= (int)DRD_(g_drd_running_tid)
258bedfd237fbdc80d0c917cfcb85a94b5561c92633bart             && DRD_(g_drd_running_tid) < DRD_N_THREADS
259bedfd237fbdc80d0c917cfcb85a94b5561c92633bart             && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
260589f9487830f84b24108bed90b4d71c392640dc9bart#endif
261bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
262d45d99553c15a361bb797d21ec6afb9bad22d2d4bart           && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_loads);
263d45d99553c15a361bb797d21ec6afb9bad22d2d4bart}
264d45d99553c15a361bb797d21ec6afb9bad22d2d4bart
265dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart/**
26631b983d29affe6c30a2283be8824c6d75c74d848bart * Reports whether or not recording memory stores is enabled for the
267dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart * currently running client thread.
268dd75cdfe7612f58bdba8e3a16d34ee32eef85980bart */
269d45d99553c15a361bb797d21ec6afb9bad22d2d4bartstatic __inline__
270d45d99553c15a361bb797d21ec6afb9bad22d2d4bartBool DRD_(running_thread_is_recording_stores)(void)
271d45d99553c15a361bb797d21ec6afb9bad22d2d4bart{
272d45d99553c15a361bb797d21ec6afb9bad22d2d4bart#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
273d45d99553c15a361bb797d21ec6afb9bad22d2d4bart   tl_assert(0 <= (int)DRD_(g_drd_running_tid)
274d45d99553c15a361bb797d21ec6afb9bad22d2d4bart             && DRD_(g_drd_running_tid) < DRD_N_THREADS
275d45d99553c15a361bb797d21ec6afb9bad22d2d4bart             && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
276d45d99553c15a361bb797d21ec6afb9bad22d2d4bart#endif
277d45d99553c15a361bb797d21ec6afb9bad22d2d4bart   return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
278d45d99553c15a361bb797d21ec6afb9bad22d2d4bart           && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_stores);
2791a473c757cae99f11477440d5473be6e5c95e0cebart}
2801a473c757cae99f11477440d5473be6e5c95e0cebart
28186a87df5949beb1f89ebbed923068faed08d048cbart/**
28286a87df5949beb1f89ebbed923068faed08d048cbart * Update the information about the lowest stack address that has ever been
28386a87df5949beb1f89ebbed923068faed08d048cbart * accessed by a thread.
28486a87df5949beb1f89ebbed923068faed08d048cbart */
285088656233045bfaa98d73a9a83eab0a412bd3f2bbartstatic __inline__
28662a784c9382fdf7184065ad76ae8d3b905605f21bartvoid DRD_(thread_set_stack_min)(const DrdThreadId tid, const Addr stack_min)
2871ea5fff9a68ea6d56cb946eb709838bd40562a79bart{
2888b4b2eee19af852047cafd270c48c09d8adea33fbart#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
289bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   tl_assert(0 <= (int)tid
290bedfd237fbdc80d0c917cfcb85a94b5561c92633bart             && tid < DRD_N_THREADS
291bedfd237fbdc80d0c917cfcb85a94b5561c92633bart             && tid != DRD_INVALID_THREADID);
2921ea5fff9a68ea6d56cb946eb709838bd40562a79bart#endif
293bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   DRD_(g_threadinfo)[tid].stack_min = stack_min;
2948b4b2eee19af852047cafd270c48c09d8adea33fbart#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
295bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   /* This function can be called after the thread has been created but */
296bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   /* before drd_post_thread_create() has filled in stack_max.          */
297bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   tl_assert(DRD_(g_threadinfo)[tid].stack_min
298bedfd237fbdc80d0c917cfcb85a94b5561c92633bart             < DRD_(g_threadinfo)[tid].stack_max
299bedfd237fbdc80d0c917cfcb85a94b5561c92633bart             || DRD_(g_threadinfo)[tid].stack_max == 0);
3001ea5fff9a68ea6d56cb946eb709838bd40562a79bart#endif
301bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   if (UNLIKELY(stack_min < DRD_(g_threadinfo)[tid].stack_min_min))
302bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   {
303bedfd237fbdc80d0c917cfcb85a94b5561c92633bart      DRD_(g_threadinfo)[tid].stack_min_min = stack_min;
304bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   }
3051ea5fff9a68ea6d56cb946eb709838bd40562a79bart}
3061ea5fff9a68ea6d56cb946eb709838bd40562a79bart
307324a23bea57e0bfc27d9442ec00a8eee8294f01abart/**
308324a23bea57e0bfc27d9442ec00a8eee8294f01abart * Return true if and only if the specified address is on the stack of the
309324a23bea57e0bfc27d9442ec00a8eee8294f01abart * currently scheduled thread.
310088656233045bfaa98d73a9a83eab0a412bd3f2bbart */
311088656233045bfaa98d73a9a83eab0a412bd3f2bbartstatic __inline__
31262a784c9382fdf7184065ad76ae8d3b905605f21bartBool DRD_(thread_address_on_stack)(const Addr a)
313088656233045bfaa98d73a9a83eab0a412bd3f2bbart{
314bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_min <= a
315bedfd237fbdc80d0c917cfcb85a94b5561c92633bart           && a < DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_max);
316088656233045bfaa98d73a9a83eab0a412bd3f2bbart}
317088656233045bfaa98d73a9a83eab0a412bd3f2bbart
3180d6d5c5a55674ddcf2842ef43f78d80a1095a770bart/**
3190d6d5c5a55674ddcf2842ef43f78d80a1095a770bart * Return true if and only if the specified address is on the stack of any
3200d6d5c5a55674ddcf2842ef43f78d80a1095a770bart * thread.
3210d6d5c5a55674ddcf2842ef43f78d80a1095a770bart */
3220d6d5c5a55674ddcf2842ef43f78d80a1095a770bartstatic __inline__
3230d6d5c5a55674ddcf2842ef43f78d80a1095a770bartBool DRD_(thread_address_on_any_stack)(const Addr a)
3240d6d5c5a55674ddcf2842ef43f78d80a1095a770bart{
325bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   int i;
326bedfd237fbdc80d0c917cfcb85a94b5561c92633bart
327bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   for (i = 1; i < DRD_N_THREADS; i++)
328bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   {
329bedfd237fbdc80d0c917cfcb85a94b5561c92633bart      if (DRD_(g_threadinfo)[i].vg_thread_exists
330bedfd237fbdc80d0c917cfcb85a94b5561c92633bart          && DRD_(g_threadinfo)[i].stack_min <= a
331bedfd237fbdc80d0c917cfcb85a94b5561c92633bart          && a < DRD_(g_threadinfo)[i].stack_max)
332bedfd237fbdc80d0c917cfcb85a94b5561c92633bart      {
333bedfd237fbdc80d0c917cfcb85a94b5561c92633bart         return True;
334bedfd237fbdc80d0c917cfcb85a94b5561c92633bart      }
335bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   }
336bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   return False;
3370d6d5c5a55674ddcf2842ef43f78d80a1095a770bart}
3380d6d5c5a55674ddcf2842ef43f78d80a1095a770bart
3391a473c757cae99f11477440d5473be6e5c95e0cebart/** Return a pointer to the latest segment for the specified thread. */
340088656233045bfaa98d73a9a83eab0a412bd3f2bbartstatic __inline__
34162a784c9382fdf7184065ad76ae8d3b905605f21bartSegment* DRD_(thread_get_segment)(const DrdThreadId tid)
3421a473c757cae99f11477440d5473be6e5c95e0cebart{
343e278ab506b9a73ef1c17a17077546b2de9a11d7cbart   struct list_head* sg_list;
344e278ab506b9a73ef1c17a17077546b2de9a11d7cbart
3458b4b2eee19af852047cafd270c48c09d8adea33fbart#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
346bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
347bedfd237fbdc80d0c917cfcb85a94b5561c92633bart             && tid != DRD_INVALID_THREADID);
348589f9487830f84b24108bed90b4d71c392640dc9bart#endif
349e278ab506b9a73ef1c17a17077546b2de9a11d7cbart   sg_list = &DRD_(g_threadinfo)[tid].sg_list;
350e278ab506b9a73ef1c17a17077546b2de9a11d7cbart#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
351e278ab506b9a73ef1c17a17077546b2de9a11d7cbart   tl_assert(!list_empty(sg_list));
352e278ab506b9a73ef1c17a17077546b2de9a11d7cbart#endif
353e278ab506b9a73ef1c17a17077546b2de9a11d7cbart   return list_last_entry(sg_list, Segment, thr_list);
354f00a85bc55a2790c31670dd21e449a335c739068bart}
355f00a85bc55a2790c31670dd21e449a335c739068bart
356a79df6ed701ed416ba6241e5b0dd95fae8fa35f9bart/** Return a pointer to the latest segment for the running thread. */
357088656233045bfaa98d73a9a83eab0a412bd3f2bbartstatic __inline__
35862a784c9382fdf7184065ad76ae8d3b905605f21bartSegment* DRD_(running_thread_get_segment)(void)
359a79df6ed701ed416ba6241e5b0dd95fae8fa35f9bart{
360bedfd237fbdc80d0c917cfcb85a94b5561c92633bart   return DRD_(thread_get_segment)(DRD_(g_drd_running_tid));
361a79df6ed701ed416ba6241e5b0dd95fae8fa35f9bart}
362f00a85bc55a2790c31670dd21e449a335c739068bart
36386a87df5949beb1f89ebbed923068faed08d048cbart#endif /* __THREAD_H */
364