blob: 4f2d8c843ae51bb0c91f22d189bc049f53542638 [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*);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700211 bool (*pArtUnlockObject)(Thread*, Object*);
212 bool (*pArtCanPutArrayElementNoThrow)(const Class*, const Class*);
213 int (*pArtInstanceofNonTrivialNoThrow) (const Class*, const Class*);
214 int (*pArtInstanceofNonTrivial) (const Class*, const Class*);
215 Method* (*pArtFindInterfaceMethodInCache)(Class*, uint32_t, const Method*, struct DvmDex*);
216 bool (*pArtUnlockObjectNoThrow)(Thread*, Object*);
217 void (*pArtLockObjectNoThrow)(Thread*, Object*);
218 Object* (*pArtAllocObjectNoThrow)(Class*, int);
219 void (*pArtThrowException)(Thread*, Object*);
buzbeee6d61962011-08-27 11:58:19 -0700220 void (*pArtHandleFillArrayDataNoThrow)(Array*, const uint16_t*);
buzbeec143c552011-08-20 17:38:58 -0700221
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700222 class StackVisitor {
223 public:
224 virtual ~StackVisitor() {};
225 virtual bool VisitFrame(const Frame& frame) = 0;
226 };
227
Carl Shapiro61e019d2011-07-14 16:53:09 -0700228 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700229 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700230
231 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700232 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700233
234 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700235 void* thread = pthread_getspecific(Thread::pthread_key_self_);
236 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700237 }
238
Carl Shapirob5573532011-07-12 18:22:59 -0700239 uint32_t GetId() const {
240 return id_;
241 }
242
Elliott Hughese27955c2011-08-26 15:21:24 -0700243 pid_t GetTid() const;
244
245 pthread_t GetImpl() const {
246 return handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700247 }
248
249 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700250 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700251 }
252
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700253 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700254 return exception_;
255 }
256
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700257 Frame GetTopOfStack() const {
258 return top_of_managed_stack_;
259 }
260
261 // TODO: this is here for testing, remove when we have exception unit tests
262 // that use the real stack
263 void SetTopOfStack(void* stack) {
264 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
265 }
266
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700267 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700268 CHECK(new_exception != NULL);
269 // TODO: CHECK(exception_ == NULL);
270 exception_ = new_exception; // TODO
271 }
272
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700273 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700274 __attribute__ ((format(printf, 3, 4)));
275
Elliott Hughes79082e32011-08-25 12:07:32 -0700276 // This exception is special, because we need to pre-allocate an instance.
277 void ThrowOutOfMemoryError();
278
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700279 void ClearException() {
280 exception_ = NULL;
281 }
282
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700283 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
284
285 void* FindExceptionHandlerInMethod(const Method* method,
286 void* throw_pc,
287 const DexFile& dex_file,
288 ClassLinker* class_linker);
289
Ian Rogers45a76cb2011-07-21 22:00:15 -0700290 // Offset of exception within Thread, used by generated code
291 static ThreadOffset ExceptionOffset() {
292 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
293 }
294
buzbeec143c552011-08-20 17:38:58 -0700295 // Offset of id within Thread, used by generated code
296 static ThreadOffset IdOffset() {
297 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
298 }
299
300 // Offset of card_table within Thread, used by generated code
301 static ThreadOffset CardTableOffset() {
302 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
303 }
304
Carl Shapirob5573532011-07-12 18:22:59 -0700305 void SetName(const char* name);
306
307 void Suspend();
308
309 bool IsSuspended();
310
311 void Resume();
312
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700313 static bool Startup();
314 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700315
Elliott Hughes330304d2011-08-12 14:28:05 -0700316 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700317 return state_;
318 }
319
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700320 State SetState(State new_state) {
321 State old_state = state_;
Carl Shapirob5573532011-07-12 18:22:59 -0700322 state_ = new_state;
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700323 return old_state;
Carl Shapirob5573532011-07-12 18:22:59 -0700324 }
325
Ian Rogers45a76cb2011-07-21 22:00:15 -0700326 static ThreadOffset SuspendCountOffset() {
327 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
328 }
329
Ian Rogersb033c752011-07-20 12:22:35 -0700330 // Offset of state within Thread, used by generated code
331 static ThreadOffset StateOffset() {
332 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
333 }
334
Ian Rogersb033c752011-07-20 12:22:35 -0700335 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700336 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700337 return jni_env_;
338 }
339
340 // Offset of JNI environment within Thread, used by generated code
341 static ThreadOffset JniEnvOffset() {
342 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
343 }
344
Ian Rogers45a76cb2011-07-21 22:00:15 -0700345 // Offset of top of managed stack address, used by generated code
346 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700347 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
348 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700349 }
350
Ian Rogers408f79a2011-08-23 18:22:33 -0700351 // Offset of top stack indirect reference table within Thread, used by
352 // generated code
353 static ThreadOffset TopSirtOffset() {
354 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700355 }
356
Ian Rogers408f79a2011-08-23 18:22:33 -0700357 // Number of references allocated in SIRTs on this thread
358 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700359
Ian Rogers408f79a2011-08-23 18:22:33 -0700360 // Is the given obj in this thread's stack indirect reference table?
361 bool SirtContains(jobject obj);
362
363 // Convert a jobject into a Object*
364 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700365
Ian Rogers45a76cb2011-07-21 22:00:15 -0700366 // Offset of exception_entry_point_ within Thread, used by generated code
367 static ThreadOffset ExceptionEntryPointOffset() {
368 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
369 }
370
371 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
372 exception_entry_point_ = handler;
373 }
374
375 // Offset of suspend_count_entry_point_ within Thread, used by generated code
376 static ThreadOffset SuspendCountEntryPointOffset() {
377 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
378 }
379
380 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
381 suspend_count_entry_point_ = handler;
382 }
383
384 // Increasing the suspend count, will cause the thread to run to safepoint
385 void IncrementSuspendCount() { suspend_count_++; }
386 void DecrementSuspendCount() { suspend_count_--; }
387
Ian Rogers6de08602011-08-19 14:52:39 -0700388 // Linked list recording transitions from native to managed code
389 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700390 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700391 record->link = native_to_managed_record_;
392 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700393 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700394 }
395 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
396 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700397 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700398 }
399
Brian Carlstrombffb1552011-08-25 12:23:53 -0700400 const ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700401 return class_loader_override_;
402 }
403
Brian Carlstrombffb1552011-08-25 12:23:53 -0700404 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700405 class_loader_override_ = class_loader_override;
406 }
407
Shih-wei Liao44175362011-08-28 16:59:17 -0700408 // Allocate stack trace
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700409 ObjectArray<StackTraceElement>* AllocStackTrace();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700410
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
514 private:
515 ThreadList();
516
517 std::list<Thread*> list_;
518
519 Mutex* lock_;
520
521 DISALLOW_COPY_AND_ASSIGN(ThreadList);
522};
523
524class ThreadListLock {
525 public:
526 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
527 : thread_list_(thread_list) {
528 if (current_thread == NULL) { // try to get it from TLS
529 current_thread = Thread::Current();
530 }
531 Thread::State old_state;
532 if (current_thread != NULL) {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700533 old_state = current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
Carl Shapirob5573532011-07-12 18:22:59 -0700534 } else {
535 // happens during VM shutdown
536 old_state = Thread::kUnknown; // TODO: something else
537 }
538 thread_list_->Lock();
539 if (current_thread != NULL) {
540 current_thread->SetState(old_state);
541 }
542 }
543
544 ~ThreadListLock() {
545 thread_list_->Unlock();
546 }
547
Carl Shapirob5573532011-07-12 18:22:59 -0700548 private:
549 ThreadList* thread_list_;
550
551 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
552};
553
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700554class ScopedThreadStateChange {
555 public:
556 ScopedThreadStateChange(Thread* thread, Thread::State new_state) : thread_(thread) {
557 old_thread_state_ = thread_->SetState(new_state);
558 }
559
560 ~ScopedThreadStateChange() {
561 thread_->SetState(old_thread_state_);
562 }
563
564 private:
565 Thread* thread_;
566 Thread::State old_thread_state_;
567 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
568};
569
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700570} // namespace art
571
572#endif // ART_SRC_THREAD_H_