blob: 6677f7c91dc11a026552b5921cd13d153b6765a2 [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
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700116class Thread {
117 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700118 enum State {
119 kUnknown = -1,
120 kNew,
121 kRunnable,
122 kBlocked,
123 kWaiting,
124 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700125 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700126 kTerminated,
127 };
128
buzbeec143c552011-08-20 17:38:58 -0700129
Carl Shapiro61e019d2011-07-14 16:53:09 -0700130 static const size_t kDefaultStackSize = 64 * KB;
131
buzbeec143c552011-08-20 17:38:58 -0700132// TODO - needs to be redone properly, just hacked into place for now
133 // Runtime support function pointers
134 void* (*pMemcpy)(void*, const void*, size_t);
135 float (*pI2f)(int);
136 int (*pF2iz)(float);
137 float (*pD2f)(double);
138 double (*pF2d)(float);
139 double (*pI2d)(int);
140 int (*pD2iz)(double);
141 float (*pL2f)(long);
142 double (*pL2d)(long);
143 long long (*pArtF2l)(float);
144 long long (*pArtD2l)(double);
145 float (*pFadd)(float, float);
146 float (*pFsub)(float, float);
147 float (*pFdiv)(float, float);
148 float (*pFmul)(float, float);
149 float (*pFmodf)(float, float);
150 double (*pDadd)(double, double);
151 double (*pDsub)(double, double);
152 double (*pDdiv)(double, double);
153 double (*pDmul)(double, double);
154 double (*pFmod)(double, double);
155 int (*pIdivmod)(int, int);
156 int (*pIdiv)(int, int);
157 long long (*pLdivmod)(long long, long long);
158 bool (*pArtUnlockObject)(struct Thread*, struct Object*);
159 bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*,
160 const struct ClassObject*);
161 int (*pArtInstanceofNonTrivialNoThrow)
162 (const struct ClassObject*, const struct ClassObject*);
163 int (*pArtInstanceofNonTrivial) (const struct ClassObject*,
164 const struct ClassObject*);
165 struct ArrayObject* (*pArtAllocArrayByClass)(struct ClassObject*,
166 size_t, int);
167 struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t,
168 const struct Method*, struct DvmDex*);
169 bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*);
170 void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*);
171 struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int);
172 void (*pArtThrowException)(struct Thread*, struct Object*);
173 bool (*pArtHandleFillArrayDataNoThrow)(struct ArrayObject*, const uint16_t*);
174
175
Carl Shapiro61e019d2011-07-14 16:53:09 -0700176 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700177 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700178
179 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700180 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700181
182 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700183 void* thread = pthread_getspecific(Thread::pthread_key_self_);
184 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700185 }
186
Carl Shapirob5573532011-07-12 18:22:59 -0700187 uint32_t GetId() const {
188 return id_;
189 }
190
191 pid_t GetNativeId() const {
192 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700193 }
194
195 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700196 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700197 }
198
Elliott Hughes37f7a402011-08-22 18:56:01 -0700199 // TODO: Throwable*
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200 Object* GetException() const {
201 return exception_;
202 }
203
Elliott Hughes37f7a402011-08-22 18:56:01 -0700204 // TODO: Throwable*
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700205 void SetException(Object* new_exception) {
206 CHECK(new_exception != NULL);
207 // TODO: CHECK(exception_ == NULL);
208 exception_ = new_exception; // TODO
209 }
210
Elliott Hughes37f7a402011-08-22 18:56:01 -0700211 void ThrowNewException(Class* c, const char* msg);
212
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700213 void ThrowNewException(const char* exception_class_name, const char* fmt, ...)
214 __attribute__ ((format(printf, 3, 4)));
215
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700216 void ClearException() {
217 exception_ = NULL;
218 }
219
Ian Rogers45a76cb2011-07-21 22:00:15 -0700220 // Offset of exception within Thread, used by generated code
221 static ThreadOffset ExceptionOffset() {
222 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
223 }
224
buzbeec143c552011-08-20 17:38:58 -0700225 // Offset of id within Thread, used by generated code
226 static ThreadOffset IdOffset() {
227 return ThreadOffset(OFFSETOF_MEMBER(Thread, id_));
228 }
229
230 // Offset of card_table within Thread, used by generated code
231 static ThreadOffset CardTableOffset() {
232 return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_));
233 }
234
Carl Shapirob5573532011-07-12 18:22:59 -0700235 void SetName(const char* name);
236
237 void Suspend();
238
239 bool IsSuspended();
240
241 void Resume();
242
243 static bool Init();
244
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245 Runtime* GetRuntime() const {
246 return runtime_;
247 }
248
Elliott Hughes330304d2011-08-12 14:28:05 -0700249 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700250 return state_;
251 }
252
253 void SetState(State new_state) {
254 state_ = new_state;
255 }
256
Ian Rogers45a76cb2011-07-21 22:00:15 -0700257 static ThreadOffset SuspendCountOffset() {
258 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
259 }
260
Ian Rogersb033c752011-07-20 12:22:35 -0700261 // Offset of state within Thread, used by generated code
262 static ThreadOffset StateOffset() {
263 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
264 }
265
Ian Rogersb033c752011-07-20 12:22:35 -0700266 // JNI methods
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700267 JNIEnv* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700268 return jni_env_;
269 }
270
271 // Offset of JNI environment within Thread, used by generated code
272 static ThreadOffset JniEnvOffset() {
273 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
274 }
275
Ian Rogers45a76cb2011-07-21 22:00:15 -0700276 // Offset of top of managed stack address, used by generated code
277 static ThreadOffset TopOfManagedStackOffset() {
278 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_));
279 }
280
Ian Rogersb033c752011-07-20 12:22:35 -0700281 // Offset of top stack handle block within Thread, used by generated code
282 static ThreadOffset TopShbOffset() {
283 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
284 }
285
286 // Number of references allocated in StackHandleBlocks on this thread
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700287 size_t NumShbHandles();
288
289 // Is the given obj in this thread's stack handle blocks?
290 bool ShbContains(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700291
Ian Rogers45a76cb2011-07-21 22:00:15 -0700292 // Offset of exception_entry_point_ within Thread, used by generated code
293 static ThreadOffset ExceptionEntryPointOffset() {
294 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
295 }
296
297 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
298 exception_entry_point_ = handler;
299 }
300
301 // Offset of suspend_count_entry_point_ within Thread, used by generated code
302 static ThreadOffset SuspendCountEntryPointOffset() {
303 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
304 }
305
306 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
307 suspend_count_entry_point_ = handler;
308 }
309
310 // Increasing the suspend count, will cause the thread to run to safepoint
311 void IncrementSuspendCount() { suspend_count_++; }
312 void DecrementSuspendCount() { suspend_count_--; }
313
Ian Rogers6de08602011-08-19 14:52:39 -0700314 // Linked list recording transitions from native to managed code
315 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
316 record->last_top_of_managed_stack = top_of_managed_stack_;
317 record->link = native_to_managed_record_;
318 native_to_managed_record_ = record;
319 top_of_managed_stack_ = NULL;
320 }
321 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
322 native_to_managed_record_ = record.link;
323 top_of_managed_stack_ = record.last_top_of_managed_stack;
324 }
325
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700326 ClassLoader* GetClassLoaderOverride() {
buzbeec143c552011-08-20 17:38:58 -0700327 return class_loader_override_;
328 }
329
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700330 void SetClassLoaderOverride(ClassLoader* class_loader_override) {
buzbeec143c552011-08-20 17:38:58 -0700331 class_loader_override_ = class_loader_override;
332 }
333
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700334 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700335 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700336 : id_(1234),
Ian Rogers6de08602011-08-19 14:52:39 -0700337 top_of_managed_stack_(NULL),
338 native_to_managed_record_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700339 top_shb_(NULL),
340 jni_env_(NULL),
341 exception_(NULL),
buzbeec143c552011-08-20 17:38:58 -0700342 suspend_count_(0),
343 class_loader_override_(NULL) {
Ian Rogersb033c752011-07-20 12:22:35 -0700344 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700345
Ian Rogersdf20fe02011-07-20 20:34:16 -0700346 ~Thread() {
347 delete jni_env_;
348 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700349
Ian Rogersb033c752011-07-20 12:22:35 -0700350 void InitCpu();
351
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352 // Managed thread id.
353 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700354
buzbeec143c552011-08-20 17:38:58 -0700355 // FIXME: placeholder for the gc cardTable
356 uint32_t card_table_;
357
Ian Rogers45a76cb2011-07-21 22:00:15 -0700358 // Top of the managed stack, written out prior to the state transition from
359 // kRunnable to kNative. Uses include to give the starting point for scanning
360 // a managed stack when a thread is in native code.
361 void* top_of_managed_stack_;
362
Ian Rogers6de08602011-08-19 14:52:39 -0700363 // A linked list (of stack allocated records) recording transitions from
364 // native to managed code.
365 NativeToManagedRecord* native_to_managed_record_;
366
Ian Rogersb033c752011-07-20 12:22:35 -0700367 // Top of linked list of stack handle blocks or NULL for none
368 StackHandleBlock* top_shb_;
369
370 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700371 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700372
Carl Shapirob5573532011-07-12 18:22:59 -0700373 State state_;
374
Carl Shapiro69759ea2011-07-21 18:13:35 -0700375 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700376 pid_t native_id_;
377
Carl Shapiro69759ea2011-07-21 18:13:35 -0700378 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700379 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380
Carl Shapiro69759ea2011-07-21 18:13:35 -0700381 // Initialized to "this". On certain architectures (such as x86) reading
382 // off of Thread::Current is easy but getting the address of Thread::Current
383 // is hard. This field can be read off of Thread::Current to give the address.
384 Thread* self_;
385
386 Runtime* runtime_;
387
388 // The pending exception or NULL.
Elliott Hughes37f7a402011-08-22 18:56:01 -0700389 // TODO: Throwable*
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700390 Object* exception_;
391
Ian Rogers45a76cb2011-07-21 22:00:15 -0700392 // A non-zero value is used to tell the current thread to enter a safe point
393 // at the next poll.
394 int suspend_count_;
395
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700396 // Needed to get the right ClassLoader in JNI_OnLoad, but also
397 // useful for testing.
398 ClassLoader* class_loader_override_;
buzbeec143c552011-08-20 17:38:58 -0700399
Brian Carlstromb765be02011-08-17 23:54:10 -0700400 // The memory mapping of the stack for non-attached threads.
401 scoped_ptr<MemMap> stack_;
402
Carl Shapiro69759ea2011-07-21 18:13:35 -0700403 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700404 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700405
406 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700407 byte* stack_limit_;
408
Carl Shapiro69759ea2011-07-21 18:13:35 -0700409 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700410 static pthread_key_t pthread_key_self_;
411
Ian Rogers45a76cb2011-07-21 22:00:15 -0700412 // Entry point called when exception_ is set
413 void (*exception_entry_point_)(Method** frame);
414
415 // Entry point called when suspend_count_ is non-zero
416 void (*suspend_count_entry_point_)(Method** frame);
417
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700418 DISALLOW_COPY_AND_ASSIGN(Thread);
419};
Elliott Hughes330304d2011-08-12 14:28:05 -0700420std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700421std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700422
Carl Shapirob5573532011-07-12 18:22:59 -0700423class ThreadList {
424 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700425 static const int kMaxId = 0xFFFF;
426 static const int kInvalidId = 0;
427 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700428
Carl Shapiro61e019d2011-07-14 16:53:09 -0700429 static ThreadList* Create();
430
431 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700432
433 void Register(Thread* thread);
434
435 void Unregister(Thread* thread);
436
Carl Shapirob5573532011-07-12 18:22:59 -0700437 void Lock() {
438 lock_->Lock();
439 }
440
441 void Unlock() {
442 lock_->Unlock();
443 };
444
445 private:
446 ThreadList();
447
448 std::list<Thread*> list_;
449
450 Mutex* lock_;
451
452 DISALLOW_COPY_AND_ASSIGN(ThreadList);
453};
454
455class ThreadListLock {
456 public:
457 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
458 : thread_list_(thread_list) {
459 if (current_thread == NULL) { // try to get it from TLS
460 current_thread = Thread::Current();
461 }
462 Thread::State old_state;
463 if (current_thread != NULL) {
464 old_state = current_thread->GetState();
465 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
466 } else {
467 // happens during VM shutdown
468 old_state = Thread::kUnknown; // TODO: something else
469 }
470 thread_list_->Lock();
471 if (current_thread != NULL) {
472 current_thread->SetState(old_state);
473 }
474 }
475
476 ~ThreadListLock() {
477 thread_list_->Unlock();
478 }
479
Carl Shapirob5573532011-07-12 18:22:59 -0700480 private:
481 ThreadList* thread_list_;
482
483 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
484};
485
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700486} // namespace art
487
488#endif // ART_SRC_THREAD_H_