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