blob: ae07fa607d83cd8954fd10002faf9a9b1e517482 [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>
Ian Rogersb033c752011-07-20 12:22:35 -07007#include <list>
Carl Shapirob5573532011-07-12 18:22:59 -07008
Brian Carlstrom1f870082011-08-23 16:02:11 -07009#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "globals.h"
Elliott Hughes69f5bc62011-08-24 09:26:14 -070011#include "jni_internal.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "logging.h"
13#include "macros.h"
Brian Carlstromb765be02011-08-17 23:54:10 -070014#include "mem_map.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "offsets.h"
Ian Rogersb033c752011-07-20 12:22:35 -070016
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070017namespace art {
18
Elliott Hughes69f5bc62011-08-24 09:26:14 -070019class Array;
Elliott Hughes37f7a402011-08-22 18:56:01 -070020class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070021class ClassLinker;
Elliott Hughesedcc09c2011-08-21 18:47:05 -070022class ClassLoader;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070023class Method;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070024class Object;
Carl Shapirob5573532011-07-12 18:22:59 -070025class Runtime;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070027class ThreadList;
Elliott Hughese5b0dc82011-08-23 09:59:02 -070028class Throwable;
Shih-wei Liao55df06b2011-08-26 14:39:27 -070029class StackTraceElement;
30template<class T> class ObjectArray;
Shih-wei Liao44175362011-08-28 16:59:17 -070031template<class T> class PrimitiveArray;
32typedef PrimitiveArray<int32_t> IntArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070033
34class Mutex {
35 public:
36 virtual ~Mutex() {}
37
Carl Shapirob5573532011-07-12 18:22:59 -070038 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070039
Carl Shapirob5573532011-07-12 18:22:59 -070040 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041
Carl Shapirob5573532011-07-12 18:22:59 -070042 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
44 const char* GetName() { return name_; }
45
46 Thread* GetOwner() { return owner_; }
47
Carl Shapirob5573532011-07-12 18:22:59 -070048 static Mutex* Create(const char* name);
49
Elliott Hughes79082e32011-08-25 12:07:32 -070050 // TODO: only needed because we lack a condition variable abstraction.
51 pthread_mutex_t* GetImpl() { return &lock_impl_; }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070052
53 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070054 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
55
Elliott Hughes79082e32011-08-25 12:07:32 -070056 void SetOwner(Thread* thread) { owner_ = thread; }
57
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070058 const char* name_;
59
60 Thread* owner_;
61
Carl Shapirob5573532011-07-12 18:22:59 -070062 pthread_mutex_t lock_impl_;
63
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070064 DISALLOW_COPY_AND_ASSIGN(Mutex);
65};
66
67class MutexLock {
68 public:
69 explicit MutexLock(Mutex *mu) : mu_(mu) {
70 mu_->Lock();
71 }
72 ~MutexLock() { mu_->Unlock(); }
73 private:
74 Mutex* const mu_;
75 DISALLOW_COPY_AND_ASSIGN(MutexLock);
76};
77
Ian Rogers408f79a2011-08-23 18:22:33 -070078// Stack allocated indirect reference table, allocated within the bridge frame
79// between managed and native code.
80class StackIndirectReferenceTable {
Ian Rogersb033c752011-07-20 12:22:35 -070081 public:
Ian Rogers408f79a2011-08-23 18:22:33 -070082 // Number of references contained within this SIRT
Ian Rogersb033c752011-07-20 12:22:35 -070083 size_t NumberOfReferences() {
84 return number_of_references_;
85 }
86
Ian Rogers408f79a2011-08-23 18:22:33 -070087 // Link to previous SIRT or NULL
88 StackIndirectReferenceTable* Link() {
Ian Rogersb033c752011-07-20 12:22:35 -070089 return link_;
90 }
91
Ian Rogers408f79a2011-08-23 18:22:33 -070092 Object** References() {
93 return references_;
Ian Rogersa8cd9f42011-08-19 16:43:41 -070094 }
95
Ian Rogers408f79a2011-08-23 18:22:33 -070096 // Offset of length within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -070097 static size_t NumberOfReferencesOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -070098 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
Ian Rogersb033c752011-07-20 12:22:35 -070099 }
100
Ian Rogers408f79a2011-08-23 18:22:33 -0700101 // Offset of link within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -0700102 static size_t LinkOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -0700103 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
Ian Rogersb033c752011-07-20 12:22:35 -0700104 }
105
106 private:
Ian Rogers408f79a2011-08-23 18:22:33 -0700107 StackIndirectReferenceTable() {}
Ian Rogersb033c752011-07-20 12:22:35 -0700108
109 size_t number_of_references_;
Ian Rogers408f79a2011-08-23 18:22:33 -0700110 StackIndirectReferenceTable* link_;
Ian Rogersb033c752011-07-20 12:22:35 -0700111
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700112 // Fake array, really allocated and filled in by jni_compiler.
Ian Rogers408f79a2011-08-23 18:22:33 -0700113 Object* references_[0];
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700114
Ian Rogers408f79a2011-08-23 18:22:33 -0700115 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
Ian Rogersb033c752011-07-20 12:22:35 -0700116};
117
Ian Rogers6de08602011-08-19 14:52:39 -0700118struct NativeToManagedRecord {
119 NativeToManagedRecord* link;
120 void* last_top_of_managed_stack;
121};
122
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700123// Iterator over managed frames up to the first native-to-managed transition
124class Frame {
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700125 public:
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700126 Frame() : sp_(NULL) {}
127
128 const Method* GetMethod() const {
129 return *sp_;
130 }
131
132 bool HasNext() const {
133 return NextMethod() != NULL;
134 }
135
136 void Next();
137
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700138 uintptr_t GetPC() const;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700139
140 const Method** GetSP() const {
141 return sp_;
142 }
143
144 // TODO: this is here for testing, remove when we have exception unit tests
145 // that use the real stack
146 void SetSP(const Method** sp) {
147 sp_ = sp;
148 }
149
150 private:
151 const Method* NextMethod() const;
152
153 friend class Thread;
154
155 const Method** sp_;
156};
157
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700158class Thread {
159 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700160 enum State {
161 kUnknown = -1,
162 kNew,
163 kRunnable,
164 kBlocked,
165 kWaiting,
166 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700167 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700168 kTerminated,
169 };
170
buzbeec143c552011-08-20 17:38:58 -0700171
Carl Shapiro61e019d2011-07-14 16:53:09 -0700172 static const size_t kDefaultStackSize = 64 * KB;
173
buzbeec143c552011-08-20 17:38:58 -0700174 // Runtime support function pointers
175 void* (*pMemcpy)(void*, const void*, size_t);
buzbee54330722011-08-23 16:46:55 -0700176 uint64_t (*pShlLong)(uint64_t, uint32_t);
177 uint64_t (*pShrLong)(uint64_t, uint32_t);
178 uint64_t (*pUshrLong)(uint64_t, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700179 float (*pI2f)(int);
180 int (*pF2iz)(float);
181 float (*pD2f)(double);
182 double (*pF2d)(float);
183 double (*pI2d)(int);
184 int (*pD2iz)(double);
185 float (*pL2f)(long);
186 double (*pL2d)(long);
buzbee1b4c8592011-08-31 10:43:51 -0700187 long long (*pF2l)(float);
188 long long (*pD2l)(double);
buzbeec143c552011-08-20 17:38:58 -0700189 float (*pFadd)(float, float);
190 float (*pFsub)(float, float);
191 float (*pFdiv)(float, float);
192 float (*pFmul)(float, float);
193 float (*pFmodf)(float, float);
194 double (*pDadd)(double, double);
195 double (*pDsub)(double, double);
196 double (*pDdiv)(double, double);
197 double (*pDmul)(double, double);
198 double (*pFmod)(double, double);
199 int (*pIdivmod)(int, int);
200 int (*pIdiv)(int, int);
buzbee439c4fa2011-08-27 15:59:07 -0700201 long long (*pLmul)(long long, long long);
buzbeec143c552011-08-20 17:38:58 -0700202 long long (*pLdivmod)(long long, long long);
buzbeedfd3d702011-08-28 12:56:51 -0700203 Array* (*pAllocFromCode)(uint32_t, Method*, int32_t);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700204 Object* (*pAllocObjectFromCode)(uint32_t, Method*);
buzbeee1931742011-08-28 21:15:53 -0700205 uint32_t (*pGet32Static)(uint32_t, const Method*);
206 void (*pSet32Static)(uint32_t, const Method*, uint32_t);
207 uint64_t (*pGet64Static)(uint32_t, const Method*);
208 void (*pSet64Static)(uint32_t, const Method*, uint64_t);
209 Object* (*pGetObjStatic)(uint32_t, const Method*);
210 void (*pSetObjStatic)(uint32_t, const Method*, Object*);
buzbee1b4c8592011-08-31 10:43:51 -0700211 bool (*pCanPutArrayElementFromCode)(const Class*, const Class*);
212 int (*pInstanceofNonTrivialFromCode) (const Class*, const Class*);
213 Method* (*pFindInterfaceMethodInCache)(Class*, uint32_t, const Method*, struct DvmDex*);
214 bool (*pUnlockObjectFromCode)(Thread*, Object*);
215 void (*pLockObjectFromCode)(Thread*, Object*);
216 void (*pThrowException)(Thread*, Throwable*);
217 void (*pHandleFillArrayDataFromCode)(Array*, const uint16_t*);
218 Class* (*pInitializeTypeFromCode)(uint32_t, Method*);
buzbeec143c552011-08-20 17:38:58 -0700219
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700220 class StackVisitor {
221 public:
222 virtual ~StackVisitor() {};
223 virtual bool VisitFrame(const Frame& frame) = 0;
224 };
225
Carl Shapiro61e019d2011-07-14 16:53:09 -0700226 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700227 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700228
229 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700230 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700231
232 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700233 void* thread = pthread_getspecific(Thread::pthread_key_self_);
234 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700235 }
236
Carl Shapirob5573532011-07-12 18:22:59 -0700237 uint32_t GetId() const {
238 return id_;
239 }
240
Elliott Hughese27955c2011-08-26 15:21:24 -0700241 pid_t GetTid() const;
242
243 pthread_t GetImpl() const {
244 return handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700245 }
246
247 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700248 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249 }
250
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700251 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700252 return exception_;
253 }
254
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700255 Frame GetTopOfStack() const {
256 return top_of_managed_stack_;
257 }
258
259 // TODO: this is here for testing, remove when we have exception unit tests
260 // that use the real stack
261 void SetTopOfStack(void* stack) {
262 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
263 }
264
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700265 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700266 CHECK(new_exception != NULL);
267 // TODO: CHECK(exception_ == NULL);
268 exception_ = new_exception; // TODO
269 }
270
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700271 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700272 __attribute__ ((format(printf, 3, 4)));
273
Elliott Hughes79082e32011-08-25 12:07:32 -0700274 // This exception is special, because we need to pre-allocate an instance.
275 void ThrowOutOfMemoryError();
276
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700277 void ClearException() {
278 exception_ = NULL;
279 }
280
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700281 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
282
283 void* FindExceptionHandlerInMethod(const Method* method,
284 void* throw_pc,
285 const DexFile& dex_file,
286 ClassLinker* class_linker);
287
Ian Rogers45a76cb2011-07-21 22:00:15 -0700288 // Offset of exception within Thread, used by generated code
289 static ThreadOffset ExceptionOffset() {
290 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
291 }
292
buzbeec143c552011-08-20 17:38:58 -0700293 // Offset of id within Thread, used by generated code
294 static ThreadOffset IdOffset() {
295 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
296 }
297
298 // Offset of card_table within Thread, used by generated code
299 static ThreadOffset CardTableOffset() {
300 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
301 }
302
Carl Shapirob5573532011-07-12 18:22:59 -0700303 void SetName(const char* name);
304
305 void Suspend();
306
307 bool IsSuspended();
308
309 void Resume();
310
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700311 static bool Startup();
312 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700313
Elliott Hughes330304d2011-08-12 14:28:05 -0700314 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700315 return state_;
316 }
317
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700318 State SetState(State new_state) {
319 State old_state = state_;
Carl Shapirob5573532011-07-12 18:22:59 -0700320 state_ = new_state;
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700321 return old_state;
Carl Shapirob5573532011-07-12 18:22:59 -0700322 }
323
Ian Rogers45a76cb2011-07-21 22:00:15 -0700324 static ThreadOffset SuspendCountOffset() {
325 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
326 }
327
Ian Rogersb033c752011-07-20 12:22:35 -0700328 // Offset of state within Thread, used by generated code
329 static ThreadOffset StateOffset() {
330 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
331 }
332
Ian Rogersb033c752011-07-20 12:22:35 -0700333 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700334 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700335 return jni_env_;
336 }
337
338 // Offset of JNI environment within Thread, used by generated code
339 static ThreadOffset JniEnvOffset() {
340 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
341 }
342
Ian Rogers45a76cb2011-07-21 22:00:15 -0700343 // Offset of top of managed stack address, used by generated code
344 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700345 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
346 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700347 }
348
Ian Rogers408f79a2011-08-23 18:22:33 -0700349 // Offset of top stack indirect reference table within Thread, used by
350 // generated code
351 static ThreadOffset TopSirtOffset() {
352 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700353 }
354
Ian Rogers408f79a2011-08-23 18:22:33 -0700355 // Number of references allocated in SIRTs on this thread
356 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700357
Ian Rogers408f79a2011-08-23 18:22:33 -0700358 // Is the given obj in this thread's stack indirect reference table?
359 bool SirtContains(jobject obj);
360
361 // Convert a jobject into a Object*
362 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700363
Ian Rogers45a76cb2011-07-21 22:00:15 -0700364 // Offset of exception_entry_point_ within Thread, used by generated code
365 static ThreadOffset ExceptionEntryPointOffset() {
366 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
367 }
368
369 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
370 exception_entry_point_ = handler;
371 }
372
373 // Offset of suspend_count_entry_point_ within Thread, used by generated code
374 static ThreadOffset SuspendCountEntryPointOffset() {
375 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
376 }
377
378 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
379 suspend_count_entry_point_ = handler;
380 }
381
382 // Increasing the suspend count, will cause the thread to run to safepoint
383 void IncrementSuspendCount() { suspend_count_++; }
384 void DecrementSuspendCount() { suspend_count_--; }
385
Ian Rogers6de08602011-08-19 14:52:39 -0700386 // Linked list recording transitions from native to managed code
387 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700388 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700389 record->link = native_to_managed_record_;
390 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700391 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700392 }
393 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
394 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700395 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700396 }
397
Brian Carlstrombffb1552011-08-25 12:23:53 -0700398 const ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700399 return class_loader_override_;
400 }
401
Brian Carlstrombffb1552011-08-25 12:23:53 -0700402 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700403 class_loader_override_ = class_loader_override;
404 }
405
Shih-wei Liao44175362011-08-28 16:59:17 -0700406 // Allocate stack trace
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700407 ObjectArray<StackTraceElement>* AllocStackTrace();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700408
Elliott Hughes410c0c82011-09-01 17:58:25 -0700409 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
410
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700411 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700412 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700413 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700414 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700415 native_to_managed_record_(NULL),
Ian Rogers408f79a2011-08-23 18:22:33 -0700416 top_sirt_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700417 jni_env_(NULL),
418 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700419 suspend_count_(0),
420 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700421 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700422 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700423
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700424 ~Thread();
425 friend class Runtime; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700426
Ian Rogersb033c752011-07-20 12:22:35 -0700427 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700428 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700429
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700430 void WalkStack(StackVisitor* visitor);
Shih-wei Liao44175362011-08-28 16:59:17 -0700431
Carl Shapiro69759ea2011-07-21 18:13:35 -0700432 // Managed thread id.
433 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700434
buzbeec143c552011-08-20 17:38:58 -0700435 // FIXME: placeholder for the gc cardTable
436 uint32_t card_table_;
437
Ian Rogers45a76cb2011-07-21 22:00:15 -0700438 // Top of the managed stack, written out prior to the state transition from
439 // kRunnable to kNative. Uses include to give the starting point for scanning
440 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700441 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700442
Ian Rogers6de08602011-08-19 14:52:39 -0700443 // A linked list (of stack allocated records) recording transitions from
444 // native to managed code.
445 NativeToManagedRecord* native_to_managed_record_;
446
Ian Rogers408f79a2011-08-23 18:22:33 -0700447 // Top of linked list of stack indirect reference tables or NULL for none
448 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700449
450 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700451 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700452
Carl Shapirob5573532011-07-12 18:22:59 -0700453 State state_;
454
Carl Shapiro69759ea2011-07-21 18:13:35 -0700455 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700456 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700457
Carl Shapiro69759ea2011-07-21 18:13:35 -0700458 // Initialized to "this". On certain architectures (such as x86) reading
459 // off of Thread::Current is easy but getting the address of Thread::Current
460 // is hard. This field can be read off of Thread::Current to give the address.
461 Thread* self_;
462
463 Runtime* runtime_;
464
465 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700466 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700467
Ian Rogers45a76cb2011-07-21 22:00:15 -0700468 // A non-zero value is used to tell the current thread to enter a safe point
469 // at the next poll.
470 int suspend_count_;
471
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700472 // Needed to get the right ClassLoader in JNI_OnLoad, but also
473 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700474 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700475
Carl Shapiro69759ea2011-07-21 18:13:35 -0700476 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700477 static pthread_key_t pthread_key_self_;
478
Ian Rogers45a76cb2011-07-21 22:00:15 -0700479 // Entry point called when exception_ is set
480 void (*exception_entry_point_)(Method** frame);
481
482 // Entry point called when suspend_count_ is non-zero
483 void (*suspend_count_entry_point_)(Method** frame);
484
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700485 DISALLOW_COPY_AND_ASSIGN(Thread);
486};
Elliott Hughes330304d2011-08-12 14:28:05 -0700487std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700488std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700489
Carl Shapirob5573532011-07-12 18:22:59 -0700490class ThreadList {
491 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700492 static const int kMaxId = 0xFFFF;
493 static const int kInvalidId = 0;
494 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700495
Carl Shapiro61e019d2011-07-14 16:53:09 -0700496 static ThreadList* Create();
497
498 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700499
500 void Register(Thread* thread);
501
502 void Unregister(Thread* thread);
503
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700504 bool Contains(Thread* thread);
505
Carl Shapirob5573532011-07-12 18:22:59 -0700506 void Lock() {
507 lock_->Lock();
508 }
509
510 void Unlock() {
511 lock_->Unlock();
512 };
513
Elliott Hughes410c0c82011-09-01 17:58:25 -0700514 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
515
Carl Shapirob5573532011-07-12 18:22:59 -0700516 private:
517 ThreadList();
518
519 std::list<Thread*> list_;
520
521 Mutex* lock_;
522
523 DISALLOW_COPY_AND_ASSIGN(ThreadList);
524};
525
526class ThreadListLock {
527 public:
528 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
529 : thread_list_(thread_list) {
530 if (current_thread == NULL) { // try to get it from TLS
531 current_thread = Thread::Current();
532 }
533 Thread::State old_state;
534 if (current_thread != NULL) {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700535 old_state = current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
Carl Shapirob5573532011-07-12 18:22:59 -0700536 } else {
537 // happens during VM shutdown
538 old_state = Thread::kUnknown; // TODO: something else
539 }
540 thread_list_->Lock();
541 if (current_thread != NULL) {
542 current_thread->SetState(old_state);
543 }
544 }
545
546 ~ThreadListLock() {
547 thread_list_->Unlock();
548 }
549
Carl Shapirob5573532011-07-12 18:22:59 -0700550 private:
551 ThreadList* thread_list_;
552
553 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
554};
555
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700556class ScopedThreadStateChange {
557 public:
558 ScopedThreadStateChange(Thread* thread, Thread::State new_state) : thread_(thread) {
559 old_thread_state_ = thread_->SetState(new_state);
560 }
561
562 ~ScopedThreadStateChange() {
563 thread_->SetState(old_thread_state_);
564 }
565
566 private:
567 Thread* thread_;
568 Thread::State old_thread_state_;
569 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
570};
571
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700572} // namespace art
573
574#endif // ART_SRC_THREAD_H_