blob: 66b5e28cd07b6b518e31e872f7b66fef6d0520b7 [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
83 // Offset of length within SHB, used by generated code
84 static size_t NumberOfReferencesOffset() {
85 return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_);
86 }
87
88 // Offset of link within SHB, used by generated code
89 static size_t LinkOffset() {
90 return OFFSETOF_MEMBER(StackHandleBlock, link_);
91 }
92
93 private:
94 StackHandleBlock() {}
95
96 size_t number_of_references_;
97 StackHandleBlock* link_;
98
99 DISALLOW_COPY_AND_ASSIGN(StackHandleBlock);
100};
101
Ian Rogers6de08602011-08-19 14:52:39 -0700102struct NativeToManagedRecord {
103 NativeToManagedRecord* link;
104 void* last_top_of_managed_stack;
105};
106
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700107class Thread {
108 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700109 enum State {
110 kUnknown = -1,
111 kNew,
112 kRunnable,
113 kBlocked,
114 kWaiting,
115 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700116 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700117 kTerminated,
118 };
119
Carl Shapiro61e019d2011-07-14 16:53:09 -0700120 static const size_t kDefaultStackSize = 64 * KB;
121
122 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700123 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700124
125 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700126 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700127
128 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700129 void* thread = pthread_getspecific(Thread::pthread_key_self_);
130 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700131 }
132
Carl Shapirob5573532011-07-12 18:22:59 -0700133 uint32_t GetId() const {
134 return id_;
135 }
136
137 pid_t GetNativeId() const {
138 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700139 }
140
141 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700142 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700143 }
144
145 Object* GetException() const {
146 return exception_;
147 }
148
149 void SetException(Object* new_exception) {
150 CHECK(new_exception != NULL);
151 // TODO: CHECK(exception_ == NULL);
152 exception_ = new_exception; // TODO
153 }
154
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700155 void ThrowNewException(const char* exception_class_name, const char* fmt, ...)
156 __attribute__ ((format(printf, 3, 4)));
157
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700158 void ClearException() {
159 exception_ = NULL;
160 }
161
Ian Rogers45a76cb2011-07-21 22:00:15 -0700162 // Offset of exception within Thread, used by generated code
163 static ThreadOffset ExceptionOffset() {
164 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
165 }
166
Carl Shapirob5573532011-07-12 18:22:59 -0700167 void SetName(const char* name);
168
169 void Suspend();
170
171 bool IsSuspended();
172
173 void Resume();
174
175 static bool Init();
176
Carl Shapiro69759ea2011-07-21 18:13:35 -0700177 Runtime* GetRuntime() const {
178 return runtime_;
179 }
180
Elliott Hughes330304d2011-08-12 14:28:05 -0700181 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700182 return state_;
183 }
184
185 void SetState(State new_state) {
186 state_ = new_state;
187 }
188
Ian Rogers45a76cb2011-07-21 22:00:15 -0700189 static ThreadOffset SuspendCountOffset() {
190 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
191 }
192
Ian Rogersb033c752011-07-20 12:22:35 -0700193 // Offset of state within Thread, used by generated code
194 static ThreadOffset StateOffset() {
195 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
196 }
197
Ian Rogersb033c752011-07-20 12:22:35 -0700198 // JNI methods
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700199 JNIEnv* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700200 return jni_env_;
201 }
202
203 // Offset of JNI environment within Thread, used by generated code
204 static ThreadOffset JniEnvOffset() {
205 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
206 }
207
Ian Rogers45a76cb2011-07-21 22:00:15 -0700208 // Offset of top of managed stack address, used by generated code
209 static ThreadOffset TopOfManagedStackOffset() {
210 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_));
211 }
212
Ian Rogersb033c752011-07-20 12:22:35 -0700213 // Offset of top stack handle block within Thread, used by generated code
214 static ThreadOffset TopShbOffset() {
215 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
216 }
217
218 // Number of references allocated in StackHandleBlocks on this thread
219 size_t NumShbHandles() {
220 size_t count = 0;
221 for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) {
222 count += cur->NumberOfReferences();
223 }
224 return count;
225 }
226
Ian Rogers45a76cb2011-07-21 22:00:15 -0700227 // Offset of exception_entry_point_ within Thread, used by generated code
228 static ThreadOffset ExceptionEntryPointOffset() {
229 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
230 }
231
232 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
233 exception_entry_point_ = handler;
234 }
235
236 // Offset of suspend_count_entry_point_ within Thread, used by generated code
237 static ThreadOffset SuspendCountEntryPointOffset() {
238 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
239 }
240
241 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
242 suspend_count_entry_point_ = handler;
243 }
244
245 // Increasing the suspend count, will cause the thread to run to safepoint
246 void IncrementSuspendCount() { suspend_count_++; }
247 void DecrementSuspendCount() { suspend_count_--; }
248
Ian Rogers6de08602011-08-19 14:52:39 -0700249 // Linked list recording transitions from native to managed code
250 void PushNativeToManagedRecord(NativeToManagedRecord* record) {
251 record->last_top_of_managed_stack = top_of_managed_stack_;
252 record->link = native_to_managed_record_;
253 native_to_managed_record_ = record;
254 top_of_managed_stack_ = NULL;
255 }
256 void PopNativeToManagedRecord(const NativeToManagedRecord& record) {
257 native_to_managed_record_ = record.link;
258 top_of_managed_stack_ = record.last_top_of_managed_stack;
259 }
260
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700261 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700262 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700263 : id_(1234),
Ian Rogers6de08602011-08-19 14:52:39 -0700264 top_of_managed_stack_(NULL),
265 native_to_managed_record_(NULL),
Elliott Hughes330304d2011-08-12 14:28:05 -0700266 top_shb_(NULL),
267 jni_env_(NULL),
268 exception_(NULL),
269 suspend_count_(0) {
Ian Rogersb033c752011-07-20 12:22:35 -0700270 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700271
Ian Rogersdf20fe02011-07-20 20:34:16 -0700272 ~Thread() {
273 delete jni_env_;
274 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700275
Ian Rogersb033c752011-07-20 12:22:35 -0700276 void InitCpu();
277
Carl Shapiro69759ea2011-07-21 18:13:35 -0700278 // Managed thread id.
279 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700280
Ian Rogers45a76cb2011-07-21 22:00:15 -0700281 // Top of the managed stack, written out prior to the state transition from
282 // kRunnable to kNative. Uses include to give the starting point for scanning
283 // a managed stack when a thread is in native code.
284 void* top_of_managed_stack_;
285
Ian Rogers6de08602011-08-19 14:52:39 -0700286 // A linked list (of stack allocated records) recording transitions from
287 // native to managed code.
288 NativeToManagedRecord* native_to_managed_record_;
289
Ian Rogersb033c752011-07-20 12:22:35 -0700290 // Top of linked list of stack handle blocks or NULL for none
291 StackHandleBlock* top_shb_;
292
293 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700294 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700295
Carl Shapirob5573532011-07-12 18:22:59 -0700296 State state_;
297
Carl Shapiro69759ea2011-07-21 18:13:35 -0700298 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700299 pid_t native_id_;
300
Carl Shapiro69759ea2011-07-21 18:13:35 -0700301 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700302 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700303
Carl Shapiro69759ea2011-07-21 18:13:35 -0700304 // Initialized to "this". On certain architectures (such as x86) reading
305 // off of Thread::Current is easy but getting the address of Thread::Current
306 // is hard. This field can be read off of Thread::Current to give the address.
307 Thread* self_;
308
309 Runtime* runtime_;
310
311 // The pending exception or NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700312 Object* exception_;
313
Ian Rogers45a76cb2011-07-21 22:00:15 -0700314 // A non-zero value is used to tell the current thread to enter a safe point
315 // at the next poll.
316 int suspend_count_;
317
Brian Carlstromb765be02011-08-17 23:54:10 -0700318 // The memory mapping of the stack for non-attached threads.
319 scoped_ptr<MemMap> stack_;
320
Carl Shapiro69759ea2011-07-21 18:13:35 -0700321 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700322 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700323
324 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700325 byte* stack_limit_;
326
Carl Shapiro69759ea2011-07-21 18:13:35 -0700327 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700328 static pthread_key_t pthread_key_self_;
329
Ian Rogers45a76cb2011-07-21 22:00:15 -0700330 // Entry point called when exception_ is set
331 void (*exception_entry_point_)(Method** frame);
332
333 // Entry point called when suspend_count_ is non-zero
334 void (*suspend_count_entry_point_)(Method** frame);
335
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700336 DISALLOW_COPY_AND_ASSIGN(Thread);
337};
Elliott Hughes330304d2011-08-12 14:28:05 -0700338std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700339std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700340
Carl Shapirob5573532011-07-12 18:22:59 -0700341class ThreadList {
342 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700343 static const int kMaxId = 0xFFFF;
344 static const int kInvalidId = 0;
345 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700346
Carl Shapiro61e019d2011-07-14 16:53:09 -0700347 static ThreadList* Create();
348
349 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700350
351 void Register(Thread* thread);
352
353 void Unregister(Thread* thread);
354
Carl Shapirob5573532011-07-12 18:22:59 -0700355 void Lock() {
356 lock_->Lock();
357 }
358
359 void Unlock() {
360 lock_->Unlock();
361 };
362
363 private:
364 ThreadList();
365
366 std::list<Thread*> list_;
367
368 Mutex* lock_;
369
370 DISALLOW_COPY_AND_ASSIGN(ThreadList);
371};
372
373class ThreadListLock {
374 public:
375 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
376 : thread_list_(thread_list) {
377 if (current_thread == NULL) { // try to get it from TLS
378 current_thread = Thread::Current();
379 }
380 Thread::State old_state;
381 if (current_thread != NULL) {
382 old_state = current_thread->GetState();
383 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
384 } else {
385 // happens during VM shutdown
386 old_state = Thread::kUnknown; // TODO: something else
387 }
388 thread_list_->Lock();
389 if (current_thread != NULL) {
390 current_thread->SetState(old_state);
391 }
392 }
393
394 ~ThreadListLock() {
395 thread_list_->Unlock();
396 }
397
Carl Shapirob5573532011-07-12 18:22:59 -0700398 private:
399 ThreadList* thread_list_;
400
401 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
402};
403
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700404} // namespace art
405
406#endif // ART_SRC_THREAD_H_