blob: e8ca1566d533ef59cacd84df7425687300020141 [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;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070030
31class Mutex {
32 public:
33 virtual ~Mutex() {}
34
Carl Shapirob5573532011-07-12 18:22:59 -070035 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070036
Carl Shapirob5573532011-07-12 18:22:59 -070037 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070038
Carl Shapirob5573532011-07-12 18:22:59 -070039 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070040
41 const char* GetName() { return name_; }
42
43 Thread* GetOwner() { return owner_; }
44
Carl Shapirob5573532011-07-12 18:22:59 -070045 static Mutex* Create(const char* name);
46
Elliott Hughes79082e32011-08-25 12:07:32 -070047 // TODO: only needed because we lack a condition variable abstraction.
48 pthread_mutex_t* GetImpl() { return &lock_impl_; }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
50 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070051 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
52
Elliott Hughes79082e32011-08-25 12:07:32 -070053 void SetOwner(Thread* thread) { owner_ = thread; }
54
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070055 const char* name_;
56
57 Thread* owner_;
58
Carl Shapirob5573532011-07-12 18:22:59 -070059 pthread_mutex_t lock_impl_;
60
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070061 DISALLOW_COPY_AND_ASSIGN(Mutex);
62};
63
64class MutexLock {
65 public:
66 explicit MutexLock(Mutex *mu) : mu_(mu) {
67 mu_->Lock();
68 }
69 ~MutexLock() { mu_->Unlock(); }
70 private:
71 Mutex* const mu_;
72 DISALLOW_COPY_AND_ASSIGN(MutexLock);
73};
74
Ian Rogers408f79a2011-08-23 18:22:33 -070075// Stack allocated indirect reference table, allocated within the bridge frame
76// between managed and native code.
77class StackIndirectReferenceTable {
Ian Rogersb033c752011-07-20 12:22:35 -070078 public:
Ian Rogers408f79a2011-08-23 18:22:33 -070079 // Number of references contained within this SIRT
Ian Rogersb033c752011-07-20 12:22:35 -070080 size_t NumberOfReferences() {
81 return number_of_references_;
82 }
83
Ian Rogers408f79a2011-08-23 18:22:33 -070084 // Link to previous SIRT or NULL
85 StackIndirectReferenceTable* Link() {
Ian Rogersb033c752011-07-20 12:22:35 -070086 return link_;
87 }
88
Ian Rogers408f79a2011-08-23 18:22:33 -070089 Object** References() {
90 return references_;
Ian Rogersa8cd9f42011-08-19 16:43:41 -070091 }
92
Ian Rogers408f79a2011-08-23 18:22:33 -070093 // Offset of length within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -070094 static size_t NumberOfReferencesOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -070095 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
Ian Rogersb033c752011-07-20 12:22:35 -070096 }
97
Ian Rogers408f79a2011-08-23 18:22:33 -070098 // Offset of link within SIRT, used by generated code
Ian Rogersb033c752011-07-20 12:22:35 -070099 static size_t LinkOffset() {
Ian Rogers408f79a2011-08-23 18:22:33 -0700100 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
Ian Rogersb033c752011-07-20 12:22:35 -0700101 }
102
103 private:
Ian Rogers408f79a2011-08-23 18:22:33 -0700104 StackIndirectReferenceTable() {}
Ian Rogersb033c752011-07-20 12:22:35 -0700105
106 size_t number_of_references_;
Ian Rogers408f79a2011-08-23 18:22:33 -0700107 StackIndirectReferenceTable* link_;
Ian Rogersb033c752011-07-20 12:22:35 -0700108
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700109 // Fake array, really allocated and filled in by jni_compiler.
Ian Rogers408f79a2011-08-23 18:22:33 -0700110 Object* references_[0];
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700111
Ian Rogers408f79a2011-08-23 18:22:33 -0700112 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
Ian Rogersb033c752011-07-20 12:22:35 -0700113};
114
Ian Rogers6de08602011-08-19 14:52:39 -0700115struct NativeToManagedRecord {
116 NativeToManagedRecord* link;
117 void* last_top_of_managed_stack;
118};
119
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700120// Iterator over managed frames up to the first native-to-managed transition
121class Frame {
122 Frame() : sp_(NULL) {}
123
124 const Method* GetMethod() const {
125 return *sp_;
126 }
127
128 bool HasNext() const {
129 return NextMethod() != NULL;
130 }
131
132 void Next();
133
134 void* GetPC() const;
135
136 const Method** GetSP() const {
137 return sp_;
138 }
139
140 // TODO: this is here for testing, remove when we have exception unit tests
141 // that use the real stack
142 void SetSP(const Method** sp) {
143 sp_ = sp;
144 }
145
146 private:
147 const Method* NextMethod() const;
148
149 friend class Thread;
150
151 const Method** sp_;
152};
153
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700154class Thread {
155 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700156 enum State {
157 kUnknown = -1,
158 kNew,
159 kRunnable,
160 kBlocked,
161 kWaiting,
162 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700163 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700164 kTerminated,
165 };
166
buzbeec143c552011-08-20 17:38:58 -0700167
Carl Shapiro61e019d2011-07-14 16:53:09 -0700168 static const size_t kDefaultStackSize = 64 * KB;
169
buzbeec143c552011-08-20 17:38:58 -0700170 // Runtime support function pointers
171 void* (*pMemcpy)(void*, const void*, size_t);
buzbee54330722011-08-23 16:46:55 -0700172 uint64_t (*pShlLong)(uint64_t, uint32_t);
173 uint64_t (*pShrLong)(uint64_t, uint32_t);
174 uint64_t (*pUshrLong)(uint64_t, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700175 float (*pI2f)(int);
176 int (*pF2iz)(float);
177 float (*pD2f)(double);
178 double (*pF2d)(float);
179 double (*pI2d)(int);
180 int (*pD2iz)(double);
181 float (*pL2f)(long);
182 double (*pL2d)(long);
183 long long (*pArtF2l)(float);
184 long long (*pArtD2l)(double);
185 float (*pFadd)(float, float);
186 float (*pFsub)(float, float);
187 float (*pFdiv)(float, float);
188 float (*pFmul)(float, float);
189 float (*pFmodf)(float, float);
190 double (*pDadd)(double, double);
191 double (*pDsub)(double, double);
192 double (*pDdiv)(double, double);
193 double (*pDmul)(double, double);
194 double (*pFmod)(double, double);
195 int (*pIdivmod)(int, int);
196 int (*pIdiv)(int, int);
197 long long (*pLdivmod)(long long, long long);
198 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
199 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
buzbee3ea4ec52011-08-22 17:37:19 -0700200 const struct ClassObject*);
201 int (*pArtInstanceofNonTrivialNoThrow) (const struct ClassObject*,
202 const struct ClassObject*);
203 int (*pArtInstanceofNonTrivial) (const struct ClassObject*, const struct ClassObject*);
204 Array* (*pArtAllocArrayByClass)(Class*, size_t);
buzbeec143c552011-08-20 17:38:58 -0700205 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
buzbee3ea4ec52011-08-22 17:37:19 -0700206 const struct Method*, struct DvmDex*);
buzbeec143c552011-08-20 17:38:58 -0700207 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
208 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
209 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
210 void (*pArtThrowException)(struct Thread*, struct Object*);
211 bool (*pArtHandleFillArrayDataNoThrow)(struct ArrayObject*, const uint16_t*);
212
Carl Shapiro61e019d2011-07-14 16:53:09 -0700213 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700214 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700215
216 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700217 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700218
219 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700220 void* thread = pthread_getspecific(Thread::pthread_key_self_);
221 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700222 }
223
Carl Shapirob5573532011-07-12 18:22:59 -0700224 uint32_t GetId() const {
225 return id_;
226 }
227
Elliott Hughese27955c2011-08-26 15:21:24 -0700228 pid_t GetTid() const;
229
230 pthread_t GetImpl() const {
231 return handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232 }
233
234 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700235 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236 }
237
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700238 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700239 return exception_;
240 }
241
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700242 Frame GetTopOfStack() const {
243 return top_of_managed_stack_;
244 }
245
246 // TODO: this is here for testing, remove when we have exception unit tests
247 // that use the real stack
248 void SetTopOfStack(void* stack) {
249 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
250 }
251
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700252 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700253 CHECK(new_exception != NULL);
254 // TODO: CHECK(exception_ == NULL);
255 exception_ = new_exception; // TODO
256 }
257
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700258 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700259 __attribute__ ((format(printf, 3, 4)));
260
Elliott Hughes79082e32011-08-25 12:07:32 -0700261 // This exception is special, because we need to pre-allocate an instance.
262 void ThrowOutOfMemoryError();
263
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700264 void ClearException() {
265 exception_ = NULL;
266 }
267
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700268 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
269
270 void* FindExceptionHandlerInMethod(const Method* method,
271 void* throw_pc,
272 const DexFile& dex_file,
273 ClassLinker* class_linker);
274
Ian Rogers45a76cb2011-07-21 22:00:15 -0700275 // Offset of exception within Thread, used by generated code
276 static ThreadOffset ExceptionOffset() {
277 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
278 }
279
buzbeec143c552011-08-20 17:38:58 -0700280 // Offset of id within Thread, used by generated code
281 static ThreadOffset IdOffset() {
282 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
283 }
284
285 // Offset of card_table within Thread, used by generated code
286 static ThreadOffset CardTableOffset() {
287 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
288 }
289
Carl Shapirob5573532011-07-12 18:22:59 -0700290 void SetName(const char* name);
291
292 void Suspend();
293
294 bool IsSuspended();
295
296 void Resume();
297
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700298 static bool Startup();
299 static void Shutdown();
Carl Shapirob5573532011-07-12 18:22:59 -0700300
Elliott Hughes330304d2011-08-12 14:28:05 -0700301 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700302 return state_;
303 }
304
305 void SetState(State new_state) {
306 state_ = new_state;
307 }
308
Ian Rogers45a76cb2011-07-21 22:00:15 -0700309 static ThreadOffset SuspendCountOffset() {
310 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
311 }
312
Ian Rogersb033c752011-07-20 12:22:35 -0700313 // Offset of state within Thread, used by generated code
314 static ThreadOffset StateOffset() {
315 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
316 }
317
Ian Rogersb033c752011-07-20 12:22:35 -0700318 // JNI methods
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700319 JNIEnvExt* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700320 return jni_env_;
321 }
322
323 // Offset of JNI environment within Thread, used by generated code
324 static ThreadOffset JniEnvOffset() {
325 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
326 }
327
Ian Rogers45a76cb2011-07-21 22:00:15 -0700328 // Offset of top of managed stack address, used by generated code
329 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700330 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
331 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700332 }
333
Ian Rogers408f79a2011-08-23 18:22:33 -0700334 // Offset of top stack indirect reference table within Thread, used by
335 // generated code
336 static ThreadOffset TopSirtOffset() {
337 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700338 }
339
Ian Rogers408f79a2011-08-23 18:22:33 -0700340 // Number of references allocated in SIRTs on this thread
341 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700342
Ian Rogers408f79a2011-08-23 18:22:33 -0700343 // Is the given obj in this thread's stack indirect reference table?
344 bool SirtContains(jobject obj);
345
346 // Convert a jobject into a Object*
347 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700348
Ian Rogers45a76cb2011-07-21 22:00:15 -0700349 // Offset of exception_entry_point_ within Thread, used by generated code
350 static ThreadOffset ExceptionEntryPointOffset() {
351 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
352 }
353
354 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
355 exception_entry_point_ = handler;
356 }
357
358 // Offset of suspend_count_entry_point_ within Thread, used by generated code
359 static ThreadOffset SuspendCountEntryPointOffset() {
360 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
361 }
362
363 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
364 suspend_count_entry_point_ = handler;
365 }
366
367 // Increasing the suspend count, will cause the thread to run to safepoint
368 void IncrementSuspendCount() { suspend_count_++; }
369 void DecrementSuspendCount() { suspend_count_--; }
370
Ian Rogers6de08602011-08-19 14:52:39 -0700371 // Linked list recording transitions from native to managed code
372 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700373 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700374 record->link = native_to_managed_record_;
375 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700376 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700377 }
378 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
379 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700380 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700381 }
382
Brian Carlstrombffb1552011-08-25 12:23:53 -0700383 const ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700384 return class_loader_override_;
385 }
386
Brian Carlstrombffb1552011-08-25 12:23:53 -0700387 void SetClassLoaderOverride(const ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700388 class_loader_override_ = class_loader_override;
389 }
390
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700392 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700393 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700394 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700395 native_to_managed_record_(NULL),
Ian Rogers408f79a2011-08-23 18:22:33 -0700396 top_sirt_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700397 jni_env_(NULL),
398 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700399 suspend_count_(0),
400 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700401 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700402 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700403
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700404 ~Thread();
405 friend class Runtime; // For ~Thread.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700406
Ian Rogersb033c752011-07-20 12:22:35 -0700407 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700408 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700409
Carl Shapiro69759ea2011-07-21 18:13:35 -0700410 // Managed thread id.
411 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700412
buzbeec143c552011-08-20 17:38:58 -0700413 // FIXME: placeholder for the gc cardTable
414 uint32_t card_table_;
415
Ian Rogers45a76cb2011-07-21 22:00:15 -0700416 // Top of the managed stack, written out prior to the state transition from
417 // kRunnable to kNative. Uses include to give the starting point for scanning
418 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700419 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700420
Ian Rogers6de08602011-08-19 14:52:39 -0700421 // A linked list (of stack allocated records) recording transitions from
422 // native to managed code.
423 NativeToManagedRecord* native_to_managed_record_;
424
Ian Rogers408f79a2011-08-23 18:22:33 -0700425 // Top of linked list of stack indirect reference tables or NULL for none
426 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700427
428 // Every thread may have an associated JNI environment
Elliott Hughes69f5bc62011-08-24 09:26:14 -0700429 JNIEnvExt* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700430
Carl Shapirob5573532011-07-12 18:22:59 -0700431 State state_;
432
Carl Shapiro69759ea2011-07-21 18:13:35 -0700433 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700434 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700435
Carl Shapiro69759ea2011-07-21 18:13:35 -0700436 // Initialized to "this". On certain architectures (such as x86) reading
437 // off of Thread::Current is easy but getting the address of Thread::Current
438 // is hard. This field can be read off of Thread::Current to give the address.
439 Thread* self_;
440
441 Runtime* runtime_;
442
443 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700444 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700445
Ian Rogers45a76cb2011-07-21 22:00:15 -0700446 // A non-zero value is used to tell the current thread to enter a safe point
447 // at the next poll.
448 int suspend_count_;
449
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700450 // Needed to get the right ClassLoader in JNI_OnLoad, but also
451 // useful for testing.
Brian Carlstrombffb1552011-08-25 12:23:53 -0700452 const ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700453
Carl Shapiro69759ea2011-07-21 18:13:35 -0700454 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700455 static pthread_key_t pthread_key_self_;
456
Ian Rogers45a76cb2011-07-21 22:00:15 -0700457 // Entry point called when exception_ is set
458 void (*exception_entry_point_)(Method** frame);
459
460 // Entry point called when suspend_count_ is non-zero
461 void (*suspend_count_entry_point_)(Method** frame);
462
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700463 DISALLOW_COPY_AND_ASSIGN(Thread);
464};
Elliott Hughes330304d2011-08-12 14:28:05 -0700465std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700466std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700467
Carl Shapirob5573532011-07-12 18:22:59 -0700468class ThreadList {
469 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700470 static const int kMaxId = 0xFFFF;
471 static const int kInvalidId = 0;
472 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700473
Carl Shapiro61e019d2011-07-14 16:53:09 -0700474 static ThreadList* Create();
475
476 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700477
478 void Register(Thread* thread);
479
480 void Unregister(Thread* thread);
481
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700482 bool Contains(Thread* thread);
483
Carl Shapirob5573532011-07-12 18:22:59 -0700484 void Lock() {
485 lock_->Lock();
486 }
487
488 void Unlock() {
489 lock_->Unlock();
490 };
491
492 private:
493 ThreadList();
494
495 std::list<Thread*> list_;
496
497 Mutex* lock_;
498
499 DISALLOW_COPY_AND_ASSIGN(ThreadList);
500};
501
502class ThreadListLock {
503 public:
504 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
505 : thread_list_(thread_list) {
506 if (current_thread == NULL) { // try to get it from TLS
507 current_thread = Thread::Current();
508 }
509 Thread::State old_state;
510 if (current_thread != NULL) {
511 old_state = current_thread->GetState();
512 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
513 } else {
514 // happens during VM shutdown
515 old_state = Thread::kUnknown; // TODO: something else
516 }
517 thread_list_->Lock();
518 if (current_thread != NULL) {
519 current_thread->SetState(old_state);
520 }
521 }
522
523 ~ThreadListLock() {
524 thread_list_->Unlock();
525 }
526
Carl Shapirob5573532011-07-12 18:22:59 -0700527 private:
528 ThreadList* thread_list_;
529
530 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
531};
532
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700533} // namespace art
534
535#endif // ART_SRC_THREAD_H_