blob: 979a3e3e9b08fb4451f3744dd64ec94e88ec49a9 [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;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070029
30class Mutex {
31 public:
32 virtual ~Mutex() {}
33
Carl Shapirob5573532011-07-12 18:22:59 -070034 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035
Carl Shapirob5573532011-07-12 18:22:59 -070036 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037
Carl Shapirob5573532011-07-12 18:22:59 -070038 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070039
40 const char* GetName() { return name_; }
41
42 Thread* GetOwner() { return owner_; }
43
Carl Shapirob5573532011-07-12 18:22:59 -070044 static Mutex* Create(const char* name);
45
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046 public: // TODO: protected
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047 void SetOwner(Thread* thread) { owner_ = thread; }
48
49 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070050 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
51
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070052 const char* name_;
53
54 Thread* owner_;
55
Carl Shapirob5573532011-07-12 18:22:59 -070056 pthread_mutex_t lock_impl_;
57
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070058 DISALLOW_COPY_AND_ASSIGN(Mutex);
59};
60
61class MutexLock {
62 public:
63 explicit MutexLock(Mutex *mu) : mu_(mu) {
64 mu_->Lock();
65 }
66 ~MutexLock() { mu_->Unlock(); }
67 private:
68 Mutex* const mu_;
69 DISALLOW_COPY_AND_ASSIGN(MutexLock);
70};
71
Ian Rogersb033c752011-07-20 12:22:35 -070072// Stack handle blocks are allocated within the bridge frame between managed
73// and native code.
74class StackHandleBlock {
75 public:
76 // Number of references contained within this SHB
77 size_t NumberOfReferences() {
78 return number_of_references_;
79 }
80
81 // Link to previous SHB or NULL
82 StackHandleBlock* Link() {
83 return link_;
84 }
85
Ian Rogersa8cd9f42011-08-19 16:43:41 -070086 Object** Handles() {
87 return handles_;
88 }
89
Ian Rogersb033c752011-07-20 12:22:35 -070090 // Offset of length within SHB, used by generated code
91 static size_t NumberOfReferencesOffset() {
92 return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_);
93 }
94
95 // Offset of link within SHB, used by generated code
96 static size_t LinkOffset() {
97 return OFFSETOF_MEMBER(StackHandleBlock, link_);
98 }
99
100 private:
101 StackHandleBlock() {}
102
103 size_t number_of_references_;
104 StackHandleBlock* link_;
105
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700106 // Fake array, really allocated and filled in by jni_compiler.
107 Object* handles_[0];
108
Ian Rogersb033c752011-07-20 12:22:35 -0700109 DISALLOW_COPY_AND_ASSIGN(StackHandleBlock);
110};
111
Ian Rogers6de08602011-08-19 14:52:39 -0700112struct NativeToManagedRecord {
113 NativeToManagedRecord* link;
114 void* last_top_of_managed_stack;
115};
116
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700117// Iterator over managed frames up to the first native-to-managed transition
118class Frame {
119 Frame() : sp_(NULL) {}
120
121 const Method* GetMethod() const {
122 return *sp_;
123 }
124
125 bool HasNext() const {
126 return NextMethod() != NULL;
127 }
128
129 void Next();
130
131 void* GetPC() const;
132
133 const Method** GetSP() const {
134 return sp_;
135 }
136
137 // TODO: this is here for testing, remove when we have exception unit tests
138 // that use the real stack
139 void SetSP(const Method** sp) {
140 sp_ = sp;
141 }
142
143 private:
144 const Method* NextMethod() const;
145
146 friend class Thread;
147
148 const Method** sp_;
149};
150
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700151class Thread {
152 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700153 enum State {
154 kUnknown = -1,
155 kNew,
156 kRunnable,
157 kBlocked,
158 kWaiting,
159 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700160 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700161 kTerminated,
162 };
163
buzbeec143c552011-08-20 17:38:58 -0700164
Carl Shapiro61e019d2011-07-14 16:53:09 -0700165 static const size_t kDefaultStackSize = 64 * KB;
166
buzbeec143c552011-08-20 17:38:58 -0700167// TODO - needs to be redone properly, just hacked into place for now
168 // Runtime support function pointers
169 void* (*pMemcpy)(void*, const void*, size_t);
170 float (*pI2f)(int);
171 int (*pF2iz)(float);
172 float (*pD2f)(double);
173 double (*pF2d)(float);
174 double (*pI2d)(int);
175 int (*pD2iz)(double);
176 float (*pL2f)(long);
177 double (*pL2d)(long);
178 long long (*pArtF2l)(float);
179 long long (*pArtD2l)(double);
180 float (*pFadd)(float, float);
181 float (*pFsub)(float, float);
182 float (*pFdiv)(float, float);
183 float (*pFmul)(float, float);
184 float (*pFmodf)(float, float);
185 double (*pDadd)(double, double);
186 double (*pDsub)(double, double);
187 double (*pDdiv)(double, double);
188 double (*pDmul)(double, double);
189 double (*pFmod)(double, double);
190 int (*pIdivmod)(int, int);
191 int (*pIdiv)(int, int);
192 long long (*pLdivmod)(long long, long long);
193 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
194 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
195 const struct ClassObject*);
196 int (*pArtInstanceofNonTrivialNoThrow)
197 (const struct ClassObject*, const struct ClassObject*);
198 int (*pArtInstanceofNonTrivial) (const struct ClassObject*,
199 const struct ClassObject*);
200 struct ArrayObject* (*pArtAllocArrayByClass)(struct ClassObject*,
201 size_t, int);
202 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
203 const struct Method*, struct DvmDex*);
204 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
205 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
206 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
207 void (*pArtThrowException)(struct Thread*, struct Object*);
208 bool (*pArtHandleFillArrayDataNoThrow)(struct ArrayObject*, const uint16_t*);
209
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) {
Ian Rogersb033c752011-07-20 12:22:35 -0700389 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700390
Ian Rogersdf20fe02011-07-20 20:34:16 -0700391 ~Thread() {
392 delete jni_env_;
393 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700394
Ian Rogersb033c752011-07-20 12:22:35 -0700395 void InitCpu();
396
Carl Shapiro69759ea2011-07-21 18:13:35 -0700397 // Managed thread id.
398 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700399
buzbeec143c552011-08-20 17:38:58 -0700400 // FIXME: placeholder for the gc cardTable
401 uint32_t card_table_;
402
Ian Rogers45a76cb2011-07-21 22:00:15 -0700403 // Top of the managed stack, written out prior to the state transition from
404 // kRunnable to kNative. Uses include to give the starting point for scanning
405 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700406 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700407
Ian Rogers6de08602011-08-19 14:52:39 -0700408 // A linked list (of stack allocated records) recording transitions from
409 // native to managed code.
410 NativeToManagedRecord* native_to_managed_record_;
411
Ian Rogersb033c752011-07-20 12:22:35 -0700412 // Top of linked list of stack handle blocks or NULL for none
413 StackHandleBlock* top_shb_;
414
415 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700416 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700417
Carl Shapirob5573532011-07-12 18:22:59 -0700418 State state_;
419
Carl Shapiro69759ea2011-07-21 18:13:35 -0700420 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700421 pid_t native_id_;
422
Carl Shapiro69759ea2011-07-21 18:13:35 -0700423 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700424 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700425
Carl Shapiro69759ea2011-07-21 18:13:35 -0700426 // Initialized to "this". On certain architectures (such as x86) reading
427 // off of Thread::Current is easy but getting the address of Thread::Current
428 // is hard. This field can be read off of Thread::Current to give the address.
429 Thread* self_;
430
431 Runtime* runtime_;
432
433 // The pending exception or NULL.
Elliott Hughese5b0dc82011-08-23 09:59:02 -0700434 Throwable* exception_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700435
Ian Rogers45a76cb2011-07-21 22:00:15 -0700436 // A non-zero value is used to tell the current thread to enter a safe point
437 // at the next poll.
438 int suspend_count_;
439
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700440 // Needed to get the right ClassLoader in JNI_OnLoad, but also
441 // useful for testing.
442 ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700443
Brian Carlstromb765be02011-08-17 23:54:10 -0700444 // The memory mapping of the stack for non-attached threads.
445 scoped_ptr<MemMap> stack_;
446
Carl Shapiro69759ea2011-07-21 18:13:35 -0700447 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700448 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700449
450 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700451 byte* stack_limit_;
452
Carl Shapiro69759ea2011-07-21 18:13:35 -0700453 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700454 static pthread_key_t pthread_key_self_;
455
Ian Rogers45a76cb2011-07-21 22:00:15 -0700456 // Entry point called when exception_ is set
457 void (*exception_entry_point_)(Method** frame);
458
459 // Entry point called when suspend_count_ is non-zero
460 void (*suspend_count_entry_point_)(Method** frame);
461
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700462 DISALLOW_COPY_AND_ASSIGN(Thread);
463};
Elliott Hughes330304d2011-08-12 14:28:05 -0700464std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700465std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700466
Carl Shapirob5573532011-07-12 18:22:59 -0700467class ThreadList {
468 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700469 static const int kMaxId = 0xFFFF;
470 static const int kInvalidId = 0;
471 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700472
Carl Shapiro61e019d2011-07-14 16:53:09 -0700473 static ThreadList* Create();
474
475 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700476
477 void Register(Thread* thread);
478
479 void Unregister(Thread* thread);
480
Carl Shapirob5573532011-07-12 18:22:59 -0700481 void Lock() {
482 lock_->Lock();
483 }
484
485 void Unlock() {
486 lock_->Unlock();
487 };
488
489 private:
490 ThreadList();
491
492 std::list<Thread*> list_;
493
494 Mutex* lock_;
495
496 DISALLOW_COPY_AND_ASSIGN(ThreadList);
497};
498
499class ThreadListLock {
500 public:
501 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
502 : thread_list_(thread_list) {
503 if (current_thread == NULL) { // try to get it from TLS
504 current_thread = Thread::Current();
505 }
506 Thread::State old_state;
507 if (current_thread != NULL) {
508 old_state = current_thread->GetState();
509 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
510 } else {
511 // happens during VM shutdown
512 old_state = Thread::kUnknown; // TODO: something else
513 }
514 thread_list_->Lock();
515 if (current_thread != NULL) {
516 current_thread->SetState(old_state);
517 }
518 }
519
520 ~ThreadListLock() {
521 thread_list_->Unlock();
522 }
523
Carl Shapirob5573532011-07-12 18:22:59 -0700524 private:
525 ThreadList* thread_list_;
526
527 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
528};
529
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700530} // namespace art
531
532#endif // ART_SRC_THREAD_H_