blob: ac332e0a9f0b8c03d85c24d17eb2d8adbac423da [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
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070020class Method;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070021class Object;
Carl Shapirob5573532011-07-12 18:22:59 -070022class Runtime;
Ian Rogersb033c752011-07-20 12:22:35 -070023class StackHandleBlock;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070024class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070025class ThreadList;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026
27class Mutex {
28 public:
29 virtual ~Mutex() {}
30
Carl Shapirob5573532011-07-12 18:22:59 -070031 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070032
Carl Shapirob5573532011-07-12 18:22:59 -070033 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
Carl Shapirob5573532011-07-12 18:22:59 -070035 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070036
37 const char* GetName() { return name_; }
38
39 Thread* GetOwner() { return owner_; }
40
Carl Shapirob5573532011-07-12 18:22:59 -070041 static Mutex* Create(const char* name);
42
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043 public: // TODO: protected
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044 void SetOwner(Thread* thread) { owner_ = thread; }
45
46 private:
Elliott Hughes18c07532011-08-18 15:50:51 -070047 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
48
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049 const char* name_;
50
51 Thread* owner_;
52
Carl Shapirob5573532011-07-12 18:22:59 -070053 pthread_mutex_t lock_impl_;
54
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070055 DISALLOW_COPY_AND_ASSIGN(Mutex);
56};
57
58class MutexLock {
59 public:
60 explicit MutexLock(Mutex *mu) : mu_(mu) {
61 mu_->Lock();
62 }
63 ~MutexLock() { mu_->Unlock(); }
64 private:
65 Mutex* const mu_;
66 DISALLOW_COPY_AND_ASSIGN(MutexLock);
67};
68
Ian Rogersb033c752011-07-20 12:22:35 -070069// Stack handle blocks are allocated within the bridge frame between managed
70// and native code.
71class StackHandleBlock {
72 public:
73 // Number of references contained within this SHB
74 size_t NumberOfReferences() {
75 return number_of_references_;
76 }
77
78 // Link to previous SHB or NULL
79 StackHandleBlock* Link() {
80 return link_;
81 }
82
Ian Rogersa8cd9f42011-08-19 16:43:41 -070083 Object** Handles() {
84 return handles_;
85 }
86
Ian Rogersb033c752011-07-20 12:22:35 -070087 // Offset of length within SHB, used by generated code
88 static size_t NumberOfReferencesOffset() {
89 return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_);
90 }
91
92 // Offset of link within SHB, used by generated code
93 static size_t LinkOffset() {
94 return OFFSETOF_MEMBER(StackHandleBlock, link_);
95 }
96
97 private:
98 StackHandleBlock() {}
99
100 size_t number_of_references_;
101 StackHandleBlock* link_;
102
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700103 // Fake array, really allocated and filled in by jni_compiler.
104 Object* handles_[0];
105
Ian Rogersb033c752011-07-20 12:22:35 -0700106 DISALLOW_COPY_AND_ASSIGN(StackHandleBlock);
107};
108
Ian Rogers6de08602011-08-19 14:52:39 -0700109struct NativeToManagedRecord {
110 NativeToManagedRecord* link;
111 void* last_top_of_managed_stack;
112};
113
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700114class Thread {
115 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700116 enum State {
117 kUnknown = -1,
118 kNew,
119 kRunnable,
120 kBlocked,
121 kWaiting,
122 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700123 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700124 kTerminated,
125 };
126
Carl Shapiro61e019d2011-07-14 16:53:09 -0700127 static const size_t kDefaultStackSize = 64 * KB;
128
129 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700130 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700131
132 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700133 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700134
135 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700136 void* thread = pthread_getspecific(Thread::pthread_key_self_);
137 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700138 }
139
Carl Shapirob5573532011-07-12 18:22:59 -0700140 uint32_t GetId() const {
141 return id_;
142 }
143
144 pid_t GetNativeId() const {
145 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700146 }
147
148 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700149 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700150 }
151
152 Object* GetException() const {
153 return exception_;
154 }
155
156 void SetException(Object* new_exception) {
157 CHECK(new_exception != NULL);
158 // TODO: CHECK(exception_ == NULL);
159 exception_ = new_exception; // TODO
160 }
161
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700162 void ThrowNewException(const char* exception_class_name, const char* fmt, ...)
163 __attribute__ ((format(printf, 3, 4)));
164
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700165 void ClearException() {
166 exception_ = NULL;
167 }
168
Ian Rogers45a76cb2011-07-21 22:00:15 -0700169 // Offset of exception within Thread, used by generated code
170 static ThreadOffset ExceptionOffset() {
171 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
172 }
173
Carl Shapirob5573532011-07-12 18:22:59 -0700174 void SetName(const char* name);
175
176 void Suspend();
177
178 bool IsSuspended();
179
180 void Resume();
181
182 static bool Init();
183
Carl Shapiro69759ea2011-07-21 18:13:35 -0700184 Runtime* GetRuntime() const {
185 return runtime_;
186 }
187
Elliott Hughes330304d2011-08-12 14:28:05 -0700188 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700189 return state_;
190 }
191
192 void SetState(State new_state) {
193 state_ = new_state;
194 }
195
Ian Rogers45a76cb2011-07-21 22:00:15 -0700196 static ThreadOffset SuspendCountOffset() {
197 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
198 }
199
Ian Rogersb033c752011-07-20 12:22:35 -0700200 // Offset of state within Thread, used by generated code
201 static ThreadOffset StateOffset() {
202 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
203 }
204
Ian Rogersb033c752011-07-20 12:22:35 -0700205 // JNI methods
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700206 JNIEnv* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700207 return jni_env_;
208 }
209
210 // Offset of JNI environment within Thread, used by generated code
211 static ThreadOffset JniEnvOffset() {
212 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
213 }
214
Ian Rogers45a76cb2011-07-21 22:00:15 -0700215 // Offset of top of managed stack address, used by generated code
216 static ThreadOffset TopOfManagedStackOffset() {
217 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_));
218 }
219
Ian Rogersb033c752011-07-20 12:22:35 -0700220 // Offset of top stack handle block within Thread, used by generated code
221 static ThreadOffset TopShbOffset() {
222 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
223 }
224
225 // Number of references allocated in StackHandleBlocks on this thread
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700226 size_t NumShbHandles();
227
228 // Is the given obj in this thread's stack handle blocks?
229 bool ShbContains(jobject obj);
Ian Rogersb033c752011-07-20 12:22:35 -0700230
Ian Rogers45a76cb2011-07-21 22:00:15 -0700231 // Offset of exception_entry_point_ within Thread, used by generated code
232 static ThreadOffset ExceptionEntryPointOffset() {
233 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
234 }
235
236 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
237 exception_entry_point_ = handler;
238 }
239
240 // Offset of suspend_count_entry_point_ within Thread, used by generated code
241 static ThreadOffset SuspendCountEntryPointOffset() {
242 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
243 }
244
245 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
246 suspend_count_entry_point_ = handler;
247 }
248
249 // Increasing the suspend count, will cause the thread to run to safepoint
250 void IncrementSuspendCount() { suspend_count_++; }
251 void DecrementSuspendCount() { suspend_count_--; }
252
Ian Rogers6de08602011-08-19 14:52:39 -0700253 // Linked list recording transitions from native to managed code
254 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
255 record->last_top_of_managed_stack = top_of_managed_stack_;
256 record->link = native_to_managed_record_;
257 native_to_managed_record_ = record;
258 top_of_managed_stack_ = NULL;
259 }
260 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
261 native_to_managed_record_ = record.link;
262 top_of_managed_stack_ = record.last_top_of_managed_stack;
263 }
264
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700265 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700266 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700267 : id_(1234),
Ian Rogers6de08602011-08-19 14:52:39 -0700268 top_of_managed_stack_(NULL),
269 native_to_managed_record_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700270 top_shb_(NULL),
271 jni_env_(NULL),
272 exception_(NULL),
273 suspend_count_(0) {
Ian Rogersb033c752011-07-20 12:22:35 -0700274 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700275
Ian Rogersdf20fe02011-07-20 20:34:16 -0700276 ~Thread() {
277 delete jni_env_;
278 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700279
Ian Rogersb033c752011-07-20 12:22:35 -0700280 void InitCpu();
281
Carl Shapiro69759ea2011-07-21 18:13:35 -0700282 // Managed thread id.
283 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700284
Ian Rogers45a76cb2011-07-21 22:00:15 -0700285 // Top of the managed stack, written out prior to the state transition from
286 // kRunnable to kNative. Uses include to give the starting point for scanning
287 // a managed stack when a thread is in native code.
288 void* top_of_managed_stack_;
289
Ian Rogers6de08602011-08-19 14:52:39 -0700290 // A linked list (of stack allocated records) recording transitions from
291 // native to managed code.
292 NativeToManagedRecord* native_to_managed_record_;
293
Ian Rogersb033c752011-07-20 12:22:35 -0700294 // Top of linked list of stack handle blocks or NULL for none
295 StackHandleBlock* top_shb_;
296
297 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700298 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700299
Carl Shapirob5573532011-07-12 18:22:59 -0700300 State state_;
301
Carl Shapiro69759ea2011-07-21 18:13:35 -0700302 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700303 pid_t native_id_;
304
Carl Shapiro69759ea2011-07-21 18:13:35 -0700305 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700306 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700307
Carl Shapiro69759ea2011-07-21 18:13:35 -0700308 // Initialized to "this". On certain architectures (such as x86) reading
309 // off of Thread::Current is easy but getting the address of Thread::Current
310 // is hard. This field can be read off of Thread::Current to give the address.
311 Thread* self_;
312
313 Runtime* runtime_;
314
315 // The pending exception or NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700316 Object* exception_;
317
Ian Rogers45a76cb2011-07-21 22:00:15 -0700318 // A non-zero value is used to tell the current thread to enter a safe point
319 // at the next poll.
320 int suspend_count_;
321
Brian Carlstromb765be02011-08-17 23:54:10 -0700322 // The memory mapping of the stack for non-attached threads.
323 scoped_ptr<MemMap> stack_;
324
Carl Shapiro69759ea2011-07-21 18:13:35 -0700325 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700326 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700327
328 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700329 byte* stack_limit_;
330
Carl Shapiro69759ea2011-07-21 18:13:35 -0700331 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700332 static pthread_key_t pthread_key_self_;
333
Ian Rogers45a76cb2011-07-21 22:00:15 -0700334 // Entry point called when exception_ is set
335 void (*exception_entry_point_)(Method** frame);
336
337 // Entry point called when suspend_count_ is non-zero
338 void (*suspend_count_entry_point_)(Method** frame);
339
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700340 DISALLOW_COPY_AND_ASSIGN(Thread);
341};
Elliott Hughes330304d2011-08-12 14:28:05 -0700342std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700343std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700344
Carl Shapirob5573532011-07-12 18:22:59 -0700345class ThreadList {
346 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700347 static const int kMaxId = 0xFFFF;
348 static const int kInvalidId = 0;
349 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700350
Carl Shapiro61e019d2011-07-14 16:53:09 -0700351 static ThreadList* Create();
352
353 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700354
355 void Register(Thread* thread);
356
357 void Unregister(Thread* thread);
358
Carl Shapirob5573532011-07-12 18:22:59 -0700359 void Lock() {
360 lock_->Lock();
361 }
362
363 void Unlock() {
364 lock_->Unlock();
365 };
366
367 private:
368 ThreadList();
369
370 std::list<Thread*> list_;
371
372 Mutex* lock_;
373
374 DISALLOW_COPY_AND_ASSIGN(ThreadList);
375};
376
377class ThreadListLock {
378 public:
379 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
380 : thread_list_(thread_list) {
381 if (current_thread == NULL) { // try to get it from TLS
382 current_thread = Thread::Current();
383 }
384 Thread::State old_state;
385 if (current_thread != NULL) {
386 old_state = current_thread->GetState();
387 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
388 } else {
389 // happens during VM shutdown
390 old_state = Thread::kUnknown; // TODO: something else
391 }
392 thread_list_->Lock();
393 if (current_thread != NULL) {
394 current_thread->SetState(old_state);
395 }
396 }
397
398 ~ThreadListLock() {
399 thread_list_->Unlock();
400 }
401
Carl Shapirob5573532011-07-12 18:22:59 -0700402 private:
403 ThreadList* thread_list_;
404
405 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
406};
407
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700408} // namespace art
409
410#endif // ART_SRC_THREAD_H_