blob: 68b6cbf118db2245bc01b150a283c2d185b20d61 [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;
Ian Rogersb033c752011-07-20 12:22:35 -070025class StackHandleBlock;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070027class ThreadList;
Elliott Hughese5b0dc82011-08-23 09:59:02 -070028class Throwable;
buzbee3ea4ec52011-08-22 17:37:19 -070029class Array;
30class Class;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070031
32class Mutex {
33 public:
34 virtual ~Mutex() {}
35
Carl Shapirob5573532011-07-12 18:22:59 -070036 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037
Carl Shapirob5573532011-07-12 18:22:59 -070038 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070039
Carl Shapirob5573532011-07-12 18:22:59 -070040 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041
42 const char* GetName() { return name_; }
43
44 Thread* GetOwner() { return owner_; }
45
Carl Shapirob5573532011-07-12 18:22:59 -070046 static Mutex* Create(const char* name);
47
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048 public: // TODO: protected
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049 void SetOwner(Thread* thread) { owner_ = thread; }
50
51 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070052 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
53
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070054 const char* name_;
55
56 Thread* owner_;
57
Carl Shapirob5573532011-07-12 18:22:59 -070058 pthread_mutex_t lock_impl_;
59
Elliott Hughes5174fe62011-08-23 15:12:35 -070060 friend class SharedLibrary; // For lock_impl_.
61
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070062 DISALLOW_COPY_AND_ASSIGN(Mutex);
63};
64
65class MutexLock {
66 public:
67 explicit MutexLock(Mutex *mu) : mu_(mu) {
68 mu_->Lock();
69 }
70 ~MutexLock() { mu_->Unlock(); }
71 private:
72 Mutex* const mu_;
73 DISALLOW_COPY_AND_ASSIGN(MutexLock);
74};
75
Ian Rogersb033c752011-07-20 12:22:35 -070076// Stack handle blocks are allocated within the bridge frame between managed
77// and native code.
78class StackHandleBlock {
79 public:
80 // Number of references contained within this SHB
81 size_t NumberOfReferences() {
82 return number_of_references_;
83 }
84
85 // Link to previous SHB or NULL
86 StackHandleBlock* Link() {
87 return link_;
88 }
89
Ian Rogersa8cd9f42011-08-19 16:43:41 -070090 Object** Handles() {
91 return handles_;
92 }
93
Ian Rogersb033c752011-07-20 12:22:35 -070094 // Offset of length within SHB, used by generated code
95 static size_t NumberOfReferencesOffset() {
96 return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_);
97 }
98
99 // Offset of link within SHB, used by generated code
100 static size_t LinkOffset() {
101 return OFFSETOF_MEMBER(StackHandleBlock, link_);
102 }
103
104 private:
105 StackHandleBlock() {}
106
107 size_t number_of_references_;
108 StackHandleBlock* link_;
109
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700110 // Fake array, really allocated and filled in by jni_compiler.
111 Object* handles_[0];
112
Ian Rogersb033c752011-07-20 12:22:35 -0700113 DISALLOW_COPY_AND_ASSIGN(StackHandleBlock);
114};
115
Ian Rogers6de08602011-08-19 14:52:39 -0700116struct NativeToManagedRecord {
117 NativeToManagedRecord* link;
118 void* last_top_of_managed_stack;
119};
120
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700121// Iterator over managed frames up to the first native-to-managed transition
122class Frame {
123 Frame() : sp_(NULL) {}
124
125 const Method* GetMethod() const {
126 return *sp_;
127 }
128
129 bool HasNext() const {
130 return NextMethod() != NULL;
131 }
132
133 void Next();
134
135 void* GetPC() const;
136
137 const Method** GetSP() const {
138 return sp_;
139 }
140
141 // TODO: this is here for testing, remove when we have exception unit tests
142 // that use the real stack
143 void SetSP(const Method** sp) {
144 sp_ = sp;
145 }
146
147 private:
148 const Method* NextMethod() const;
149
150 friend class Thread;
151
152 const Method** sp_;
153};
154
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700155class Thread {
156 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700157 enum State {
158 kUnknown = -1,
159 kNew,
160 kRunnable,
161 kBlocked,
162 kWaiting,
163 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700164 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700165 kTerminated,
166 };
167
buzbeec143c552011-08-20 17:38:58 -0700168
Carl Shapiro61e019d2011-07-14 16:53:09 -0700169 static const size_t kDefaultStackSize = 64 * KB;
170
buzbeec143c552011-08-20 17:38:58 -0700171 // Runtime support function pointers
172 void* (*pMemcpy)(void*, const void*, size_t);
buzbee54330722011-08-23 16:46:55 -0700173 uint64_t (*pShlLong)(uint64_t, uint32_t);
174 uint64_t (*pShrLong)(uint64_t, uint32_t);
175 uint64_t (*pUshrLong)(uint64_t, uint32_t);
buzbeec143c552011-08-20 17:38:58 -0700176 float (*pI2f)(int);
177 int (*pF2iz)(float);
178 float (*pD2f)(double);
179 double (*pF2d)(float);
180 double (*pI2d)(int);
181 int (*pD2iz)(double);
182 float (*pL2f)(long);
183 double (*pL2d)(long);
184 long long (*pArtF2l)(float);
185 long long (*pArtD2l)(double);
186 float (*pFadd)(float, float);
187 float (*pFsub)(float, float);
188 float (*pFdiv)(float, float);
189 float (*pFmul)(float, float);
190 float (*pFmodf)(float, float);
191 double (*pDadd)(double, double);
192 double (*pDsub)(double, double);
193 double (*pDdiv)(double, double);
194 double (*pDmul)(double, double);
195 double (*pFmod)(double, double);
196 int (*pIdivmod)(int, int);
197 int (*pIdiv)(int, int);
198 long long (*pLdivmod)(long long, long long);
199 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
200 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
buzbee3ea4ec52011-08-22 17:37:19 -0700201 const struct ClassObject*);
202 int (*pArtInstanceofNonTrivialNoThrow) (const struct ClassObject*,
203 const struct ClassObject*);
204 int (*pArtInstanceofNonTrivial) (const struct ClassObject*, const struct ClassObject*);
205 Array* (*pArtAllocArrayByClass)(Class*, size_t);
buzbeec143c552011-08-20 17:38:58 -0700206 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
buzbee3ea4ec52011-08-22 17:37:19 -0700207 const struct Method*, struct DvmDex*);
buzbeec143c552011-08-20 17:38:58 -0700208 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
209 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
210 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
211 void (*pArtThrowException)(struct Thread*, struct Object*);
212 bool (*pArtHandleFillArrayDataNoThrow)(struct ArrayObject*, const uint16_t*);
213
Carl Shapiro61e019d2011-07-14 16:53:09 -0700214 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700215 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700216
217 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700218 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700219
220 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700221 void* thread = pthread_getspecific(Thread::pthread_key_self_);
222 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223 }
224
Carl Shapirob5573532011-07-12 18:22:59 -0700225 uint32_t GetId() const {
226 return id_;
227 }
228
229 pid_t GetNativeId() const {
230 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231 }
232
233 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700234 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700235 }
236
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700237 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700238 return exception_;
239 }
240
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700241 Frame GetTopOfStack() const {
242 return top_of_managed_stack_;
243 }
244
245 // TODO: this is here for testing, remove when we have exception unit tests
246 // that use the real stack
247 void SetTopOfStack(void* stack) {
248 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
249 }
250
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700251 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700252 CHECK(new_exception != NULL);
253 // TODO: CHECK(exception_ == NULL);
254 exception_ = new_exception; // TODO
255 }
256
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700257 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700258 __attribute__ ((format(printf, 3, 4)));
259
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700260 void ClearException() {
261 exception_ = NULL;
262 }
263
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700264 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
265
266 void* FindExceptionHandlerInMethod(const Method* method,
267 void* throw_pc,
268 const DexFile& dex_file,
269 ClassLinker* class_linker);
270
Ian Rogers45a76cb2011-07-21 22:00:15 -0700271 // Offset of exception within Thread, used by generated code
272 static ThreadOffset ExceptionOffset() {
273 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
274 }
275
buzbeec143c552011-08-20 17:38:58 -0700276 // Offset of id within Thread, used by generated code
277 static ThreadOffset IdOffset() {
278 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
279 }
280
281 // Offset of card_table within Thread, used by generated code
282 static ThreadOffset CardTableOffset() {
283 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
284 }
285
Carl Shapirob5573532011-07-12 18:22:59 -0700286 void SetName(const char* name);
287
288 void Suspend();
289
290 bool IsSuspended();
291
292 void Resume();
293
294 static bool Init();
295
Elliott Hughes330304d2011-08-12 14:28:05 -0700296 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700297 return state_;
298 }
299
300 void SetState(State new_state) {
301 state_ = new_state;
302 }
303
Ian Rogers45a76cb2011-07-21 22:00:15 -0700304 static ThreadOffset SuspendCountOffset() {
305 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
306 }
307
Ian Rogersb033c752011-07-20 12:22:35 -0700308 // Offset of state within Thread, used by generated code
309 static ThreadOffset StateOffset() {
310 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
311 }
312
Ian Rogersb033c752011-07-20 12:22:35 -0700313 // JNI methods
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700314 JNIEnv* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700315 return jni_env_;
316 }
317
318 // Offset of JNI environment within Thread, used by generated code
319 static ThreadOffset JniEnvOffset() {
320 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
321 }
322
Ian Rogers45a76cb2011-07-21 22:00:15 -0700323 // Offset of top of managed stack address, used by generated code
324 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700325 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
326 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700327 }
328
Ian Rogersb033c752011-07-20 12:22:35 -0700329 // Offset of top stack handle block within Thread, used by generated code
330 static ThreadOffset TopShbOffset() {
331 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
332 }
333
334 // Number of references allocated in StackHandleBlocks on this thread
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700335 size_t NumShbHandles();
336
337 // Is the given obj in this thread's stack handle blocks?
338 bool ShbContains(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700339
Ian Rogers45a76cb2011-07-21 22:00:15 -0700340 // Offset of exception_entry_point_ within Thread, used by generated code
341 static ThreadOffset ExceptionEntryPointOffset() {
342 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
343 }
344
345 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
346 exception_entry_point_ = handler;
347 }
348
349 // Offset of suspend_count_entry_point_ within Thread, used by generated code
350 static ThreadOffset SuspendCountEntryPointOffset() {
351 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
352 }
353
354 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
355 suspend_count_entry_point_ = handler;
356 }
357
358 // Increasing the suspend count, will cause the thread to run to safepoint
359 void IncrementSuspendCount() { suspend_count_++; }
360 void DecrementSuspendCount() { suspend_count_--; }
361
Ian Rogers6de08602011-08-19 14:52:39 -0700362 // Linked list recording transitions from native to managed code
363 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700364 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700365 record->link = native_to_managed_record_;
366 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700367 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700368 }
369 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
370 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700371 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700372 }
373
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700374 ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700375 return class_loader_override_;
376 }
377
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700378 void SetClassLoaderOverride(ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700379 class_loader_override_ = class_loader_override;
380 }
381
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700382 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700383 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700384 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700385 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700386 native_to_managed_record_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700387 top_shb_(NULL),
388 jni_env_(NULL),
389 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700390 suspend_count_(0),
391 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700392 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700393 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700394
Ian Rogersdf20fe02011-07-20 20:34:16 -0700395 ~Thread() {
396 delete jni_env_;
397 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700398
Ian Rogersb033c752011-07-20 12:22:35 -0700399 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700400 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700401
Carl Shapiro69759ea2011-07-21 18:13:35 -0700402 // Managed thread id.
403 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700404
buzbeec143c552011-08-20 17:38:58 -0700405 // FIXME: placeholder for the gc cardTable
406 uint32_t card_table_;
407
Ian Rogers45a76cb2011-07-21 22:00:15 -0700408 // Top of the managed stack, written out prior to the state transition from
409 // kRunnable to kNative. Uses include to give the starting point for scanning
410 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700411 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700412
Ian Rogers6de08602011-08-19 14:52:39 -0700413 // A linked list (of stack allocated records) recording transitions from
414 // native to managed code.
415 NativeToManagedRecord* native_to_managed_record_;
416
Ian Rogersb033c752011-07-20 12:22:35 -0700417 // Top of linked list of stack handle blocks or NULL for none
418 StackHandleBlock* top_shb_;
419
420 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700421 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700422
Carl Shapirob5573532011-07-12 18:22:59 -0700423 State state_;
424
Carl Shapiro69759ea2011-07-21 18:13:35 -0700425 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700426 pid_t native_id_;
427
Carl Shapiro69759ea2011-07-21 18:13:35 -0700428 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700429 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700430
Carl Shapiro69759ea2011-07-21 18:13:35 -0700431 // Initialized to "this". On certain architectures (such as x86) reading
432 // off of Thread::Current is easy but getting the address of Thread::Current
433 // is hard. This field can be read off of Thread::Current to give the address.
434 Thread* self_;
435
436 Runtime* runtime_;
437
438 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700439 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700440
Ian Rogers45a76cb2011-07-21 22:00:15 -0700441 // A non-zero value is used to tell the current thread to enter a safe point
442 // at the next poll.
443 int suspend_count_;
444
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700445 // Needed to get the right ClassLoader in JNI_OnLoad, but also
446 // useful for testing.
447 ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700448
Brian Carlstromb765be02011-08-17 23:54:10 -0700449 // The memory mapping of the stack for non-attached threads.
450 scoped_ptr<MemMap> stack_;
451
Carl Shapiro69759ea2011-07-21 18:13:35 -0700452 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700453 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700454
455 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700456 byte* stack_limit_;
457
Carl Shapiro69759ea2011-07-21 18:13:35 -0700458 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700459 static pthread_key_t pthread_key_self_;
460
Ian Rogers45a76cb2011-07-21 22:00:15 -0700461 // Entry point called when exception_ is set
462 void (*exception_entry_point_)(Method** frame);
463
464 // Entry point called when suspend_count_ is non-zero
465 void (*suspend_count_entry_point_)(Method** frame);
466
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700467 DISALLOW_COPY_AND_ASSIGN(Thread);
468};
Elliott Hughes330304d2011-08-12 14:28:05 -0700469std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700470std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700471
Carl Shapirob5573532011-07-12 18:22:59 -0700472class ThreadList {
473 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700474 static const int kMaxId = 0xFFFF;
475 static const int kInvalidId = 0;
476 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700477
Carl Shapiro61e019d2011-07-14 16:53:09 -0700478 static ThreadList* Create();
479
480 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700481
482 void Register(Thread* thread);
483
484 void Unregister(Thread* thread);
485
Carl Shapirob5573532011-07-12 18:22:59 -0700486 void Lock() {
487 lock_->Lock();
488 }
489
490 void Unlock() {
491 lock_->Unlock();
492 };
493
494 private:
495 ThreadList();
496
497 std::list<Thread*> list_;
498
499 Mutex* lock_;
500
501 DISALLOW_COPY_AND_ASSIGN(ThreadList);
502};
503
504class ThreadListLock {
505 public:
506 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
507 : thread_list_(thread_list) {
508 if (current_thread == NULL) { // try to get it from TLS
509 current_thread = Thread::Current();
510 }
511 Thread::State old_state;
512 if (current_thread != NULL) {
513 old_state = current_thread->GetState();
514 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
515 } else {
516 // happens during VM shutdown
517 old_state = Thread::kUnknown; // TODO: something else
518 }
519 thread_list_->Lock();
520 if (current_thread != NULL) {
521 current_thread->SetState(old_state);
522 }
523 }
524
525 ~ThreadListLock() {
526 thread_list_->Unlock();
527 }
528
Carl Shapirob5573532011-07-12 18:22:59 -0700529 private:
530 ThreadList* thread_list_;
531
532 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
533};
534
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535} // namespace art
536
537#endif // ART_SRC_THREAD_H_