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