blob: 0a9415106ab7f212f029e2fd329e4ef227187e0c [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>
Elliott Hughesa0957642011-09-02 14:27:33 -07007
8#include <iosfwd>
Ian Rogersb033c752011-07-20 12:22:35 -07009#include <list>
Carl Shapirob5573532011-07-12 18:22:59 -070010
Brian Carlstrom1f870082011-08-23 16:02:11 -070011#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "globals.h"
Elliott Hughes69f5bc62011-08-24 09:26:14 -070013#include "jni_internal.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "logging.h"
15#include "macros.h"
Brian Carlstromb765be02011-08-17 23:54:10 -070016#include "mem_map.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "offsets.h"
Ian Rogersb033c752011-07-20 12:22:35 -070018
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019namespace art {
20
Elliott Hughes69f5bc62011-08-24 09:26:14 -070021class Array;
Elliott Hughes37f7a402011-08-22 18:56:01 -070022class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070023class ClassLinker;
Elliott Hughesedcc09c2011-08-21 18:47:05 -070024class ClassLoader;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070025class Method;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026class Object;
Carl Shapirob5573532011-07-12 18:22:59 -070027class Runtime;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070028class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070029class ThreadList;
Elliott Hughese5b0dc82011-08-23 09:59:02 -070030class Throwable;
Shih-wei Liao55df06b2011-08-26 14:39:27 -070031class StackTraceElement;
32template<class T> class ObjectArray;
Shih-wei Liao44175362011-08-28 16:59:17 -070033template<class T> class PrimitiveArray;
34typedef PrimitiveArray<int32_t> IntArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035
36class Mutex {
37 public:
38 virtual ~Mutex() {}
39
Carl Shapirob5573532011-07-12 18:22:59 -070040 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041
Carl Shapirob5573532011-07-12 18:22:59 -070042 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
Carl Shapirob5573532011-07-12 18:22:59 -070044 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
46 const char* GetName() { return name_; }
47
48 Thread* GetOwner() { return owner_; }
49
Carl Shapirob5573532011-07-12 18:22:59 -070050 static Mutex* Create(const char* name);
51
Elliott Hughes79082e32011-08-25 12:07:32 -070052 // TODO: only needed because we lack a condition variable abstraction.
53 pthread_mutex_t* GetImpl() { return &lock_impl_; }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070054
55 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070056 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
57
Elliott Hughes79082e32011-08-25 12:07:32 -070058 void SetOwner(Thread* thread) { owner_ = thread; }
59
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060 const char* name_;
61
62 Thread* owner_;
63
Carl Shapirob5573532011-07-12 18:22:59 -070064 pthread_mutex_t lock_impl_;
65
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070066 DISALLOW_COPY_AND_ASSIGN(Mutex);
67};
68
69class MutexLock {
70 public:
71 explicit MutexLock(Mutex *mu) : mu_(mu) {
72 mu_->Lock();
73 }
74 ~MutexLock() { mu_->Unlock(); }
75 private:
76 Mutex* const mu_;
77 DISALLOW_COPY_AND_ASSIGN(MutexLock);
78};
79
Ian Rogers408f79a2011-08-23 18:22:33 -070080// Stack allocated indirect reference table, allocated within the bridge frame
81// between managed and native code.
82class StackIndirectReferenceTable {
Ian Rogersb033c752011-07-20 12:22:35 -070083 public:
Ian Rogers408f79a2011-08-23 18:22:33 -070084 // Number of references contained within this SIRT
Ian Rogersb033c752011-07-20 12:22:35 -070085 size_t NumberOfReferences() {
86 return number_of_references_;
87 }
88
Ian Rogers408f79a2011-08-23 18:22:33 -070089 // Link to previous SIRT or NULL
90 StackIndirectReferenceTable* Link() {
Ian Rogersb033c752011-07-20 12:22:35 -070091 return link_;
92 }
93
Ian Rogers408f79a2011-08-23 18:22:33 -070094 Object** References() {
95 return references_;
Ian Rogersa8cd9f42011-08-19 16:43:41 -070096 }
97
Ian Rogers408f79a2011-08-23 18:22:33 -070098 // Offset of length within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -070099 static size_t NumberOfReferencesOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -0700100 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
Ian Rogersb033c752011-07-20 12:22:35 -0700101 }
102
Ian Rogers408f79a2011-08-23 18:22:33 -0700103 // Offset of link within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -0700104 static size_t LinkOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -0700105 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
Ian Rogersb033c752011-07-20 12:22:35 -0700106 }
107
108 private:
Ian Rogers408f79a2011-08-23 18:22:33 -0700109 StackIndirectReferenceTable() {}
Ian Rogersb033c752011-07-20 12:22:35 -0700110
111 size_t number_of_references_;
Ian Rogers408f79a2011-08-23 18:22:33 -0700112 StackIndirectReferenceTable* link_;
Ian Rogersb033c752011-07-20 12:22:35 -0700113
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700114 // Fake array, really allocated and filled in by jni_compiler.
Ian Rogers408f79a2011-08-23 18:22:33 -0700115 Object* references_[0];
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700116
Ian Rogers408f79a2011-08-23 18:22:33 -0700117 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
Ian Rogersb033c752011-07-20 12:22:35 -0700118};
119
Ian Rogers6de08602011-08-19 14:52:39 -0700120struct NativeToManagedRecord {
121 NativeToManagedRecord* link;
122 void* last_top_of_managed_stack;
123};
124
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700125// Iterator over managed frames up to the first native-to-managed transition
126class Frame {
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700127 public:
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700128 Frame() : sp_(NULL) {}
129
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130 Method* GetMethod() const {
Elliott Hughesa0957642011-09-02 14:27:33 -0700131 return (sp_ != NULL) ? *sp_ : NULL;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700132 }
133
134 bool HasNext() const {
135 return NextMethod() != NULL;
136 }
137
138 void Next();
139
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700140 uintptr_t GetPC() const;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700141
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700142 Method** GetSP() const {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700143 return sp_;
144 }
145
146 // TODO: this is here for testing, remove when we have exception unit tests
147 // that use the real stack
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700148 void SetSP(Method** sp) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700149 sp_ = sp;
150 }
151
152 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700153 Method* NextMethod() const;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700154
155 friend class Thread;
156
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700157 Method** sp_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700158};
159
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700160class Thread {
161 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700162 enum State {
163 kUnknown = -1,
164 kNew,
165 kRunnable,
166 kBlocked,
167 kWaiting,
168 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700169 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700170 kTerminated,
171 };
172
buzbeec143c552011-08-20 17:38:58 -0700173
Carl Shapiro61e019d2011-07-14 16:53:09 -0700174 static const size_t kDefaultStackSize = 64 * KB;
175
buzbeec143c552011-08-20 17:38:58 -0700176 // Runtime support function pointers
177 void* (*pMemcpy)(void*, const void*, size_t);
buzbee54330722011-08-23 16:46:55 -0700178 uint64_t (*pShlLong)(uint64_t, uint32_t);
179 uint64_t (*pShrLong)(uint64_t, uint32_t);
180 uint64_t (*pUshrLong)(uint64_t, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700181 float (*pI2f)(int);
182 int (*pF2iz)(float);
183 float (*pD2f)(double);
184 double (*pF2d)(float);
185 double (*pI2d)(int);
186 int (*pD2iz)(double);
187 float (*pL2f)(long);
188 double (*pL2d)(long);
buzbee1b4c8592011-08-31 10:43:51 -0700189 long long (*pF2l)(float);
190 long long (*pD2l)(double);
buzbeec143c552011-08-20 17:38:58 -0700191 float (*pFadd)(float, float);
192 float (*pFsub)(float, float);
193 float (*pFdiv)(float, float);
194 float (*pFmul)(float, float);
195 float (*pFmodf)(float, float);
196 double (*pDadd)(double, double);
197 double (*pDsub)(double, double);
198 double (*pDdiv)(double, double);
199 double (*pDmul)(double, double);
200 double (*pFmod)(double, double);
201 int (*pIdivmod)(int, int);
202 int (*pIdiv)(int, int);
buzbee439c4fa2011-08-27 15:59:07 -0700203 long long (*pLmul)(long long, long long);
buzbeec143c552011-08-20 17:38:58 -0700204 long long (*pLdivmod)(long long, long long);
buzbeedfd3d702011-08-28 12:56:51 -0700205 Array* (*pAllocFromCode)(uint32_t, Method*, int32_t);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700206 Object* (*pAllocObjectFromCode)(uint32_t, Method*);
buzbeee1931742011-08-28 21:15:53 -0700207 uint32_t (*pGet32Static)(uint32_t, const Method*);
208 void (*pSet32Static)(uint32_t, const Method*, uint32_t);
209 uint64_t (*pGet64Static)(uint32_t, const Method*);
210 void (*pSet64Static)(uint32_t, const Method*, uint64_t);
211 Object* (*pGetObjStatic)(uint32_t, const Method*);
212 void (*pSetObjStatic)(uint32_t, const Method*, Object*);
buzbee1b4c8592011-08-31 10:43:51 -0700213 bool (*pCanPutArrayElementFromCode)(const Class*, const Class*);
214 int (*pInstanceofNonTrivialFromCode) (const Class*, const Class*);
215 Method* (*pFindInterfaceMethodInCache)(Class*, uint32_t, const Method*, struct DvmDex*);
216 bool (*pUnlockObjectFromCode)(Thread*, Object*);
217 void (*pLockObjectFromCode)(Thread*, Object*);
218 void (*pThrowException)(Thread*, Throwable*);
219 void (*pHandleFillArrayDataFromCode)(Array*, const uint16_t*);
220 Class* (*pInitializeTypeFromCode)(uint32_t, Method*);
buzbee561227c2011-09-02 15:28:19 -0700221 void (*pResolveMethodFromCode)(Method*, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700222
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700223 class StackVisitor {
224 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 virtual ~StackVisitor() {}
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700226 virtual bool VisitFrame(const Frame& frame) = 0;
227 };
228
Carl Shapiro61e019d2011-07-14 16:53:09 -0700229 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700230 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700231
232 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700233 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700234
235 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700236 void* thread = pthread_getspecific(Thread::pthread_key_self_);
237 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700238 }
239
Elliott Hughesa0957642011-09-02 14:27:33 -0700240 void Dump(std::ostream& os) const;
241
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700242 State GetState() const {
243 return state_;
244 }
245
246 State SetState(State new_state) {
247 State old_state = state_;
248 state_ = new_state;
249 return old_state;
250 }
251
252 bool CanAccessDirectReferences() const {
253 return state_ == kRunnable;
254 }
255
Carl Shapirob5573532011-07-12 18:22:59 -0700256 uint32_t GetId() const {
257 return id_;
258 }
259
Elliott Hughesd92bec42011-09-02 17:04:36 -0700260 pid_t GetTid() const {
261 return tid_;
262 }
Elliott Hughese27955c2011-08-26 15:21:24 -0700263
264 pthread_t GetImpl() const {
265 return handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700266 }
267
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700268 // Returns the Method* for the current method.
269 // This is used by the JNI implementation for logging and diagnostic purposes.
270 const Method* GetCurrentMethod() const {
271 return top_of_managed_stack_.GetMethod();
272 }
273
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700274 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700275 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700276 }
277
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700278 Throwable* GetException() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700279 DCHECK(CanAccessDirectReferences());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700280 return exception_;
281 }
282
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 void SetException(Throwable* new_exception) {
284 DCHECK(CanAccessDirectReferences());
285 CHECK(new_exception != NULL);
286 // TODO: CHECK(exception_ == NULL);
287 exception_ = new_exception; // TODO
288 }
289
290 void ClearException() {
291 exception_ = NULL;
Elliott Hughesa0957642011-09-02 14:27:33 -0700292 }
293
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700294 Frame GetTopOfStack() const {
295 return top_of_managed_stack_;
296 }
297
298 // TODO: this is here for testing, remove when we have exception unit tests
299 // that use the real stack
300 void SetTopOfStack(void* stack) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(stack));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700302 }
303
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700304 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700305 __attribute__ ((format(printf, 3, 4)));
306
Elliott Hughes79082e32011-08-25 12:07:32 -0700307 // This exception is special, because we need to pre-allocate an instance.
308 void ThrowOutOfMemoryError();
309
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700310 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
311
312 void* FindExceptionHandlerInMethod(const Method* method,
313 void* throw_pc,
314 const DexFile& dex_file,
315 ClassLinker* class_linker);
316
Ian Rogers45a76cb2011-07-21 22:00:15 -0700317 // Offset of exception within Thread, used by generated code
318 static ThreadOffset ExceptionOffset() {
319 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
320 }
321
buzbeec143c552011-08-20 17:38:58 -0700322 // Offset of id within Thread, used by generated code
323 static ThreadOffset IdOffset() {
324 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
325 }
326
327 // Offset of card_table within Thread, used by generated code
328 static ThreadOffset CardTableOffset() {
329 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
330 }
331
Carl Shapirob5573532011-07-12 18:22:59 -0700332 void SetName(const char* name);
333
334 void Suspend();
335
336 bool IsSuspended();
337
338 void Resume();
339
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700340 static bool Startup();
341 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700342
Ian Rogers45a76cb2011-07-21 22:00:15 -0700343 static ThreadOffset SuspendCountOffset() {
344 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
345 }
346
Ian Rogersb033c752011-07-20 12:22:35 -0700347 // Offset of state within Thread, used by generated code
348 static ThreadOffset StateOffset() {
349 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
350 }
351
Ian Rogersb033c752011-07-20 12:22:35 -0700352 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700353 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700354 return jni_env_;
355 }
356
357 // Offset of JNI environment within Thread, used by generated code
358 static ThreadOffset JniEnvOffset() {
359 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
360 }
361
Ian Rogers45a76cb2011-07-21 22:00:15 -0700362 // Offset of top of managed stack address, used by generated code
363 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700364 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
365 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700366 }
367
Ian Rogers408f79a2011-08-23 18:22:33 -0700368 // Offset of top stack indirect reference table within Thread, used by
369 // generated code
370 static ThreadOffset TopSirtOffset() {
371 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700372 }
373
Ian Rogers408f79a2011-08-23 18:22:33 -0700374 // Number of references allocated in SIRTs on this thread
375 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700376
Ian Rogers408f79a2011-08-23 18:22:33 -0700377 // Is the given obj in this thread's stack indirect reference table?
378 bool SirtContains(jobject obj);
379
380 // Convert a jobject into a Object*
381 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700382
Ian Rogers45a76cb2011-07-21 22:00:15 -0700383 // Offset of exception_entry_point_ within Thread, used by generated code
384 static ThreadOffset ExceptionEntryPointOffset() {
385 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
386 }
387
388 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
389 exception_entry_point_ = handler;
390 }
391
392 // Offset of suspend_count_entry_point_ within Thread, used by generated code
393 static ThreadOffset SuspendCountEntryPointOffset() {
394 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
395 }
396
397 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
398 suspend_count_entry_point_ = handler;
399 }
400
401 // Increasing the suspend count, will cause the thread to run to safepoint
402 void IncrementSuspendCount() { suspend_count_++; }
403 void DecrementSuspendCount() { suspend_count_--; }
404
Ian Rogers6de08602011-08-19 14:52:39 -0700405 // Linked list recording transitions from native to managed code
406 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700407 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700408 record->link = native_to_managed_record_;
409 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700410 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700411 }
412 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
413 native_to_managed_record_ = record.link;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700414 top_of_managed_stack_.SetSP(reinterpret_cast<Method**>(record.last_top_of_managed_stack));
Ian Rogers6de08602011-08-19 14:52:39 -0700415 }
416
Brian Carlstrombffb1552011-08-25 12:23:53 -0700417 const ClassLoader* GetClassLoaderOverride() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700418 // TODO: need to place the class_loader_override_ in a handle
419 // DCHECK(CanAccessDirectReferences());
buzbeec143c552011-08-20 17:38:58 -0700420 return class_loader_override_;
421 }
422
Brian Carlstrombffb1552011-08-25 12:23:53 -0700423 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700424 class_loader_override_ = class_loader_override;
425 }
426
Shih-wei Liao44175362011-08-28 16:59:17 -0700427 // Allocate stack trace
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700428 ObjectArray<StackTraceElement>* AllocStackTrace();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700429
Elliott Hughes410c0c82011-09-01 17:58:25 -0700430 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
431
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700432 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700433 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700434 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700435 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700436 native_to_managed_record_(NULL),
Ian Rogers408f79a2011-08-23 18:22:33 -0700437 top_sirt_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700438 jni_env_(NULL),
439 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700440 suspend_count_(0),
441 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700442 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700443 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700444
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700445 ~Thread();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700446 friend class Runtime; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700447
Elliott Hughesd92bec42011-09-02 17:04:36 -0700448 void DumpState(std::ostream& os) const;
449 void DumpStack(std::ostream& os) const;
450
Ian Rogersb033c752011-07-20 12:22:35 -0700451 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700452 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700453
Shih-wei Liao9b576b42011-08-29 01:45:07 -0700454 void WalkStack(StackVisitor* visitor);
Shih-wei Liao44175362011-08-28 16:59:17 -0700455
Carl Shapiro69759ea2011-07-21 18:13:35 -0700456 // Managed thread id.
457 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700458
Elliott Hughesd92bec42011-09-02 17:04:36 -0700459 // System thread id.
460 pid_t tid_;
461
462 // Native thread handle.
463 pthread_t handle_;
464
buzbeec143c552011-08-20 17:38:58 -0700465 // FIXME: placeholder for the gc cardTable
466 uint32_t card_table_;
467
Ian Rogers45a76cb2011-07-21 22:00:15 -0700468 // Top of the managed stack, written out prior to the state transition from
469 // kRunnable to kNative. Uses include to give the starting point for scanning
470 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700471 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700472
Ian Rogers6de08602011-08-19 14:52:39 -0700473 // A linked list (of stack allocated records) recording transitions from
474 // native to managed code.
475 NativeToManagedRecord* native_to_managed_record_;
476
Ian Rogers408f79a2011-08-23 18:22:33 -0700477 // Top of linked list of stack indirect reference tables or NULL for none
478 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700479
480 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700481 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700482
Carl Shapirob5573532011-07-12 18:22:59 -0700483 State state_;
484
Carl Shapiro69759ea2011-07-21 18:13:35 -0700485 // Initialized to "this". On certain architectures (such as x86) reading
486 // off of Thread::Current is easy but getting the address of Thread::Current
487 // is hard. This field can be read off of Thread::Current to give the address.
488 Thread* self_;
489
490 Runtime* runtime_;
491
492 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700493 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700494
Ian Rogers45a76cb2011-07-21 22:00:15 -0700495 // A non-zero value is used to tell the current thread to enter a safe point
496 // at the next poll.
497 int suspend_count_;
498
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700499 // Needed to get the right ClassLoader in JNI_OnLoad, but also
500 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700501 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700502
Carl Shapiro69759ea2011-07-21 18:13:35 -0700503 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700504 static pthread_key_t pthread_key_self_;
505
Ian Rogers45a76cb2011-07-21 22:00:15 -0700506 // Entry point called when exception_ is set
507 void (*exception_entry_point_)(Method** frame);
508
509 // Entry point called when suspend_count_ is non-zero
510 void (*suspend_count_entry_point_)(Method** frame);
511
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700512 DISALLOW_COPY_AND_ASSIGN(Thread);
513};
Elliott Hughes330304d2011-08-12 14:28:05 -0700514std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700515std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700516
Carl Shapirob5573532011-07-12 18:22:59 -0700517class ThreadList {
518 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700519 static const int kMaxId = 0xFFFF;
520 static const int kInvalidId = 0;
521 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700522
Carl Shapiro61e019d2011-07-14 16:53:09 -0700523 static ThreadList* Create();
524
525 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700526
Elliott Hughesd92bec42011-09-02 17:04:36 -0700527 void Dump(std::ostream& os);
528
Carl Shapirob5573532011-07-12 18:22:59 -0700529 void Register(Thread* thread);
530
531 void Unregister(Thread* thread);
532
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700533 bool Contains(Thread* thread);
534
Carl Shapirob5573532011-07-12 18:22:59 -0700535 void Lock() {
536 lock_->Lock();
537 }
538
539 void Unlock() {
540 lock_->Unlock();
541 };
542
Elliott Hughes410c0c82011-09-01 17:58:25 -0700543 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
544
Carl Shapirob5573532011-07-12 18:22:59 -0700545 private:
546 ThreadList();
547
548 std::list<Thread*> list_;
549
550 Mutex* lock_;
551
552 DISALLOW_COPY_AND_ASSIGN(ThreadList);
553};
554
555class ThreadListLock {
556 public:
557 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
558 : thread_list_(thread_list) {
559 if (current_thread == NULL) { // try to get it from TLS
560 current_thread = Thread::Current();
561 }
562 Thread::State old_state;
563 if (current_thread != NULL) {
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700564 old_state = current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
Carl Shapirob5573532011-07-12 18:22:59 -0700565 } else {
566 // happens during VM shutdown
567 old_state = Thread::kUnknown; // TODO: something else
568 }
569 thread_list_->Lock();
570 if (current_thread != NULL) {
571 current_thread->SetState(old_state);
572 }
573 }
574
575 ~ThreadListLock() {
576 thread_list_->Unlock();
577 }
578
Carl Shapirob5573532011-07-12 18:22:59 -0700579 private:
580 ThreadList* thread_list_;
581
582 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
583};
584
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700585class ScopedThreadStateChange {
586 public:
587 ScopedThreadStateChange(Thread* thread, Thread::State new_state) : thread_(thread) {
588 old_thread_state_ = thread_->SetState(new_state);
589 }
590
591 ~ScopedThreadStateChange() {
592 thread_->SetState(old_thread_state_);
593 }
594
595 private:
596 Thread* thread_;
597 Thread::State old_thread_state_;
598 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
599};
600
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700601} // namespace art
602
603#endif // ART_SRC_THREAD_H_