blob: 15781ff3c595f58a1fbe5d78e08ecfd70a91feeb [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"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "logging.h"
11#include "macros.h"
Brian Carlstromb765be02011-08-17 23:54:10 -070012#include "mem_map.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "offsets.h"
14#include "runtime.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070015
Ian Rogersb033c752011-07-20 12:22:35 -070016#include "jni.h"
17
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018namespace art {
19
Elliott Hughes37f7a402011-08-22 18:56:01 -070020class Class;
Elliott Hughesedcc09c2011-08-21 18:47:05 -070021class ClassLoader;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070022class Method;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070023class Object;
Carl Shapirob5573532011-07-12 18:22:59 -070024class Runtime;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070025class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070026class ThreadList;
Elliott Hughese5b0dc82011-08-23 09:59:02 -070027class Throwable;
buzbee3ea4ec52011-08-22 17:37:19 -070028class Array;
29class Class;
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
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047 public: // TODO: protected
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048 void SetOwner(Thread* thread) { owner_ = thread; }
49
50 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070051 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
52
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053 const char* name_;
54
55 Thread* owner_;
56
Carl Shapirob5573532011-07-12 18:22:59 -070057 pthread_mutex_t lock_impl_;
58
Elliott Hughes5174fe62011-08-23 15:12:35 -070059 friend class SharedLibrary; // For 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
228 pid_t GetNativeId() const {
229 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700230 }
231
232 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700233 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234 }
235
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700236 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700237 return exception_;
238 }
239
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700240 Frame GetTopOfStack() const {
241 return top_of_managed_stack_;
242 }
243
244 // TODO: this is here for testing, remove when we have exception unit tests
245 // that use the real stack
246 void SetTopOfStack(void* stack) {
247 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
248 }
249
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700250 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700251 CHECK(new_exception != NULL);
252 // TODO: CHECK(exception_ == NULL);
253 exception_ = new_exception; // TODO
254 }
255
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700256 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700257 __attribute__ ((format(printf, 3, 4)));
258
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700259 void ClearException() {
260 exception_ = NULL;
261 }
262
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700263 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
264
265 void* FindExceptionHandlerInMethod(const Method* method,
266 void* throw_pc,
267 const DexFile& dex_file,
268 ClassLinker* class_linker);
269
Ian Rogers45a76cb2011-07-21 22:00:15 -0700270 // Offset of exception within Thread, used by generated code
271 static ThreadOffset ExceptionOffset() {
272 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
273 }
274
buzbeec143c552011-08-20 17:38:58 -0700275 // Offset of id within Thread, used by generated code
276 static ThreadOffset IdOffset() {
277 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
278 }
279
280 // Offset of card_table within Thread, used by generated code
281 static ThreadOffset CardTableOffset() {
282 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
283 }
284
Carl Shapirob5573532011-07-12 18:22:59 -0700285 void SetName(const char* name);
286
287 void Suspend();
288
289 bool IsSuspended();
290
291 void Resume();
292
293 static bool Init();
294
Elliott Hughes330304d2011-08-12 14:28:05 -0700295 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700296 return state_;
297 }
298
299 void SetState(State new_state) {
300 state_ = new_state;
301 }
302
Ian Rogers45a76cb2011-07-21 22:00:15 -0700303 static ThreadOffset SuspendCountOffset() {
304 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
305 }
306
Ian Rogersb033c752011-07-20 12:22:35 -0700307 // Offset of state within Thread, used by generated code
308 static ThreadOffset StateOffset() {
309 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
310 }
311
Ian Rogersb033c752011-07-20 12:22:35 -0700312 // JNI methods
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700313 JNIEnv* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700314 return jni_env_;
315 }
316
317 // Offset of JNI environment within Thread, used by generated code
318 static ThreadOffset JniEnvOffset() {
319 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
320 }
321
Ian Rogers45a76cb2011-07-21 22:00:15 -0700322 // Offset of top of managed stack address, used by generated code
323 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700324 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
325 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700326 }
327
Ian Rogers408f79a2011-08-23 18:22:33 -0700328 // Offset of top stack indirect reference table within Thread, used by
329 // generated code
330 static ThreadOffset TopSirtOffset() {
331 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_sirt_));
Ian Rogersb033c752011-07-20 12:22:35 -0700332 }
333
Ian Rogers408f79a2011-08-23 18:22:33 -0700334 // Number of references allocated in SIRTs on this thread
335 size_t NumSirtReferences();
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700336
Ian Rogers408f79a2011-08-23 18:22:33 -0700337 // Is the given obj in this thread's stack indirect reference table?
338 bool SirtContains(jobject obj);
339
340 // Convert a jobject into a Object*
341 Object* DecodeJObject(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700342
Ian Rogers45a76cb2011-07-21 22:00:15 -0700343 // Offset of exception_entry_point_ within Thread, used by generated code
344 static ThreadOffset ExceptionEntryPointOffset() {
345 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
346 }
347
348 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
349 exception_entry_point_ = handler;
350 }
351
352 // Offset of suspend_count_entry_point_ within Thread, used by generated code
353 static ThreadOffset SuspendCountEntryPointOffset() {
354 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
355 }
356
357 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
358 suspend_count_entry_point_ = handler;
359 }
360
361 // Increasing the suspend count, will cause the thread to run to safepoint
362 void IncrementSuspendCount() { suspend_count_++; }
363 void DecrementSuspendCount() { suspend_count_--; }
364
Ian Rogers6de08602011-08-19 14:52:39 -0700365 // Linked list recording transitions from native to managed code
366 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700367 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700368 record->link = native_to_managed_record_;
369 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700370 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700371 }
372 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
373 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700374 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700375 }
376
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700377 ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700378 return class_loader_override_;
379 }
380
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700381 void SetClassLoaderOverride(ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700382 class_loader_override_ = class_loader_override;
383 }
384
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700385 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700386 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700387 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700388 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700389 native_to_managed_record_(NULL),
Ian Rogers408f79a2011-08-23 18:22:33 -0700390 top_sirt_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700391 jni_env_(NULL),
392 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700393 suspend_count_(0),
394 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700395 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700396 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700397
Ian Rogersdf20fe02011-07-20 20:34:16 -0700398 ~Thread() {
399 delete jni_env_;
400 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700401
Ian Rogersb033c752011-07-20 12:22:35 -0700402 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700403 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700404
Carl Shapiro69759ea2011-07-21 18:13:35 -0700405 // Managed thread id.
406 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700407
buzbeec143c552011-08-20 17:38:58 -0700408 // FIXME: placeholder for the gc cardTable
409 uint32_t card_table_;
410
Ian Rogers45a76cb2011-07-21 22:00:15 -0700411 // Top of the managed stack, written out prior to the state transition from
412 // kRunnable to kNative. Uses include to give the starting point for scanning
413 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700414 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700415
Ian Rogers6de08602011-08-19 14:52:39 -0700416 // A linked list (of stack allocated records) recording transitions from
417 // native to managed code.
418 NativeToManagedRecord* native_to_managed_record_;
419
Ian Rogers408f79a2011-08-23 18:22:33 -0700420 // Top of linked list of stack indirect reference tables or NULL for none
421 StackIndirectReferenceTable* top_sirt_;
Ian Rogersb033c752011-07-20 12:22:35 -0700422
423 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700424 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700425
Carl Shapirob5573532011-07-12 18:22:59 -0700426 State state_;
427
Carl Shapiro69759ea2011-07-21 18:13:35 -0700428 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700429 pid_t native_id_;
430
Carl Shapiro69759ea2011-07-21 18:13:35 -0700431 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700432 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700433
Carl Shapiro69759ea2011-07-21 18:13:35 -0700434 // Initialized to "this". On certain architectures (such as x86) reading
435 // off of Thread::Current is easy but getting the address of Thread::Current
436 // is hard. This field can be read off of Thread::Current to give the address.
437 Thread* self_;
438
439 Runtime* runtime_;
440
441 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700442 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700443
Ian Rogers45a76cb2011-07-21 22:00:15 -0700444 // A non-zero value is used to tell the current thread to enter a safe point
445 // at the next poll.
446 int suspend_count_;
447
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700448 // Needed to get the right ClassLoader in JNI_OnLoad, but also
449 // useful for testing.
450 ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700451
Brian Carlstromb765be02011-08-17 23:54:10 -0700452 // The memory mapping of the stack for non-attached threads.
453 scoped_ptr<MemMap> stack_;
454
Carl Shapiro69759ea2011-07-21 18:13:35 -0700455 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700456 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700457
458 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700459 byte* stack_limit_;
460
Carl Shapiro69759ea2011-07-21 18:13:35 -0700461 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700462 static pthread_key_t pthread_key_self_;
463
Ian Rogers45a76cb2011-07-21 22:00:15 -0700464 // Entry point called when exception_ is set
465 void (*exception_entry_point_)(Method** frame);
466
467 // Entry point called when suspend_count_ is non-zero
468 void (*suspend_count_entry_point_)(Method** frame);
469
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700470 DISALLOW_COPY_AND_ASSIGN(Thread);
471};
Elliott Hughes330304d2011-08-12 14:28:05 -0700472std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700473std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700474
Carl Shapirob5573532011-07-12 18:22:59 -0700475class ThreadList {
476 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700477 static const int kMaxId = 0xFFFF;
478 static const int kInvalidId = 0;
479 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700480
Carl Shapiro61e019d2011-07-14 16:53:09 -0700481 static ThreadList* Create();
482
483 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700484
485 void Register(Thread* thread);
486
487 void Unregister(Thread* thread);
488
Carl Shapirob5573532011-07-12 18:22:59 -0700489 void Lock() {
490 lock_->Lock();
491 }
492
493 void Unlock() {
494 lock_->Unlock();
495 };
496
497 private:
498 ThreadList();
499
500 std::list<Thread*> list_;
501
502 Mutex* lock_;
503
504 DISALLOW_COPY_AND_ASSIGN(ThreadList);
505};
506
507class ThreadListLock {
508 public:
509 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
510 : thread_list_(thread_list) {
511 if (current_thread == NULL) { // try to get it from TLS
512 current_thread = Thread::Current();
513 }
514 Thread::State old_state;
515 if (current_thread != NULL) {
516 old_state = current_thread->GetState();
517 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
518 } else {
519 // happens during VM shutdown
520 old_state = Thread::kUnknown; // TODO: something else
521 }
522 thread_list_->Lock();
523 if (current_thread != NULL) {
524 current_thread->SetState(old_state);
525 }
526 }
527
528 ~ThreadListLock() {
529 thread_list_->Unlock();
530 }
531
Carl Shapirob5573532011-07-12 18:22:59 -0700532 private:
533 ThreadList* thread_list_;
534
535 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
536};
537
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700538} // namespace art
539
540#endif // ART_SRC_THREAD_H_