blob: 85abf2ff444309e6fd0112880699581125ac8146 [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 {
126 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);
204 Object* (*pNewInstanceFromCode)(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
Carl Shapiro61e019d2011-07-14 16:53:09 -0700225 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700226 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700227
228 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700229 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700230
231 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700232 void* thread = pthread_getspecific(Thread::pthread_key_self_);
233 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234 }
235
Carl Shapirob5573532011-07-12 18:22:59 -0700236 uint32_t GetId() const {
237 return id_;
238 }
239
Elliott Hughese27955c2011-08-26 15:21:24 -0700240 pid_t GetTid() const;
241
242 pthread_t GetImpl() const {
243 return handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700244 }
245
246 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700247 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700248 }
249
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700250 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700251 return exception_;
252 }
253
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700254 Frame GetTopOfStack() const {
255 return top_of_managed_stack_;
256 }
257
258 // TODO: this is here for testing, remove when we have exception unit tests
259 // that use the real stack
260 void SetTopOfStack(void* stack) {
261 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
262 }
263
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700264 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700265 CHECK(new_exception != NULL);
266 // TODO: CHECK(exception_ == NULL);
267 exception_ = new_exception; // TODO
268 }
269
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700270 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700271 __attribute__ ((format(printf, 3, 4)));
272
Elliott Hughes79082e32011-08-25 12:07:32 -0700273 // This exception is special, because we need to pre-allocate an instance.
274 void ThrowOutOfMemoryError();
275
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700276 void ClearException() {
277 exception_ = NULL;
278 }
279
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700280 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
281
282 void* FindExceptionHandlerInMethod(const Method* method,
283 void* throw_pc,
284 const DexFile& dex_file,
285 ClassLinker* class_linker);
286
Ian Rogers45a76cb2011-07-21 22:00:15 -0700287 // Offset of exception within Thread, used by generated code
288 static ThreadOffset ExceptionOffset() {
289 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
290 }
291
buzbeec143c552011-08-20 17:38:58 -0700292 // Offset of id within Thread, used by generated code
293 static ThreadOffset IdOffset() {
294 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
295 }
296
297 // Offset of card_table within Thread, used by generated code
298 static ThreadOffset CardTableOffset() {
299 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
300 }
301
Carl Shapirob5573532011-07-12 18:22:59 -0700302 void SetName(const char* name);
303
304 void Suspend();
305
306 bool IsSuspended();
307
308 void Resume();
309
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700310 static bool Startup();
311 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700312
Elliott Hughes330304d2011-08-12 14:28:05 -0700313 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700314 return state_;
315 }
316
317 void SetState(State new_state) {
318 state_ = new_state;
319 }
320
Ian Rogers45a76cb2011-07-21 22:00:15 -0700321 static ThreadOffset SuspendCountOffset() {
322 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
323 }
324
Ian Rogersb033c752011-07-20 12:22:35 -0700325 // Offset of state within Thread, used by generated code
326 static ThreadOffset StateOffset() {
327 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
328 }
329
Ian Rogersb033c752011-07-20 12:22:35 -0700330 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700331 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700332 return jni_env_;
333 }
334
335 // Offset of JNI environment within Thread, used by generated code
336 static ThreadOffset JniEnvOffset() {
337 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
338 }
339
Ian Rogers45a76cb2011-07-21 22:00:15 -0700340 // Offset of top of managed stack address, used by generated code
341 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700342 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
343 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700344 }
345
Ian Rogers408f79a2011-08-23 18:22:33 -0700346 // Offset of top stack indirect reference table within Thread, used by
347 // generated code
348 static ThreadOffset TopSirtOffset() {
349 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700350 }
351
Ian Rogers408f79a2011-08-23 18:22:33 -0700352 // Number of references allocated in SIRTs on this thread
353 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700354
Ian Rogers408f79a2011-08-23 18:22:33 -0700355 // Is the given obj in this thread's stack indirect reference table?
356 bool SirtContains(jobject obj);
357
358 // Convert a jobject into a Object*
359 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700360
Ian Rogers45a76cb2011-07-21 22:00:15 -0700361 // Offset of exception_entry_point_ within Thread, used by generated code
362 static ThreadOffset ExceptionEntryPointOffset() {
363 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
364 }
365
366 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
367 exception_entry_point_ = handler;
368 }
369
370 // Offset of suspend_count_entry_point_ within Thread, used by generated code
371 static ThreadOffset SuspendCountEntryPointOffset() {
372 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
373 }
374
375 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
376 suspend_count_entry_point_ = handler;
377 }
378
379 // Increasing the suspend count, will cause the thread to run to safepoint
380 void IncrementSuspendCount() { suspend_count_++; }
381 void DecrementSuspendCount() { suspend_count_--; }
382
Ian Rogers6de08602011-08-19 14:52:39 -0700383 // Linked list recording transitions from native to managed code
384 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700385 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700386 record->link = native_to_managed_record_;
387 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700388 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700389 }
390 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
391 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700392 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700393 }
394
Brian Carlstrombffb1552011-08-25 12:23:53 -0700395 const ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700396 return class_loader_override_;
397 }
398
Brian Carlstrombffb1552011-08-25 12:23:53 -0700399 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700400 class_loader_override_ = class_loader_override;
401 }
402
Shih-wei Liao44175362011-08-28 16:59:17 -0700403 // Allocate stack trace
404 ObjectArray<StackTraceElement>* AllocStackTrace(uint16_t length);
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700405
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700406 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700407 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700408 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700409 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700410 native_to_managed_record_(NULL),
Ian Rogers408f79a2011-08-23 18:22:33 -0700411 top_sirt_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700412 jni_env_(NULL),
413 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700414 suspend_count_(0),
415 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700416 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700417 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700418
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700419 ~Thread();
420 friend class Runtime; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700421
Ian Rogersb033c752011-07-20 12:22:35 -0700422 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700423 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700424
Shih-wei Liao44175362011-08-28 16:59:17 -0700425 bool WalkStack(uint16_t length, ObjectArray<Method>* method_trace, IntArray* pc_trace);
426
Carl Shapiro69759ea2011-07-21 18:13:35 -0700427 // Managed thread id.
428 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700429
buzbeec143c552011-08-20 17:38:58 -0700430 // FIXME: placeholder for the gc cardTable
431 uint32_t card_table_;
432
Ian Rogers45a76cb2011-07-21 22:00:15 -0700433 // Top of the managed stack, written out prior to the state transition from
434 // kRunnable to kNative. Uses include to give the starting point for scanning
435 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700436 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700437
Ian Rogers6de08602011-08-19 14:52:39 -0700438 // A linked list (of stack allocated records) recording transitions from
439 // native to managed code.
440 NativeToManagedRecord* native_to_managed_record_;
441
Ian Rogers408f79a2011-08-23 18:22:33 -0700442 // Top of linked list of stack indirect reference tables or NULL for none
443 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700444
445 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700446 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700447
Carl Shapirob5573532011-07-12 18:22:59 -0700448 State state_;
449
Carl Shapiro69759ea2011-07-21 18:13:35 -0700450 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700451 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700452
Carl Shapiro69759ea2011-07-21 18:13:35 -0700453 // Initialized to "this". On certain architectures (such as x86) reading
454 // off of Thread::Current is easy but getting the address of Thread::Current
455 // is hard. This field can be read off of Thread::Current to give the address.
456 Thread* self_;
457
458 Runtime* runtime_;
459
460 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700461 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700462
Ian Rogers45a76cb2011-07-21 22:00:15 -0700463 // A non-zero value is used to tell the current thread to enter a safe point
464 // at the next poll.
465 int suspend_count_;
466
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700467 // Needed to get the right ClassLoader in JNI_OnLoad, but also
468 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700469 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700470
Carl Shapiro69759ea2011-07-21 18:13:35 -0700471 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700472 static pthread_key_t pthread_key_self_;
473
Ian Rogers45a76cb2011-07-21 22:00:15 -0700474 // Entry point called when exception_ is set
475 void (*exception_entry_point_)(Method** frame);
476
477 // Entry point called when suspend_count_ is non-zero
478 void (*suspend_count_entry_point_)(Method** frame);
479
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700480 DISALLOW_COPY_AND_ASSIGN(Thread);
481};
Elliott Hughes330304d2011-08-12 14:28:05 -0700482std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700483std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700484
Carl Shapirob5573532011-07-12 18:22:59 -0700485class ThreadList {
486 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700487 static const int kMaxId = 0xFFFF;
488 static const int kInvalidId = 0;
489 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700490
Carl Shapiro61e019d2011-07-14 16:53:09 -0700491 static ThreadList* Create();
492
493 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700494
495 void Register(Thread* thread);
496
497 void Unregister(Thread* thread);
498
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700499 bool Contains(Thread* thread);
500
Carl Shapirob5573532011-07-12 18:22:59 -0700501 void Lock() {
502 lock_->Lock();
503 }
504
505 void Unlock() {
506 lock_->Unlock();
507 };
508
509 private:
510 ThreadList();
511
512 std::list<Thread*> list_;
513
514 Mutex* lock_;
515
516 DISALLOW_COPY_AND_ASSIGN(ThreadList);
517};
518
519class ThreadListLock {
520 public:
521 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
522 : thread_list_(thread_list) {
523 if (current_thread == NULL) { // try to get it from TLS
524 current_thread = Thread::Current();
525 }
526 Thread::State old_state;
527 if (current_thread != NULL) {
528 old_state = current_thread->GetState();
529 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
530 } else {
531 // happens during VM shutdown
532 old_state = Thread::kUnknown; // TODO: something else
533 }
534 thread_list_->Lock();
535 if (current_thread != NULL) {
536 current_thread->SetState(old_state);
537 }
538 }
539
540 ~ThreadListLock() {
541 thread_list_->Unlock();
542 }
543
Carl Shapirob5573532011-07-12 18:22:59 -0700544 private:
545 ThreadList* thread_list_;
546
547 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
548};
549
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700550} // namespace art
551
552#endif // ART_SRC_THREAD_H_