blob: 41bb0dac671613dbb39f80536664d7249d74e341 [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);
187 long long (*pArtF2l)(float);
188 long long (*pArtD2l)(double);
189 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*);
buzbeec143c552011-08-20 17:38:58 -0700211 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
212 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
buzbee3ea4ec52011-08-22 17:37:19 -0700213 const struct ClassObject*);
214 int (*pArtInstanceofNonTrivialNoThrow) (const struct ClassObject*,
215 const struct ClassObject*);
216 int (*pArtInstanceofNonTrivial) (const struct ClassObject*, const struct ClassObject*);
buzbeec143c552011-08-20 17:38:58 -0700217 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
buzbee3ea4ec52011-08-22 17:37:19 -0700218 const struct Method*, struct DvmDex*);
buzbeec143c552011-08-20 17:38:58 -0700219 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
220 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
221 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
222 void (*pArtThrowException)(struct Thread*, struct Object*);
buzbeee6d61962011-08-27 11:58:19 -0700223 void (*pArtHandleFillArrayDataNoThrow)(Array*, const uint16_t*);
buzbeec143c552011-08-20 17:38:58 -0700224
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700225 class StackVisitor {
226 public:
227 virtual ~StackVisitor() {};
228 virtual bool VisitFrame(const Frame& frame) = 0;
229 };
230
Carl Shapiro61e019d2011-07-14 16:53:09 -0700231 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700232 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700233
234 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700235 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700236
237 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700238 void* thread = pthread_getspecific(Thread::pthread_key_self_);
239 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700240 }
241
Carl Shapirob5573532011-07-12 18:22:59 -0700242 uint32_t GetId() const {
243 return id_;
244 }
245
Elliott Hughese27955c2011-08-26 15:21:24 -0700246 pid_t GetTid() const;
247
248 pthread_t GetImpl() const {
249 return handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700250 }
251
252 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700253 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700254 }
255
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700256 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700257 return exception_;
258 }
259
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700260 Frame GetTopOfStack() const {
261 return top_of_managed_stack_;
262 }
263
264 // TODO: this is here for testing, remove when we have exception unit tests
265 // that use the real stack
266 void SetTopOfStack(void* stack) {
267 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
268 }
269
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700270 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700271 CHECK(new_exception != NULL);
272 // TODO: CHECK(exception_ == NULL);
273 exception_ = new_exception; // TODO
274 }
275
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700276 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700277 __attribute__ ((format(printf, 3, 4)));
278
Elliott Hughes79082e32011-08-25 12:07:32 -0700279 // This exception is special, because we need to pre-allocate an instance.
280 void ThrowOutOfMemoryError();
281
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700282 void ClearException() {
283 exception_ = NULL;
284 }
285
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700286 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
287
288 void* FindExceptionHandlerInMethod(const Method* method,
289 void* throw_pc,
290 const DexFile& dex_file,
291 ClassLinker* class_linker);
292
Ian Rogers45a76cb2011-07-21 22:00:15 -0700293 // Offset of exception within Thread, used by generated code
294 static ThreadOffset ExceptionOffset() {
295 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
296 }
297
buzbeec143c552011-08-20 17:38:58 -0700298 // Offset of id within Thread, used by generated code
299 static ThreadOffset IdOffset() {
300 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
301 }
302
303 // Offset of card_table within Thread, used by generated code
304 static ThreadOffset CardTableOffset() {
305 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
306 }
307
Carl Shapirob5573532011-07-12 18:22:59 -0700308 void SetName(const char* name);
309
310 void Suspend();
311
312 bool IsSuspended();
313
314 void Resume();
315
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700316 static bool Startup();
317 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700318
Elliott Hughes330304d2011-08-12 14:28:05 -0700319 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700320 return state_;
321 }
322
323 void SetState(State new_state) {
324 state_ = new_state;
325 }
326
Ian Rogers45a76cb2011-07-21 22:00:15 -0700327 static ThreadOffset SuspendCountOffset() {
328 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
329 }
330
Ian Rogersb033c752011-07-20 12:22:35 -0700331 // Offset of state within Thread, used by generated code
332 static ThreadOffset StateOffset() {
333 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
334 }
335
Ian Rogersb033c752011-07-20 12:22:35 -0700336 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700337 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700338 return jni_env_;
339 }
340
341 // Offset of JNI environment within Thread, used by generated code
342 static ThreadOffset JniEnvOffset() {
343 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
344 }
345
Ian Rogers45a76cb2011-07-21 22:00:15 -0700346 // Offset of top of managed stack address, used by generated code
347 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700348 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
349 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700350 }
351
Ian Rogers408f79a2011-08-23 18:22:33 -0700352 // Offset of top stack indirect reference table within Thread, used by
353 // generated code
354 static ThreadOffset TopSirtOffset() {
355 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700356 }
357
Ian Rogers408f79a2011-08-23 18:22:33 -0700358 // Number of references allocated in SIRTs on this thread
359 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700360
Ian Rogers408f79a2011-08-23 18:22:33 -0700361 // Is the given obj in this thread's stack indirect reference table?
362 bool SirtContains(jobject obj);
363
364 // Convert a jobject into a Object*
365 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700366
Ian Rogers45a76cb2011-07-21 22:00:15 -0700367 // Offset of exception_entry_point_ within Thread, used by generated code
368 static ThreadOffset ExceptionEntryPointOffset() {
369 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
370 }
371
372 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
373 exception_entry_point_ = handler;
374 }
375
376 // Offset of suspend_count_entry_point_ within Thread, used by generated code
377 static ThreadOffset SuspendCountEntryPointOffset() {
378 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
379 }
380
381 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
382 suspend_count_entry_point_ = handler;
383 }
384
385 // Increasing the suspend count, will cause the thread to run to safepoint
386 void IncrementSuspendCount() { suspend_count_++; }
387 void DecrementSuspendCount() { suspend_count_--; }
388
Ian Rogers6de08602011-08-19 14:52:39 -0700389 // Linked list recording transitions from native to managed code
390 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700391 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700392 record->link = native_to_managed_record_;
393 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700394 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700395 }
396 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
397 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700398 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700399 }
400
Brian Carlstrombffb1552011-08-25 12:23:53 -0700401 const ClassLoader* GetClassLoaderOverride() {
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
Shih-wei Liao44175362011-08-28 16:59:17 -0700409 // Allocate stack trace
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700410 ObjectArray<StackTraceElement>* AllocStackTrace();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700411
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700412 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700413 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700414 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700415 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700416 native_to_managed_record_(NULL),
Ian Rogers408f79a2011-08-23 18:22:33 -0700417 top_sirt_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700418 jni_env_(NULL),
419 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700420 suspend_count_(0),
421 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700422 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700423 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700424
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700425 ~Thread();
426 friend class Runtime; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700427
Ian Rogersb033c752011-07-20 12:22:35 -0700428 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700429 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700430
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700431 void WalkStack(StackVisitor* visitor);
Shih-wei Liao44175362011-08-28 16:59:17 -0700432
Carl Shapiro69759ea2011-07-21 18:13:35 -0700433 // Managed thread id.
434 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700435
buzbeec143c552011-08-20 17:38:58 -0700436 // FIXME: placeholder for the gc cardTable
437 uint32_t card_table_;
438
Ian Rogers45a76cb2011-07-21 22:00:15 -0700439 // Top of the managed stack, written out prior to the state transition from
440 // kRunnable to kNative. Uses include to give the starting point for scanning
441 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700442 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700443
Ian Rogers6de08602011-08-19 14:52:39 -0700444 // A linked list (of stack allocated records) recording transitions from
445 // native to managed code.
446 NativeToManagedRecord* native_to_managed_record_;
447
Ian Rogers408f79a2011-08-23 18:22:33 -0700448 // Top of linked list of stack indirect reference tables or NULL for none
449 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700450
451 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700452 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700453
Carl Shapirob5573532011-07-12 18:22:59 -0700454 State state_;
455
Carl Shapiro69759ea2011-07-21 18:13:35 -0700456 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700457 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700458
Carl Shapiro69759ea2011-07-21 18:13:35 -0700459 // Initialized to "this". On certain architectures (such as x86) reading
460 // off of Thread::Current is easy but getting the address of Thread::Current
461 // is hard. This field can be read off of Thread::Current to give the address.
462 Thread* self_;
463
464 Runtime* runtime_;
465
466 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700467 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700468
Ian Rogers45a76cb2011-07-21 22:00:15 -0700469 // A non-zero value is used to tell the current thread to enter a safe point
470 // at the next poll.
471 int suspend_count_;
472
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700473 // Needed to get the right ClassLoader in JNI_OnLoad, but also
474 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700475 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700476
Carl Shapiro69759ea2011-07-21 18:13:35 -0700477 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700478 static pthread_key_t pthread_key_self_;
479
Ian Rogers45a76cb2011-07-21 22:00:15 -0700480 // Entry point called when exception_ is set
481 void (*exception_entry_point_)(Method** frame);
482
483 // Entry point called when suspend_count_ is non-zero
484 void (*suspend_count_entry_point_)(Method** frame);
485
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700486 DISALLOW_COPY_AND_ASSIGN(Thread);
487};
Elliott Hughes330304d2011-08-12 14:28:05 -0700488std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700489std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700490
Carl Shapirob5573532011-07-12 18:22:59 -0700491class ThreadList {
492 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700493 static const int kMaxId = 0xFFFF;
494 static const int kInvalidId = 0;
495 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700496
Carl Shapiro61e019d2011-07-14 16:53:09 -0700497 static ThreadList* Create();
498
499 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700500
501 void Register(Thread* thread);
502
503 void Unregister(Thread* thread);
504
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700505 bool Contains(Thread* thread);
506
Carl Shapirob5573532011-07-12 18:22:59 -0700507 void Lock() {
508 lock_->Lock();
509 }
510
511 void Unlock() {
512 lock_->Unlock();
513 };
514
515 private:
516 ThreadList();
517
518 std::list<Thread*> list_;
519
520 Mutex* lock_;
521
522 DISALLOW_COPY_AND_ASSIGN(ThreadList);
523};
524
525class ThreadListLock {
526 public:
527 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
528 : thread_list_(thread_list) {
529 if (current_thread == NULL) { // try to get it from TLS
530 current_thread = Thread::Current();
531 }
532 Thread::State old_state;
533 if (current_thread != NULL) {
534 old_state = current_thread->GetState();
535 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
536 } 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
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700556} // namespace art
557
558#endif // ART_SRC_THREAD_H_