blob: 6b7e27cd04d12fabce91e91c8b09d362a5b90935 [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
8#include <iosfwd>
Ian Rogersb033c752011-07-20 12:22:35 -07009#include <list>
Carl Shapirob5573532011-07-12 18:22:59 -070010
Brian Carlstrom1f870082011-08-23 16:02:11 -070011#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "globals.h"
Elliott Hughes69f5bc62011-08-24 09:26:14 -070013#include "jni_internal.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "logging.h"
15#include "macros.h"
Brian Carlstromb765be02011-08-17 23:54:10 -070016#include "mem_map.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "offsets.h"
Ian Rogersb033c752011-07-20 12:22:35 -070018
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019namespace art {
20
Elliott Hughes69f5bc62011-08-24 09:26:14 -070021class Array;
Elliott Hughes37f7a402011-08-22 18:56:01 -070022class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070023class ClassLinker;
Elliott Hughesedcc09c2011-08-21 18:47:05 -070024class ClassLoader;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070025class Method;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026class Object;
Carl Shapirob5573532011-07-12 18:22:59 -070027class Runtime;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070028class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070029class ThreadList;
Elliott Hughese5b0dc82011-08-23 09:59:02 -070030class Throwable;
Shih-wei Liao55df06b2011-08-26 14:39:27 -070031class StackTraceElement;
buzbee1da522d2011-09-04 11:22:20 -070032class StaticStorageBase;
33
Shih-wei Liao55df06b2011-08-26 14:39:27 -070034template<class T> class ObjectArray;
Shih-wei Liao44175362011-08-28 16:59:17 -070035template<class T> class PrimitiveArray;
36typedef PrimitiveArray<int32_t> IntArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037
38class Mutex {
39 public:
40 virtual ~Mutex() {}
41
Carl Shapirob5573532011-07-12 18:22:59 -070042 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
Carl Shapirob5573532011-07-12 18:22:59 -070044 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
Carl Shapirob5573532011-07-12 18:22:59 -070046 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
48 const char* GetName() { return name_; }
49
50 Thread* GetOwner() { return owner_; }
51
Carl Shapirob5573532011-07-12 18:22:59 -070052 static Mutex* Create(const char* name);
53
Elliott Hughes79082e32011-08-25 12:07:32 -070054 // TODO: only needed because we lack a condition variable abstraction.
55 pthread_mutex_t* GetImpl() { return &lock_impl_; }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070056
57 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070058 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
59
Elliott Hughes79082e32011-08-25 12:07:32 -070060 void SetOwner(Thread* thread) { owner_ = thread; }
61
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070062 const char* name_;
63
64 Thread* owner_;
65
Carl Shapirob5573532011-07-12 18:22:59 -070066 pthread_mutex_t lock_impl_;
67
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068 DISALLOW_COPY_AND_ASSIGN(Mutex);
69};
70
71class MutexLock {
72 public:
73 explicit MutexLock(Mutex *mu) : mu_(mu) {
74 mu_->Lock();
75 }
76 ~MutexLock() { mu_->Unlock(); }
77 private:
78 Mutex* const mu_;
79 DISALLOW_COPY_AND_ASSIGN(MutexLock);
80};
81
Ian Rogers408f79a2011-08-23 18:22:33 -070082// Stack allocated indirect reference table, allocated within the bridge frame
83// between managed and native code.
84class StackIndirectReferenceTable {
Ian Rogersb033c752011-07-20 12:22:35 -070085 public:
Ian Rogers408f79a2011-08-23 18:22:33 -070086 // Number of references contained within this SIRT
Ian Rogersb033c752011-07-20 12:22:35 -070087 size_t NumberOfReferences() {
88 return number_of_references_;
89 }
90
Ian Rogers408f79a2011-08-23 18:22:33 -070091 // Link to previous SIRT or NULL
92 StackIndirectReferenceTable* Link() {
Ian Rogersb033c752011-07-20 12:22:35 -070093 return link_;
94 }
95
Ian Rogers408f79a2011-08-23 18:22:33 -070096 Object** References() {
97 return references_;
Ian Rogersa8cd9f42011-08-19 16:43:41 -070098 }
99
Ian Rogers408f79a2011-08-23 18:22:33 -0700100 // Offset of length within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -0700101 static size_t NumberOfReferencesOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -0700102 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
Ian Rogersb033c752011-07-20 12:22:35 -0700103 }
104
Ian Rogers408f79a2011-08-23 18:22:33 -0700105 // Offset of link within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -0700106 static size_t LinkOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -0700107 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
Ian Rogersb033c752011-07-20 12:22:35 -0700108 }
109
110 private:
Ian Rogers408f79a2011-08-23 18:22:33 -0700111 StackIndirectReferenceTable() {}
Ian Rogersb033c752011-07-20 12:22:35 -0700112
113 size_t number_of_references_;
Ian Rogers408f79a2011-08-23 18:22:33 -0700114 StackIndirectReferenceTable* link_;
Ian Rogersb033c752011-07-20 12:22:35 -0700115
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700116 // Fake array, really allocated and filled in by jni_compiler.
Ian Rogers408f79a2011-08-23 18:22:33 -0700117 Object* references_[0];
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700118
Ian Rogers408f79a2011-08-23 18:22:33 -0700119 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
Ian Rogersb033c752011-07-20 12:22:35 -0700120};
121
Ian Rogers6de08602011-08-19 14:52:39 -0700122struct NativeToManagedRecord {
123 NativeToManagedRecord* link;
124 void* last_top_of_managed_stack;
125};
126
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700127// Iterator over managed frames up to the first native-to-managed transition
128class Frame {
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700129 public:
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700130 Frame() : sp_(NULL) {}
131
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700132 Method* GetMethod() const {
Elliott Hughesa0957642011-09-02 14:27:33 -0700133 return (sp_ != NULL) ? *sp_ : NULL;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700134 }
135
136 bool HasNext() const {
137 return NextMethod() != NULL;
138 }
139
140 void Next();
141
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700142 uintptr_t GetPC() const;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700143
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700144 Method** GetSP() const {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700145 return sp_;
146 }
147
148 // TODO: this is here for testing, remove when we have exception unit tests
149 // that use the real stack
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150 void SetSP(Method** sp) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700151 sp_ = sp;
152 }
153
154 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700155 Method* NextMethod() const;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700156
157 friend class Thread;
158
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700159 Method** sp_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700160};
161
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700162class Thread {
163 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700164 enum State {
165 kUnknown = -1,
166 kNew,
167 kRunnable,
168 kBlocked,
169 kWaiting,
170 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700171 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700172 kTerminated,
173 };
174
buzbeec143c552011-08-20 17:38:58 -0700175
Carl Shapiro61e019d2011-07-14 16:53:09 -0700176 static const size_t kDefaultStackSize = 64 * KB;
177
buzbeec143c552011-08-20 17:38:58 -0700178 // Runtime support function pointers
buzbee4a3164f2011-09-03 11:25:10 -0700179 void (*pDebugMe)(Method*, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700180 void* (*pMemcpy)(void*, const void*, size_t);
buzbee54330722011-08-23 16:46:55 -0700181 uint64_t (*pShlLong)(uint64_t, uint32_t);
182 uint64_t (*pShrLong)(uint64_t, uint32_t);
183 uint64_t (*pUshrLong)(uint64_t, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700184 float (*pI2f)(int);
185 int (*pF2iz)(float);
186 float (*pD2f)(double);
187 double (*pF2d)(float);
188 double (*pI2d)(int);
189 int (*pD2iz)(double);
190 float (*pL2f)(long);
191 double (*pL2d)(long);
buzbee1b4c8592011-08-31 10:43:51 -0700192 long long (*pF2l)(float);
193 long long (*pD2l)(double);
buzbeec143c552011-08-20 17:38:58 -0700194 float (*pFadd)(float, float);
195 float (*pFsub)(float, float);
196 float (*pFdiv)(float, float);
197 float (*pFmul)(float, float);
198 float (*pFmodf)(float, float);
199 double (*pDadd)(double, double);
200 double (*pDsub)(double, double);
201 double (*pDdiv)(double, double);
202 double (*pDmul)(double, double);
203 double (*pFmod)(double, double);
204 int (*pIdivmod)(int, int);
205 int (*pIdiv)(int, int);
buzbee439c4fa2011-08-27 15:59:07 -0700206 long long (*pLmul)(long long, long long);
buzbeec143c552011-08-20 17:38:58 -0700207 long long (*pLdivmod)(long long, long long);
buzbeedfd3d702011-08-28 12:56:51 -0700208 Array* (*pAllocFromCode)(uint32_t, Method*, int32_t);
buzbee1da522d2011-09-04 11:22:20 -0700209 Array* (*pCheckAndAllocFromCode)(uint32_t, Method*, int32_t);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700210 Object* (*pAllocObjectFromCode)(uint32_t, Method*);
buzbeee1931742011-08-28 21:15:53 -0700211 uint32_t (*pGet32Static)(uint32_t, const Method*);
212 void (*pSet32Static)(uint32_t, const Method*, uint32_t);
213 uint64_t (*pGet64Static)(uint32_t, const Method*);
214 void (*pSet64Static)(uint32_t, const Method*, uint64_t);
215 Object* (*pGetObjStatic)(uint32_t, const Method*);
216 void (*pSetObjStatic)(uint32_t, const Method*, Object*);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700217 void (*pCanPutArrayElementFromCode)(const Class*, const Class*);
buzbee1b4c8592011-08-31 10:43:51 -0700218 int (*pInstanceofNonTrivialFromCode) (const Class*, const Class*);
219 Method* (*pFindInterfaceMethodInCache)(Class*, uint32_t, const Method*, struct DvmDex*);
220 bool (*pUnlockObjectFromCode)(Thread*, Object*);
221 void (*pLockObjectFromCode)(Thread*, Object*);
222 void (*pThrowException)(Thread*, Throwable*);
223 void (*pHandleFillArrayDataFromCode)(Array*, const uint16_t*);
224 Class* (*pInitializeTypeFromCode)(uint32_t, Method*);
buzbee561227c2011-09-02 15:28:19 -0700225 void (*pResolveMethodFromCode)(Method*, uint32_t);
buzbee4a3164f2011-09-03 11:25:10 -0700226 void (*pInvokeInterfaceTrampoline)(void*, void*, void*, void*);
buzbee1da522d2011-09-04 11:22:20 -0700227 StaticStorageBase* (*pInitializeStaticStorage)(uint32_t, const Method*);
buzbeec143c552011-08-20 17:38:58 -0700228
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700229 class StackVisitor {
230 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700231 virtual ~StackVisitor() {}
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700232 virtual bool VisitFrame(const Frame& frame) = 0;
233 };
234
Carl Shapiro61e019d2011-07-14 16:53:09 -0700235 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700236 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700237
238 // Creates a new thread from the calling thread.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700239 static Thread* Attach(const Runtime* runtime, const char* name, bool as_daemon);
Carl Shapirob5573532011-07-12 18:22:59 -0700240
241 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700242 void* thread = pthread_getspecific(Thread::pthread_key_self_);
243 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700244 }
245
Elliott Hughesa0957642011-09-02 14:27:33 -0700246 void Dump(std::ostream& os) const;
247
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700248 State GetState() const {
249 return state_;
250 }
251
252 State SetState(State new_state) {
253 State old_state = state_;
254 state_ = new_state;
255 return old_state;
256 }
257
258 bool CanAccessDirectReferences() const {
Elliott Hughesa59d1792011-09-04 18:42:35 -0700259 // TODO: when we have a moving collector, we'll need: return state_ == kRunnable;
260 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 }
262
Elliott Hughesdcc24742011-09-07 14:02:44 -0700263 uint32_t GetThinLockId() const {
264 return thin_lock_id_;
Carl Shapirob5573532011-07-12 18:22:59 -0700265 }
266
Elliott Hughesd92bec42011-09-02 17:04:36 -0700267 pid_t GetTid() const {
268 return tid_;
269 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700270
271 pthread_t GetImpl() const {
272 return handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700273 }
274
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 // Returns the Method* for the current method.
276 // This is used by the JNI implementation for logging and diagnostic purposes.
277 const Method* GetCurrentMethod() const {
278 return top_of_managed_stack_.GetMethod();
279 }
280
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700281 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700282 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700283 }
284
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700285 Throwable* GetException() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700286 DCHECK(CanAccessDirectReferences());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700287 return exception_;
288 }
289
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700290 void SetException(Throwable* new_exception) {
291 DCHECK(CanAccessDirectReferences());
292 CHECK(new_exception != NULL);
293 // TODO: CHECK(exception_ == NULL);
294 exception_ = new_exception; // TODO
295 }
296
297 void ClearException() {
298 exception_ = NULL;
Elliott Hughesa0957642011-09-02 14:27:33 -0700299 }
300
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700301 Frame GetTopOfStack() const {
302 return top_of_managed_stack_;
303 }
304
305 // TODO: this is here for testing, remove when we have exception unit tests
306 // that use the real stack
307 void SetTopOfStack(void* stack) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700308 top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(stack));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700309 }
310
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700311 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700312 __attribute__ ((format(printf, 3, 4)));
313
Elliott Hughes79082e32011-08-25 12:07:32 -0700314 // This exception is special, because we need to pre-allocate an instance.
315 void ThrowOutOfMemoryError();
316
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700317 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
318
319 void* FindExceptionHandlerInMethod(const Method* method,
320 void* throw_pc,
321 const DexFile& dex_file,
322 ClassLinker* class_linker);
Shih-wei Liao668512a2011-09-01 14:18:34 -0700323 static ThreadOffset SelfOffset() {
324 return ThreadOffset(OFFSETOF_MEMBER(Thread, self_));
325 }
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700326
Elliott Hughesdcc24742011-09-07 14:02:44 -0700327 // Offset of exception_ within Thread, used by generated code
Ian Rogers45a76cb2011-07-21 22:00:15 -0700328 static ThreadOffset ExceptionOffset() {
329 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
330 }
331
Elliott Hughesdcc24742011-09-07 14:02:44 -0700332 // Offset of thin_lock_id_ within Thread, used by generated code
buzbeec143c552011-08-20 17:38:58 -0700333 static ThreadOffset IdOffset() {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700334 return ThreadOffset(OFFSETOF_MEMBER(Thread, thin_lock_id_));
buzbeec143c552011-08-20 17:38:58 -0700335 }
336
337 // Offset of card_table within Thread, used by generated code
338 static ThreadOffset CardTableOffset() {
339 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
340 }
341
Carl Shapirob5573532011-07-12 18:22:59 -0700342 void SetName(const char* name);
343
344 void Suspend();
345
346 bool IsSuspended();
347
348 void Resume();
349
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700350 static bool Startup();
351 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700352
Ian Rogers45a76cb2011-07-21 22:00:15 -0700353 static ThreadOffset SuspendCountOffset() {
354 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
355 }
356
Ian Rogersb033c752011-07-20 12:22:35 -0700357 // Offset of state within Thread, used by generated code
358 static ThreadOffset StateOffset() {
359 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
360 }
361
Ian Rogersb033c752011-07-20 12:22:35 -0700362 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700363 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700364 return jni_env_;
365 }
366
367 // Offset of JNI environment within Thread, used by generated code
368 static ThreadOffset JniEnvOffset() {
369 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
370 }
371
Ian Rogers45a76cb2011-07-21 22:00:15 -0700372 // Offset of top of managed stack address, used by generated code
373 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700374 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
375 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700376 }
377
Ian Rogers408f79a2011-08-23 18:22:33 -0700378 // Offset of top stack indirect reference table within Thread, used by
379 // generated code
380 static ThreadOffset TopSirtOffset() {
381 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700382 }
383
Ian Rogers408f79a2011-08-23 18:22:33 -0700384 // Number of references allocated in SIRTs on this thread
385 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700386
Ian Rogers408f79a2011-08-23 18:22:33 -0700387 // Is the given obj in this thread's stack indirect reference table?
388 bool SirtContains(jobject obj);
389
390 // Convert a jobject into a Object*
391 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700392
Ian Rogers45a76cb2011-07-21 22:00:15 -0700393 // Offset of exception_entry_point_ within Thread, used by generated code
394 static ThreadOffset ExceptionEntryPointOffset() {
395 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
396 }
397
398 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
399 exception_entry_point_ = handler;
400 }
401
402 // Offset of suspend_count_entry_point_ within Thread, used by generated code
403 static ThreadOffset SuspendCountEntryPointOffset() {
404 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
405 }
406
407 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
408 suspend_count_entry_point_ = handler;
409 }
410
411 // Increasing the suspend count, will cause the thread to run to safepoint
412 void IncrementSuspendCount() { suspend_count_++; }
413 void DecrementSuspendCount() { suspend_count_--; }
414
Ian Rogers6de08602011-08-19 14:52:39 -0700415 // Linked list recording transitions from native to managed code
416 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700417 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700418 record->link = native_to_managed_record_;
419 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700420 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700421 }
422 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
423 native_to_managed_record_ = record.link;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(record.last_top_of_managed_stack));
Ian Rogers6de08602011-08-19 14:52:39 -0700425 }
426
Brian Carlstrombffb1552011-08-25 12:23:53 -0700427 const ClassLoader* GetClassLoaderOverride() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700428 // TODO: need to place the class_loader_override_ in a handle
429 // DCHECK(CanAccessDirectReferences());
buzbeec143c552011-08-20 17:38:58 -0700430 return class_loader_override_;
431 }
432
Brian Carlstrombffb1552011-08-25 12:23:53 -0700433 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700434 class_loader_override_ = class_loader_override;
435 }
436
Shih-wei Liao44175362011-08-28 16:59:17 -0700437 // Allocate stack trace
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700438 ObjectArray<StackTraceElement>* AllocStackTrace();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700439
Elliott Hughes410c0c82011-09-01 17:58:25 -0700440 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
441
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700442 private:
Elliott Hughesdcc24742011-09-07 14:02:44 -0700443 Thread();
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700444 ~Thread();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700445 friend class Runtime; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700446
Elliott Hughesd92bec42011-09-02 17:04:36 -0700447 void DumpState(std::ostream& os) const;
448 void DumpStack(std::ostream& os) const;
449
Ian Rogersb033c752011-07-20 12:22:35 -0700450 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700451 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700452
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700453 void WalkStack(StackVisitor* visitor);
Shih-wei Liao44175362011-08-28 16:59:17 -0700454
Elliott Hughesdcc24742011-09-07 14:02:44 -0700455 // Thin lock thread id. This is a small integer used by the thin lock implementation.
456 // This is not to be confused with the native thread's tid, nor is it the value returned
457 // by java.lang.Thread.getId --- this is a distinct value, used only for locking. One
458 // important difference between this id and the ids visible to managed code is that these
459 // ones get reused (to ensure that they fit in the number of bits available).
460 uint32_t thin_lock_id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700461
Elliott Hughesd92bec42011-09-02 17:04:36 -0700462 // System thread id.
463 pid_t tid_;
464
465 // Native thread handle.
466 pthread_t handle_;
467
Elliott Hughesdcc24742011-09-07 14:02:44 -0700468 bool is_daemon_;
469
470 // Our managed peer (an instance of java.lang.Thread).
471 Object* peer_;
472
buzbeec143c552011-08-20 17:38:58 -0700473 // FIXME: placeholder for the gc cardTable
474 uint32_t card_table_;
475
Ian Rogers45a76cb2011-07-21 22:00:15 -0700476 // Top of the managed stack, written out prior to the state transition from
477 // kRunnable to kNative. Uses include to give the starting point for scanning
478 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700479 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700480
Ian Rogers6de08602011-08-19 14:52:39 -0700481 // A linked list (of stack allocated records) recording transitions from
482 // native to managed code.
483 NativeToManagedRecord* native_to_managed_record_;
484
Ian Rogers408f79a2011-08-23 18:22:33 -0700485 // Top of linked list of stack indirect reference tables or NULL for none
486 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700487
488 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700489 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700490
Carl Shapirob5573532011-07-12 18:22:59 -0700491 State state_;
492
Carl Shapiro69759ea2011-07-21 18:13:35 -0700493 // Initialized to "this". On certain architectures (such as x86) reading
494 // off of Thread::Current is easy but getting the address of Thread::Current
495 // is hard. This field can be read off of Thread::Current to give the address.
496 Thread* self_;
497
498 Runtime* runtime_;
499
500 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700501 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700502
Ian Rogers45a76cb2011-07-21 22:00:15 -0700503 // A non-zero value is used to tell the current thread to enter a safe point
504 // at the next poll.
505 int suspend_count_;
506
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700507 // Needed to get the right ClassLoader in JNI_OnLoad, but also
508 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700509 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700510
Carl Shapiro69759ea2011-07-21 18:13:35 -0700511 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700512 static pthread_key_t pthread_key_self_;
513
Ian Rogers45a76cb2011-07-21 22:00:15 -0700514 // Entry point called when exception_ is set
515 void (*exception_entry_point_)(Method** frame);
516
517 // Entry point called when suspend_count_ is non-zero
518 void (*suspend_count_entry_point_)(Method** frame);
519
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700520 DISALLOW_COPY_AND_ASSIGN(Thread);
521};
Elliott Hughes330304d2011-08-12 14:28:05 -0700522std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700523std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700524
Carl Shapirob5573532011-07-12 18:22:59 -0700525class ThreadList {
526 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700527 static const int kMaxId = 0xFFFF;
528 static const int kInvalidId = 0;
529 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700530
Carl Shapiro61e019d2011-07-14 16:53:09 -0700531 static ThreadList* Create();
532
533 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700534
Elliott Hughesd92bec42011-09-02 17:04:36 -0700535 void Dump(std::ostream& os);
536
Carl Shapirob5573532011-07-12 18:22:59 -0700537 void Register(Thread* thread);
538
539 void Unregister(Thread* thread);
540
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700541 bool Contains(Thread* thread);
542
Carl Shapirob5573532011-07-12 18:22:59 -0700543 void Lock() {
544 lock_->Lock();
545 }
546
547 void Unlock() {
548 lock_->Unlock();
549 };
550
Elliott Hughes410c0c82011-09-01 17:58:25 -0700551 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
552
Carl Shapirob5573532011-07-12 18:22:59 -0700553 private:
554 ThreadList();
555
556 std::list<Thread*> list_;
557
558 Mutex* lock_;
559
560 DISALLOW_COPY_AND_ASSIGN(ThreadList);
561};
562
563class ThreadListLock {
564 public:
565 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
566 : thread_list_(thread_list) {
567 if (current_thread == NULL) { // try to get it from TLS
568 current_thread = Thread::Current();
569 }
570 Thread::State old_state;
571 if (current_thread != NULL) {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700572 old_state = current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
Carl Shapirob5573532011-07-12 18:22:59 -0700573 } else {
574 // happens during VM shutdown
575 old_state = Thread::kUnknown; // TODO: something else
576 }
577 thread_list_->Lock();
578 if (current_thread != NULL) {
579 current_thread->SetState(old_state);
580 }
581 }
582
583 ~ThreadListLock() {
584 thread_list_->Unlock();
585 }
586
Carl Shapirob5573532011-07-12 18:22:59 -0700587 private:
588 ThreadList* thread_list_;
589
590 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
591};
592
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700593class ScopedThreadStateChange {
594 public:
595 ScopedThreadStateChange(Thread* thread, Thread::State new_state) : thread_(thread) {
596 old_thread_state_ = thread_->SetState(new_state);
597 }
598
599 ~ScopedThreadStateChange() {
600 thread_->SetState(old_thread_state_);
601 }
602
603 private:
604 Thread* thread_;
605 Thread::State old_thread_state_;
606 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
607};
608
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700609} // namespace art
610
611#endif // ART_SRC_THREAD_H_