blob: bb97d4ec4fee2caf1b760e4c7e194200822e364b [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);
173 float (*pI2f)(int);
174 int (*pF2iz)(float);
175 float (*pD2f)(double);
176 double (*pF2d)(float);
177 double (*pI2d)(int);
178 int (*pD2iz)(double);
179 float (*pL2f)(long);
180 double (*pL2d)(long);
181 long long (*pArtF2l)(float);
182 long long (*pArtD2l)(double);
183 float (*pFadd)(float, float);
184 float (*pFsub)(float, float);
185 float (*pFdiv)(float, float);
186 float (*pFmul)(float, float);
187 float (*pFmodf)(float, float);
188 double (*pDadd)(double, double);
189 double (*pDsub)(double, double);
190 double (*pDdiv)(double, double);
191 double (*pDmul)(double, double);
192 double (*pFmod)(double, double);
193 int (*pIdivmod)(int, int);
194 int (*pIdiv)(int, int);
195 long long (*pLdivmod)(long long, long long);
196 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
197 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
buzbee3ea4ec52011-08-22 17:37:19 -0700198 const struct ClassObject*);
199 int (*pArtInstanceofNonTrivialNoThrow) (const struct ClassObject*,
200 const struct ClassObject*);
201 int (*pArtInstanceofNonTrivial) (const struct ClassObject*, const struct ClassObject*);
202 Array* (*pArtAllocArrayByClass)(Class*, size_t);
buzbeec143c552011-08-20 17:38:58 -0700203 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
buzbee3ea4ec52011-08-22 17:37:19 -0700204 const struct Method*, struct DvmDex*);
buzbeec143c552011-08-20 17:38:58 -0700205 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
206 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
207 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
208 void (*pArtThrowException)(struct Thread*, struct Object*);
209 bool (*pArtHandleFillArrayDataNoThrow)(struct ArrayObject*, const uint16_t*);
210
Carl Shapiro61e019d2011-07-14 16:53:09 -0700211 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700212 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700213
214 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700215 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700216
217 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700218 void* thread = pthread_getspecific(Thread::pthread_key_self_);
219 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220 }
221
Carl Shapirob5573532011-07-12 18:22:59 -0700222 uint32_t GetId() const {
223 return id_;
224 }
225
226 pid_t GetNativeId() const {
227 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700228 }
229
230 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700231 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232 }
233
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700234 Throwable* GetException() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700235 return exception_;
236 }
237
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700238 Frame GetTopOfStack() const {
239 return top_of_managed_stack_;
240 }
241
242 // TODO: this is here for testing, remove when we have exception unit tests
243 // that use the real stack
244 void SetTopOfStack(void* stack) {
245 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
246 }
247
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700248 void SetException(Throwable* new_exception) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249 CHECK(new_exception != NULL);
250 // TODO: CHECK(exception_ == NULL);
251 exception_ = new_exception; // TODO
252 }
253
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700254 void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...)
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700255 __attribute__ ((format(printf, 3, 4)));
256
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700257 void ClearException() {
258 exception_ = NULL;
259 }
260
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700261 Frame FindExceptionHandler(void* throw_pc, void** handler_pc);
262
263 void* FindExceptionHandlerInMethod(const Method* method,
264 void* throw_pc,
265 const DexFile& dex_file,
266 ClassLinker* class_linker);
267
Ian Rogers45a76cb2011-07-21 22:00:15 -0700268 // Offset of exception within Thread, used by generated code
269 static ThreadOffset ExceptionOffset() {
270 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
271 }
272
buzbeec143c552011-08-20 17:38:58 -0700273 // Offset of id within Thread, used by generated code
274 static ThreadOffset IdOffset() {
275 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
276 }
277
278 // Offset of card_table within Thread, used by generated code
279 static ThreadOffset CardTableOffset() {
280 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
281 }
282
Carl Shapirob5573532011-07-12 18:22:59 -0700283 void SetName(const char* name);
284
285 void Suspend();
286
287 bool IsSuspended();
288
289 void Resume();
290
291 static bool Init();
292
Elliott Hughes330304d2011-08-12 14:28:05 -0700293 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700294 return state_;
295 }
296
297 void SetState(State new_state) {
298 state_ = new_state;
299 }
300
Ian Rogers45a76cb2011-07-21 22:00:15 -0700301 static ThreadOffset SuspendCountOffset() {
302 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
303 }
304
Ian Rogersb033c752011-07-20 12:22:35 -0700305 // Offset of state within Thread, used by generated code
306 static ThreadOffset StateOffset() {
307 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
308 }
309
Ian Rogersb033c752011-07-20 12:22:35 -0700310 // JNI methods
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700311 JNIEnv* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700312 return jni_env_;
313 }
314
315 // Offset of JNI environment within Thread, used by generated code
316 static ThreadOffset JniEnvOffset() {
317 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
318 }
319
Ian Rogers45a76cb2011-07-21 22:00:15 -0700320 // Offset of top of managed stack address, used by generated code
321 static ThreadOffset TopOfManagedStackOffset() {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700322 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) +
323 OFFSETOF_MEMBER(Frame, sp_));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700324 }
325
Ian Rogersb033c752011-07-20 12:22:35 -0700326 // Offset of top stack handle block within Thread, used by generated code
327 static ThreadOffset TopShbOffset() {
328 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
329 }
330
331 // Number of references allocated in StackHandleBlocks on this thread
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700332 size_t NumShbHandles();
333
334 // Is the given obj in this thread's stack handle blocks?
335 bool ShbContains(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700336
Ian Rogers45a76cb2011-07-21 22:00:15 -0700337 // Offset of exception_entry_point_ within Thread, used by generated code
338 static ThreadOffset ExceptionEntryPointOffset() {
339 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
340 }
341
342 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
343 exception_entry_point_ = handler;
344 }
345
346 // Offset of suspend_count_entry_point_ within Thread, used by generated code
347 static ThreadOffset SuspendCountEntryPointOffset() {
348 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
349 }
350
351 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
352 suspend_count_entry_point_ = handler;
353 }
354
355 // Increasing the suspend count, will cause the thread to run to safepoint
356 void IncrementSuspendCount() { suspend_count_++; }
357 void DecrementSuspendCount() { suspend_count_--; }
358
Ian Rogers6de08602011-08-19 14:52:39 -0700359 // Linked list recording transitions from native to managed code
360 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700361 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700362 record->link = native_to_managed_record_;
363 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700364 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700365 }
366 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
367 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700368 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700369 }
370
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700371 ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700372 return class_loader_override_;
373 }
374
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700375 void SetClassLoaderOverride(ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700376 class_loader_override_ = class_loader_override;
377 }
378
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700379 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700380 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700381 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700382 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700383 native_to_managed_record_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700384 top_shb_(NULL),
385 jni_env_(NULL),
386 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700387 suspend_count_(0),
388 class_loader_override_(NULL) {
buzbee3ea4ec52011-08-22 17:37:19 -0700389 InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700390 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700391
Ian Rogersdf20fe02011-07-20 20:34:16 -0700392 ~Thread() {
393 delete jni_env_;
394 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700395
Ian Rogersb033c752011-07-20 12:22:35 -0700396 void InitCpu();
buzbee3ea4ec52011-08-22 17:37:19 -0700397 void InitFunctionPointers();
Ian Rogersb033c752011-07-20 12:22:35 -0700398
Carl Shapiro69759ea2011-07-21 18:13:35 -0700399 // Managed thread id.
400 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700401
buzbeec143c552011-08-20 17:38:58 -0700402 // FIXME: placeholder for the gc cardTable
403 uint32_t card_table_;
404
Ian Rogers45a76cb2011-07-21 22:00:15 -0700405 // Top of the managed stack, written out prior to the state transition from
406 // kRunnable to kNative. Uses include to give the starting point for scanning
407 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700408 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700409
Ian Rogers6de08602011-08-19 14:52:39 -0700410 // A linked list (of stack allocated records) recording transitions from
411 // native to managed code.
412 NativeToManagedRecord* native_to_managed_record_;
413
Ian Rogersb033c752011-07-20 12:22:35 -0700414 // Top of linked list of stack handle blocks or NULL for none
415 StackHandleBlock* top_shb_;
416
417 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700418 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700419
Carl Shapirob5573532011-07-12 18:22:59 -0700420 State state_;
421
Carl Shapiro69759ea2011-07-21 18:13:35 -0700422 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700423 pid_t native_id_;
424
Carl Shapiro69759ea2011-07-21 18:13:35 -0700425 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700426 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700427
Carl Shapiro69759ea2011-07-21 18:13:35 -0700428 // Initialized to "this". On certain architectures (such as x86) reading
429 // off of Thread::Current is easy but getting the address of Thread::Current
430 // is hard. This field can be read off of Thread::Current to give the address.
431 Thread* self_;
432
433 Runtime* runtime_;
434
435 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700436 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700437
Ian Rogers45a76cb2011-07-21 22:00:15 -0700438 // A non-zero value is used to tell the current thread to enter a safe point
439 // at the next poll.
440 int suspend_count_;
441
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700442 // Needed to get the right ClassLoader in JNI_OnLoad, but also
443 // useful for testing.
444 ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700445
Brian Carlstromb765be02011-08-17 23:54:10 -0700446 // The memory mapping of the stack for non-attached threads.
447 scoped_ptr<MemMap> stack_;
448
Carl Shapiro69759ea2011-07-21 18:13:35 -0700449 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700450 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700451
452 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700453 byte* stack_limit_;
454
Carl Shapiro69759ea2011-07-21 18:13:35 -0700455 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700456 static pthread_key_t pthread_key_self_;
457
Ian Rogers45a76cb2011-07-21 22:00:15 -0700458 // Entry point called when exception_ is set
459 void (*exception_entry_point_)(Method** frame);
460
461 // Entry point called when suspend_count_ is non-zero
462 void (*suspend_count_entry_point_)(Method** frame);
463
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700464 DISALLOW_COPY_AND_ASSIGN(Thread);
465};
Elliott Hughes330304d2011-08-12 14:28:05 -0700466std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700467std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700468
Carl Shapirob5573532011-07-12 18:22:59 -0700469class ThreadList {
470 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700471 static const int kMaxId = 0xFFFF;
472 static const int kInvalidId = 0;
473 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700474
Carl Shapiro61e019d2011-07-14 16:53:09 -0700475 static ThreadList* Create();
476
477 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700478
479 void Register(Thread* thread);
480
481 void Unregister(Thread* thread);
482
Carl Shapirob5573532011-07-12 18:22:59 -0700483 void Lock() {
484 lock_->Lock();
485 }
486
487 void Unlock() {
488 lock_->Unlock();
489 };
490
491 private:
492 ThreadList();
493
494 std::list<Thread*> list_;
495
496 Mutex* lock_;
497
498 DISALLOW_COPY_AND_ASSIGN(ThreadList);
499};
500
501class ThreadListLock {
502 public:
503 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
504 : thread_list_(thread_list) {
505 if (current_thread == NULL) { // try to get it from TLS
506 current_thread = Thread::Current();
507 }
508 Thread::State old_state;
509 if (current_thread != NULL) {
510 old_state = current_thread->GetState();
511 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
512 } else {
513 // happens during VM shutdown
514 old_state = Thread::kUnknown; // TODO: something else
515 }
516 thread_list_->Lock();
517 if (current_thread != NULL) {
518 current_thread->SetState(old_state);
519 }
520 }
521
522 ~ThreadListLock() {
523 thread_list_->Unlock();
524 }
525
Carl Shapirob5573532011-07-12 18:22:59 -0700526 private:
527 ThreadList* thread_list_;
528
529 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
530};
531
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700532} // namespace art
533
534#endif // ART_SRC_THREAD_H_