njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 1 | |
| 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 | |
sewardj | 03f8d3f | 2012-08-05 15:46:46 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2012 Julian Seward |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 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" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 32 | #include "pub_core_vki.h" |
sewardj | 6c591e1 | 2011-04-11 16:17:51 +0000 | [diff] [blame] | 33 | #include "pub_core_libcsetjmp.h" // to keep _threadstate.h happy |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 34 | #include "pub_core_threadstate.h" |
| 35 | #include "pub_core_libcassert.h" |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 36 | #include "pub_tool_inner.h" |
| 37 | #if defined(ENABLE_INNER_CLIENT_REQUEST) |
| 38 | #include "helgrind/helgrind.h" |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 39 | #endif |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 40 | |
| 41 | /*------------------------------------------------------------*/ |
| 42 | /*--- Data structures. ---*/ |
| 43 | /*------------------------------------------------------------*/ |
| 44 | |
| 45 | ThreadId VG_(running_tid) = VG_INVALID_THREADID; |
| 46 | |
sewardj | 02e97e9 | 2012-08-02 22:08:53 +0000 | [diff] [blame] | 47 | ThreadState VG_(threads)[VG_N_THREADS] __attribute__((aligned(16))); |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 48 | |
| 49 | /*------------------------------------------------------------*/ |
| 50 | /*--- Operations. ---*/ |
| 51 | /*------------------------------------------------------------*/ |
| 52 | |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 53 | void VG_(init_Threads)(void) |
| 54 | { |
| 55 | ThreadId tid; |
| 56 | |
bart | 92e2c64 | 2012-03-08 19:07:08 +0000 | [diff] [blame] | 57 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
| 58 | INNER_REQUEST( |
| 59 | ANNOTATE_BENIGN_RACE_SIZED(&VG_(threads)[tid].status, |
| 60 | sizeof(VG_(threads)[tid].status), "")); |
| 61 | INNER_REQUEST( |
| 62 | ANNOTATE_BENIGN_RACE_SIZED(&VG_(threads)[tid].os_state.exitcode, |
| 63 | sizeof(VG_(threads)[tid].os_state.exitcode), |
| 64 | "")); |
| 65 | } |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 68 | const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status ) |
| 69 | { |
| 70 | switch (status) { |
| 71 | case VgTs_Empty: return "VgTs_Empty"; |
| 72 | case VgTs_Init: return "VgTs_Init"; |
| 73 | case VgTs_Runnable: return "VgTs_Runnable"; |
| 74 | case VgTs_WaitSys: return "VgTs_WaitSys"; |
| 75 | case VgTs_Yielding: return "VgTs_Yielding"; |
| 76 | case VgTs_Zombie: return "VgTs_Zombie"; |
| 77 | default: return "VgTs_???"; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | ThreadState *VG_(get_ThreadState)(ThreadId tid) |
| 82 | { |
| 83 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
sewardj | 13ac35b | 2005-07-08 18:23:40 +0000 | [diff] [blame] | 84 | vg_assert(VG_(threads)[tid].tid == tid); |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 85 | return &VG_(threads)[tid]; |
| 86 | } |
| 87 | |
| 88 | Bool VG_(is_valid_tid) ( ThreadId tid ) |
| 89 | { |
| 90 | /* tid is unsigned, hence no < 0 test. */ |
| 91 | if (tid == 0) return False; |
| 92 | if (tid >= VG_N_THREADS) return False; |
| 93 | if (VG_(threads)[tid].status == VgTs_Empty) return False; |
| 94 | return True; |
| 95 | } |
| 96 | |
| 97 | // This function is for tools to call. |
| 98 | ThreadId VG_(get_running_tid)(void) |
| 99 | { |
| 100 | return VG_(running_tid); |
| 101 | } |
| 102 | |
| 103 | Bool VG_(is_running_thread)(ThreadId tid) |
| 104 | { |
| 105 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 106 | |
| 107 | return |
| 108 | // tst->os_state.lwpid == VG_(gettid)() && // check we're this tid |
| 109 | VG_(running_tid) == tid && // and that we've got the lock |
| 110 | tst->status == VgTs_Runnable; // and we're runnable |
| 111 | } |
| 112 | |
| 113 | /* Return true if the thread is still alive but in the process of exiting. */ |
| 114 | inline Bool VG_(is_exiting)(ThreadId tid) |
| 115 | { |
| 116 | vg_assert(VG_(is_valid_tid)(tid)); |
| 117 | return VG_(threads)[tid].exitreason != VgSrc_None; |
| 118 | } |
| 119 | |
| 120 | /* Return the number of non-dead Threads */ |
| 121 | Int VG_(count_living_threads)(void) |
| 122 | { |
| 123 | Int count = 0; |
| 124 | ThreadId tid; |
| 125 | |
| 126 | for(tid = 1; tid < VG_N_THREADS; tid++) |
| 127 | if (VG_(threads)[tid].status != VgTs_Empty && |
| 128 | VG_(threads)[tid].status != VgTs_Zombie) |
| 129 | count++; |
| 130 | |
| 131 | return count; |
| 132 | } |
| 133 | |
sewardj | d8a725e | 2006-10-17 02:01:12 +0000 | [diff] [blame] | 134 | /* Return the number of threads in VgTs_Runnable state */ |
| 135 | Int VG_(count_runnable_threads)(void) |
| 136 | { |
| 137 | Int count = 0; |
| 138 | ThreadId tid; |
| 139 | |
| 140 | for(tid = 1; tid < VG_N_THREADS; tid++) |
| 141 | if (VG_(threads)[tid].status == VgTs_Runnable) |
| 142 | count++; |
| 143 | |
| 144 | return count; |
| 145 | } |
| 146 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 147 | /* Given an LWP id (ie, real kernel thread id), find the corresponding |
| 148 | ThreadId */ |
sewardj | 4278172 | 2006-12-17 19:36:06 +0000 | [diff] [blame] | 149 | ThreadId VG_(lwpid_to_vgtid)(Int lwp) |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 150 | { |
| 151 | ThreadId tid; |
| 152 | |
| 153 | for(tid = 1; tid < VG_N_THREADS; tid++) |
sewardj | d8a725e | 2006-10-17 02:01:12 +0000 | [diff] [blame] | 154 | if (VG_(threads)[tid].status != VgTs_Empty |
| 155 | && VG_(threads)[tid].os_state.lwpid == lwp) |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 156 | return tid; |
| 157 | |
| 158 | return VG_INVALID_THREADID; |
| 159 | } |
| 160 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 161 | /*--------------------------------------------------------------------*/ |
| 162 | /*--- end ---*/ |
| 163 | /*--------------------------------------------------------------------*/ |