2 /*--------------------------------------------------------------------*/
3 /*--- The thread state. m_threadstate.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2010 Julian Seward
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, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics.h"
32 #include "pub_core_vki.h"
33 #include "pub_core_threadstate.h"
34 #include "pub_core_libcassert.h"
36 /*------------------------------------------------------------*/
37 /*--- Data structures. ---*/
38 /*------------------------------------------------------------*/
40 ThreadId VG_(running_tid) = VG_INVALID_THREADID;
42 ThreadState VG_(threads)[VG_N_THREADS];
44 /*------------------------------------------------------------*/
45 /*--- Operations. ---*/
46 /*------------------------------------------------------------*/
48 const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status )
51 case VgTs_Empty: return "VgTs_Empty";
52 case VgTs_Init: return "VgTs_Init";
53 case VgTs_Runnable: return "VgTs_Runnable";
54 case VgTs_WaitSys: return "VgTs_WaitSys";
55 case VgTs_Yielding: return "VgTs_Yielding";
56 case VgTs_Zombie: return "VgTs_Zombie";
57 default: return "VgTs_???";
61 ThreadState *VG_(get_ThreadState)(ThreadId tid)
63 vg_assert(tid >= 0 && tid < VG_N_THREADS);
64 vg_assert(VG_(threads)[tid].tid == tid);
65 return &VG_(threads)[tid];
68 Bool VG_(is_valid_tid) ( ThreadId tid )
70 /* tid is unsigned, hence no < 0 test. */
71 if (tid == 0) return False;
72 if (tid >= VG_N_THREADS) return False;
73 if (VG_(threads)[tid].status == VgTs_Empty) return False;
77 // This function is for tools to call.
78 ThreadId VG_(get_running_tid)(void)
80 return VG_(running_tid);
83 Bool VG_(is_running_thread)(ThreadId tid)
85 ThreadState *tst = VG_(get_ThreadState)(tid);
88 // tst->os_state.lwpid == VG_(gettid)() && // check we're this tid
89 VG_(running_tid) == tid && // and that we've got the lock
90 tst->status == VgTs_Runnable; // and we're runnable
93 /* Return true if the thread is still alive but in the process of exiting. */
94 inline Bool VG_(is_exiting)(ThreadId tid)
96 vg_assert(VG_(is_valid_tid)(tid));
97 return VG_(threads)[tid].exitreason != VgSrc_None;
100 /* Return the number of non-dead Threads */
101 Int VG_(count_living_threads)(void)
106 for(tid = 1; tid < VG_N_THREADS; tid++)
107 if (VG_(threads)[tid].status != VgTs_Empty &&
108 VG_(threads)[tid].status != VgTs_Zombie)
114 /* Return the number of threads in VgTs_Runnable state */
115 Int VG_(count_runnable_threads)(void)
120 for(tid = 1; tid < VG_N_THREADS; tid++)
121 if (VG_(threads)[tid].status == VgTs_Runnable)
127 /* Given an LWP id (ie, real kernel thread id), find the corresponding
129 ThreadId VG_(lwpid_to_vgtid)(Int lwp)
133 for(tid = 1; tid < VG_N_THREADS; tid++)
134 if (VG_(threads)[tid].status != VgTs_Empty
135 && VG_(threads)[tid].os_state.lwpid == lwp)
138 return VG_INVALID_THREADID;
141 /*--------------------------------------------------------------------*/
143 /*--------------------------------------------------------------------*/