The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame] | 16 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
| 18 | * VM thread support. |
| 19 | */ |
| 20 | #ifndef _DALVIK_THREAD |
| 21 | #define _DALVIK_THREAD |
| 22 | |
| 23 | #include "jni.h" |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 24 | #include "interp/InterpState.h" |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 26 | #include <errno.h> |
Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 27 | #include <cutils/sched_policy.h> |
| 28 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | #if defined(CHECK_MUTEX) && !defined(__USE_UNIX98) |
Andy McFadden | 0083d37 | 2009-08-21 14:44:04 -0700 | [diff] [blame] | 30 | /* glibc lacks this unless you #define __USE_UNIX98 */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 31 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type); |
| 32 | enum { PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP }; |
| 33 | #endif |
| 34 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 35 | /* |
| 36 | * Current status; these map to JDWP constants, so don't rearrange them. |
| 37 | * (If you do alter this, update the strings in dvmDumpThread and the |
| 38 | * conversion table in VMThread.java.) |
| 39 | * |
| 40 | * Note that "suspended" is orthogonal to these values (so says JDWP). |
| 41 | */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 42 | enum ThreadStatus { |
Andy McFadden | b5f3c0b | 2010-08-23 16:45:24 -0700 | [diff] [blame] | 43 | THREAD_UNDEFINED = -1, /* makes enum compatible with int32_t */ |
Andy McFadden | ab227f7 | 2010-04-06 12:37:48 -0700 | [diff] [blame] | 44 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 45 | /* these match up with JDWP values */ |
| 46 | THREAD_ZOMBIE = 0, /* TERMINATED */ |
| 47 | THREAD_RUNNING = 1, /* RUNNABLE or running now */ |
| 48 | THREAD_TIMED_WAIT = 2, /* TIMED_WAITING in Object.wait() */ |
| 49 | THREAD_MONITOR = 3, /* BLOCKED on a monitor */ |
| 50 | THREAD_WAIT = 4, /* WAITING in Object.wait() */ |
| 51 | /* non-JDWP states */ |
| 52 | THREAD_INITIALIZING = 5, /* allocated, not yet running */ |
| 53 | THREAD_STARTING = 6, /* started, not yet on thread list */ |
| 54 | THREAD_NATIVE = 7, /* off in a JNI native method */ |
| 55 | THREAD_VMWAIT = 8, /* waiting on a VM resource */ |
Andy McFadden | b5f3c0b | 2010-08-23 16:45:24 -0700 | [diff] [blame] | 56 | THREAD_SUSPENDED = 9, /* suspended, usually by GC or debugger */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 57 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 58 | |
| 59 | /* thread priorities, from java.lang.Thread */ |
| 60 | enum { |
| 61 | THREAD_MIN_PRIORITY = 1, |
| 62 | THREAD_NORM_PRIORITY = 5, |
| 63 | THREAD_MAX_PRIORITY = 10, |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | /* initialization */ |
| 68 | bool dvmThreadStartup(void); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 69 | void dvmThreadShutdown(void); |
| 70 | void dvmSlayDaemons(void); |
| 71 | |
| 72 | |
Carl Shapiro | 77ebd06 | 2011-04-04 10:43:02 -0700 | [diff] [blame] | 73 | #define kJniLocalRefMin 64 |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 74 | #define kJniLocalRefMax 512 /* arbitrary; should be plenty */ |
| 75 | #define kInternalRefDefault 32 /* equally arbitrary */ |
| 76 | #define kInternalRefMax 4096 /* mainly a sanity check */ |
| 77 | |
| 78 | #define kMinStackSize (512 + STACK_OVERFLOW_RESERVE) |
Andy McFadden | 80e8d7f | 2011-01-25 12:26:41 -0800 | [diff] [blame] | 79 | #define kDefaultStackSize (16*1024) /* four 4K pages */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 80 | #define kMaxStackSize (256*1024 + STACK_OVERFLOW_RESERVE) |
| 81 | |
| 82 | /* |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 83 | * Interpreter control struction. Packed into a long long to enable |
| 84 | * atomic updates. |
| 85 | */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 86 | union InterpBreak { |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 87 | volatile int64_t all; |
| 88 | struct { |
buzbee | 389e258 | 2011-04-22 15:12:40 -0700 | [diff] [blame] | 89 | uint16_t subMode; |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 90 | uint8_t breakFlags; |
buzbee | 389e258 | 2011-04-22 15:12:40 -0700 | [diff] [blame] | 91 | int8_t unused; /* for future expansion */ |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 92 | #ifndef DVM_NO_ASM_INTERP |
| 93 | void* curHandlerTable; |
| 94 | #else |
| 95 | void* unused; |
| 96 | #endif |
| 97 | } ctl; |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 98 | }; |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 99 | |
| 100 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 101 | * Our per-thread data. |
| 102 | * |
| 103 | * These are allocated on the system heap. |
| 104 | */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 105 | struct Thread { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 106 | /* |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 107 | * Interpreter state which must be preserved across nested |
| 108 | * interpreter invocations (via JNI callbacks). Must be the first |
| 109 | * element in Thread. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 110 | */ |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 111 | InterpSaveState interpSave; |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 112 | |
| 113 | /* small unique integer; useful for "thin" locks and debug messages */ |
| 114 | u4 threadId; |
| 115 | |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 116 | /* |
| 117 | * Begin interpreter state which does not need to be preserved, but should |
| 118 | * be located towards the beginning of the Thread structure for |
| 119 | * efficiency. |
| 120 | */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 121 | |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 122 | /* |
| 123 | * interpBreak contains info about the interpreter mode, as well as |
| 124 | * a count of the number of times the thread has been suspended. When |
| 125 | * the count drops to zero, the thread resumes. |
buzbee | 389e258 | 2011-04-22 15:12:40 -0700 | [diff] [blame] | 126 | */ |
| 127 | InterpBreak interpBreak; |
| 128 | |
| 129 | /* |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 130 | * "dbgSuspendCount" is the portion of the suspend count that the |
| 131 | * debugger is responsible for. This has to be tracked separately so |
| 132 | * that we can recover correctly if the debugger abruptly disconnects |
| 133 | * (suspendCount -= dbgSuspendCount). The debugger should not be able |
| 134 | * to resume GC-suspended threads, because we ignore the debugger while |
| 135 | * a GC is in progress. |
| 136 | * |
| 137 | * Both of these are guarded by gDvm.threadSuspendCountLock. |
| 138 | * |
| 139 | * Note the non-debug component will rarely be other than 1 or 0 -- (not |
| 140 | * sure it's even possible with the way mutexes are currently used.) |
| 141 | */ |
buzbee | 389e258 | 2011-04-22 15:12:40 -0700 | [diff] [blame] | 142 | |
Carl Shapiro | a62c3a0 | 2011-05-03 17:59:35 -0700 | [diff] [blame] | 143 | int suspendCount; |
| 144 | int dbgSuspendCount; |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 145 | |
buzbee | 30bc0d4 | 2011-04-22 10:27:14 -0700 | [diff] [blame] | 146 | u1* cardTable; |
| 147 | |
| 148 | /* current limit of stack; flexes for StackOverflowError */ |
| 149 | const u1* interpStackEnd; |
| 150 | |
| 151 | /* FP of bottom-most (currently executing) stack frame on interp stack */ |
| 152 | void* XcurFrame; |
| 153 | /* current exception, or NULL if nothing pending */ |
| 154 | Object* exception; |
| 155 | |
| 156 | bool debugIsMethodEntry; |
| 157 | /* interpreter stack size; our stacks are fixed-length */ |
| 158 | int interpStackSize; |
| 159 | bool stackOverflowed; |
| 160 | |
| 161 | /* thread handle, as reported by pthread_self() */ |
| 162 | pthread_t handle; |
| 163 | |
buzbee | a7d59bb | 2011-02-24 09:38:17 -0800 | [diff] [blame] | 164 | /* Assembly interpreter handler tables */ |
| 165 | #ifndef DVM_NO_ASM_INTERP |
buzbee | a7d59bb | 2011-02-24 09:38:17 -0800 | [diff] [blame] | 166 | void* mainHandlerTable; // Table of actual instruction handler |
| 167 | void* altHandlerTable; // Table of breakout handlers |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 168 | #else |
buzbee | a7d59bb | 2011-02-24 09:38:17 -0800 | [diff] [blame] | 169 | void* unused0; // Consume space to keep offsets |
| 170 | void* unused1; // the same between builds with |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 171 | #endif |
| 172 | |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 173 | /* |
| 174 | * singleStepCount is a countdown timer used with the breakFlag |
| 175 | * kInterpSingleStep. If kInterpSingleStep is set in breakFlags, |
| 176 | * singleStepCount will decremented each instruction execution. |
| 177 | * Once it reaches zero, the kInterpSingleStep flag in breakFlags |
| 178 | * will be cleared. This can be used to temporarily prevent |
| 179 | * execution from re-entering JIT'd code or force inter-instruction |
| 180 | * checks by delaying the reset of curHandlerTable to mainHandlerTable. |
| 181 | */ |
| 182 | int singleStepCount; |
| 183 | |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 184 | #ifdef WITH_JIT |
| 185 | struct JitToInterpEntries jitToInterpEntries; |
| 186 | /* |
| 187 | * Whether the current top VM frame is in the interpreter or JIT cache: |
| 188 | * NULL : in the interpreter |
| 189 | * non-NULL: entry address of the JIT'ed code (the actual value doesn't |
| 190 | * matter) |
| 191 | */ |
| 192 | void* inJitCodeCache; |
| 193 | unsigned char* pJitProfTable; |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 194 | int jitThreshold; |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 195 | const void* jitResumeNPC; // Translation return point |
| 196 | const u4* jitResumeNSP; // Native SP at return point |
| 197 | const u2* jitResumeDPC; // Dalvik inst following single-step |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 198 | JitState jitState; |
| 199 | int icRechainCount; |
| 200 | const void* pProfileCountdown; |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 201 | const ClassObject* callsiteClass; |
| 202 | const Method* methodToCall; |
buzbee | a7d59bb | 2011-02-24 09:38:17 -0800 | [diff] [blame] | 203 | #endif |
| 204 | |
| 205 | /* JNI local reference tracking */ |
buzbee | a7d59bb | 2011-02-24 09:38:17 -0800 | [diff] [blame] | 206 | IndirectRefTable jniLocalRefTable; |
buzbee | a7d59bb | 2011-02-24 09:38:17 -0800 | [diff] [blame] | 207 | |
| 208 | #if defined(WITH_JIT) |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 209 | #if defined(WITH_SELF_VERIFICATION) |
| 210 | /* Buffer for register state during self verification */ |
| 211 | struct ShadowSpace* shadowSpace; |
| 212 | #endif |
| 213 | int currTraceRun; |
| 214 | int totalTraceLen; // Number of Dalvik insts in trace |
| 215 | const u2* currTraceHead; // Start of the trace we're building |
| 216 | const u2* currRunHead; // Start of run we're building |
| 217 | int currRunLen; // Length of run in 16-bit words |
| 218 | const u2* lastPC; // Stage the PC for the threaded interpreter |
buzbee | 9a3147c | 2011-03-02 15:43:48 -0800 | [diff] [blame] | 219 | const Method* traceMethod; // Starting method of current trace |
buzbee | 9f601a9 | 2011-02-11 17:48:20 -0800 | [diff] [blame] | 220 | intptr_t threshFilter[JIT_TRACE_THRESH_FILTER_SIZE]; |
| 221 | JitTraceRun trace[MAX_JIT_RUN_LEN]; |
| 222 | #endif |
| 223 | |
| 224 | /* |
| 225 | * Thread's current status. Can only be changed by the thread itself |
| 226 | * (i.e. don't mess with this from other threads). |
| 227 | */ |
| 228 | volatile ThreadStatus status; |
| 229 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 230 | /* thread ID, only useful under Linux */ |
| 231 | pid_t systemTid; |
| 232 | |
| 233 | /* start (high addr) of interp stack (subtract size to get malloc addr) */ |
| 234 | u1* interpStackStart; |
| 235 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 236 | /* the java/lang/Thread that we are associated with */ |
| 237 | Object* threadObj; |
| 238 | |
| 239 | /* the JNIEnv pointer associated with this thread */ |
| 240 | JNIEnv* jniEnv; |
| 241 | |
| 242 | /* internal reference tracking */ |
| 243 | ReferenceTable internalLocalRefTable; |
| 244 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 245 | |
| 246 | /* JNI native monitor reference tracking (initialized on first use) */ |
| 247 | ReferenceTable jniMonitorRefTable; |
| 248 | |
| 249 | /* hack to make JNI_OnLoad work right */ |
| 250 | Object* classLoaderOverride; |
| 251 | |
Carl Shapiro | b453919 | 2010-01-04 16:50:00 -0800 | [diff] [blame] | 252 | /* mutex to guard the interrupted and the waitMonitor members */ |
| 253 | pthread_mutex_t waitMutex; |
| 254 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 255 | /* pointer to the monitor lock we're currently waiting on */ |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 256 | /* guarded by waitMutex */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 257 | /* TODO: consider changing this to Object* for better JDWP interaction */ |
| 258 | Monitor* waitMonitor; |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 259 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 260 | /* thread "interrupted" status; stays raised until queried or thrown */ |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 261 | /* guarded by waitMutex */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 262 | bool interrupted; |
| 263 | |
Carl Shapiro | 77f52eb | 2009-12-24 19:56:53 -0800 | [diff] [blame] | 264 | /* links to the next thread in the wait set this thread is part of */ |
| 265 | struct Thread* waitNext; |
| 266 | |
| 267 | /* object to sleep on while we are waiting for a monitor */ |
| 268 | pthread_cond_t waitCond; |
| 269 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 270 | /* |
| 271 | * Set to true when the thread is in the process of throwing an |
| 272 | * OutOfMemoryError. |
| 273 | */ |
| 274 | bool throwingOOME; |
| 275 | |
| 276 | /* links to rest of thread list; grab global lock before traversing */ |
| 277 | struct Thread* prev; |
| 278 | struct Thread* next; |
| 279 | |
Andy McFadden | 909ce24 | 2009-12-10 16:38:30 -0800 | [diff] [blame] | 280 | /* used by threadExitCheck when a thread exits without detaching */ |
| 281 | int threadExitCheckCount; |
| 282 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 283 | /* JDWP invoke-during-breakpoint support */ |
| 284 | DebugInvokeReq invokeReq; |
| 285 | |
Andy McFadden | 0d615c3 | 2010-08-18 12:19:51 -0700 | [diff] [blame] | 286 | /* base time for per-thread CPU timing (used by method profiling) */ |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 287 | bool cpuClockBaseSet; |
| 288 | u8 cpuClockBase; |
| 289 | |
| 290 | /* memory allocation profiling state */ |
| 291 | AllocProfState allocProf; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 292 | |
| 293 | #ifdef WITH_JNI_STACK_CHECK |
| 294 | u4 stackCrc; |
| 295 | #endif |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 296 | |
| 297 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 298 | /* PC, saved on every instruction; redundant with StackSaveArea */ |
| 299 | const u2* currentPc2; |
| 300 | #endif |
buzbee | 94d6525 | 2011-03-24 16:41:03 -0700 | [diff] [blame] | 301 | |
| 302 | /* Safepoint callback state */ |
| 303 | pthread_mutex_t callbackMutex; |
| 304 | SafePointCallback callback; |
| 305 | void* callbackArg; |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 306 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 307 | |
| 308 | /* start point for an internal thread; mimics pthread args */ |
| 309 | typedef void* (*InternalThreadStart)(void* arg); |
| 310 | |
| 311 | /* args for internal thread creation */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 312 | struct InternalStartArgs { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 313 | /* inputs */ |
| 314 | InternalThreadStart func; |
| 315 | void* funcArg; |
| 316 | char* name; |
| 317 | Object* group; |
| 318 | bool isDaemon; |
| 319 | /* result */ |
| 320 | volatile Thread** pThread; |
| 321 | volatile int* pCreateStatus; |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 322 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 323 | |
| 324 | /* finish init */ |
| 325 | bool dvmPrepMainForJni(JNIEnv* pEnv); |
| 326 | bool dvmPrepMainThread(void); |
| 327 | |
| 328 | /* utility function to get the tid */ |
| 329 | pid_t dvmGetSysThreadId(void); |
| 330 | |
| 331 | /* |
| 332 | * Get our Thread* from TLS. |
| 333 | * |
| 334 | * Returns NULL if this isn't a thread that the VM is aware of. |
| 335 | */ |
| 336 | Thread* dvmThreadSelf(void); |
| 337 | |
| 338 | /* grab the thread list global lock */ |
| 339 | void dvmLockThreadList(Thread* self); |
Andy McFadden | d19988d | 2010-10-22 13:32:12 -0700 | [diff] [blame] | 340 | /* try to grab the thread list global lock */ |
| 341 | bool dvmTryLockThreadList(void); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 342 | /* release the thread list global lock */ |
| 343 | void dvmUnlockThreadList(void); |
| 344 | |
| 345 | /* |
| 346 | * Thread suspend/resume, used by the GC and debugger. |
| 347 | */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 348 | enum SuspendCause { |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 349 | SUSPEND_NOT = 0, |
| 350 | SUSPEND_FOR_GC, |
| 351 | SUSPEND_FOR_DEBUG, |
| 352 | SUSPEND_FOR_DEBUG_EVENT, |
| 353 | SUSPEND_FOR_STACK_DUMP, |
| 354 | SUSPEND_FOR_DEX_OPT, |
Carl Shapiro | 1e714bb | 2010-03-16 03:26:49 -0700 | [diff] [blame] | 355 | SUSPEND_FOR_VERIFY, |
Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 356 | SUSPEND_FOR_HPROF, |
Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 357 | #if defined(WITH_JIT) |
Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 358 | SUSPEND_FOR_TBL_RESIZE, // jit-table resize |
| 359 | SUSPEND_FOR_IC_PATCH, // polymorphic callsite inline-cache patch |
| 360 | SUSPEND_FOR_CC_RESET, // code-cache reset |
Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 361 | SUSPEND_FOR_REFRESH, // Reload data cached in interpState |
Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 362 | #endif |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 363 | }; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 364 | void dvmSuspendThread(Thread* thread); |
| 365 | void dvmSuspendSelf(bool jdwpActivity); |
| 366 | void dvmResumeThread(Thread* thread); |
| 367 | void dvmSuspendAllThreads(SuspendCause why); |
| 368 | void dvmResumeAllThreads(SuspendCause why); |
| 369 | void dvmUndoDebuggerSuspensions(void); |
| 370 | |
| 371 | /* |
| 372 | * Check suspend state. Grab threadListLock before calling. |
| 373 | */ |
Andy McFadden | 3469a7e | 2010-08-04 16:09:10 -0700 | [diff] [blame] | 374 | bool dvmIsSuspended(const Thread* thread); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * Wait until a thread has suspended. (Used by debugger support.) |
| 378 | */ |
| 379 | void dvmWaitForSuspend(Thread* thread); |
| 380 | |
| 381 | /* |
| 382 | * Check to see if we should be suspended now. If so, suspend ourselves |
| 383 | * by sleeping on a condition variable. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 384 | */ |
Carl Shapiro | d862faa | 2011-04-27 23:00:01 -0700 | [diff] [blame] | 385 | extern "C" bool dvmCheckSuspendPending(Thread* self); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 386 | |
| 387 | /* |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 388 | * Fast test for use in the interpreter. Returns "true" if our suspend |
| 389 | * count is nonzero. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 390 | */ |
The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 391 | INLINE bool dvmCheckSuspendQuick(Thread* self) { |
buzbee | cf2aac7 | 2011-04-25 11:23:46 -0700 | [diff] [blame] | 392 | return (self->interpBreak.ctl.subMode & kSubModeSuspendPending); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Used when changing thread state. Threads may only change their own. |
| 397 | * The "self" argument, which may be NULL, is accepted as an optimization. |
| 398 | * |
| 399 | * If you're calling this before waiting on a resource (e.g. THREAD_WAIT |
| 400 | * or THREAD_MONITOR), do so in the same function as the wait -- this records |
| 401 | * the current stack depth for the GC. |
| 402 | * |
| 403 | * If you're changing to THREAD_RUNNING, this will check for suspension. |
| 404 | * |
| 405 | * Returns the old status. |
| 406 | */ |
| 407 | ThreadStatus dvmChangeStatus(Thread* self, ThreadStatus newStatus); |
| 408 | |
| 409 | /* |
| 410 | * Initialize a mutex. |
| 411 | */ |
| 412 | INLINE void dvmInitMutex(pthread_mutex_t* pMutex) |
| 413 | { |
| 414 | #ifdef CHECK_MUTEX |
| 415 | pthread_mutexattr_t attr; |
| 416 | int cc; |
| 417 | |
| 418 | pthread_mutexattr_init(&attr); |
| 419 | cc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP); |
| 420 | assert(cc == 0); |
| 421 | pthread_mutex_init(pMutex, &attr); |
| 422 | pthread_mutexattr_destroy(&attr); |
| 423 | #else |
| 424 | pthread_mutex_init(pMutex, NULL); // default=PTHREAD_MUTEX_FAST_NP |
| 425 | #endif |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Grab a plain mutex. |
| 430 | */ |
| 431 | INLINE void dvmLockMutex(pthread_mutex_t* pMutex) |
| 432 | { |
Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 433 | int cc __attribute__ ((__unused__)) = pthread_mutex_lock(pMutex); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 434 | assert(cc == 0); |
| 435 | } |
| 436 | |
| 437 | /* |
Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 438 | * Try grabbing a plain mutex. Returns 0 if successful. |
| 439 | */ |
| 440 | INLINE int dvmTryLockMutex(pthread_mutex_t* pMutex) |
| 441 | { |
Carl Shapiro | 980ffb0 | 2010-03-13 22:34:01 -0800 | [diff] [blame] | 442 | int cc = pthread_mutex_trylock(pMutex); |
| 443 | assert(cc == 0 || cc == EBUSY); |
| 444 | return cc; |
Bill Buzbee | 964a7b0 | 2010-01-28 12:54:19 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 448 | * Unlock pthread mutex. |
| 449 | */ |
| 450 | INLINE void dvmUnlockMutex(pthread_mutex_t* pMutex) |
| 451 | { |
Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 452 | int cc __attribute__ ((__unused__)) = pthread_mutex_unlock(pMutex); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 453 | assert(cc == 0); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Destroy a mutex. |
| 458 | */ |
| 459 | INLINE void dvmDestroyMutex(pthread_mutex_t* pMutex) |
| 460 | { |
Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 461 | int cc __attribute__ ((__unused__)) = pthread_mutex_destroy(pMutex); |
| 462 | assert(cc == 0); |
| 463 | } |
| 464 | |
| 465 | INLINE void dvmBroadcastCond(pthread_cond_t* pCond) |
| 466 | { |
| 467 | int cc __attribute__ ((__unused__)) = pthread_cond_broadcast(pCond); |
| 468 | assert(cc == 0); |
| 469 | } |
| 470 | |
| 471 | INLINE void dvmSignalCond(pthread_cond_t* pCond) |
| 472 | { |
| 473 | int cc __attribute__ ((__unused__)) = pthread_cond_signal(pCond); |
| 474 | assert(cc == 0); |
| 475 | } |
| 476 | |
| 477 | INLINE void dvmWaitCond(pthread_cond_t* pCond, pthread_mutex_t* pMutex) |
| 478 | { |
| 479 | int cc __attribute__ ((__unused__)) = pthread_cond_wait(pCond, pMutex); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 480 | assert(cc == 0); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Create a thread as a result of java.lang.Thread.start(). |
| 485 | */ |
| 486 | bool dvmCreateInterpThread(Object* threadObj, int reqStackSize); |
| 487 | |
| 488 | /* |
| 489 | * Create a thread internal to the VM. It's visible to interpreted code, |
| 490 | * but found in the "system" thread group rather than "main". |
| 491 | */ |
| 492 | bool dvmCreateInternalThread(pthread_t* pHandle, const char* name, |
| 493 | InternalThreadStart func, void* funcArg); |
| 494 | |
| 495 | /* |
| 496 | * Attach or detach the current thread from the VM. |
| 497 | */ |
| 498 | bool dvmAttachCurrentThread(const JavaVMAttachArgs* pArgs, bool isDaemon); |
| 499 | void dvmDetachCurrentThread(void); |
| 500 | |
| 501 | /* |
| 502 | * Get the "main" or "system" thread group. |
| 503 | */ |
| 504 | Object* dvmGetMainThreadGroup(void); |
| 505 | Object* dvmGetSystemThreadGroup(void); |
| 506 | |
| 507 | /* |
| 508 | * Given a java/lang/VMThread object, return our Thread. |
| 509 | */ |
| 510 | Thread* dvmGetThreadFromThreadObject(Object* vmThreadObj); |
| 511 | |
| 512 | /* |
Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 513 | * Given a pthread handle, return the associated Thread*. |
Andy McFadden | fd54266 | 2010-03-12 13:39:59 -0800 | [diff] [blame] | 514 | * Caller must hold the thread list lock. |
Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 515 | * |
| 516 | * Returns NULL if the thread was not found. |
| 517 | */ |
| 518 | Thread* dvmGetThreadByHandle(pthread_t handle); |
| 519 | |
| 520 | /* |
Andy McFadden | fd54266 | 2010-03-12 13:39:59 -0800 | [diff] [blame] | 521 | * Given a thread ID, return the associated Thread*. |
| 522 | * Caller must hold the thread list lock. |
| 523 | * |
| 524 | * Returns NULL if the thread was not found. |
| 525 | */ |
| 526 | Thread* dvmGetThreadByThreadId(u4 threadId); |
| 527 | |
| 528 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 529 | * Sleep in a thread. Returns when the sleep timer returns or the thread |
| 530 | * is interrupted. |
| 531 | */ |
| 532 | void dvmThreadSleep(u8 msec, u4 nsec); |
| 533 | |
| 534 | /* |
| 535 | * Get the name of a thread. (For safety, hold the thread list lock.) |
| 536 | */ |
| 537 | char* dvmGetThreadName(Thread* thread); |
| 538 | |
| 539 | /* |
Ben Cheng | 7a0bcd0 | 2010-01-22 16:45:45 -0800 | [diff] [blame] | 540 | * Convert ThreadStatus to a string. |
| 541 | */ |
| 542 | const char* dvmGetThreadStatusStr(ThreadStatus status); |
| 543 | |
| 544 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 545 | * Return true if a thread is on the internal list. If it is, the |
| 546 | * thread is part of the GC's root set. |
| 547 | */ |
| 548 | bool dvmIsOnThreadList(const Thread* thread); |
Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 549 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 550 | /* |
| 551 | * Get/set the JNIEnv field. |
| 552 | */ |
| 553 | INLINE JNIEnv* dvmGetThreadJNIEnv(Thread* self) { return self->jniEnv; } |
| 554 | INLINE void dvmSetThreadJNIEnv(Thread* self, JNIEnv* env) { self->jniEnv = env;} |
| 555 | |
| 556 | /* |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 557 | * Update the priority value of the underlying pthread. |
| 558 | */ |
| 559 | void dvmChangeThreadPriority(Thread* thread, int newPriority); |
| 560 | |
Andy McFadden | 2b94b30 | 2010-03-09 16:38:36 -0800 | [diff] [blame] | 561 | /* "change flags" values for raise/reset thread priority calls */ |
| 562 | #define kChangedPriority 0x01 |
| 563 | #define kChangedPolicy 0x02 |
| 564 | |
| 565 | /* |
| 566 | * If necessary, raise the thread's priority to nice=0 cgroup=fg. |
| 567 | * |
| 568 | * Returns bit flags indicating changes made (zero if nothing was done). |
| 569 | */ |
| 570 | int dvmRaiseThreadPriorityIfNeeded(Thread* thread, int* pSavedThreadPrio, |
| 571 | SchedPolicy* pSavedThreadPolicy); |
| 572 | |
| 573 | /* |
| 574 | * Drop the thread priority to what it was before an earlier call to |
| 575 | * dvmRaiseThreadPriorityIfNeeded(). |
| 576 | */ |
| 577 | void dvmResetThreadPriority(Thread* thread, int changeFlags, |
| 578 | int savedThreadPrio, SchedPolicy savedThreadPolicy); |
| 579 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 580 | /* |
| 581 | * Debug: dump information about a single thread. |
| 582 | */ |
| 583 | void dvmDumpThread(Thread* thread, bool isRunning); |
| 584 | void dvmDumpThreadEx(const DebugOutputTarget* target, Thread* thread, |
| 585 | bool isRunning); |
| 586 | |
| 587 | /* |
| 588 | * Debug: dump information about all threads. |
| 589 | */ |
| 590 | void dvmDumpAllThreads(bool grabLock); |
| 591 | void dvmDumpAllThreadsEx(const DebugOutputTarget* target, bool grabLock); |
| 592 | |
Andy McFadden | 384ef6b | 2010-03-15 17:24:55 -0700 | [diff] [blame] | 593 | /* |
| 594 | * Debug: kill a thread to get a debuggerd stack trace. Leaves the VM |
| 595 | * in an uncertain state. |
| 596 | */ |
| 597 | void dvmNukeThread(Thread* thread); |
| 598 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 599 | #endif /*_DALVIK_THREAD*/ |