blob: d59aafb6ad442eb6d37748f0f5bc5745d122d1cd [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
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 McFadden0083d372009-08-21 14:44:04 -070016
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080017/*
18 * VM thread support.
19 */
20#ifndef _DALVIK_THREAD
21#define _DALVIK_THREAD
22
23#include "jni.h"
buzbee9f601a92011-02-11 17:48:20 -080024#include "interp/InterpState.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080025
Carl Shapiro980ffb02010-03-13 22:34:01 -080026#include <errno.h>
Andy McFadden2b94b302010-03-09 16:38:36 -080027#include <cutils/sched_policy.h>
28
29
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080030#if defined(CHECK_MUTEX) && !defined(__USE_UNIX98)
Andy McFadden0083d372009-08-21 14:44:04 -070031/* glibc lacks this unless you #define __USE_UNIX98 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080032int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
33enum { PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP };
34#endif
35
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080036/*
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 */
43typedef enum ThreadStatus {
Andy McFaddenb5f3c0b2010-08-23 16:45:24 -070044 THREAD_UNDEFINED = -1, /* makes enum compatible with int32_t */
Andy McFaddenab227f72010-04-06 12:37:48 -070045
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080046 /* 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 McFaddenb5f3c0b2010-08-23 16:45:24 -070057 THREAD_SUSPENDED = 9, /* suspended, usually by GC or debugger */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080058} ThreadStatus;
59
60/* thread priorities, from java.lang.Thread */
61enum {
62 THREAD_MIN_PRIORITY = 1,
63 THREAD_NORM_PRIORITY = 5,
64 THREAD_MAX_PRIORITY = 10,
65};
66
67
68/* initialization */
69bool dvmThreadStartup(void);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080070void dvmThreadShutdown(void);
71void dvmSlayDaemons(void);
72
73
Andy McFaddend5ab7262009-08-25 07:19:34 -070074#define kJniLocalRefMin 32
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080075#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 McFadden80e8d7f2011-01-25 12:26:41 -080080#define kDefaultStackSize (16*1024) /* four 4K pages */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080081#define kMaxStackSize (256*1024 + STACK_OVERFLOW_RESERVE)
82
83/*
buzbee9a3147c2011-03-02 15:43:48 -080084 * Interpreter control struction. Packed into a long long to enable
85 * atomic updates.
86 */
87typedef 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 Projectf6c38712009-03-03 19:28:47 -0800103 * Our per-thread data.
104 *
105 * These are allocated on the system heap.
106 */
107typedef struct Thread {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800108 /*
buzbee9f601a92011-02-11 17:48:20 -0800109 * 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 Projectf6c38712009-03-03 19:28:47 -0800112 */
buzbee9f601a92011-02-11 17:48:20 -0800113 InterpSaveState interpSave;
buzbee9a3147c2011-03-02 15:43:48 -0800114
115 /* small unique integer; useful for "thin" locks and debug messages */
116 u4 threadId;
117
buzbee9f601a92011-02-11 17:48:20 -0800118 /*
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 Projectf6c38712009-03-03 19:28:47 -0800124
buzbee9f601a92011-02-11 17:48:20 -0800125 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
buzbee9f601a92011-02-11 17:48:20 -0800135 bool debugIsMethodEntry;
136 /* interpreter stack size; our stacks are fixed-length */
137 int interpStackSize;
138 bool stackOverflowed;
139
buzbee9a3147c2011-03-02 15:43:48 -0800140 /* 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
buzbee9f601a92011-02-11 17:48:20 -0800162
buzbeea7d59bb2011-02-24 09:38:17 -0800163 /* Assembly interpreter handler tables */
164#ifndef DVM_NO_ASM_INTERP
buzbeea7d59bb2011-02-24 09:38:17 -0800165 void* mainHandlerTable; // Table of actual instruction handler
166 void* altHandlerTable; // Table of breakout handlers
buzbee9f601a92011-02-11 17:48:20 -0800167#else
buzbeea7d59bb2011-02-24 09:38:17 -0800168 void* unused0; // Consume space to keep offsets
169 void* unused1; // the same between builds with
buzbee9f601a92011-02-11 17:48:20 -0800170#endif
171
buzbee9a3147c2011-03-02 15:43:48 -0800172 /*
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
buzbee9f601a92011-02-11 17:48:20 -0800183#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;
buzbee9f601a92011-02-11 17:48:20 -0800193 int jitThreshold;
buzbee9a3147c2011-03-02 15:43:48 -0800194 const void* jitResumeNPC; // Translation return point
195 const u4* jitResumeNSP; // Native SP at return point
196 const u2* jitResumeDPC; // Dalvik inst following single-step
buzbee9f601a92011-02-11 17:48:20 -0800197 JitState jitState;
198 int icRechainCount;
199 const void* pProfileCountdown;
buzbee9a3147c2011-03-02 15:43:48 -0800200 const ClassObject* callsiteClass;
201 const Method* methodToCall;
buzbeea7d59bb2011-02-24 09:38:17 -0800202#endif
203
204 /* JNI local reference tracking */
buzbeea7d59bb2011-02-24 09:38:17 -0800205 IndirectRefTable jniLocalRefTable;
buzbeea7d59bb2011-02-24 09:38:17 -0800206
207#if defined(WITH_JIT)
buzbee9f601a92011-02-11 17:48:20 -0800208#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
buzbee9a3147c2011-03-02 15:43:48 -0800218 const Method* traceMethod; // Starting method of current trace
buzbee9f601a92011-02-11 17:48:20 -0800219 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 Projectf6c38712009-03-03 19:28:47 -0800229 /* 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 Projectf6c38712009-03-03 19:28:47 -0800235 /* 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 Projectf6c38712009-03-03 19:28:47 -0800244
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 Shapirob4539192010-01-04 16:50:00 -0800251 /* mutex to guard the interrupted and the waitMonitor members */
252 pthread_mutex_t waitMutex;
253
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800254 /* pointer to the monitor lock we're currently waiting on */
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800255 /* guarded by waitMutex */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800256 /* TODO: consider changing this to Object* for better JDWP interaction */
257 Monitor* waitMonitor;
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800258
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800259 /* thread "interrupted" status; stays raised until queried or thrown */
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800260 /* guarded by waitMutex */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800261 bool interrupted;
262
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800263 /* 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 Projectf6c38712009-03-03 19:28:47 -0800269 /*
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 McFadden909ce242009-12-10 16:38:30 -0800279 /* used by threadExitCheck when a thread exits without detaching */
280 int threadExitCheckCount;
281
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800282 /* JDWP invoke-during-breakpoint support */
283 DebugInvokeReq invokeReq;
284
Andy McFadden0d615c32010-08-18 12:19:51 -0700285 /* base time for per-thread CPU timing (used by method profiling) */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800286 bool cpuClockBaseSet;
287 u8 cpuClockBase;
288
289 /* memory allocation profiling state */
290 AllocProfState allocProf;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800291
292#ifdef WITH_JNI_STACK_CHECK
293 u4 stackCrc;
294#endif
The Android Open Source Project99409882009-03-18 22:20:24 -0700295
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 Projectf6c38712009-03-03 19:28:47 -0800300} Thread;
301
302/* start point for an internal thread; mimics pthread args */
303typedef void* (*InternalThreadStart)(void* arg);
304
305/* args for internal thread creation */
306typedef 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 */
319bool dvmPrepMainForJni(JNIEnv* pEnv);
320bool dvmPrepMainThread(void);
321
322/* utility function to get the tid */
323pid_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 */
330Thread* dvmThreadSelf(void);
331
332/* grab the thread list global lock */
333void dvmLockThreadList(Thread* self);
Andy McFaddend19988d2010-10-22 13:32:12 -0700334/* try to grab the thread list global lock */
335bool dvmTryLockThreadList(void);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800336/* release the thread list global lock */
337void dvmUnlockThreadList(void);
338
339/*
340 * Thread suspend/resume, used by the GC and debugger.
341 */
342typedef 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 Shapiro1e714bb2010-03-16 03:26:49 -0700349 SUSPEND_FOR_VERIFY,
Carl Shapiro07018e22010-10-26 21:07:41 -0700350 SUSPEND_FOR_HPROF,
Bill Buzbee27176222009-06-09 09:20:16 -0700351#if defined(WITH_JIT)
Ben Cheng60c24f42010-01-04 12:29:56 -0800352 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 Buzbee964a7b02010-01-28 12:54:19 -0800355 SUSPEND_FOR_REFRESH, // Reload data cached in interpState
Bill Buzbee27176222009-06-09 09:20:16 -0700356#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800357} SuspendCause;
358void dvmSuspendThread(Thread* thread);
359void dvmSuspendSelf(bool jdwpActivity);
360void dvmResumeThread(Thread* thread);
361void dvmSuspendAllThreads(SuspendCause why);
362void dvmResumeAllThreads(SuspendCause why);
363void dvmUndoDebuggerSuspensions(void);
364
365/*
366 * Check suspend state. Grab threadListLock before calling.
367 */
Andy McFadden3469a7e2010-08-04 16:09:10 -0700368bool dvmIsSuspended(const Thread* thread);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800369
370/*
371 * Wait until a thread has suspended. (Used by debugger support.)
372 */
373void 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 Projectf6c38712009-03-03 19:28:47 -0800378 */
379bool dvmCheckSuspendPending(Thread* self);
380
381/*
The Android Open Source Project99409882009-03-18 22:20:24 -0700382 * Fast test for use in the interpreter. Returns "true" if our suspend
383 * count is nonzero.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800384 */
The Android Open Source Project99409882009-03-18 22:20:24 -0700385INLINE bool dvmCheckSuspendQuick(Thread* self) {
buzbee9a3147c2011-03-02 15:43:48 -0800386 return (self->interpBreak.ctl.breakFlags & kInterpSuspendBreak);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800387}
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 */
401ThreadStatus dvmChangeStatus(Thread* self, ThreadStatus newStatus);
402
403/*
404 * Initialize a mutex.
405 */
406INLINE 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 */
425INLINE void dvmLockMutex(pthread_mutex_t* pMutex)
426{
Carl Shapirob31b3012010-05-25 18:35:37 -0700427 int cc __attribute__ ((__unused__)) = pthread_mutex_lock(pMutex);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800428 assert(cc == 0);
429}
430
431/*
Bill Buzbee964a7b02010-01-28 12:54:19 -0800432 * Try grabbing a plain mutex. Returns 0 if successful.
433 */
434INLINE int dvmTryLockMutex(pthread_mutex_t* pMutex)
435{
Carl Shapiro980ffb02010-03-13 22:34:01 -0800436 int cc = pthread_mutex_trylock(pMutex);
437 assert(cc == 0 || cc == EBUSY);
438 return cc;
Bill Buzbee964a7b02010-01-28 12:54:19 -0800439}
440
441/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800442 * Unlock pthread mutex.
443 */
444INLINE void dvmUnlockMutex(pthread_mutex_t* pMutex)
445{
Carl Shapirob31b3012010-05-25 18:35:37 -0700446 int cc __attribute__ ((__unused__)) = pthread_mutex_unlock(pMutex);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800447 assert(cc == 0);
448}
449
450/*
451 * Destroy a mutex.
452 */
453INLINE void dvmDestroyMutex(pthread_mutex_t* pMutex)
454{
Carl Shapirob31b3012010-05-25 18:35:37 -0700455 int cc __attribute__ ((__unused__)) = pthread_mutex_destroy(pMutex);
456 assert(cc == 0);
457}
458
459INLINE void dvmBroadcastCond(pthread_cond_t* pCond)
460{
461 int cc __attribute__ ((__unused__)) = pthread_cond_broadcast(pCond);
462 assert(cc == 0);
463}
464
465INLINE void dvmSignalCond(pthread_cond_t* pCond)
466{
467 int cc __attribute__ ((__unused__)) = pthread_cond_signal(pCond);
468 assert(cc == 0);
469}
470
471INLINE 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 Projectf6c38712009-03-03 19:28:47 -0800474 assert(cc == 0);
475}
476
477/*
478 * Create a thread as a result of java.lang.Thread.start().
479 */
480bool 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 */
486bool 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 */
492bool dvmAttachCurrentThread(const JavaVMAttachArgs* pArgs, bool isDaemon);
493void dvmDetachCurrentThread(void);
494
495/*
496 * Get the "main" or "system" thread group.
497 */
498Object* dvmGetMainThreadGroup(void);
499Object* dvmGetSystemThreadGroup(void);
500
501/*
502 * Given a java/lang/VMThread object, return our Thread.
503 */
504Thread* dvmGetThreadFromThreadObject(Object* vmThreadObj);
505
506/*
Andy McFadden2b94b302010-03-09 16:38:36 -0800507 * Given a pthread handle, return the associated Thread*.
Andy McFaddenfd542662010-03-12 13:39:59 -0800508 * Caller must hold the thread list lock.
Andy McFadden2b94b302010-03-09 16:38:36 -0800509 *
510 * Returns NULL if the thread was not found.
511 */
512Thread* dvmGetThreadByHandle(pthread_t handle);
513
514/*
Andy McFaddenfd542662010-03-12 13:39:59 -0800515 * 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 */
520Thread* dvmGetThreadByThreadId(u4 threadId);
521
522/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800523 * Sleep in a thread. Returns when the sleep timer returns or the thread
524 * is interrupted.
525 */
526void dvmThreadSleep(u8 msec, u4 nsec);
527
528/*
529 * Get the name of a thread. (For safety, hold the thread list lock.)
530 */
531char* dvmGetThreadName(Thread* thread);
532
533/*
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800534 * Convert ThreadStatus to a string.
535 */
536const char* dvmGetThreadStatusStr(ThreadStatus status);
537
538/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800539 * 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 */
542bool dvmIsOnThreadList(const Thread* thread);
Jeff Hao97319a82009-08-12 16:57:15 -0700543
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800544/*
545 * Get/set the JNIEnv field.
546 */
547INLINE JNIEnv* dvmGetThreadJNIEnv(Thread* self) { return self->jniEnv; }
548INLINE void dvmSetThreadJNIEnv(Thread* self, JNIEnv* env) { self->jniEnv = env;}
549
550/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800551 * Update the priority value of the underlying pthread.
552 */
553void dvmChangeThreadPriority(Thread* thread, int newPriority);
554
Andy McFadden2b94b302010-03-09 16:38:36 -0800555/* "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 */
564int 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 */
571void dvmResetThreadPriority(Thread* thread, int changeFlags,
572 int savedThreadPrio, SchedPolicy savedThreadPolicy);
573
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800574/*
575 * Debug: dump information about a single thread.
576 */
577void dvmDumpThread(Thread* thread, bool isRunning);
578void dvmDumpThreadEx(const DebugOutputTarget* target, Thread* thread,
579 bool isRunning);
580
581/*
582 * Debug: dump information about all threads.
583 */
584void dvmDumpAllThreads(bool grabLock);
585void dvmDumpAllThreadsEx(const DebugOutputTarget* target, bool grabLock);
586
Andy McFadden384ef6b2010-03-15 17:24:55 -0700587/*
588 * Debug: kill a thread to get a debuggerd stack trace. Leaves the VM
589 * in an uncertain state.
590 */
591void dvmNukeThread(Thread* thread);
592
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800593#endif /*_DALVIK_THREAD*/