blob: c45ce214b5bc464438f24ab27433f6aaf37924a5 [file] [log] [blame]
njnc7561b92005-06-19 01:24:32 +00001
2/*--------------------------------------------------------------------*/
3/*--- The thread state. m_threadstate.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj9eecbbb2010-05-03 21:37:12 +000010 Copyright (C) 2000-2010 Julian Seward
njnc7561b92005-06-19 01:24:32 +000011 jseward@acm.org
12
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.
17
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.
22
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
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31#include "pub_core_basics.h"
sewardj4cfea4f2006-10-14 19:26:10 +000032#include "pub_core_vki.h"
sewardj6c591e12011-04-11 16:17:51 +000033#include "pub_core_libcsetjmp.h" // to keep _threadstate.h happy
njnc7561b92005-06-19 01:24:32 +000034#include "pub_core_threadstate.h"
35#include "pub_core_libcassert.h"
njnc7561b92005-06-19 01:24:32 +000036
37/*------------------------------------------------------------*/
38/*--- Data structures. ---*/
39/*------------------------------------------------------------*/
40
41ThreadId VG_(running_tid) = VG_INVALID_THREADID;
42
43ThreadState VG_(threads)[VG_N_THREADS];
44
45/*------------------------------------------------------------*/
46/*--- Operations. ---*/
47/*------------------------------------------------------------*/
48
49const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status )
50{
51 switch (status) {
52 case VgTs_Empty: return "VgTs_Empty";
53 case VgTs_Init: return "VgTs_Init";
54 case VgTs_Runnable: return "VgTs_Runnable";
55 case VgTs_WaitSys: return "VgTs_WaitSys";
56 case VgTs_Yielding: return "VgTs_Yielding";
57 case VgTs_Zombie: return "VgTs_Zombie";
58 default: return "VgTs_???";
59 }
60}
61
62ThreadState *VG_(get_ThreadState)(ThreadId tid)
63{
64 vg_assert(tid >= 0 && tid < VG_N_THREADS);
sewardj13ac35b2005-07-08 18:23:40 +000065 vg_assert(VG_(threads)[tid].tid == tid);
njnc7561b92005-06-19 01:24:32 +000066 return &VG_(threads)[tid];
67}
68
69Bool VG_(is_valid_tid) ( ThreadId tid )
70{
71 /* tid is unsigned, hence no < 0 test. */
72 if (tid == 0) return False;
73 if (tid >= VG_N_THREADS) return False;
74 if (VG_(threads)[tid].status == VgTs_Empty) return False;
75 return True;
76}
77
78// This function is for tools to call.
79ThreadId VG_(get_running_tid)(void)
80{
81 return VG_(running_tid);
82}
83
84Bool VG_(is_running_thread)(ThreadId tid)
85{
86 ThreadState *tst = VG_(get_ThreadState)(tid);
87
88 return
89// tst->os_state.lwpid == VG_(gettid)() && // check we're this tid
90 VG_(running_tid) == tid && // and that we've got the lock
91 tst->status == VgTs_Runnable; // and we're runnable
92}
93
94/* Return true if the thread is still alive but in the process of exiting. */
95inline Bool VG_(is_exiting)(ThreadId tid)
96{
97 vg_assert(VG_(is_valid_tid)(tid));
98 return VG_(threads)[tid].exitreason != VgSrc_None;
99}
100
101/* Return the number of non-dead Threads */
102Int VG_(count_living_threads)(void)
103{
104 Int count = 0;
105 ThreadId tid;
106
107 for(tid = 1; tid < VG_N_THREADS; tid++)
108 if (VG_(threads)[tid].status != VgTs_Empty &&
109 VG_(threads)[tid].status != VgTs_Zombie)
110 count++;
111
112 return count;
113}
114
sewardjd8a725e2006-10-17 02:01:12 +0000115/* Return the number of threads in VgTs_Runnable state */
116Int VG_(count_runnable_threads)(void)
117{
118 Int count = 0;
119 ThreadId tid;
120
121 for(tid = 1; tid < VG_N_THREADS; tid++)
122 if (VG_(threads)[tid].status == VgTs_Runnable)
123 count++;
124
125 return count;
126}
127
njnc7561b92005-06-19 01:24:32 +0000128/* Given an LWP id (ie, real kernel thread id), find the corresponding
129 ThreadId */
sewardj42781722006-12-17 19:36:06 +0000130ThreadId VG_(lwpid_to_vgtid)(Int lwp)
njnc7561b92005-06-19 01:24:32 +0000131{
132 ThreadId tid;
133
134 for(tid = 1; tid < VG_N_THREADS; tid++)
sewardjd8a725e2006-10-17 02:01:12 +0000135 if (VG_(threads)[tid].status != VgTs_Empty
136 && VG_(threads)[tid].os_state.lwpid == lwp)
njnc7561b92005-06-19 01:24:32 +0000137 return tid;
138
139 return VG_INVALID_THREADID;
140}
141
njnc7561b92005-06-19 01:24:32 +0000142/*--------------------------------------------------------------------*/
143/*--- end ---*/
144/*--------------------------------------------------------------------*/