blob: 215da717c1c4b4399506f5a7e00ae9d67ff1ebbe [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;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070032
33class Mutex {
34 public:
35 virtual ~Mutex() {}
36
Carl Shapirob5573532011-07-12 18:22:59 -070037 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070038
Carl Shapirob5573532011-07-12 18:22:59 -070039 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070040
Carl Shapirob5573532011-07-12 18:22:59 -070041 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042
43 const char* GetName() { return name_; }
44
45 Thread* GetOwner() { return owner_; }
46
Carl Shapirob5573532011-07-12 18:22:59 -070047 static Mutex* Create(const char* name);
48
Elliott Hughes79082e32011-08-25 12:07:32 -070049 // TODO: only needed because we lack a condition variable abstraction.
50 pthread_mutex_t* GetImpl() { return &lock_impl_; }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
52 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070053 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
54
Elliott Hughes79082e32011-08-25 12:07:32 -070055 void SetOwner(Thread* thread) { owner_ = thread; }
56
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070057 const char* name_;
58
59 Thread* owner_;
60
Carl Shapirob5573532011-07-12 18:22:59 -070061 pthread_mutex_t lock_impl_;
62
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070063 DISALLOW_COPY_AND_ASSIGN(Mutex);
64};
65
66class MutexLock {
67 public:
68 explicit MutexLock(Mutex *mu) : mu_(mu) {
69 mu_->Lock();
70 }
71 ~MutexLock() { mu_->Unlock(); }
72 private:
73 Mutex* const mu_;
74 DISALLOW_COPY_AND_ASSIGN(MutexLock);
75};
76
Ian Rogers408f79a2011-08-23 18:22:33 -070077// Stack allocated indirect reference table, allocated within the bridge frame
78// between managed and native code.
79class StackIndirectReferenceTable {
Ian Rogersb033c752011-07-20 12:22:35 -070080 public:
Ian Rogers408f79a2011-08-23 18:22:33 -070081 // Number of references contained within this SIRT
Ian Rogersb033c752011-07-20 12:22:35 -070082 size_t NumberOfReferences() {
83 return number_of_references_;
84 }
85
Ian Rogers408f79a2011-08-23 18:22:33 -070086 // Link to previous SIRT or NULL
87 StackIndirectReferenceTable* Link() {
Ian Rogersb033c752011-07-20 12:22:35 -070088 return link_;
89 }
90
Ian Rogers408f79a2011-08-23 18:22:33 -070091 Object** References() {
92 return references_;
Ian Rogersa8cd9f42011-08-19 16:43:41 -070093 }
94
Ian Rogers408f79a2011-08-23 18:22:33 -070095 // Offset of length within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -070096 static size_t NumberOfReferencesOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -070097 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
Ian Rogersb033c752011-07-20 12:22:35 -070098 }
99
Ian Rogers408f79a2011-08-23 18:22:33 -0700100 // Offset of link within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -0700101 static size_t LinkOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -0700102 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
Ian Rogersb033c752011-07-20 12:22:35 -0700103 }
104
105 private:
Ian Rogers408f79a2011-08-23 18:22:33 -0700106 StackIndirectReferenceTable() {}
Ian Rogersb033c752011-07-20 12:22:35 -0700107
108 size_t number_of_references_;
Ian Rogers408f79a2011-08-23 18:22:33 -0700109 StackIndirectReferenceTable* link_;
Ian Rogersb033c752011-07-20 12:22:35 -0700110
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700111 // Fake array, really allocated and filled in by jni_compiler.
Ian Rogers408f79a2011-08-23 18:22:33 -0700112 Object* references_[0];
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700113
Ian Rogers408f79a2011-08-23 18:22:33 -0700114 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
Ian Rogersb033c752011-07-20 12:22:35 -0700115};
116
Ian Rogers6de08602011-08-19 14:52:39 -0700117struct NativeToManagedRecord {
118 NativeToManagedRecord* link;
119 void* last_top_of_managed_stack;
120};
121
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700122// Iterator over managed frames up to the first native-to-managed transition
123class Frame {
124 Frame() : sp_(NULL) {}
125
126 const Method* GetMethod() const {
127 return *sp_;
128 }
129
130 bool HasNext() const {
131 return NextMethod() != NULL;
132 }
133
134 void Next();
135
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700136 uintptr_t GetPC() const;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700137
138 const Method** GetSP() const {
139 return sp_;
140 }
141
142 // TODO: this is here for testing, remove when we have exception unit tests
143 // that use the real stack
144 void SetSP(const Method** sp) {
145 sp_ = sp;
146 }
147
148 private:
149 const Method* NextMethod() const;
150
151 friend class Thread;
152
153 const Method** sp_;
154};
155
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700156class Thread {
157 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700158 enum State {
159 kUnknown = -1,
160 kNew,
161 kRunnable,
162 kBlocked,
163 kWaiting,
164 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700165 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700166 kTerminated,
167 };
168
buzbeec143c552011-08-20 17:38:58 -0700169
Carl Shapiro61e019d2011-07-14 16:53:09 -0700170 static const size_t kDefaultStackSize = 64 * KB;
171
buzbeec143c552011-08-20 17:38:58 -0700172 // Runtime support function pointers
173 void* (*pMemcpy)(void*, const void*, size_t);
buzbee54330722011-08-23 16:46:55 -0700174 uint64_t (*pShlLong)(uint64_t, uint32_t);
175 uint64_t (*pShrLong)(uint64_t, uint32_t);
176 uint64_t (*pUshrLong)(uint64_t, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700177 float (*pI2f)(int);
178 int (*pF2iz)(float);
179 float (*pD2f)(double);
180 double (*pF2d)(float);
181 double (*pI2d)(int);
182 int (*pD2iz)(double);
183 float (*pL2f)(long);
184 double (*pL2d)(long);
185 long long (*pArtF2l)(float);
186 long long (*pArtD2l)(double);
187 float (*pFadd)(float, float);
188 float (*pFsub)(float, float);
189 float (*pFdiv)(float, float);
190 float (*pFmul)(float, float);
191 float (*pFmodf)(float, float);
192 double (*pDadd)(double, double);
193 double (*pDsub)(double, double);
194 double (*pDdiv)(double, double);
195 double (*pDmul)(double, double);
196 double (*pFmod)(double, double);
197 int (*pIdivmod)(int, int);
198 int (*pIdiv)(int, int);
buzbee439c4fa2011-08-27 15:59:07 -0700199 long long (*pLmul)(long long, long long);
buzbeec143c552011-08-20 17:38:58 -0700200 long long (*pLdivmod)(long long, long long);
201 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
202 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
buzbee3ea4ec52011-08-22 17:37:19 -0700203 const struct ClassObject*);
204 int (*pArtInstanceofNonTrivialNoThrow) (const struct ClassObject*,
205 const struct ClassObject*);
206 int (*pArtInstanceofNonTrivial) (const struct ClassObject*, const struct ClassObject*);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700207 Array* (*pArtAllocArrayByClass)(Class*, int32_t);
buzbeec143c552011-08-20 17:38:58 -0700208 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
buzbee3ea4ec52011-08-22 17:37:19 -0700209 const struct Method*, struct DvmDex*);
buzbeec143c552011-08-20 17:38:58 -0700210 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
211 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
212 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
213 void (*pArtThrowException)(struct Thread*, struct Object*);
buzbeee6d61962011-08-27 11:58:19 -0700214 void (*pArtHandleFillArrayDataNoThrow)(Array*, const uint16_t*);
buzbeec143c552011-08-20 17:38:58 -0700215
Carl Shapiro61e019d2011-07-14 16:53:09 -0700216 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700217 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700218
219 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700220 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700221
222 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700223 void* thread = pthread_getspecific(Thread::pthread_key_self_);
224 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700225 }
226
Carl Shapirob5573532011-07-12 18:22:59 -0700227 uint32_t GetId() const {
228 return id_;
229 }
230
Elliott Hughese27955c2011-08-26 15:21:24 -0700231 pid_t GetTid() const;
232
233 pthread_t GetImpl() const {
234 return handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700235 }
236
237 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700238 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700239 }
240
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700241 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700242 return exception_;
243 }
244
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700245 Frame GetTopOfStack() const {
246 return top_of_managed_stack_;
247 }
248
249 // TODO: this is here for testing, remove when we have exception unit tests
250 // that use the real stack
251 void SetTopOfStack(void* stack) {
252 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
253 }
254
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700255 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700256 CHECK(new_exception != NULL);
257 // TODO: CHECK(exception_ == NULL);
258 exception_ = new_exception; // TODO
259 }
260
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700261 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700262 __attribute__ ((format(printf, 3, 4)));
263
Elliott Hughes79082e32011-08-25 12:07:32 -0700264 // This exception is special, because we need to pre-allocate an instance.
265 void ThrowOutOfMemoryError();
266
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700267 void ClearException() {
268 exception_ = NULL;
269 }
270
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700271 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
272
273 void* FindExceptionHandlerInMethod(const Method* method,
274 void* throw_pc,
275 const DexFile& dex_file,
276 ClassLinker* class_linker);
277
Ian Rogers45a76cb2011-07-21 22:00:15 -0700278 // Offset of exception within Thread, used by generated code
279 static ThreadOffset ExceptionOffset() {
280 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
281 }
282
buzbeec143c552011-08-20 17:38:58 -0700283 // Offset of id within Thread, used by generated code
284 static ThreadOffset IdOffset() {
285 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
286 }
287
288 // Offset of card_table within Thread, used by generated code
289 static ThreadOffset CardTableOffset() {
290 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
291 }
292
Carl Shapirob5573532011-07-12 18:22:59 -0700293 void SetName(const char* name);
294
295 void Suspend();
296
297 bool IsSuspended();
298
299 void Resume();
300
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700301 static bool Startup();
302 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700303
Elliott Hughes330304d2011-08-12 14:28:05 -0700304 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700305 return state_;
306 }
307
308 void SetState(State new_state) {
309 state_ = new_state;
310 }
311
Ian Rogers45a76cb2011-07-21 22:00:15 -0700312 static ThreadOffset SuspendCountOffset() {
313 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
314 }
315
Ian Rogersb033c752011-07-20 12:22:35 -0700316 // Offset of state within Thread, used by generated code
317 static ThreadOffset StateOffset() {
318 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
319 }
320
Ian Rogersb033c752011-07-20 12:22:35 -0700321 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700322 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700323 return jni_env_;
324 }
325
326 // Offset of JNI environment within Thread, used by generated code
327 static ThreadOffset JniEnvOffset() {
328 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
329 }
330
Ian Rogers45a76cb2011-07-21 22:00:15 -0700331 // Offset of top of managed stack address, used by generated code
332 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700333 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
334 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700335 }
336
Ian Rogers408f79a2011-08-23 18:22:33 -0700337 // Offset of top stack indirect reference table within Thread, used by
338 // generated code
339 static ThreadOffset TopSirtOffset() {
340 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700341 }
342
Ian Rogers408f79a2011-08-23 18:22:33 -0700343 // Number of references allocated in SIRTs on this thread
344 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700345
Ian Rogers408f79a2011-08-23 18:22:33 -0700346 // Is the given obj in this thread's stack indirect reference table?
347 bool SirtContains(jobject obj);
348
349 // Convert a jobject into a Object*
350 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700351
Ian Rogers45a76cb2011-07-21 22:00:15 -0700352 // Offset of exception_entry_point_ within Thread, used by generated code
353 static ThreadOffset ExceptionEntryPointOffset() {
354 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
355 }
356
357 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
358 exception_entry_point_ = handler;
359 }
360
361 // Offset of suspend_count_entry_point_ within Thread, used by generated code
362 static ThreadOffset SuspendCountEntryPointOffset() {
363 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
364 }
365
366 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
367 suspend_count_entry_point_ = handler;
368 }
369
370 // Increasing the suspend count, will cause the thread to run to safepoint
371 void IncrementSuspendCount() { suspend_count_++; }
372 void DecrementSuspendCount() { suspend_count_--; }
373
Ian Rogers6de08602011-08-19 14:52:39 -0700374 // Linked list recording transitions from native to managed code
375 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700376 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700377 record->link = native_to_managed_record_;
378 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700379 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700380 }
381 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
382 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700383 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700384 }
385
Brian Carlstrombffb1552011-08-25 12:23:53 -0700386 const ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700387 return class_loader_override_;
388 }
389
Brian Carlstrombffb1552011-08-25 12:23:53 -0700390 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700391 class_loader_override_ = class_loader_override;
392 }
393
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700394 struct InternalStackTrace {
395 const Method* method;
396 uintptr_t pc;
397 };
398
399 // Get the top length frames information
400 InternalStackTrace* GetStackTrace(uint16_t length);
401
402 ObjectArray<StackTraceElement>* GetStackTraceElement(uint16_t length, InternalStackTrace *raw_trace);
403
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700404 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700405 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700406 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700407 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700408 native_to_managed_record_(NULL),
Ian Rogers408f79a2011-08-23 18:22:33 -0700409 top_sirt_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700410 jni_env_(NULL),
411 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700412 suspend_count_(0),
413 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700414 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700415 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700416
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700417 ~Thread();
418 friend class Runtime; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700419
Ian Rogersb033c752011-07-20 12:22:35 -0700420 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700421 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700422
Carl Shapiro69759ea2011-07-21 18:13:35 -0700423 // Managed thread id.
424 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700425
buzbeec143c552011-08-20 17:38:58 -0700426 // FIXME: placeholder for the gc cardTable
427 uint32_t card_table_;
428
Ian Rogers45a76cb2011-07-21 22:00:15 -0700429 // Top of the managed stack, written out prior to the state transition from
430 // kRunnable to kNative. Uses include to give the starting point for scanning
431 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700432 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700433
Ian Rogers6de08602011-08-19 14:52:39 -0700434 // A linked list (of stack allocated records) recording transitions from
435 // native to managed code.
436 NativeToManagedRecord* native_to_managed_record_;
437
Ian Rogers408f79a2011-08-23 18:22:33 -0700438 // Top of linked list of stack indirect reference tables or NULL for none
439 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700440
441 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700442 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700443
Carl Shapirob5573532011-07-12 18:22:59 -0700444 State state_;
445
Carl Shapiro69759ea2011-07-21 18:13:35 -0700446 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700447 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700448
Carl Shapiro69759ea2011-07-21 18:13:35 -0700449 // Initialized to "this". On certain architectures (such as x86) reading
450 // off of Thread::Current is easy but getting the address of Thread::Current
451 // is hard. This field can be read off of Thread::Current to give the address.
452 Thread* self_;
453
454 Runtime* runtime_;
455
456 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700457 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700458
Ian Rogers45a76cb2011-07-21 22:00:15 -0700459 // A non-zero value is used to tell the current thread to enter a safe point
460 // at the next poll.
461 int suspend_count_;
462
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700463 // Needed to get the right ClassLoader in JNI_OnLoad, but also
464 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700465 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700466
Carl Shapiro69759ea2011-07-21 18:13:35 -0700467 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700468 static pthread_key_t pthread_key_self_;
469
Ian Rogers45a76cb2011-07-21 22:00:15 -0700470 // Entry point called when exception_ is set
471 void (*exception_entry_point_)(Method** frame);
472
473 // Entry point called when suspend_count_ is non-zero
474 void (*suspend_count_entry_point_)(Method** frame);
475
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700476 DISALLOW_COPY_AND_ASSIGN(Thread);
477};
Elliott Hughes330304d2011-08-12 14:28:05 -0700478std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700479std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700480
Carl Shapirob5573532011-07-12 18:22:59 -0700481class ThreadList {
482 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700483 static const int kMaxId = 0xFFFF;
484 static const int kInvalidId = 0;
485 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700486
Carl Shapiro61e019d2011-07-14 16:53:09 -0700487 static ThreadList* Create();
488
489 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700490
491 void Register(Thread* thread);
492
493 void Unregister(Thread* thread);
494
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700495 bool Contains(Thread* thread);
496
Carl Shapirob5573532011-07-12 18:22:59 -0700497 void Lock() {
498 lock_->Lock();
499 }
500
501 void Unlock() {
502 lock_->Unlock();
503 };
504
505 private:
506 ThreadList();
507
508 std::list<Thread*> list_;
509
510 Mutex* lock_;
511
512 DISALLOW_COPY_AND_ASSIGN(ThreadList);
513};
514
515class ThreadListLock {
516 public:
517 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
518 : thread_list_(thread_list) {
519 if (current_thread == NULL) { // try to get it from TLS
520 current_thread = Thread::Current();
521 }
522 Thread::State old_state;
523 if (current_thread != NULL) {
524 old_state = current_thread->GetState();
525 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
526 } else {
527 // happens during VM shutdown
528 old_state = Thread::kUnknown; // TODO: something else
529 }
530 thread_list_->Lock();
531 if (current_thread != NULL) {
532 current_thread->SetState(old_state);
533 }
534 }
535
536 ~ThreadListLock() {
537 thread_list_->Unlock();
538 }
539
Carl Shapirob5573532011-07-12 18:22:59 -0700540 private:
541 ThreadList* thread_list_;
542
543 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
544};
545
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700546} // namespace art
547
548#endif // ART_SRC_THREAD_H_