blob: 82cca6d687213c72774e6742c20ae017c250ec6d [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 Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "globals.h"
Elliott Hughes69f5bc62011-08-24 09:26:14 -070010#include "jni_internal.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "logging.h"
12#include "macros.h"
Brian Carlstromb765be02011-08-17 23:54:10 -070013#include "mem_map.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "offsets.h"
15#include "runtime.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogersb033c752011-07-20 12:22:35 -070017
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018namespace art {
19
Elliott Hughes69f5bc62011-08-24 09:26:14 -070020class Array;
Elliott Hughes37f7a402011-08-22 18:56:01 -070021class Class;
Elliott Hughesedcc09c2011-08-21 18:47:05 -070022class ClassLoader;
Elliott Hughes69f5bc62011-08-24 09:26:14 -070023class JNIEnvExt;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070024class Method;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070025class Object;
Carl Shapirob5573532011-07-12 18:22:59 -070026class Runtime;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070027class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070028class ThreadList;
Elliott Hughese5b0dc82011-08-23 09:59:02 -070029class Throwable;
Shih-wei Liao55df06b2011-08-26 14:39:27 -070030class StackTraceElement;
31template<class T> class ObjectArray;
Shih-wei Liao44175362011-08-28 16:59:17 -070032template<class T> class PrimitiveArray;
33typedef PrimitiveArray<int32_t> IntArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
35class Mutex {
36 public:
37 virtual ~Mutex() {}
38
Carl Shapirob5573532011-07-12 18:22:59 -070039 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070040
Carl Shapirob5573532011-07-12 18:22:59 -070041 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042
Carl Shapirob5573532011-07-12 18:22:59 -070043 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044
45 const char* GetName() { return name_; }
46
47 Thread* GetOwner() { return owner_; }
48
Carl Shapirob5573532011-07-12 18:22:59 -070049 static Mutex* Create(const char* name);
50
Elliott Hughes79082e32011-08-25 12:07:32 -070051 // TODO: only needed because we lack a condition variable abstraction.
52 pthread_mutex_t* GetImpl() { return &lock_impl_; }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053
54 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070055 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
56
Elliott Hughes79082e32011-08-25 12:07:32 -070057 void SetOwner(Thread* thread) { owner_ = thread; }
58
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070059 const char* name_;
60
61 Thread* owner_;
62
Carl Shapirob5573532011-07-12 18:22:59 -070063 pthread_mutex_t lock_impl_;
64
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070065 DISALLOW_COPY_AND_ASSIGN(Mutex);
66};
67
68class MutexLock {
69 public:
70 explicit MutexLock(Mutex *mu) : mu_(mu) {
71 mu_->Lock();
72 }
73 ~MutexLock() { mu_->Unlock(); }
74 private:
75 Mutex* const mu_;
76 DISALLOW_COPY_AND_ASSIGN(MutexLock);
77};
78
Ian Rogers408f79a2011-08-23 18:22:33 -070079// Stack allocated indirect reference table, allocated within the bridge frame
80// between managed and native code.
81class StackIndirectReferenceTable {
Ian Rogersb033c752011-07-20 12:22:35 -070082 public:
Ian Rogers408f79a2011-08-23 18:22:33 -070083 // Number of references contained within this SIRT
Ian Rogersb033c752011-07-20 12:22:35 -070084 size_t NumberOfReferences() {
85 return number_of_references_;
86 }
87
Ian Rogers408f79a2011-08-23 18:22:33 -070088 // Link to previous SIRT or NULL
89 StackIndirectReferenceTable* Link() {
Ian Rogersb033c752011-07-20 12:22:35 -070090 return link_;
91 }
92
Ian Rogers408f79a2011-08-23 18:22:33 -070093 Object** References() {
94 return references_;
Ian Rogersa8cd9f42011-08-19 16:43:41 -070095 }
96
Ian Rogers408f79a2011-08-23 18:22:33 -070097 // Offset of length within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -070098 static size_t NumberOfReferencesOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -070099 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
Ian Rogersb033c752011-07-20 12:22:35 -0700100 }
101
Ian Rogers408f79a2011-08-23 18:22:33 -0700102 // Offset of link within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -0700103 static size_t LinkOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -0700104 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
Ian Rogersb033c752011-07-20 12:22:35 -0700105 }
106
107 private:
Ian Rogers408f79a2011-08-23 18:22:33 -0700108 StackIndirectReferenceTable() {}
Ian Rogersb033c752011-07-20 12:22:35 -0700109
110 size_t number_of_references_;
Ian Rogers408f79a2011-08-23 18:22:33 -0700111 StackIndirectReferenceTable* link_;
Ian Rogersb033c752011-07-20 12:22:35 -0700112
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700113 // Fake array, really allocated and filled in by jni_compiler.
Ian Rogers408f79a2011-08-23 18:22:33 -0700114 Object* references_[0];
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700115
Ian Rogers408f79a2011-08-23 18:22:33 -0700116 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
Ian Rogersb033c752011-07-20 12:22:35 -0700117};
118
Ian Rogers6de08602011-08-19 14:52:39 -0700119struct NativeToManagedRecord {
120 NativeToManagedRecord* link;
121 void* last_top_of_managed_stack;
122};
123
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700124// Iterator over managed frames up to the first native-to-managed transition
125class Frame {
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700126 public:
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700127 Frame() : sp_(NULL) {}
128
129 const Method* GetMethod() const {
130 return *sp_;
131 }
132
133 bool HasNext() const {
134 return NextMethod() != NULL;
135 }
136
137 void Next();
138
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700139 uintptr_t GetPC() const;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700140
141 const Method** GetSP() const {
142 return sp_;
143 }
144
145 // TODO: this is here for testing, remove when we have exception unit tests
146 // that use the real stack
147 void SetSP(const Method** sp) {
148 sp_ = sp;
149 }
150
151 private:
152 const Method* NextMethod() const;
153
154 friend class Thread;
155
156 const Method** sp_;
157};
158
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700159class Thread {
160 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700161 enum State {
162 kUnknown = -1,
163 kNew,
164 kRunnable,
165 kBlocked,
166 kWaiting,
167 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700168 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700169 kTerminated,
170 };
171
buzbeec143c552011-08-20 17:38:58 -0700172
Carl Shapiro61e019d2011-07-14 16:53:09 -0700173 static const size_t kDefaultStackSize = 64 * KB;
174
buzbeec143c552011-08-20 17:38:58 -0700175 // Runtime support function pointers
176 void* (*pMemcpy)(void*, const void*, size_t);
buzbee54330722011-08-23 16:46:55 -0700177 uint64_t (*pShlLong)(uint64_t, uint32_t);
178 uint64_t (*pShrLong)(uint64_t, uint32_t);
179 uint64_t (*pUshrLong)(uint64_t, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700180 float (*pI2f)(int);
181 int (*pF2iz)(float);
182 float (*pD2f)(double);
183 double (*pF2d)(float);
184 double (*pI2d)(int);
185 int (*pD2iz)(double);
186 float (*pL2f)(long);
187 double (*pL2d)(long);
188 long long (*pArtF2l)(float);
189 long long (*pArtD2l)(double);
190 float (*pFadd)(float, float);
191 float (*pFsub)(float, float);
192 float (*pFdiv)(float, float);
193 float (*pFmul)(float, float);
194 float (*pFmodf)(float, float);
195 double (*pDadd)(double, double);
196 double (*pDsub)(double, double);
197 double (*pDdiv)(double, double);
198 double (*pDmul)(double, double);
199 double (*pFmod)(double, double);
200 int (*pIdivmod)(int, int);
201 int (*pIdiv)(int, int);
buzbee439c4fa2011-08-27 15:59:07 -0700202 long long (*pLmul)(long long, long long);
buzbeec143c552011-08-20 17:38:58 -0700203 long long (*pLdivmod)(long long, long long);
buzbeedfd3d702011-08-28 12:56:51 -0700204 Array* (*pAllocFromCode)(uint32_t, Method*, int32_t);
205 Object* (*pNewInstanceFromCode)(uint32_t, Method*);
buzbeee1931742011-08-28 21:15:53 -0700206 uint32_t (*pGet32Static)(uint32_t, const Method*);
207 void (*pSet32Static)(uint32_t, const Method*, uint32_t);
208 uint64_t (*pGet64Static)(uint32_t, const Method*);
209 void (*pSet64Static)(uint32_t, const Method*, uint64_t);
210 Object* (*pGetObjStatic)(uint32_t, const Method*);
211 void (*pSetObjStatic)(uint32_t, const Method*, Object*);
buzbeec143c552011-08-20 17:38:58 -0700212 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
213 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
buzbee3ea4ec52011-08-22 17:37:19 -0700214 const struct ClassObject*);
215 int (*pArtInstanceofNonTrivialNoThrow) (const struct ClassObject*,
216 const struct ClassObject*);
217 int (*pArtInstanceofNonTrivial) (const struct ClassObject*, const struct ClassObject*);
buzbeec143c552011-08-20 17:38:58 -0700218 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
buzbee3ea4ec52011-08-22 17:37:19 -0700219 const struct Method*, struct DvmDex*);
buzbeec143c552011-08-20 17:38:58 -0700220 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
221 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
222 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
223 void (*pArtThrowException)(struct Thread*, struct Object*);
buzbeee6d61962011-08-27 11:58:19 -0700224 void (*pArtHandleFillArrayDataNoThrow)(Array*, const uint16_t*);
buzbeec143c552011-08-20 17:38:58 -0700225
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700226 class StackVisitor {
227 public:
228 virtual ~StackVisitor() {};
229 virtual bool VisitFrame(const Frame& frame) = 0;
230 };
231
Carl Shapiro61e019d2011-07-14 16:53:09 -0700232 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700233 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700234
235 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700236 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700237
238 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700239 void* thread = pthread_getspecific(Thread::pthread_key_self_);
240 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700241 }
242
Carl Shapirob5573532011-07-12 18:22:59 -0700243 uint32_t GetId() const {
244 return id_;
245 }
246
Elliott Hughese27955c2011-08-26 15:21:24 -0700247 pid_t GetTid() const;
248
249 pthread_t GetImpl() const {
250 return handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700251 }
252
253 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700254 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700255 }
256
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700257 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700258 return exception_;
259 }
260
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700261 Frame GetTopOfStack() const {
262 return top_of_managed_stack_;
263 }
264
265 // TODO: this is here for testing, remove when we have exception unit tests
266 // that use the real stack
267 void SetTopOfStack(void* stack) {
268 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
269 }
270
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700271 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700272 CHECK(new_exception != NULL);
273 // TODO: CHECK(exception_ == NULL);
274 exception_ = new_exception; // TODO
275 }
276
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700277 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700278 __attribute__ ((format(printf, 3, 4)));
279
Elliott Hughes79082e32011-08-25 12:07:32 -0700280 // This exception is special, because we need to pre-allocate an instance.
281 void ThrowOutOfMemoryError();
282
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700283 void ClearException() {
284 exception_ = NULL;
285 }
286
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700287 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
288
289 void* FindExceptionHandlerInMethod(const Method* method,
290 void* throw_pc,
291 const DexFile& dex_file,
292 ClassLinker* class_linker);
293
Ian Rogers45a76cb2011-07-21 22:00:15 -0700294 // Offset of exception within Thread, used by generated code
295 static ThreadOffset ExceptionOffset() {
296 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
297 }
298
buzbeec143c552011-08-20 17:38:58 -0700299 // Offset of id within Thread, used by generated code
300 static ThreadOffset IdOffset() {
301 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
302 }
303
304 // Offset of card_table within Thread, used by generated code
305 static ThreadOffset CardTableOffset() {
306 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
307 }
308
Carl Shapirob5573532011-07-12 18:22:59 -0700309 void SetName(const char* name);
310
311 void Suspend();
312
313 bool IsSuspended();
314
315 void Resume();
316
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700317 static bool Startup();
318 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700319
Elliott Hughes330304d2011-08-12 14:28:05 -0700320 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700321 return state_;
322 }
323
324 void SetState(State new_state) {
325 state_ = new_state;
326 }
327
Ian Rogers45a76cb2011-07-21 22:00:15 -0700328 static ThreadOffset SuspendCountOffset() {
329 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
330 }
331
Ian Rogersb033c752011-07-20 12:22:35 -0700332 // Offset of state within Thread, used by generated code
333 static ThreadOffset StateOffset() {
334 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
335 }
336
Ian Rogersb033c752011-07-20 12:22:35 -0700337 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700338 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700339 return jni_env_;
340 }
341
342 // Offset of JNI environment within Thread, used by generated code
343 static ThreadOffset JniEnvOffset() {
344 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
345 }
346
Ian Rogers45a76cb2011-07-21 22:00:15 -0700347 // Offset of top of managed stack address, used by generated code
348 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700349 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
350 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700351 }
352
Ian Rogers408f79a2011-08-23 18:22:33 -0700353 // Offset of top stack indirect reference table within Thread, used by
354 // generated code
355 static ThreadOffset TopSirtOffset() {
356 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700357 }
358
Ian Rogers408f79a2011-08-23 18:22:33 -0700359 // Number of references allocated in SIRTs on this thread
360 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700361
Ian Rogers408f79a2011-08-23 18:22:33 -0700362 // Is the given obj in this thread's stack indirect reference table?
363 bool SirtContains(jobject obj);
364
365 // Convert a jobject into a Object*
366 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700367
Ian Rogers45a76cb2011-07-21 22:00:15 -0700368 // Offset of exception_entry_point_ within Thread, used by generated code
369 static ThreadOffset ExceptionEntryPointOffset() {
370 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
371 }
372
373 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
374 exception_entry_point_ = handler;
375 }
376
377 // Offset of suspend_count_entry_point_ within Thread, used by generated code
378 static ThreadOffset SuspendCountEntryPointOffset() {
379 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
380 }
381
382 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
383 suspend_count_entry_point_ = handler;
384 }
385
386 // Increasing the suspend count, will cause the thread to run to safepoint
387 void IncrementSuspendCount() { suspend_count_++; }
388 void DecrementSuspendCount() { suspend_count_--; }
389
Ian Rogers6de08602011-08-19 14:52:39 -0700390 // Linked list recording transitions from native to managed code
391 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700392 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700393 record->link = native_to_managed_record_;
394 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700395 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700396 }
397 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
398 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700399 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700400 }
401
Brian Carlstrombffb1552011-08-25 12:23:53 -0700402 const ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700403 return class_loader_override_;
404 }
405
Brian Carlstrombffb1552011-08-25 12:23:53 -0700406 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700407 class_loader_override_ = class_loader_override;
408 }
409
Shih-wei Liao44175362011-08-28 16:59:17 -0700410 // Allocate stack trace
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700411 ObjectArray<StackTraceElement>* AllocStackTrace();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700412
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700413 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700414 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700415 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700416 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700417 native_to_managed_record_(NULL),
Ian Rogers408f79a2011-08-23 18:22:33 -0700418 top_sirt_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700419 jni_env_(NULL),
420 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700421 suspend_count_(0),
422 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700423 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700424 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700425
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700426 ~Thread();
427 friend class Runtime; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700428
Ian Rogersb033c752011-07-20 12:22:35 -0700429 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700430 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700431
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700432 void WalkStack(StackVisitor* visitor);
Shih-wei Liao44175362011-08-28 16:59:17 -0700433
Carl Shapiro69759ea2011-07-21 18:13:35 -0700434 // Managed thread id.
435 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700436
buzbeec143c552011-08-20 17:38:58 -0700437 // FIXME: placeholder for the gc cardTable
438 uint32_t card_table_;
439
Ian Rogers45a76cb2011-07-21 22:00:15 -0700440 // Top of the managed stack, written out prior to the state transition from
441 // kRunnable to kNative. Uses include to give the starting point for scanning
442 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700443 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700444
Ian Rogers6de08602011-08-19 14:52:39 -0700445 // A linked list (of stack allocated records) recording transitions from
446 // native to managed code.
447 NativeToManagedRecord* native_to_managed_record_;
448
Ian Rogers408f79a2011-08-23 18:22:33 -0700449 // Top of linked list of stack indirect reference tables or NULL for none
450 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700451
452 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700453 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700454
Carl Shapirob5573532011-07-12 18:22:59 -0700455 State state_;
456
Carl Shapiro69759ea2011-07-21 18:13:35 -0700457 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700458 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700459
Carl Shapiro69759ea2011-07-21 18:13:35 -0700460 // Initialized to "this". On certain architectures (such as x86) reading
461 // off of Thread::Current is easy but getting the address of Thread::Current
462 // is hard. This field can be read off of Thread::Current to give the address.
463 Thread* self_;
464
465 Runtime* runtime_;
466
467 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700468 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700469
Ian Rogers45a76cb2011-07-21 22:00:15 -0700470 // A non-zero value is used to tell the current thread to enter a safe point
471 // at the next poll.
472 int suspend_count_;
473
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700474 // Needed to get the right ClassLoader in JNI_OnLoad, but also
475 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700476 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700477
Carl Shapiro69759ea2011-07-21 18:13:35 -0700478 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700479 static pthread_key_t pthread_key_self_;
480
Ian Rogers45a76cb2011-07-21 22:00:15 -0700481 // Entry point called when exception_ is set
482 void (*exception_entry_point_)(Method** frame);
483
484 // Entry point called when suspend_count_ is non-zero
485 void (*suspend_count_entry_point_)(Method** frame);
486
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700487 DISALLOW_COPY_AND_ASSIGN(Thread);
488};
Elliott Hughes330304d2011-08-12 14:28:05 -0700489std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700490std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700491
Carl Shapirob5573532011-07-12 18:22:59 -0700492class ThreadList {
493 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700494 static const int kMaxId = 0xFFFF;
495 static const int kInvalidId = 0;
496 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700497
Carl Shapiro61e019d2011-07-14 16:53:09 -0700498 static ThreadList* Create();
499
500 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700501
502 void Register(Thread* thread);
503
504 void Unregister(Thread* thread);
505
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700506 bool Contains(Thread* thread);
507
Carl Shapirob5573532011-07-12 18:22:59 -0700508 void Lock() {
509 lock_->Lock();
510 }
511
512 void Unlock() {
513 lock_->Unlock();
514 };
515
516 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) {
535 old_state = current_thread->GetState();
536 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
537 } else {
538 // happens during VM shutdown
539 old_state = Thread::kUnknown; // TODO: something else
540 }
541 thread_list_->Lock();
542 if (current_thread != NULL) {
543 current_thread->SetState(old_state);
544 }
545 }
546
547 ~ThreadListLock() {
548 thread_list_->Unlock();
549 }
550
Carl Shapirob5573532011-07-12 18:22:59 -0700551 private:
552 ThreadList* thread_list_;
553
554 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
555};
556
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557} // namespace art
558
559#endif // ART_SRC_THREAD_H_