blob: 993327da50edc53dd244e45056bdd8445231f717 [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 Hughesedcc09c2011-08-21 18:47:05 -070020class ClassLoader;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070021class Method;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070022class Object;
Carl Shapirob5573532011-07-12 18:22:59 -070023class Runtime;
Ian Rogersb033c752011-07-20 12:22:35 -070024class StackHandleBlock;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070025class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070026class ThreadList;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070027
28class Mutex {
29 public:
30 virtual ~Mutex() {}
31
Carl Shapirob5573532011-07-12 18:22:59 -070032 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070033
Carl Shapirob5573532011-07-12 18:22:59 -070034 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035
Carl Shapirob5573532011-07-12 18:22:59 -070036 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037
38 const char* GetName() { return name_; }
39
40 Thread* GetOwner() { return owner_; }
41
Carl Shapirob5573532011-07-12 18:22:59 -070042 static Mutex* Create(const char* name);
43
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044 public: // TODO: protected
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045 void SetOwner(Thread* thread) { owner_ = thread; }
46
47 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070048 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
49
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050 const char* name_;
51
52 Thread* owner_;
53
Carl Shapirob5573532011-07-12 18:22:59 -070054 pthread_mutex_t lock_impl_;
55
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070056 DISALLOW_COPY_AND_ASSIGN(Mutex);
57};
58
59class MutexLock {
60 public:
61 explicit MutexLock(Mutex *mu) : mu_(mu) {
62 mu_->Lock();
63 }
64 ~MutexLock() { mu_->Unlock(); }
65 private:
66 Mutex* const mu_;
67 DISALLOW_COPY_AND_ASSIGN(MutexLock);
68};
69
Ian Rogersb033c752011-07-20 12:22:35 -070070// Stack handle blocks are allocated within the bridge frame between managed
71// and native code.
72class StackHandleBlock {
73 public:
74 // Number of references contained within this SHB
75 size_t NumberOfReferences() {
76 return number_of_references_;
77 }
78
79 // Link to previous SHB or NULL
80 StackHandleBlock* Link() {
81 return link_;
82 }
83
Ian Rogersa8cd9f42011-08-19 16:43:41 -070084 Object** Handles() {
85 return handles_;
86 }
87
Ian Rogersb033c752011-07-20 12:22:35 -070088 // Offset of length within SHB, used by generated code
89 static size_t NumberOfReferencesOffset() {
90 return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_);
91 }
92
93 // Offset of link within SHB, used by generated code
94 static size_t LinkOffset() {
95 return OFFSETOF_MEMBER(StackHandleBlock, link_);
96 }
97
98 private:
99 StackHandleBlock() {}
100
101 size_t number_of_references_;
102 StackHandleBlock* link_;
103
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700104 // Fake array, really allocated and filled in by jni_compiler.
105 Object* handles_[0];
106
Ian Rogersb033c752011-07-20 12:22:35 -0700107 DISALLOW_COPY_AND_ASSIGN(StackHandleBlock);
108};
109
Ian Rogers6de08602011-08-19 14:52:39 -0700110struct NativeToManagedRecord {
111 NativeToManagedRecord* link;
112 void* last_top_of_managed_stack;
113};
114
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700115class Thread {
116 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700117 enum State {
118 kUnknown = -1,
119 kNew,
120 kRunnable,
121 kBlocked,
122 kWaiting,
123 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700124 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700125 kTerminated,
126 };
127
buzbeec143c552011-08-20 17:38:58 -0700128
Carl Shapiro61e019d2011-07-14 16:53:09 -0700129 static const size_t kDefaultStackSize = 64 * KB;
130
buzbeec143c552011-08-20 17:38:58 -0700131// TODO - needs to be redone properly, just hacked into place for now
132 // Runtime support function pointers
133 void* (*pMemcpy)(void*, const void*, size_t);
134 float (*pI2f)(int);
135 int (*pF2iz)(float);
136 float (*pD2f)(double);
137 double (*pF2d)(float);
138 double (*pI2d)(int);
139 int (*pD2iz)(double);
140 float (*pL2f)(long);
141 double (*pL2d)(long);
142 long long (*pArtF2l)(float);
143 long long (*pArtD2l)(double);
144 float (*pFadd)(float, float);
145 float (*pFsub)(float, float);
146 float (*pFdiv)(float, float);
147 float (*pFmul)(float, float);
148 float (*pFmodf)(float, float);
149 double (*pDadd)(double, double);
150 double (*pDsub)(double, double);
151 double (*pDdiv)(double, double);
152 double (*pDmul)(double, double);
153 double (*pFmod)(double, double);
154 int (*pIdivmod)(int, int);
155 int (*pIdiv)(int, int);
156 long long (*pLdivmod)(long long, long long);
157 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
158 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
159 const struct ClassObject*);
160 int (*pArtInstanceofNonTrivialNoThrow)
161 (const struct ClassObject*, const struct ClassObject*);
162 int (*pArtInstanceofNonTrivial) (const struct ClassObject*,
163 const struct ClassObject*);
164 struct ArrayObject* (*pArtAllocArrayByClass)(struct ClassObject*,
165 size_t, int);
166 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
167 const struct Method*, struct DvmDex*);
168 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
169 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
170 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
171 void (*pArtThrowException)(struct Thread*, struct Object*);
172 bool (*pArtHandleFillArrayDataNoThrow)(struct ArrayObject*, const uint16_t*);
173
174
Carl Shapiro61e019d2011-07-14 16:53:09 -0700175 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700176 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700177
178 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700179 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700180
181 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700182 void* thread = pthread_getspecific(Thread::pthread_key_self_);
183 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700184 }
185
Carl Shapirob5573532011-07-12 18:22:59 -0700186 uint32_t GetId() const {
187 return id_;
188 }
189
190 pid_t GetNativeId() const {
191 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700192 }
193
194 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700195 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700196 }
197
198 Object* GetException() const {
199 return exception_;
200 }
201
202 void SetException(Object* new_exception) {
203 CHECK(new_exception != NULL);
204 // TODO: CHECK(exception_ == NULL);
205 exception_ = new_exception; // TODO
206 }
207
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700208 void ThrowNewException(const char* exception_class_name, const char* fmt, ...)
209 __attribute__ ((format(printf, 3, 4)));
210
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700211 void ClearException() {
212 exception_ = NULL;
213 }
214
Ian Rogers45a76cb2011-07-21 22:00:15 -0700215 // Offset of exception within Thread, used by generated code
216 static ThreadOffset ExceptionOffset() {
217 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
218 }
219
buzbeec143c552011-08-20 17:38:58 -0700220 // Offset of id within Thread, used by generated code
221 static ThreadOffset IdOffset() {
222 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
223 }
224
225 // Offset of card_table within Thread, used by generated code
226 static ThreadOffset CardTableOffset() {
227 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
228 }
229
Carl Shapirob5573532011-07-12 18:22:59 -0700230 void SetName(const char* name);
231
232 void Suspend();
233
234 bool IsSuspended();
235
236 void Resume();
237
238 static bool Init();
239
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240 Runtime* GetRuntime() const {
241 return runtime_;
242 }
243
Elliott Hughes330304d2011-08-12 14:28:05 -0700244 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700245 return state_;
246 }
247
248 void SetState(State new_state) {
249 state_ = new_state;
250 }
251
Ian Rogers45a76cb2011-07-21 22:00:15 -0700252 static ThreadOffset SuspendCountOffset() {
253 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
254 }
255
Ian Rogersb033c752011-07-20 12:22:35 -0700256 // Offset of state within Thread, used by generated code
257 static ThreadOffset StateOffset() {
258 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
259 }
260
Ian Rogersb033c752011-07-20 12:22:35 -0700261 // JNI methods
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700262 JNIEnv* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700263 return jni_env_;
264 }
265
266 // Offset of JNI environment within Thread, used by generated code
267 static ThreadOffset JniEnvOffset() {
268 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
269 }
270
Ian Rogers45a76cb2011-07-21 22:00:15 -0700271 // Offset of top of managed stack address, used by generated code
272 static ThreadOffset TopOfManagedStackOffset() {
273 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_));
274 }
275
Ian Rogersb033c752011-07-20 12:22:35 -0700276 // Offset of top stack handle block within Thread, used by generated code
277 static ThreadOffset TopShbOffset() {
278 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
279 }
280
281 // Number of references allocated in StackHandleBlocks on this thread
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700282 size_t NumShbHandles();
283
284 // Is the given obj in this thread's stack handle blocks?
285 bool ShbContains(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700286
Ian Rogers45a76cb2011-07-21 22:00:15 -0700287 // Offset of exception_entry_point_ within Thread, used by generated code
288 static ThreadOffset ExceptionEntryPointOffset() {
289 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
290 }
291
292 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
293 exception_entry_point_ = handler;
294 }
295
296 // Offset of suspend_count_entry_point_ within Thread, used by generated code
297 static ThreadOffset SuspendCountEntryPointOffset() {
298 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
299 }
300
301 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
302 suspend_count_entry_point_ = handler;
303 }
304
305 // Increasing the suspend count, will cause the thread to run to safepoint
306 void IncrementSuspendCount() { suspend_count_++; }
307 void DecrementSuspendCount() { suspend_count_--; }
308
Ian Rogers6de08602011-08-19 14:52:39 -0700309 // Linked list recording transitions from native to managed code
310 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
311 record->last_top_of_managed_stack = top_of_managed_stack_;
312 record->link = native_to_managed_record_;
313 native_to_managed_record_ = record;
314 top_of_managed_stack_ = NULL;
315 }
316 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
317 native_to_managed_record_ = record.link;
318 top_of_managed_stack_ = record.last_top_of_managed_stack;
319 }
320
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700321 ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700322 return class_loader_override_;
323 }
324
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700325 void SetClassLoaderOverride(ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700326 class_loader_override_ = class_loader_override;
327 }
328
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700329 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700330 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700331 : id_(1234),
Ian Rogers6de08602011-08-19 14:52:39 -0700332 top_of_managed_stack_(NULL),
333 native_to_managed_record_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700334 top_shb_(NULL),
335 jni_env_(NULL),
336 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700337 suspend_count_(0),
338 class_loader_override_(NULL) {
Ian Rogersb033c752011-07-20 12:22:35 -0700339 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700340
Ian Rogersdf20fe02011-07-20 20:34:16 -0700341 ~Thread() {
342 delete jni_env_;
343 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700344
Ian Rogersb033c752011-07-20 12:22:35 -0700345 void InitCpu();
346
Carl Shapiro69759ea2011-07-21 18:13:35 -0700347 // Managed thread id.
348 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700349
buzbeec143c552011-08-20 17:38:58 -0700350 // FIXME: placeholder for the gc cardTable
351 uint32_t card_table_;
352
Ian Rogers45a76cb2011-07-21 22:00:15 -0700353 // Top of the managed stack, written out prior to the state transition from
354 // kRunnable to kNative. Uses include to give the starting point for scanning
355 // a managed stack when a thread is in native code.
356 void* top_of_managed_stack_;
357
Ian Rogers6de08602011-08-19 14:52:39 -0700358 // A linked list (of stack allocated records) recording transitions from
359 // native to managed code.
360 NativeToManagedRecord* native_to_managed_record_;
361
Ian Rogersb033c752011-07-20 12:22:35 -0700362 // Top of linked list of stack handle blocks or NULL for none
363 StackHandleBlock* top_shb_;
364
365 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700366 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700367
Carl Shapirob5573532011-07-12 18:22:59 -0700368 State state_;
369
Carl Shapiro69759ea2011-07-21 18:13:35 -0700370 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700371 pid_t native_id_;
372
Carl Shapiro69759ea2011-07-21 18:13:35 -0700373 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700374 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700375
Carl Shapiro69759ea2011-07-21 18:13:35 -0700376 // Initialized to "this". On certain architectures (such as x86) reading
377 // off of Thread::Current is easy but getting the address of Thread::Current
378 // is hard. This field can be read off of Thread::Current to give the address.
379 Thread* self_;
380
381 Runtime* runtime_;
382
383 // The pending exception or NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700384 Object* exception_;
385
Ian Rogers45a76cb2011-07-21 22:00:15 -0700386 // A non-zero value is used to tell the current thread to enter a safe point
387 // at the next poll.
388 int suspend_count_;
389
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700390 // Needed to get the right ClassLoader in JNI_OnLoad, but also
391 // useful for testing.
392 ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700393
Brian Carlstromb765be02011-08-17 23:54:10 -0700394 // The memory mapping of the stack for non-attached threads.
395 scoped_ptr<MemMap> stack_;
396
Carl Shapiro69759ea2011-07-21 18:13:35 -0700397 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700398 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700399
400 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700401 byte* stack_limit_;
402
Carl Shapiro69759ea2011-07-21 18:13:35 -0700403 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700404 static pthread_key_t pthread_key_self_;
405
Ian Rogers45a76cb2011-07-21 22:00:15 -0700406 // Entry point called when exception_ is set
407 void (*exception_entry_point_)(Method** frame);
408
409 // Entry point called when suspend_count_ is non-zero
410 void (*suspend_count_entry_point_)(Method** frame);
411
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700412 DISALLOW_COPY_AND_ASSIGN(Thread);
413};
Elliott Hughes330304d2011-08-12 14:28:05 -0700414std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700415std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700416
Carl Shapirob5573532011-07-12 18:22:59 -0700417class ThreadList {
418 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700419 static const int kMaxId = 0xFFFF;
420 static const int kInvalidId = 0;
421 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700422
Carl Shapiro61e019d2011-07-14 16:53:09 -0700423 static ThreadList* Create();
424
425 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700426
427 void Register(Thread* thread);
428
429 void Unregister(Thread* thread);
430
Carl Shapirob5573532011-07-12 18:22:59 -0700431 void Lock() {
432 lock_->Lock();
433 }
434
435 void Unlock() {
436 lock_->Unlock();
437 };
438
439 private:
440 ThreadList();
441
442 std::list<Thread*> list_;
443
444 Mutex* lock_;
445
446 DISALLOW_COPY_AND_ASSIGN(ThreadList);
447};
448
449class ThreadListLock {
450 public:
451 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
452 : thread_list_(thread_list) {
453 if (current_thread == NULL) { // try to get it from TLS
454 current_thread = Thread::Current();
455 }
456 Thread::State old_state;
457 if (current_thread != NULL) {
458 old_state = current_thread->GetState();
459 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
460 } else {
461 // happens during VM shutdown
462 old_state = Thread::kUnknown; // TODO: something else
463 }
464 thread_list_->Lock();
465 if (current_thread != NULL) {
466 current_thread->SetState(old_state);
467 }
468 }
469
470 ~ThreadListLock() {
471 thread_list_->Unlock();
472 }
473
Carl Shapirob5573532011-07-12 18:22:59 -0700474 private:
475 ThreadList* thread_list_;
476
477 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
478};
479
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700480} // namespace art
481
482#endif // ART_SRC_THREAD_H_