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