blob: 0d79019eb8a6128cf1f77a4e72b44e4f05b67cc3 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002
3#ifndef ART_SRC_THREAD_H_
4#define ART_SRC_THREAD_H_
5
Carl Shapirob5573532011-07-12 18:22:59 -07006#include <pthread.h>
Elliott Hughesa0957642011-09-02 14:27:33 -07007
Elliott Hughes02b48d12011-09-07 17:15:51 -07008#include <bitset>
Elliott Hughesa0957642011-09-02 14:27:33 -07009#include <iosfwd>
Ian Rogersb033c752011-07-20 12:22:35 -070010#include <list>
Elliott Hughes8daa0922011-09-11 13:46:25 -070011#include <string>
Carl Shapirob5573532011-07-12 18:22:59 -070012
Brian Carlstrom1f870082011-08-23 16:02:11 -070013#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "globals.h"
Elliott Hughes69f5bc62011-08-24 09:26:14 -070015#include "jni_internal.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "logging.h"
17#include "macros.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070018#include "mutex.h"
Brian Carlstromb765be02011-08-17 23:54:10 -070019#include "mem_map.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020#include "offsets.h"
Ian Rogersb033c752011-07-20 12:22:35 -070021
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070022namespace art {
23
Elliott Hughes69f5bc62011-08-24 09:26:14 -070024class Array;
Elliott Hughes37f7a402011-08-22 18:56:01 -070025class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070026class ClassLinker;
Elliott Hughesedcc09c2011-08-21 18:47:05 -070027class ClassLoader;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070028class Method;
Elliott Hughes8daa0922011-09-11 13:46:25 -070029class Monitor;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070030class Object;
Carl Shapirob5573532011-07-12 18:22:59 -070031class Runtime;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070032class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070033class ThreadList;
Elliott Hughese5b0dc82011-08-23 09:59:02 -070034class Throwable;
Shih-wei Liao55df06b2011-08-26 14:39:27 -070035class StackTraceElement;
buzbee1da522d2011-09-04 11:22:20 -070036class StaticStorageBase;
37
Shih-wei Liao55df06b2011-08-26 14:39:27 -070038template<class T> class ObjectArray;
Shih-wei Liao44175362011-08-28 16:59:17 -070039template<class T> class PrimitiveArray;
40typedef PrimitiveArray<int32_t> IntArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041
Ian Rogers408f79a2011-08-23 18:22:33 -070042// Stack allocated indirect reference table, allocated within the bridge frame
43// between managed and native code.
44class StackIndirectReferenceTable {
Ian Rogersb033c752011-07-20 12:22:35 -070045 public:
Ian Rogers408f79a2011-08-23 18:22:33 -070046 // Number of references contained within this SIRT
Ian Rogersb033c752011-07-20 12:22:35 -070047 size_t NumberOfReferences() {
48 return number_of_references_;
49 }
50
Ian Rogers408f79a2011-08-23 18:22:33 -070051 // Link to previous SIRT or NULL
52 StackIndirectReferenceTable* Link() {
Ian Rogersb033c752011-07-20 12:22:35 -070053 return link_;
54 }
55
Ian Rogers408f79a2011-08-23 18:22:33 -070056 Object** References() {
57 return references_;
Ian Rogersa8cd9f42011-08-19 16:43:41 -070058 }
59
Ian Rogers408f79a2011-08-23 18:22:33 -070060 // Offset of length within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -070061 static size_t NumberOfReferencesOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -070062 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
Ian Rogersb033c752011-07-20 12:22:35 -070063 }
64
Ian Rogers408f79a2011-08-23 18:22:33 -070065 // Offset of link within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -070066 static size_t LinkOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -070067 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
Ian Rogersb033c752011-07-20 12:22:35 -070068 }
69
70 private:
Ian Rogers408f79a2011-08-23 18:22:33 -070071 StackIndirectReferenceTable() {}
Ian Rogersb033c752011-07-20 12:22:35 -070072
73 size_t number_of_references_;
Ian Rogers408f79a2011-08-23 18:22:33 -070074 StackIndirectReferenceTable* link_;
Ian Rogersb033c752011-07-20 12:22:35 -070075
Ian Rogersa8cd9f42011-08-19 16:43:41 -070076 // Fake array, really allocated and filled in by jni_compiler.
Ian Rogers408f79a2011-08-23 18:22:33 -070077 Object* references_[0];
Ian Rogersa8cd9f42011-08-19 16:43:41 -070078
Ian Rogers408f79a2011-08-23 18:22:33 -070079 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
Ian Rogersb033c752011-07-20 12:22:35 -070080};
81
Ian Rogers6de08602011-08-19 14:52:39 -070082struct NativeToManagedRecord {
83 NativeToManagedRecord* link;
84 void* last_top_of_managed_stack;
85};
86
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070087// Iterator over managed frames up to the first native-to-managed transition
88class Frame {
Shih-wei Liao9b576b42011-08-29 01:45:07 -070089 public:
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070090 Frame() : sp_(NULL) {}
91
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070092 Method* GetMethod() const {
Elliott Hughesa0957642011-09-02 14:27:33 -070093 return (sp_ != NULL) ? *sp_ : NULL;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -070094 }
95
96 bool HasNext() const {
97 return NextMethod() != NULL;
98 }
99
100 void Next();
101
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700102 uintptr_t GetPC() const;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700103
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700104 Method** GetSP() const {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700105 return sp_;
106 }
107
108 // TODO: this is here for testing, remove when we have exception unit tests
109 // that use the real stack
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700110 void SetSP(Method** sp) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700111 sp_ = sp;
112 }
113
114 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700115 Method* NextMethod() const;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700116
117 friend class Thread;
118
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700119 Method** sp_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700120};
121
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700122class Thread {
123 public:
Elliott Hughes8daa0922011-09-11 13:46:25 -0700124 /* thread priorities, from java.lang.Thread */
125 enum Priority {
126 kMinPriority = 1,
127 kNormPriority = 5,
128 kMaxPriority = 10,
129 };
Carl Shapirob5573532011-07-12 18:22:59 -0700130 enum State {
131 kUnknown = -1,
Elliott Hughes93e74e82011-09-13 11:07:03 -0700132
133 // These match up with JDWP values.
134 kTerminated = 0, // TERMINATED
135 kRunnable = 1, // RUNNABLE or running now
136 kTimedWaiting = 2, // TIMED_WAITING in Object.wait()
137 kBlocked = 3, // BLOCKED on a monitor
138 kWaiting = 4, // WAITING in Object.wait()
139 // Non-JDWP states.
140 kInitializing = 5, // allocated, not yet running --- TODO: unnecessary?
141 kStarting = 6, // native thread started, not yet ready to run managed code
142 kNative = 7, // off in a JNI native method
143 kVmWait = 8, // waiting on a VM resource
144 kSuspended = 9, // suspended, usually by GC or debugger
Carl Shapirob5573532011-07-12 18:22:59 -0700145 };
146
buzbeecefd1872011-09-09 09:59:52 -0700147 static const size_t kStackOverflowReservedBytes = 1024; // Space to throw a StackOverflowError in.
buzbeec143c552011-08-20 17:38:58 -0700148
Carl Shapiro61e019d2011-07-14 16:53:09 -0700149 static const size_t kDefaultStackSize = 64 * KB;
150
buzbeec143c552011-08-20 17:38:58 -0700151 // Runtime support function pointers
buzbee4a3164f2011-09-03 11:25:10 -0700152 void (*pDebugMe)(Method*, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700153 void* (*pMemcpy)(void*, const void*, size_t);
buzbee54330722011-08-23 16:46:55 -0700154 uint64_t (*pShlLong)(uint64_t, uint32_t);
155 uint64_t (*pShrLong)(uint64_t, uint32_t);
156 uint64_t (*pUshrLong)(uint64_t, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700157 float (*pI2f)(int);
158 int (*pF2iz)(float);
159 float (*pD2f)(double);
160 double (*pF2d)(float);
161 double (*pI2d)(int);
162 int (*pD2iz)(double);
163 float (*pL2f)(long);
164 double (*pL2d)(long);
buzbee1b4c8592011-08-31 10:43:51 -0700165 long long (*pF2l)(float);
166 long long (*pD2l)(double);
buzbeec143c552011-08-20 17:38:58 -0700167 float (*pFadd)(float, float);
168 float (*pFsub)(float, float);
169 float (*pFdiv)(float, float);
170 float (*pFmul)(float, float);
171 float (*pFmodf)(float, float);
172 double (*pDadd)(double, double);
173 double (*pDsub)(double, double);
174 double (*pDdiv)(double, double);
175 double (*pDmul)(double, double);
176 double (*pFmod)(double, double);
177 int (*pIdivmod)(int, int);
178 int (*pIdiv)(int, int);
buzbee439c4fa2011-08-27 15:59:07 -0700179 long long (*pLmul)(long long, long long);
buzbeec143c552011-08-20 17:38:58 -0700180 long long (*pLdivmod)(long long, long long);
buzbeedfd3d702011-08-28 12:56:51 -0700181 Array* (*pAllocFromCode)(uint32_t, Method*, int32_t);
buzbee1da522d2011-09-04 11:22:20 -0700182 Array* (*pCheckAndAllocFromCode)(uint32_t, Method*, int32_t);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700183 Object* (*pAllocObjectFromCode)(uint32_t, Method*);
buzbeee1931742011-08-28 21:15:53 -0700184 uint32_t (*pGet32Static)(uint32_t, const Method*);
185 void (*pSet32Static)(uint32_t, const Method*, uint32_t);
186 uint64_t (*pGet64Static)(uint32_t, const Method*);
187 void (*pSet64Static)(uint32_t, const Method*, uint64_t);
188 Object* (*pGetObjStatic)(uint32_t, const Method*);
189 void (*pSetObjStatic)(uint32_t, const Method*, Object*);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700190 void (*pCanPutArrayElementFromCode)(const Class*, const Class*);
buzbee2a475e72011-09-07 17:19:17 -0700191 bool (*pInstanceofNonTrivialFromCode) (const Object*, const Class*);
192 void (*pCheckCastFromCode) (const Class*, const Class*);
buzbee1b4c8592011-08-31 10:43:51 -0700193 Method* (*pFindInterfaceMethodInCache)(Class*, uint32_t, const Method*, struct DvmDex*);
buzbee2a475e72011-09-07 17:19:17 -0700194 void (*pUnlockObjectFromCode)(Thread*, Object*);
buzbee1b4c8592011-08-31 10:43:51 -0700195 void (*pLockObjectFromCode)(Thread*, Object*);
196 void (*pThrowException)(Thread*, Throwable*);
197 void (*pHandleFillArrayDataFromCode)(Array*, const uint16_t*);
198 Class* (*pInitializeTypeFromCode)(uint32_t, Method*);
buzbee561227c2011-09-02 15:28:19 -0700199 void (*pResolveMethodFromCode)(Method*, uint32_t);
buzbee4a3164f2011-09-03 11:25:10 -0700200 void (*pInvokeInterfaceTrampoline)(void*, void*, void*, void*);
buzbee1da522d2011-09-04 11:22:20 -0700201 StaticStorageBase* (*pInitializeStaticStorage)(uint32_t, const Method*);
buzbee34cd9e52011-09-08 14:31:52 -0700202 Field* (*pFindFieldFromCode)(uint32_t, const Method*);
buzbee0d966cf2011-09-08 17:34:58 -0700203 void (*pCheckSuspendFromCode)(Thread*);
buzbeecefd1872011-09-09 09:59:52 -0700204 void (*pStackOverflowFromCode)(Method*);
buzbee5ade1d22011-09-09 14:44:52 -0700205 void (*pThrowNullPointerFromCode)();
206 void (*pThrowArrayBoundsFromCode)(int32_t, int32_t);
207 void (*pThrowDivZeroFromCode)();
208 void (*pThrowVerificationErrorFromCode)(int32_t, int32_t);
209 void (*pThrowNegArraySizeFromCode)(int32_t);
210 void (*pThrowRuntimeExceptionFromCode)(int32_t);
211 void (*pThrowInternalErrorFromCode)(int32_t);
212 void (*pThrowNoSuchMethodFromCode)(int32_t);
buzbeec143c552011-08-20 17:38:58 -0700213
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700214 class StackVisitor {
215 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 virtual ~StackVisitor() {}
Elliott Hughesd369bb72011-09-12 14:41:14 -0700217 virtual void VisitFrame(const Frame& frame) = 0;
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700218 };
219
Carl Shapiro61e019d2011-07-14 16:53:09 -0700220 // Creates a new thread.
Elliott Hughesd369bb72011-09-12 14:41:14 -0700221 static void Create(Object* peer, size_t stack_size);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700222
223 // Creates a new thread from the calling thread.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700224 static Thread* Attach(const Runtime* runtime, const char* name, bool as_daemon);
Carl Shapirob5573532011-07-12 18:22:59 -0700225
226 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700227 void* thread = pthread_getspecific(Thread::pthread_key_self_);
228 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229 }
230
Elliott Hughes8daa0922011-09-11 13:46:25 -0700231 static Thread* FromManagedThread(JNIEnv* env, jobject thread) {
232 // TODO: make these more generally available, and cached.
233 jclass java_lang_Thread = env->FindClass("java/lang/Thread");
234 jfieldID fid = env->GetFieldID(java_lang_Thread, "vmData", "I");
235 return reinterpret_cast<Thread*>(static_cast<uintptr_t>(env->GetIntField(thread, fid)));
236 }
237
Elliott Hughesa0957642011-09-02 14:27:33 -0700238 void Dump(std::ostream& os) const;
239
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240 State GetState() const {
241 return state_;
242 }
243
244 State SetState(State new_state) {
245 State old_state = state_;
246 state_ = new_state;
247 return old_state;
248 }
249
Elliott Hughes8daa0922011-09-11 13:46:25 -0700250 /*
251 * Changes the priority of this thread to match that of the java.lang.Thread object.
252 *
253 * We map a priority value from 1-10 to Linux "nice" values, where lower
254 * numbers indicate higher priority.
255 */
256 void SetNativePriority(int newPriority);
257
258 /*
259 * Returns the thread priority for the current thread by querying the system.
260 * This is useful when attaching a thread through JNI.
261 *
262 * Returns a value from 1 to 10 (compatible with java.lang.Thread values).
263 */
264 static int GetNativePriority();
265
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700266 bool CanAccessDirectReferences() const {
Elliott Hughesa59d1792011-09-04 18:42:35 -0700267 // TODO: when we have a moving collector, we'll need: return state_ == kRunnable;
268 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700269 }
270
Elliott Hughesdcc24742011-09-07 14:02:44 -0700271 uint32_t GetThinLockId() const {
272 return thin_lock_id_;
Carl Shapirob5573532011-07-12 18:22:59 -0700273 }
274
Elliott Hughesd92bec42011-09-02 17:04:36 -0700275 pid_t GetTid() const {
276 return tid_;
277 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700278
279 pthread_t GetImpl() const {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700280 return pthread_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700281 }
282
Elliott Hughesd369bb72011-09-12 14:41:14 -0700283 Object* GetPeer() const {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700284 return peer_;
285 }
286
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700287 // Returns the Method* for the current method.
288 // This is used by the JNI implementation for logging and diagnostic purposes.
289 const Method* GetCurrentMethod() const {
290 return top_of_managed_stack_.GetMethod();
291 }
292
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700293 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700294 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700295 }
296
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700297 Throwable* GetException() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700298 DCHECK(CanAccessDirectReferences());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700299 return exception_;
300 }
301
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 void SetException(Throwable* new_exception) {
303 DCHECK(CanAccessDirectReferences());
304 CHECK(new_exception != NULL);
305 // TODO: CHECK(exception_ == NULL);
306 exception_ = new_exception; // TODO
307 }
308
309 void ClearException() {
310 exception_ = NULL;
Elliott Hughesa0957642011-09-02 14:27:33 -0700311 }
312
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700313 Frame GetTopOfStack() const {
314 return top_of_managed_stack_;
315 }
316
317 // TODO: this is here for testing, remove when we have exception unit tests
318 // that use the real stack
319 void SetTopOfStack(void* stack) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(stack));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700321 }
322
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700323 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700324 __attribute__ ((format(printf, 3, 4)));
325
Elliott Hughes79082e32011-08-25 12:07:32 -0700326 // This exception is special, because we need to pre-allocate an instance.
327 void ThrowOutOfMemoryError();
328
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700329 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
330
331 void* FindExceptionHandlerInMethod(const Method* method,
332 void* throw_pc,
333 const DexFile& dex_file,
334 ClassLinker* class_linker);
buzbeec143c552011-08-20 17:38:58 -0700335
Carl Shapirob5573532011-07-12 18:22:59 -0700336 void SetName(const char* name);
337
338 void Suspend();
339
340 bool IsSuspended();
341
342 void Resume();
343
Elliott Hughesbe759c62011-09-08 19:38:21 -0700344 static void Startup();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700345 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700346
Ian Rogersb033c752011-07-20 12:22:35 -0700347 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700348 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700349 return jni_env_;
350 }
351
Ian Rogers408f79a2011-08-23 18:22:33 -0700352 // Number of references allocated in SIRTs on this thread
353 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700354
Ian Rogers408f79a2011-08-23 18:22:33 -0700355 // Is the given obj in this thread's stack indirect reference table?
356 bool SirtContains(jobject obj);
357
358 // Convert a jobject into a Object*
359 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700360
Elliott Hughes8daa0922011-09-11 13:46:25 -0700361 // Implements java.lang.Thread.interrupted.
362 bool Interrupted() {
363 MutexLock mu(wait_mutex_);
364 bool interrupted = interrupted_;
365 interrupted_ = false;
366 return interrupted;
367 }
368
369 // Implements java.lang.Thread.isInterrupted.
370 bool IsInterrupted() {
371 MutexLock mu(wait_mutex_);
372 return interrupted_;
373 }
374
Ian Rogers45a76cb2011-07-21 22:00:15 -0700375 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
376 exception_entry_point_ = handler;
377 }
378
Ian Rogers45a76cb2011-07-21 22:00:15 -0700379 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
380 suspend_count_entry_point_ = handler;
381 }
382
383 // Increasing the suspend count, will cause the thread to run to safepoint
384 void IncrementSuspendCount() { suspend_count_++; }
385 void DecrementSuspendCount() { suspend_count_--; }
386
Ian Rogers6de08602011-08-19 14:52:39 -0700387 // Linked list recording transitions from native to managed code
388 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700389 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700390 record->link = native_to_managed_record_;
391 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700392 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700393 }
394 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
395 native_to_managed_record_ = record.link;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396 top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(record.last_top_of_managed_stack));
Ian Rogers6de08602011-08-19 14:52:39 -0700397 }
398
Brian Carlstrombffb1552011-08-25 12:23:53 -0700399 const ClassLoader* GetClassLoaderOverride() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700400 // TODO: need to place the class_loader_override_ in a handle
401 // DCHECK(CanAccessDirectReferences());
buzbeec143c552011-08-20 17:38:58 -0700402 return class_loader_override_;
403 }
404
Brian Carlstrombffb1552011-08-25 12:23:53 -0700405 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700406 class_loader_override_ = class_loader_override;
407 }
408
Ian Rogersaaa20802011-09-11 21:47:37 -0700409 // Create the internal representation of a stack trace, that is more time
410 // and space efficient to compute than the StackTraceElement[]
411 jobject CreateInternalStackTrace() const;
412
413 // Convert an internal stack trace representation to a StackTraceElement[]
414 static jobjectArray
415 InternalStackTraceToStackTraceElementArray(jobject internal, JNIEnv* env);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700416
Elliott Hughes410c0c82011-09-01 17:58:25 -0700417 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
418
Elliott Hughesbe759c62011-09-08 19:38:21 -0700419 //
420 // Offsets of various members of native Thread class, used by compiled code.
421 //
422
423 static ThreadOffset SelfOffset() {
424 return ThreadOffset(OFFSETOF_MEMBER(Thread, self_));
425 }
426
427 static ThreadOffset ExceptionOffset() {
428 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
429 }
430
431 static ThreadOffset IdOffset() {
432 return ThreadOffset(OFFSETOF_MEMBER(Thread, thin_lock_id_));
433 }
434
435 static ThreadOffset CardTableOffset() {
436 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
437 }
438
439 static ThreadOffset SuspendCountOffset() {
440 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
441 }
442
443 static ThreadOffset StateOffset() {
Elliott Hughes93e74e82011-09-13 11:07:03 -0700444 return ThreadOffset(OFFSETOF_VOLATILE_MEMBER(Thread, state_));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700445 }
446
Elliott Hughes449b4bd2011-09-09 12:01:38 -0700447 static ThreadOffset StackEndOffset() {
448 return ThreadOffset(OFFSETOF_MEMBER(Thread, stack_end_));
Elliott Hughesbe759c62011-09-08 19:38:21 -0700449 }
450
451 static ThreadOffset JniEnvOffset() {
452 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
453 }
454
455 static ThreadOffset TopOfManagedStackOffset() {
456 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
457 OFFSETOF_MEMBER(Frame, sp_));
458 }
459
460 static ThreadOffset TopSirtOffset() {
461 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
462 }
463
464 static ThreadOffset ExceptionEntryPointOffset() {
465 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
466 }
467
468 static ThreadOffset SuspendCountEntryPointOffset() {
469 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
470 }
471
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700472 private:
Elliott Hughesdcc24742011-09-07 14:02:44 -0700473 Thread();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700474 ~Thread();
Elliott Hughes02b48d12011-09-07 17:15:51 -0700475 friend class ThreadList; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700476
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700477 void CreatePeer(const char* name, bool as_daemon);
478 friend class Runtime; // For CreatePeer.
479
Elliott Hughesd92bec42011-09-02 17:04:36 -0700480 void DumpState(std::ostream& os) const;
481 void DumpStack(std::ostream& os) const;
482
Elliott Hughes93e74e82011-09-13 11:07:03 -0700483 void Attach(const Runtime* runtime);
484 static void* CreateCallback(void* arg);
485
Ian Rogersb033c752011-07-20 12:22:35 -0700486 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700487 void InitFunctionPointers();
Elliott Hughesbe759c62011-09-08 19:38:21 -0700488 void InitStackHwm();
489
490 static void ThreadExitCallback(void* arg);
Ian Rogersb033c752011-07-20 12:22:35 -0700491
Ian Rogersaaa20802011-09-11 21:47:37 -0700492 void WalkStack(StackVisitor* visitor) const;
Shih-wei Liao44175362011-08-28 16:59:17 -0700493
Elliott Hughesdcc24742011-09-07 14:02:44 -0700494 // Thin lock thread id. This is a small integer used by the thin lock implementation.
495 // This is not to be confused with the native thread's tid, nor is it the value returned
496 // by java.lang.Thread.getId --- this is a distinct value, used only for locking. One
497 // important difference between this id and the ids visible to managed code is that these
498 // ones get reused (to ensure that they fit in the number of bits available).
499 uint32_t thin_lock_id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700500
Elliott Hughesd92bec42011-09-02 17:04:36 -0700501 // System thread id.
502 pid_t tid_;
503
504 // Native thread handle.
Elliott Hughesbe759c62011-09-08 19:38:21 -0700505 pthread_t pthread_;
Elliott Hughesd92bec42011-09-02 17:04:36 -0700506
Elliott Hughesdcc24742011-09-07 14:02:44 -0700507 // Our managed peer (an instance of java.lang.Thread).
Elliott Hughesd369bb72011-09-12 14:41:14 -0700508 Object* peer_;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700509
510 // Guards the 'interrupted_' and 'wait_monitor_' members.
511 mutable Mutex wait_mutex_;
512 // Pointer to the monitor lock we're currently waiting on (or NULL), guarded by wait_mutex_.
513 Monitor* wait_monitor_;
514 // Thread "interrupted" status; stays raised until queried or thrown, guarded by wait_mutex_.
515 bool interrupted_;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700516
buzbeec143c552011-08-20 17:38:58 -0700517 // FIXME: placeholder for the gc cardTable
518 uint32_t card_table_;
519
Elliott Hughes449b4bd2011-09-09 12:01:38 -0700520 // The end of this thread's stack. This is the lowest safely-addressable address on the stack.
521 // We leave extra space so there's room for the code that throws StackOverflowError.
522 byte* stack_end_;
Elliott Hughesbe759c62011-09-08 19:38:21 -0700523
Ian Rogers45a76cb2011-07-21 22:00:15 -0700524 // Top of the managed stack, written out prior to the state transition from
525 // kRunnable to kNative. Uses include to give the starting point for scanning
526 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700527 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700528
Ian Rogers6de08602011-08-19 14:52:39 -0700529 // A linked list (of stack allocated records) recording transitions from
530 // native to managed code.
531 NativeToManagedRecord* native_to_managed_record_;
532
Ian Rogers408f79a2011-08-23 18:22:33 -0700533 // Top of linked list of stack indirect reference tables or NULL for none
534 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700535
536 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700537 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700538
Elliott Hughes93e74e82011-09-13 11:07:03 -0700539 volatile State state_;
Carl Shapirob5573532011-07-12 18:22:59 -0700540
Carl Shapiro69759ea2011-07-21 18:13:35 -0700541 // Initialized to "this". On certain architectures (such as x86) reading
542 // off of Thread::Current is easy but getting the address of Thread::Current
543 // is hard. This field can be read off of Thread::Current to give the address.
544 Thread* self_;
545
546 Runtime* runtime_;
547
548 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700549 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700550
Ian Rogers45a76cb2011-07-21 22:00:15 -0700551 // A non-zero value is used to tell the current thread to enter a safe point
552 // at the next poll.
553 int suspend_count_;
554
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700555 // Needed to get the right ClassLoader in JNI_OnLoad, but also
556 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700557 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700558
Carl Shapiro69759ea2011-07-21 18:13:35 -0700559 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700560 static pthread_key_t pthread_key_self_;
561
Ian Rogers45a76cb2011-07-21 22:00:15 -0700562 // Entry point called when exception_ is set
563 void (*exception_entry_point_)(Method** frame);
564
565 // Entry point called when suspend_count_ is non-zero
566 void (*suspend_count_entry_point_)(Method** frame);
567
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700568 DISALLOW_COPY_AND_ASSIGN(Thread);
569};
Elliott Hughes330304d2011-08-12 14:28:05 -0700570std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700571std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700572
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700573class ScopedThreadStateChange {
574 public:
575 ScopedThreadStateChange(Thread* thread, Thread::State new_state) : thread_(thread) {
576 old_thread_state_ = thread_->SetState(new_state);
577 }
578
579 ~ScopedThreadStateChange() {
580 thread_->SetState(old_thread_state_);
581 }
582
583 private:
584 Thread* thread_;
585 Thread::State old_thread_state_;
586 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
587};
588
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700589} // namespace art
590
591#endif // ART_SRC_THREAD_H_