FreeBSD: add file descriptor tracking for _umtx_op
[valgrind.git] / coregrind / m_threadstate.c
blob85ae03569d3e4618781d5ec0500bba00b3e48274
2 /*--------------------------------------------------------------------*/
3 /*--- The thread state. m_threadstate.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2000-2017 Julian Seward
11 jseward@acm.org
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
29 #include "pub_core_basics.h"
30 #include "pub_core_vki.h"
31 #include "pub_core_threadstate.h"
32 #include "pub_core_mallocfree.h" // VG_(malloc)
33 #include "pub_core_libcassert.h"
34 #include "pub_core_inner.h"
35 #if defined(ENABLE_INNER_CLIENT_REQUEST)
36 #include "helgrind/helgrind.h"
37 #endif
39 /*------------------------------------------------------------*/
40 /*--- Data structures. ---*/
41 /*------------------------------------------------------------*/
43 ThreadId VG_(running_tid) = VG_INVALID_THREADID;
45 ThreadState *VG_(threads);
46 UInt VG_N_THREADS;
48 ThreadState *VG_(inner_threads);
50 /*------------------------------------------------------------*/
51 /*--- Operations. ---*/
52 /*------------------------------------------------------------*/
54 void VG_(init_Threads)(void)
56 ThreadId tid;
58 VG_(threads) = VG_(arena_memalign) (VG_AR_CORE, "init_Threads",
59 LibVEX_GUEST_STATE_ALIGN,
60 VG_N_THREADS * sizeof VG_(threads)[0]);
62 for (tid = 1; tid < VG_N_THREADS; tid++) {
63 INNER_REQUEST(
64 ANNOTATE_BENIGN_RACE_SIZED(&VG_(threads)[tid].status,
65 sizeof(VG_(threads)[tid].status), ""));
66 INNER_REQUEST(
67 ANNOTATE_BENIGN_RACE_SIZED(&VG_(threads)[tid].os_state.exitcode,
68 sizeof(VG_(threads)[tid].os_state.exitcode),
69 ""));
71 INNER_REQUEST(VALGRIND_INNER_THREADS(VG_(threads)));
74 const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status )
76 switch (status) {
77 case VgTs_Empty: return "VgTs_Empty";
78 case VgTs_Init: return "VgTs_Init";
79 case VgTs_Runnable: return "VgTs_Runnable";
80 case VgTs_WaitSys: return "VgTs_WaitSys";
81 case VgTs_Yielding: return "VgTs_Yielding";
82 case VgTs_Zombie: return "VgTs_Zombie";
83 default: return "VgTs_???";
87 const HChar* VG_(name_of_VgSchedReturnCode) ( VgSchedReturnCode retcode )
89 switch (retcode) {
90 case VgSrc_None: return "VgSrc_None";
91 case VgSrc_ExitThread: return "VgSrc_ExitThread";
92 case VgSrc_ExitProcess: return "VgSrc_ExitProcess";
93 case VgSrc_FatalSig: return "VgSrc_FatalSig";
94 default: return "VgSrc_???";
98 ThreadState *VG_(get_ThreadState)(ThreadId tid)
100 vg_assert(tid < VG_N_THREADS);
101 vg_assert(VG_(threads)[tid].tid == tid);
102 return &VG_(threads)[tid];
105 Bool VG_(is_valid_tid) ( ThreadId tid )
107 /* tid is unsigned, hence no < 0 test. */
108 if (tid == 0) return False;
109 if (tid >= VG_N_THREADS) return False;
110 if (VG_(threads)[tid].status == VgTs_Empty) return False;
111 return True;
114 // This function is for tools to call.
115 ThreadId VG_(get_running_tid)(void)
117 return VG_(running_tid);
120 Bool VG_(is_running_thread)(ThreadId tid)
122 ThreadState *tst = VG_(get_ThreadState)(tid);
124 return
125 // tst->os_state.lwpid == VG_(gettid)() && // check we're this tid
126 VG_(running_tid) == tid && // and that we've got the lock
127 tst->status == VgTs_Runnable; // and we're runnable
130 /* Return true if the thread is still alive but in the process of exiting. */
131 inline Bool VG_(is_exiting)(ThreadId tid)
133 vg_assert(VG_(is_valid_tid)(tid));
134 return VG_(threads)[tid].exitreason != VgSrc_None;
137 /* Return the number of non-dead Threads */
138 Int VG_(count_living_threads)(void)
140 Int count = 0;
141 ThreadId tid;
143 for(tid = 1; tid < VG_N_THREADS; tid++)
144 if (VG_(threads)[tid].status != VgTs_Empty &&
145 VG_(threads)[tid].status != VgTs_Zombie)
146 count++;
148 return count;
151 /* Return the number of threads in VgTs_Runnable state */
152 Int VG_(count_runnable_threads)(void)
154 Int count = 0;
155 ThreadId tid;
157 for(tid = 1; tid < VG_N_THREADS; tid++)
158 if (VG_(threads)[tid].status == VgTs_Runnable)
159 count++;
161 return count;
164 /* Given an LWP id (ie, real kernel thread id), find the corresponding
165 ThreadId */
166 ThreadId VG_(lwpid_to_vgtid)(Int lwp)
168 ThreadId tid;
170 for(tid = 1; tid < VG_N_THREADS; tid++)
171 if (VG_(threads)[tid].status != VgTs_Empty
172 && VG_(threads)[tid].os_state.lwpid == lwp)
173 return tid;
175 return VG_INVALID_THREADID;
178 ThreadId VG_(lwpid_to_vgtid_dead_ok)(Int lwp)
180 ThreadId tid = VG_(lwpid_to_vgtid)(lwp);
182 if (tid != VG_INVALID_THREADID)
183 return tid;
185 for(tid = 1; tid < VG_N_THREADS; tid++)
186 if (VG_(threads)[tid].os_state.lwpid == lwp)
187 return tid;
189 return VG_INVALID_THREADID;
192 /*--------------------------------------------------------------------*/
193 /*--- end ---*/
194 /*--------------------------------------------------------------------*/