blob: 617e702550a080c7c793b45dc17bb8056a07f7ac [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
Carl Shapiroae188c62011-04-08 13:11:58 -070029#ifdef __cplusplus
30extern "C" {
31#endif
Andy McFadden2b94b302010-03-09 16:38:36 -080032
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080033#if defined(CHECK_MUTEX) && !defined(__USE_UNIX98)
Andy McFadden0083d372009-08-21 14:44:04 -070034/* glibc lacks this unless you #define __USE_UNIX98 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080035int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
36enum { PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP };
37#endif
38
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080039/*
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 */
46typedef enum ThreadStatus {
Andy McFaddenb5f3c0b2010-08-23 16:45:24 -070047 THREAD_UNDEFINED = -1, /* makes enum compatible with int32_t */
Andy McFaddenab227f72010-04-06 12:37:48 -070048
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080049 /* 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 McFaddenb5f3c0b2010-08-23 16:45:24 -070060 THREAD_SUSPENDED = 9, /* suspended, usually by GC or debugger */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080061} ThreadStatus;
62
63/* thread priorities, from java.lang.Thread */
64enum {
65 THREAD_MIN_PRIORITY = 1,
66 THREAD_NORM_PRIORITY = 5,
67 THREAD_MAX_PRIORITY = 10,
68};
69
70
71/* initialization */
72bool dvmThreadStartup(void);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080073void dvmThreadShutdown(void);
74void dvmSlayDaemons(void);
75
76
Carl Shapiro77ebd062011-04-04 10:43:02 -070077#define kJniLocalRefMin 64
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080078#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 McFadden80e8d7f2011-01-25 12:26:41 -080083#define kDefaultStackSize (16*1024) /* four 4K pages */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080084#define kMaxStackSize (256*1024 + STACK_OVERFLOW_RESERVE)
85
86/*
buzbee9a3147c2011-03-02 15:43:48 -080087 * Interpreter control struction. Packed into a long long to enable
88 * atomic updates.
89 */
90typedef union InterpBreak {
91 volatile int64_t all;
92 struct {
buzbee389e2582011-04-22 15:12:40 -070093 uint16_t subMode;
buzbee9a3147c2011-03-02 15:43:48 -080094 uint8_t breakFlags;
buzbee389e2582011-04-22 15:12:40 -070095 int8_t unused; /* for future expansion */
buzbee9a3147c2011-03-02 15:43:48 -080096#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 Projectf6c38712009-03-03 19:28:47 -0800105 * Our per-thread data.
106 *
107 * These are allocated on the system heap.
108 */
109typedef struct Thread {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800110 /*
buzbee9f601a92011-02-11 17:48:20 -0800111 * 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 Projectf6c38712009-03-03 19:28:47 -0800114 */
buzbee9f601a92011-02-11 17:48:20 -0800115 InterpSaveState interpSave;
buzbee9a3147c2011-03-02 15:43:48 -0800116
117 /* small unique integer; useful for "thin" locks and debug messages */
118 u4 threadId;
119
buzbee9f601a92011-02-11 17:48:20 -0800120 /*
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 Projectf6c38712009-03-03 19:28:47 -0800126
buzbee9a3147c2011-03-02 15:43:48 -0800127 /*
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.
buzbee389e2582011-04-22 15:12:40 -0700131 */
132 InterpBreak interpBreak;
133
134 /*
buzbee9a3147c2011-03-02 15:43:48 -0800135 * "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 */
buzbee389e2582011-04-22 15:12:40 -0700147
148 volatile int suspendCount;
149 volatile int dbgSuspendCount;
buzbee9a3147c2011-03-02 15:43:48 -0800150
buzbee30bc0d42011-04-22 10:27:14 -0700151 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
buzbeea7d59bb2011-02-24 09:38:17 -0800169 /* Assembly interpreter handler tables */
170#ifndef DVM_NO_ASM_INTERP
buzbeea7d59bb2011-02-24 09:38:17 -0800171 void* mainHandlerTable; // Table of actual instruction handler
172 void* altHandlerTable; // Table of breakout handlers
buzbee9f601a92011-02-11 17:48:20 -0800173#else
buzbeea7d59bb2011-02-24 09:38:17 -0800174 void* unused0; // Consume space to keep offsets
175 void* unused1; // the same between builds with
buzbee9f601a92011-02-11 17:48:20 -0800176#endif
177
buzbee9a3147c2011-03-02 15:43:48 -0800178 /*
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
buzbee9f601a92011-02-11 17:48:20 -0800189#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;
buzbee9f601a92011-02-11 17:48:20 -0800199 int jitThreshold;
buzbee9a3147c2011-03-02 15:43:48 -0800200 const void* jitResumeNPC; // Translation return point
201 const u4* jitResumeNSP; // Native SP at return point
202 const u2* jitResumeDPC; // Dalvik inst following single-step
buzbee9f601a92011-02-11 17:48:20 -0800203 JitState jitState;
204 int icRechainCount;
205 const void* pProfileCountdown;
buzbee9a3147c2011-03-02 15:43:48 -0800206 const ClassObject* callsiteClass;
207 const Method* methodToCall;
buzbeea7d59bb2011-02-24 09:38:17 -0800208#endif
209
210 /* JNI local reference tracking */
buzbeea7d59bb2011-02-24 09:38:17 -0800211 IndirectRefTable jniLocalRefTable;
buzbeea7d59bb2011-02-24 09:38:17 -0800212
213#if defined(WITH_JIT)
buzbee9f601a92011-02-11 17:48:20 -0800214#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
buzbee9a3147c2011-03-02 15:43:48 -0800224 const Method* traceMethod; // Starting method of current trace
buzbee9f601a92011-02-11 17:48:20 -0800225 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 Projectf6c38712009-03-03 19:28:47 -0800235 /* 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 Projectf6c38712009-03-03 19:28:47 -0800241 /* 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 Projectf6c38712009-03-03 19:28:47 -0800250
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 Shapirob4539192010-01-04 16:50:00 -0800257 /* mutex to guard the interrupted and the waitMonitor members */
258 pthread_mutex_t waitMutex;
259
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800260 /* pointer to the monitor lock we're currently waiting on */
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800261 /* guarded by waitMutex */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800262 /* TODO: consider changing this to Object* for better JDWP interaction */
263 Monitor* waitMonitor;
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800264
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800265 /* thread "interrupted" status; stays raised until queried or thrown */
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800266 /* guarded by waitMutex */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800267 bool interrupted;
268
Carl Shapiro77f52eb2009-12-24 19:56:53 -0800269 /* 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 Projectf6c38712009-03-03 19:28:47 -0800275 /*
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 McFadden909ce242009-12-10 16:38:30 -0800285 /* used by threadExitCheck when a thread exits without detaching */
286 int threadExitCheckCount;
287
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800288 /* JDWP invoke-during-breakpoint support */
289 DebugInvokeReq invokeReq;
290
Andy McFadden0d615c32010-08-18 12:19:51 -0700291 /* base time for per-thread CPU timing (used by method profiling) */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800292 bool cpuClockBaseSet;
293 u8 cpuClockBase;
294
295 /* memory allocation profiling state */
296 AllocProfState allocProf;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800297
298#ifdef WITH_JNI_STACK_CHECK
299 u4 stackCrc;
300#endif
The Android Open Source Project99409882009-03-18 22:20:24 -0700301
302#if WITH_EXTRA_GC_CHECKS > 1
303 /* PC, saved on every instruction; redundant with StackSaveArea */
304 const u2* currentPc2;
305#endif
buzbee94d65252011-03-24 16:41:03 -0700306
307 /* Safepoint callback state */
308 pthread_mutex_t callbackMutex;
309 SafePointCallback callback;
310 void* callbackArg;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800311} Thread;
312
313/* start point for an internal thread; mimics pthread args */
314typedef void* (*InternalThreadStart)(void* arg);
315
316/* args for internal thread creation */
317typedef 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 */
330bool dvmPrepMainForJni(JNIEnv* pEnv);
331bool dvmPrepMainThread(void);
332
333/* utility function to get the tid */
334pid_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 */
341Thread* dvmThreadSelf(void);
342
343/* grab the thread list global lock */
344void dvmLockThreadList(Thread* self);
Andy McFaddend19988d2010-10-22 13:32:12 -0700345/* try to grab the thread list global lock */
346bool dvmTryLockThreadList(void);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800347/* release the thread list global lock */
348void dvmUnlockThreadList(void);
349
350/*
351 * Thread suspend/resume, used by the GC and debugger.
352 */
353typedef 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 Shapiro1e714bb2010-03-16 03:26:49 -0700360 SUSPEND_FOR_VERIFY,
Carl Shapiro07018e22010-10-26 21:07:41 -0700361 SUSPEND_FOR_HPROF,
Bill Buzbee27176222009-06-09 09:20:16 -0700362#if defined(WITH_JIT)
Ben Cheng60c24f42010-01-04 12:29:56 -0800363 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 Buzbee964a7b02010-01-28 12:54:19 -0800366 SUSPEND_FOR_REFRESH, // Reload data cached in interpState
Bill Buzbee27176222009-06-09 09:20:16 -0700367#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800368} SuspendCause;
369void dvmSuspendThread(Thread* thread);
370void dvmSuspendSelf(bool jdwpActivity);
371void dvmResumeThread(Thread* thread);
372void dvmSuspendAllThreads(SuspendCause why);
373void dvmResumeAllThreads(SuspendCause why);
374void dvmUndoDebuggerSuspensions(void);
375
376/*
377 * Check suspend state. Grab threadListLock before calling.
378 */
Andy McFadden3469a7e2010-08-04 16:09:10 -0700379bool dvmIsSuspended(const Thread* thread);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800380
381/*
382 * Wait until a thread has suspended. (Used by debugger support.)
383 */
384void 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 Projectf6c38712009-03-03 19:28:47 -0800389 */
390bool dvmCheckSuspendPending(Thread* self);
391
392/*
The Android Open Source Project99409882009-03-18 22:20:24 -0700393 * Fast test for use in the interpreter. Returns "true" if our suspend
394 * count is nonzero.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800395 */
The Android Open Source Project99409882009-03-18 22:20:24 -0700396INLINE bool dvmCheckSuspendQuick(Thread* self) {
buzbee9a3147c2011-03-02 15:43:48 -0800397 return (self->interpBreak.ctl.breakFlags & kInterpSuspendBreak);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800398}
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 */
412ThreadStatus dvmChangeStatus(Thread* self, ThreadStatus newStatus);
413
414/*
415 * Initialize a mutex.
416 */
417INLINE 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 */
436INLINE void dvmLockMutex(pthread_mutex_t* pMutex)
437{
Carl Shapirob31b3012010-05-25 18:35:37 -0700438 int cc __attribute__ ((__unused__)) = pthread_mutex_lock(pMutex);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800439 assert(cc == 0);
440}
441
442/*
Bill Buzbee964a7b02010-01-28 12:54:19 -0800443 * Try grabbing a plain mutex. Returns 0 if successful.
444 */
445INLINE int dvmTryLockMutex(pthread_mutex_t* pMutex)
446{
Carl Shapiro980ffb02010-03-13 22:34:01 -0800447 int cc = pthread_mutex_trylock(pMutex);
448 assert(cc == 0 || cc == EBUSY);
449 return cc;
Bill Buzbee964a7b02010-01-28 12:54:19 -0800450}
451
452/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800453 * Unlock pthread mutex.
454 */
455INLINE void dvmUnlockMutex(pthread_mutex_t* pMutex)
456{
Carl Shapirob31b3012010-05-25 18:35:37 -0700457 int cc __attribute__ ((__unused__)) = pthread_mutex_unlock(pMutex);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800458 assert(cc == 0);
459}
460
461/*
462 * Destroy a mutex.
463 */
464INLINE void dvmDestroyMutex(pthread_mutex_t* pMutex)
465{
Carl Shapirob31b3012010-05-25 18:35:37 -0700466 int cc __attribute__ ((__unused__)) = pthread_mutex_destroy(pMutex);
467 assert(cc == 0);
468}
469
470INLINE void dvmBroadcastCond(pthread_cond_t* pCond)
471{
472 int cc __attribute__ ((__unused__)) = pthread_cond_broadcast(pCond);
473 assert(cc == 0);
474}
475
476INLINE void dvmSignalCond(pthread_cond_t* pCond)
477{
478 int cc __attribute__ ((__unused__)) = pthread_cond_signal(pCond);
479 assert(cc == 0);
480}
481
482INLINE 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 Projectf6c38712009-03-03 19:28:47 -0800485 assert(cc == 0);
486}
487
488/*
489 * Create a thread as a result of java.lang.Thread.start().
490 */
491bool 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 */
497bool 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 */
503bool dvmAttachCurrentThread(const JavaVMAttachArgs* pArgs, bool isDaemon);
504void dvmDetachCurrentThread(void);
505
506/*
507 * Get the "main" or "system" thread group.
508 */
509Object* dvmGetMainThreadGroup(void);
510Object* dvmGetSystemThreadGroup(void);
511
512/*
513 * Given a java/lang/VMThread object, return our Thread.
514 */
515Thread* dvmGetThreadFromThreadObject(Object* vmThreadObj);
516
517/*
Andy McFadden2b94b302010-03-09 16:38:36 -0800518 * Given a pthread handle, return the associated Thread*.
Andy McFaddenfd542662010-03-12 13:39:59 -0800519 * Caller must hold the thread list lock.
Andy McFadden2b94b302010-03-09 16:38:36 -0800520 *
521 * Returns NULL if the thread was not found.
522 */
523Thread* dvmGetThreadByHandle(pthread_t handle);
524
525/*
Andy McFaddenfd542662010-03-12 13:39:59 -0800526 * 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 */
531Thread* dvmGetThreadByThreadId(u4 threadId);
532
533/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800534 * Sleep in a thread. Returns when the sleep timer returns or the thread
535 * is interrupted.
536 */
537void dvmThreadSleep(u8 msec, u4 nsec);
538
539/*
540 * Get the name of a thread. (For safety, hold the thread list lock.)
541 */
542char* dvmGetThreadName(Thread* thread);
543
544/*
Ben Cheng7a0bcd02010-01-22 16:45:45 -0800545 * Convert ThreadStatus to a string.
546 */
547const char* dvmGetThreadStatusStr(ThreadStatus status);
548
549/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800550 * 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 */
553bool dvmIsOnThreadList(const Thread* thread);
Jeff Hao97319a82009-08-12 16:57:15 -0700554
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800555/*
556 * Get/set the JNIEnv field.
557 */
558INLINE JNIEnv* dvmGetThreadJNIEnv(Thread* self) { return self->jniEnv; }
559INLINE void dvmSetThreadJNIEnv(Thread* self, JNIEnv* env) { self->jniEnv = env;}
560
561/*
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800562 * Update the priority value of the underlying pthread.
563 */
564void dvmChangeThreadPriority(Thread* thread, int newPriority);
565
Andy McFadden2b94b302010-03-09 16:38:36 -0800566/* "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 */
575int 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 */
582void dvmResetThreadPriority(Thread* thread, int changeFlags,
583 int savedThreadPrio, SchedPolicy savedThreadPolicy);
584
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800585/*
586 * Debug: dump information about a single thread.
587 */
588void dvmDumpThread(Thread* thread, bool isRunning);
589void dvmDumpThreadEx(const DebugOutputTarget* target, Thread* thread,
590 bool isRunning);
591
592/*
593 * Debug: dump information about all threads.
594 */
595void dvmDumpAllThreads(bool grabLock);
596void dvmDumpAllThreadsEx(const DebugOutputTarget* target, bool grabLock);
597
Andy McFadden384ef6b2010-03-15 17:24:55 -0700598/*
599 * Debug: kill a thread to get a debuggerd stack trace. Leaves the VM
600 * in an uncertain state.
601 */
602void dvmNukeThread(Thread* thread);
603
Carl Shapiroae188c62011-04-08 13:11:58 -0700604#ifdef __cplusplus
605}
606#endif
607
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800608#endif /*_DALVIK_THREAD*/