blob: 86e78b7f3bf5c982f44513c020f5b422639ee7a1 [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
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700323 State SetState(State new_state) {
324 State old_state = state_;
Carl Shapirob5573532011-07-12 18:22:59 -0700325 state_ = new_state;
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700326 return old_state;
Carl Shapirob5573532011-07-12 18:22:59 -0700327 }
328
Ian Rogers45a76cb2011-07-21 22:00:15 -0700329 static ThreadOffset SuspendCountOffset() {
330 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
331 }
332
Ian Rogersb033c752011-07-20 12:22:35 -0700333 // Offset of state within Thread, used by generated code
334 static ThreadOffset StateOffset() {
335 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
336 }
337
Ian Rogersb033c752011-07-20 12:22:35 -0700338 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700339 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700340 return jni_env_;
341 }
342
343 // Offset of JNI environment within Thread, used by generated code
344 static ThreadOffset JniEnvOffset() {
345 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
346 }
347
Ian Rogers45a76cb2011-07-21 22:00:15 -0700348 // Offset of top of managed stack address, used by generated code
349 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700350 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
351 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700352 }
353
Ian Rogers408f79a2011-08-23 18:22:33 -0700354 // Offset of top stack indirect reference table within Thread, used by
355 // generated code
356 static ThreadOffset TopSirtOffset() {
357 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700358 }
359
Ian Rogers408f79a2011-08-23 18:22:33 -0700360 // Number of references allocated in SIRTs on this thread
361 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700362
Ian Rogers408f79a2011-08-23 18:22:33 -0700363 // Is the given obj in this thread's stack indirect reference table?
364 bool SirtContains(jobject obj);
365
366 // Convert a jobject into a Object*
367 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700368
Ian Rogers45a76cb2011-07-21 22:00:15 -0700369 // Offset of exception_entry_point_ within Thread, used by generated code
370 static ThreadOffset ExceptionEntryPointOffset() {
371 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
372 }
373
374 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
375 exception_entry_point_ = handler;
376 }
377
378 // Offset of suspend_count_entry_point_ within Thread, used by generated code
379 static ThreadOffset SuspendCountEntryPointOffset() {
380 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
381 }
382
383 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
384 suspend_count_entry_point_ = handler;
385 }
386
387 // Increasing the suspend count, will cause the thread to run to safepoint
388 void IncrementSuspendCount() { suspend_count_++; }
389 void DecrementSuspendCount() { suspend_count_--; }
390
Ian Rogers6de08602011-08-19 14:52:39 -0700391 // Linked list recording transitions from native to managed code
392 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700393 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700394 record->link = native_to_managed_record_;
395 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700396 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700397 }
398 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
399 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700400 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700401 }
402
Brian Carlstrombffb1552011-08-25 12:23:53 -0700403 const ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700404 return class_loader_override_;
405 }
406
Brian Carlstrombffb1552011-08-25 12:23:53 -0700407 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700408 class_loader_override_ = class_loader_override;
409 }
410
Shih-wei Liao44175362011-08-28 16:59:17 -0700411 // Allocate stack trace
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700412 ObjectArray<StackTraceElement>* AllocStackTrace();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700413
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700414 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700415 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700416 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700417 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700418 native_to_managed_record_(NULL),
Ian Rogers408f79a2011-08-23 18:22:33 -0700419 top_sirt_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700420 jni_env_(NULL),
421 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700422 suspend_count_(0),
423 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700424 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700425 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700426
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700427 ~Thread();
428 friend class Runtime; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700429
Ian Rogersb033c752011-07-20 12:22:35 -0700430 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700431 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700432
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700433 void WalkStack(StackVisitor* visitor);
Shih-wei Liao44175362011-08-28 16:59:17 -0700434
Carl Shapiro69759ea2011-07-21 18:13:35 -0700435 // Managed thread id.
436 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700437
buzbeec143c552011-08-20 17:38:58 -0700438 // FIXME: placeholder for the gc cardTable
439 uint32_t card_table_;
440
Ian Rogers45a76cb2011-07-21 22:00:15 -0700441 // Top of the managed stack, written out prior to the state transition from
442 // kRunnable to kNative. Uses include to give the starting point for scanning
443 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700444 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700445
Ian Rogers6de08602011-08-19 14:52:39 -0700446 // A linked list (of stack allocated records) recording transitions from
447 // native to managed code.
448 NativeToManagedRecord* native_to_managed_record_;
449
Ian Rogers408f79a2011-08-23 18:22:33 -0700450 // Top of linked list of stack indirect reference tables or NULL for none
451 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700452
453 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700454 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700455
Carl Shapirob5573532011-07-12 18:22:59 -0700456 State state_;
457
Carl Shapiro69759ea2011-07-21 18:13:35 -0700458 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700459 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700460
Carl Shapiro69759ea2011-07-21 18:13:35 -0700461 // Initialized to "this". On certain architectures (such as x86) reading
462 // off of Thread::Current is easy but getting the address of Thread::Current
463 // is hard. This field can be read off of Thread::Current to give the address.
464 Thread* self_;
465
466 Runtime* runtime_;
467
468 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700469 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700470
Ian Rogers45a76cb2011-07-21 22:00:15 -0700471 // A non-zero value is used to tell the current thread to enter a safe point
472 // at the next poll.
473 int suspend_count_;
474
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700475 // Needed to get the right ClassLoader in JNI_OnLoad, but also
476 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700477 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700478
Carl Shapiro69759ea2011-07-21 18:13:35 -0700479 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700480 static pthread_key_t pthread_key_self_;
481
Ian Rogers45a76cb2011-07-21 22:00:15 -0700482 // Entry point called when exception_ is set
483 void (*exception_entry_point_)(Method** frame);
484
485 // Entry point called when suspend_count_ is non-zero
486 void (*suspend_count_entry_point_)(Method** frame);
487
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700488 DISALLOW_COPY_AND_ASSIGN(Thread);
489};
Elliott Hughes330304d2011-08-12 14:28:05 -0700490std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700491std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700492
Carl Shapirob5573532011-07-12 18:22:59 -0700493class ThreadList {
494 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700495 static const int kMaxId = 0xFFFF;
496 static const int kInvalidId = 0;
497 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700498
Carl Shapiro61e019d2011-07-14 16:53:09 -0700499 static ThreadList* Create();
500
501 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700502
503 void Register(Thread* thread);
504
505 void Unregister(Thread* thread);
506
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700507 bool Contains(Thread* thread);
508
Carl Shapirob5573532011-07-12 18:22:59 -0700509 void Lock() {
510 lock_->Lock();
511 }
512
513 void Unlock() {
514 lock_->Unlock();
515 };
516
517 private:
518 ThreadList();
519
520 std::list<Thread*> list_;
521
522 Mutex* lock_;
523
524 DISALLOW_COPY_AND_ASSIGN(ThreadList);
525};
526
527class ThreadListLock {
528 public:
529 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
530 : thread_list_(thread_list) {
531 if (current_thread == NULL) { // try to get it from TLS
532 current_thread = Thread::Current();
533 }
534 Thread::State old_state;
535 if (current_thread != NULL) {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700536 old_state = current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
Carl Shapirob5573532011-07-12 18:22:59 -0700537 } 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
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700557class ScopedThreadStateChange {
558 public:
559 ScopedThreadStateChange(Thread* thread, Thread::State new_state) : thread_(thread) {
560 old_thread_state_ = thread_->SetState(new_state);
561 }
562
563 ~ScopedThreadStateChange() {
564 thread_->SetState(old_thread_state_);
565 }
566
567 private:
568 Thread* thread_;
569 Thread::State old_thread_state_;
570 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
571};
572
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700573} // namespace art
574
575#endif // ART_SRC_THREAD_H_