blob: b38642bce36fa63504f0e88fefafdf8eec53509d [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
10 Copyright (C) 2000-2005 Julian Seward
11 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"
32#include "pub_core_threadstate.h"
33#include "pub_core_libcassert.h"
34#include "pub_core_machine.h" // For VG_(get_SP)
35
36/*------------------------------------------------------------*/
37/*--- Data structures. ---*/
38/*------------------------------------------------------------*/
39
40ThreadId VG_(running_tid) = VG_INVALID_THREADID;
41
42ThreadState VG_(threads)[VG_N_THREADS];
43
44/*------------------------------------------------------------*/
45/*--- Operations. ---*/
46/*------------------------------------------------------------*/
47
48const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status )
49{
50 switch (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_???";
58 }
59}
60
61ThreadState *VG_(get_ThreadState)(ThreadId tid)
62{
63 vg_assert(tid >= 0 && tid < VG_N_THREADS);
64 return &VG_(threads)[tid];
65}
66
67Bool VG_(is_valid_tid) ( ThreadId tid )
68{
69 /* tid is unsigned, hence no < 0 test. */
70 if (tid == 0) return False;
71 if (tid >= VG_N_THREADS) return False;
72 if (VG_(threads)[tid].status == VgTs_Empty) return False;
73 return True;
74}
75
76// This function is for tools to call.
77ThreadId VG_(get_running_tid)(void)
78{
79 return VG_(running_tid);
80}
81
82Bool VG_(is_running_thread)(ThreadId tid)
83{
84 ThreadState *tst = VG_(get_ThreadState)(tid);
85
86 return
87// tst->os_state.lwpid == VG_(gettid)() && // check we're this tid
88 VG_(running_tid) == tid && // and that we've got the lock
89 tst->status == VgTs_Runnable; // and we're runnable
90}
91
92/* Return true if the thread is still alive but in the process of exiting. */
93inline Bool VG_(is_exiting)(ThreadId tid)
94{
95 vg_assert(VG_(is_valid_tid)(tid));
96 return VG_(threads)[tid].exitreason != VgSrc_None;
97}
98
99/* Return the number of non-dead Threads */
100Int VG_(count_living_threads)(void)
101{
102 Int count = 0;
103 ThreadId tid;
104
105 for(tid = 1; tid < VG_N_THREADS; tid++)
106 if (VG_(threads)[tid].status != VgTs_Empty &&
107 VG_(threads)[tid].status != VgTs_Zombie)
108 count++;
109
110 return count;
111}
112
113/* Given an LWP id (ie, real kernel thread id), find the corresponding
114 ThreadId */
115ThreadId VG_(get_lwp_tid)(Int lwp)
116{
117 ThreadId tid;
118
119 for(tid = 1; tid < VG_N_THREADS; tid++)
120 if (VG_(threads)[tid].status != VgTs_Empty && VG_(threads)[tid].os_state.lwpid == lwp)
121 return tid;
122
123 return VG_INVALID_THREADID;
124}
125
126/* For constructing error messages only: try and identify a thread
127 whose stack satisfies the predicate p, or return VG_INVALID_THREADID
128 if none do.
129*/
130ThreadId VG_(first_matching_thread_stack)
131 ( Bool (*p) ( Addr stack_min, Addr stack_max, void* d ),
132 void* d )
133{
134 ThreadId tid;
135
136 for (tid = 1; tid < VG_N_THREADS; tid++) {
137 if (VG_(threads)[tid].status == VgTs_Empty) continue;
138
139 if ( p ( VG_(get_SP)(tid),
140 VG_(threads)[tid].client_stack_highest_word, d ) )
141 return tid;
142 }
143 return VG_INVALID_THREADID;
144}
145
146/*--------------------------------------------------------------------*/
147/*--- end ---*/
148/*--------------------------------------------------------------------*/