blob: 7204577cf1a1a662710424df4d8ff57000966e63 [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
Elliott Hughes5174fe62011-08-23 15:12:35 -070058 friend class SharedLibrary; // For lock_impl_.
59
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060 DISALLOW_COPY_AND_ASSIGN(Mutex);
61};
62
63class MutexLock {
64 public:
65 explicit MutexLock(Mutex *mu) : mu_(mu) {
66 mu_->Lock();
67 }
68 ~MutexLock() { mu_->Unlock(); }
69 private:
70 Mutex* const mu_;
71 DISALLOW_COPY_AND_ASSIGN(MutexLock);
72};
73
Ian Rogersb033c752011-07-20 12:22:35 -070074// Stack handle blocks are allocated within the bridge frame between managed
75// and native code.
76class StackHandleBlock {
77 public:
78 // Number of references contained within this SHB
79 size_t NumberOfReferences() {
80 return number_of_references_;
81 }
82
83 // Link to previous SHB or NULL
84 StackHandleBlock* Link() {
85 return link_;
86 }
87
Ian Rogersa8cd9f42011-08-19 16:43:41 -070088 Object** Handles() {
89 return handles_;
90 }
91
Ian Rogersb033c752011-07-20 12:22:35 -070092 // Offset of length within SHB, used by generated code
93 static size_t NumberOfReferencesOffset() {
94 return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_);
95 }
96
97 // Offset of link within SHB, used by generated code
98 static size_t LinkOffset() {
99 return OFFSETOF_MEMBER(StackHandleBlock, link_);
100 }
101
102 private:
103 StackHandleBlock() {}
104
105 size_t number_of_references_;
106 StackHandleBlock* link_;
107
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700108 // Fake array, really allocated and filled in by jni_compiler.
109 Object* handles_[0];
110
Ian Rogersb033c752011-07-20 12:22:35 -0700111 DISALLOW_COPY_AND_ASSIGN(StackHandleBlock);
112};
113
Ian Rogers6de08602011-08-19 14:52:39 -0700114struct NativeToManagedRecord {
115 NativeToManagedRecord* link;
116 void* last_top_of_managed_stack;
117};
118
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700119// Iterator over managed frames up to the first native-to-managed transition
120class Frame {
121 Frame() : sp_(NULL) {}
122
123 const Method* GetMethod() const {
124 return *sp_;
125 }
126
127 bool HasNext() const {
128 return NextMethod() != NULL;
129 }
130
131 void Next();
132
133 void* GetPC() const;
134
135 const Method** GetSP() const {
136 return sp_;
137 }
138
139 // TODO: this is here for testing, remove when we have exception unit tests
140 // that use the real stack
141 void SetSP(const Method** sp) {
142 sp_ = sp;
143 }
144
145 private:
146 const Method* NextMethod() const;
147
148 friend class Thread;
149
150 const Method** sp_;
151};
152
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700153class Thread {
154 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700155 enum State {
156 kUnknown = -1,
157 kNew,
158 kRunnable,
159 kBlocked,
160 kWaiting,
161 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700162 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700163 kTerminated,
164 };
165
buzbeec143c552011-08-20 17:38:58 -0700166
Carl Shapiro61e019d2011-07-14 16:53:09 -0700167 static const size_t kDefaultStackSize = 64 * KB;
168
buzbeec143c552011-08-20 17:38:58 -0700169// TODO - needs to be redone properly, just hacked into place for now
170 // Runtime support function pointers
171 void* (*pMemcpy)(void*, const void*, size_t);
172 float (*pI2f)(int);
173 int (*pF2iz)(float);
174 float (*pD2f)(double);
175 double (*pF2d)(float);
176 double (*pI2d)(int);
177 int (*pD2iz)(double);
178 float (*pL2f)(long);
179 double (*pL2d)(long);
180 long long (*pArtF2l)(float);
181 long long (*pArtD2l)(double);
182 float (*pFadd)(float, float);
183 float (*pFsub)(float, float);
184 float (*pFdiv)(float, float);
185 float (*pFmul)(float, float);
186 float (*pFmodf)(float, float);
187 double (*pDadd)(double, double);
188 double (*pDsub)(double, double);
189 double (*pDdiv)(double, double);
190 double (*pDmul)(double, double);
191 double (*pFmod)(double, double);
192 int (*pIdivmod)(int, int);
193 int (*pIdiv)(int, int);
194 long long (*pLdivmod)(long long, long long);
195 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
196 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
197 const struct ClassObject*);
198 int (*pArtInstanceofNonTrivialNoThrow)
199 (const struct ClassObject*, const struct ClassObject*);
200 int (*pArtInstanceofNonTrivial) (const struct ClassObject*,
201 const struct ClassObject*);
202 struct ArrayObject* (*pArtAllocArrayByClass)(struct ClassObject*,
203 size_t, int);
204 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
205 const struct Method*, struct DvmDex*);
206 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
207 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
208 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
209 void (*pArtThrowException)(struct Thread*, struct Object*);
210 bool (*pArtHandleFillArrayDataNoThrow)(struct ArrayObject*, const uint16_t*);
211
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 Rogersb033c752011-07-20 12:22:35 -0700328 // Offset of top stack handle block within Thread, used by generated code
329 static ThreadOffset TopShbOffset() {
330 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
331 }
332
333 // Number of references allocated in StackHandleBlocks on this thread
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700334 size_t NumShbHandles();
335
336 // Is the given obj in this thread's stack handle blocks?
337 bool ShbContains(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700338
Ian Rogers45a76cb2011-07-21 22:00:15 -0700339 // Offset of exception_entry_point_ within Thread, used by generated code
340 static ThreadOffset ExceptionEntryPointOffset() {
341 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
342 }
343
344 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
345 exception_entry_point_ = handler;
346 }
347
348 // Offset of suspend_count_entry_point_ within Thread, used by generated code
349 static ThreadOffset SuspendCountEntryPointOffset() {
350 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
351 }
352
353 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
354 suspend_count_entry_point_ = handler;
355 }
356
357 // Increasing the suspend count, will cause the thread to run to safepoint
358 void IncrementSuspendCount() { suspend_count_++; }
359 void DecrementSuspendCount() { suspend_count_--; }
360
Ian Rogers6de08602011-08-19 14:52:39 -0700361 // Linked list recording transitions from native to managed code
362 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700363 record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP());
Ian Rogers6de08602011-08-19 14:52:39 -0700364 record->link = native_to_managed_record_;
365 native_to_managed_record_ = record;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700366 top_of_managed_stack_.SetSP(NULL);
Ian Rogers6de08602011-08-19 14:52:39 -0700367 }
368 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
369 native_to_managed_record_ = record.link;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700370 top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) );
Ian Rogers6de08602011-08-19 14:52:39 -0700371 }
372
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700373 ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700374 return class_loader_override_;
375 }
376
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700377 void SetClassLoaderOverride(ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700378 class_loader_override_ = class_loader_override;
379 }
380
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700381 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700382 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700383 : id_(1234),
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700384 top_of_managed_stack_(),
Ian Rogers6de08602011-08-19 14:52:39 -0700385 native_to_managed_record_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700386 top_shb_(NULL),
387 jni_env_(NULL),
388 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700389 suspend_count_(0),
390 class_loader_override_(NULL) {
Ian Rogersb033c752011-07-20 12:22:35 -0700391 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700392
Ian Rogersdf20fe02011-07-20 20:34:16 -0700393 ~Thread() {
394 delete jni_env_;
395 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700396
Ian Rogersb033c752011-07-20 12:22:35 -0700397 void InitCpu();
398
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_