1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*--------------------------------------------------------------------*/
/*--- The thread state. m_threadstate.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Valgrind, a dynamic binary instrumentation
framework.
Copyright (C) 2000-2011 Julian Seward
jseward@acm.org
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#include "pub_core_basics.h"
#include "pub_core_vki.h"
#include "pub_core_libcsetjmp.h" // to keep _threadstate.h happy
#include "pub_core_threadstate.h"
#include "pub_core_libcassert.h"
/*------------------------------------------------------------*/
/*--- Data structures. ---*/
/*------------------------------------------------------------*/
ThreadId VG_(running_tid) = VG_INVALID_THREADID;
ThreadState VG_(threads)[VG_N_THREADS];
/*------------------------------------------------------------*/
/*--- Operations. ---*/
/*------------------------------------------------------------*/
const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status )
{
switch (status) {
case VgTs_Empty: return "VgTs_Empty";
case VgTs_Init: return "VgTs_Init";
case VgTs_Runnable: return "VgTs_Runnable";
case VgTs_WaitSys: return "VgTs_WaitSys";
case VgTs_Yielding: return "VgTs_Yielding";
case VgTs_Zombie: return "VgTs_Zombie";
default: return "VgTs_???";
}
}
ThreadState *VG_(get_ThreadState)(ThreadId tid)
{
vg_assert(tid >= 0 && tid < VG_N_THREADS);
vg_assert(VG_(threads)[tid].tid == tid);
return &VG_(threads)[tid];
}
Bool VG_(is_valid_tid) ( ThreadId tid )
{
/* tid is unsigned, hence no < 0 test. */
if (tid == 0) return False;
if (tid >= VG_N_THREADS) return False;
if (VG_(threads)[tid].status == VgTs_Empty) return False;
return True;
}
// This function is for tools to call.
ThreadId VG_(get_running_tid)(void)
{
return VG_(running_tid);
}
Bool VG_(is_running_thread)(ThreadId tid)
{
ThreadState *tst = VG_(get_ThreadState)(tid);
return
// tst->os_state.lwpid == VG_(gettid)() && // check we're this tid
VG_(running_tid) == tid && // and that we've got the lock
tst->status == VgTs_Runnable; // and we're runnable
}
/* Return true if the thread is still alive but in the process of exiting. */
inline Bool VG_(is_exiting)(ThreadId tid)
{
vg_assert(VG_(is_valid_tid)(tid));
return VG_(threads)[tid].exitreason != VgSrc_None;
}
/* Return the number of non-dead Threads */
Int VG_(count_living_threads)(void)
{
Int count = 0;
ThreadId tid;
for(tid = 1; tid < VG_N_THREADS; tid++)
if (VG_(threads)[tid].status != VgTs_Empty &&
VG_(threads)[tid].status != VgTs_Zombie)
count++;
return count;
}
/* Return the number of threads in VgTs_Runnable state */
Int VG_(count_runnable_threads)(void)
{
Int count = 0;
ThreadId tid;
for(tid = 1; tid < VG_N_THREADS; tid++)
if (VG_(threads)[tid].status == VgTs_Runnable)
count++;
return count;
}
/* Given an LWP id (ie, real kernel thread id), find the corresponding
ThreadId */
ThreadId VG_(lwpid_to_vgtid)(Int lwp)
{
ThreadId tid;
for(tid = 1; tid < VG_N_THREADS; tid++)
if (VG_(threads)[tid].status != VgTs_Empty
&& VG_(threads)[tid].os_state.lwpid == lwp)
return tid;
return VG_INVALID_THREADID;
}
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/
|