blob: b6fad0f7ddde446ff2118b62741e7aa311feb9c2 [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;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070028
29class Mutex {
30 public:
31 virtual ~Mutex() {}
32
Carl Shapirob5573532011-07-12 18:22:59 -070033 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
Carl Shapirob5573532011-07-12 18:22:59 -070035 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070036
Carl Shapirob5573532011-07-12 18:22:59 -070037 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070038
39 const char* GetName() { return name_; }
40
41 Thread* GetOwner() { return owner_; }
42
Carl Shapirob5573532011-07-12 18:22:59 -070043 static Mutex* Create(const char* name);
44
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045 public: // TODO: protected
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046 void SetOwner(Thread* thread) { owner_ = thread; }
47
48 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070049 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
50
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051 const char* name_;
52
53 Thread* owner_;
54
Carl Shapirob5573532011-07-12 18:22:59 -070055 pthread_mutex_t lock_impl_;
56
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070057 DISALLOW_COPY_AND_ASSIGN(Mutex);
58};
59
60class MutexLock {
61 public:
62 explicit MutexLock(Mutex *mu) : mu_(mu) {
63 mu_->Lock();
64 }
65 ~MutexLock() { mu_->Unlock(); }
66 private:
67 Mutex* const mu_;
68 DISALLOW_COPY_AND_ASSIGN(MutexLock);
69};
70
Ian Rogersb033c752011-07-20 12:22:35 -070071// Stack handle blocks are allocated within the bridge frame between managed
72// and native code.
73class StackHandleBlock {
74 public:
75 // Number of references contained within this SHB
76 size_t NumberOfReferences() {
77 return number_of_references_;
78 }
79
80 // Link to previous SHB or NULL
81 StackHandleBlock* Link() {
82 return link_;
83 }
84
Ian Rogersa8cd9f42011-08-19 16:43:41 -070085 Object** Handles() {
86 return handles_;
87 }
88
Ian Rogersb033c752011-07-20 12:22:35 -070089 // Offset of length within SHB, used by generated code
90 static size_t NumberOfReferencesOffset() {
91 return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_);
92 }
93
94 // Offset of link within SHB, used by generated code
95 static size_t LinkOffset() {
96 return OFFSETOF_MEMBER(StackHandleBlock, link_);
97 }
98
99 private:
100 StackHandleBlock() {}
101
102 size_t number_of_references_;
103 StackHandleBlock* link_;
104
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700105 // Fake array, really allocated and filled in by jni_compiler.
106 Object* handles_[0];
107
Ian Rogersb033c752011-07-20 12:22:35 -0700108 DISALLOW_COPY_AND_ASSIGN(StackHandleBlock);
109};
110
Ian Rogers6de08602011-08-19 14:52:39 -0700111struct NativeToManagedRecord {
112 NativeToManagedRecord* link;
113 void* last_top_of_managed_stack;
114};
115
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700116// Iterator over managed frames up to the first native-to-managed transition
117class Frame {
118 Frame() : sp_(NULL) {}
119
120 const Method* GetMethod() const {
121 return *sp_;
122 }
123
124 bool HasNext() const {
125 return NextMethod() != NULL;
126 }
127
128 void Next();
129
130 void* GetPC() const;
131
132 const Method** GetSP() const {
133 return sp_;
134 }
135
136 // TODO: this is here for testing, remove when we have exception unit tests
137 // that use the real stack
138 void SetSP(const Method** sp) {
139 sp_ = sp;
140 }
141
142 private:
143 const Method* NextMethod() const;
144
145 friend class Thread;
146
147 const Method** sp_;
148};
149
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700150class Thread {
151 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700152 enum State {
153 kUnknown = -1,
154 kNew,
155 kRunnable,
156 kBlocked,
157 kWaiting,
158 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700159 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700160 kTerminated,
161 };
162
buzbeec143c552011-08-20 17:38:58 -0700163
Carl Shapiro61e019d2011-07-14 16:53:09 -0700164 static const size_t kDefaultStackSize = 64 * KB;
165
buzbeec143c552011-08-20 17:38:58 -0700166// TODO - needs to be redone properly, just hacked into place for now
167 // Runtime support function pointers
168 void* (*pMemcpy)(void*, const void*, size_t);
169 float (*pI2f)(int);
170 int (*pF2iz)(float);
171 float (*pD2f)(double);
172 double (*pF2d)(float);
173 double (*pI2d)(int);
174 int (*pD2iz)(double);
175 float (*pL2f)(long);
176 double (*pL2d)(long);
177 long long (*pArtF2l)(float);
178 long long (*pArtD2l)(double);
179 float (*pFadd)(float, float);
180 float (*pFsub)(float, float);
181 float (*pFdiv)(float, float);
182 float (*pFmul)(float, float);
183 float (*pFmodf)(float, float);
184 double (*pDadd)(double, double);
185 double (*pDsub)(double, double);
186 double (*pDdiv)(double, double);
187 double (*pDmul)(double, double);
188 double (*pFmod)(double, double);
189 int (*pIdivmod)(int, int);
190 int (*pIdiv)(int, int);
191 long long (*pLdivmod)(long long, long long);
192 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
193 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
194 const struct ClassObject*);
195 int (*pArtInstanceofNonTrivialNoThrow)
196 (const struct ClassObject*, const struct ClassObject*);
197 int (*pArtInstanceofNonTrivial) (const struct ClassObject*,
198 const struct ClassObject*);
199 struct ArrayObject* (*pArtAllocArrayByClass)(struct ClassObject*,
200 size_t, int);
201 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
202 const struct Method*, struct DvmDex*);
203 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
204 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
205 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
206 void (*pArtThrowException)(struct Thread*, struct Object*);
207 bool (*pArtHandleFillArrayDataNoThrow)(struct ArrayObject*, const uint16_t*);
208
209
Carl Shapiro61e019d2011-07-14 16:53:09 -0700210 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700211 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700212
213 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700214 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700215
216 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700217 void* thread = pthread_getspecific(Thread::pthread_key_self_);
218 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700219 }
220
Carl Shapirob5573532011-07-12 18:22:59 -0700221 uint32_t GetId() const {
222 return id_;
223 }
224
225 pid_t GetNativeId() const {
226 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227 }
228
229 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700230 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231 }
232
Elliott Hughes37f7a402011-08-22 18:56:01 -0700233 // TODO: Throwable*
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234 Object* GetException() const {
235 return exception_;
236 }
237
Elliott Hughes37f7a402011-08-22 18:56:01 -0700238 // TODO: Throwable*
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700239 Frame GetTopOfStack() const {
240 return top_of_managed_stack_;
241 }
242
243 // TODO: this is here for testing, remove when we have exception unit tests
244 // that use the real stack
245 void SetTopOfStack(void* stack) {
246 top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack));
247 }
248
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249 void SetException(Object* new_exception) {
250 CHECK(new_exception != NULL);
251 // TODO: CHECK(exception_ == NULL);
252 exception_ = new_exception; // TODO
253 }
254
Elliott Hughes37f7a402011-08-22 18:56:01 -0700255 void ThrowNewException(Class* c, const char* msg);
256
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700257 void ThrowNewException(const char* exception_class_name, const char* fmt, ...)
258 __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) {
Ian Rogersb033c752011-07-20 12:22:35 -0700392 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700393
Ian Rogersdf20fe02011-07-20 20:34:16 -0700394 ~Thread() {
395 delete jni_env_;
396 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700397
Ian Rogersb033c752011-07-20 12:22:35 -0700398 void InitCpu();
399
Carl Shapiro69759ea2011-07-21 18:13:35 -0700400 // Managed thread id.
401 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700402
buzbeec143c552011-08-20 17:38:58 -0700403 // FIXME: placeholder for the gc cardTable
404 uint32_t card_table_;
405
Ian Rogers45a76cb2011-07-21 22:00:15 -0700406 // Top of the managed stack, written out prior to the state transition from
407 // kRunnable to kNative. Uses include to give the starting point for scanning
408 // a managed stack when a thread is in native code.
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700409 Frame top_of_managed_stack_;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700410
Ian Rogers6de08602011-08-19 14:52:39 -0700411 // A linked list (of stack allocated records) recording transitions from
412 // native to managed code.
413 NativeToManagedRecord* native_to_managed_record_;
414
Ian Rogersb033c752011-07-20 12:22:35 -0700415 // Top of linked list of stack handle blocks or NULL for none
416 StackHandleBlock* top_shb_;
417
418 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700419 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700420
Carl Shapirob5573532011-07-12 18:22:59 -0700421 State state_;
422
Carl Shapiro69759ea2011-07-21 18:13:35 -0700423 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700424 pid_t native_id_;
425
Carl Shapiro69759ea2011-07-21 18:13:35 -0700426 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700427 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700428
Carl Shapiro69759ea2011-07-21 18:13:35 -0700429 // Initialized to "this". On certain architectures (such as x86) reading
430 // off of Thread::Current is easy but getting the address of Thread::Current
431 // is hard. This field can be read off of Thread::Current to give the address.
432 Thread* self_;
433
434 Runtime* runtime_;
435
436 // The pending exception or NULL.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700437 // TODO: Throwable*
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700438 Object* exception_;
439
Ian Rogers45a76cb2011-07-21 22:00:15 -0700440 // A non-zero value is used to tell the current thread to enter a safe point
441 // at the next poll.
442 int suspend_count_;
443
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700444 // Needed to get the right ClassLoader in JNI_OnLoad, but also
445 // useful for testing.
446 ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700447
Brian Carlstromb765be02011-08-17 23:54:10 -0700448 // The memory mapping of the stack for non-attached threads.
449 scoped_ptr<MemMap> stack_;
450
Carl Shapiro69759ea2011-07-21 18:13:35 -0700451 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700452 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700453
454 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700455 byte* stack_limit_;
456
Carl Shapiro69759ea2011-07-21 18:13:35 -0700457 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700458 static pthread_key_t pthread_key_self_;
459
Ian Rogers45a76cb2011-07-21 22:00:15 -0700460 // Entry point called when exception_ is set
461 void (*exception_entry_point_)(Method** frame);
462
463 // Entry point called when suspend_count_ is non-zero
464 void (*suspend_count_entry_point_)(Method** frame);
465
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700466 DISALLOW_COPY_AND_ASSIGN(Thread);
467};
Elliott Hughes330304d2011-08-12 14:28:05 -0700468std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700469std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700470
Carl Shapirob5573532011-07-12 18:22:59 -0700471class ThreadList {
472 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700473 static const int kMaxId = 0xFFFF;
474 static const int kInvalidId = 0;
475 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700476
Carl Shapiro61e019d2011-07-14 16:53:09 -0700477 static ThreadList* Create();
478
479 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700480
481 void Register(Thread* thread);
482
483 void Unregister(Thread* thread);
484
Carl Shapirob5573532011-07-12 18:22:59 -0700485 void Lock() {
486 lock_->Lock();
487 }
488
489 void Unlock() {
490 lock_->Unlock();
491 };
492
493 private:
494 ThreadList();
495
496 std::list<Thread*> list_;
497
498 Mutex* lock_;
499
500 DISALLOW_COPY_AND_ASSIGN(ThreadList);
501};
502
503class ThreadListLock {
504 public:
505 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
506 : thread_list_(thread_list) {
507 if (current_thread == NULL) { // try to get it from TLS
508 current_thread = Thread::Current();
509 }
510 Thread::State old_state;
511 if (current_thread != NULL) {
512 old_state = current_thread->GetState();
513 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
514 } else {
515 // happens during VM shutdown
516 old_state = Thread::kUnknown; // TODO: something else
517 }
518 thread_list_->Lock();
519 if (current_thread != NULL) {
520 current_thread->SetState(old_state);
521 }
522 }
523
524 ~ThreadListLock() {
525 thread_list_->Unlock();
526 }
527
Carl Shapirob5573532011-07-12 18:22:59 -0700528 private:
529 ThreadList* thread_list_;
530
531 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
532};
533
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700534} // namespace art
535
536#endif // ART_SRC_THREAD_H_