blob: e065f689f23eb95c0e94c18db617dbdad6a31912 [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"
10#include "jni_internal.h"
11#include "logging.h"
12#include "macros.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
44 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
45
46 void SetOwner(Thread* thread) { owner_ = thread; }
47
48 private:
49 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
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700102class Thread {
103 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700104 enum State {
105 kUnknown = -1,
106 kNew,
107 kRunnable,
108 kBlocked,
109 kWaiting,
110 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700111 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700112 kTerminated,
113 };
114
Carl Shapiro61e019d2011-07-14 16:53:09 -0700115 static const size_t kDefaultStackSize = 64 * KB;
116
117 // Creates a new thread.
118 static Thread* Create(size_t stack_size);
119
120 // Creates a new thread from the calling thread.
121 static Thread* Attach();
Carl Shapirob5573532011-07-12 18:22:59 -0700122
123 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700124 void* thread = pthread_getspecific(Thread::pthread_key_self_);
125 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700126 }
127
Carl Shapirob5573532011-07-12 18:22:59 -0700128 uint32_t GetId() const {
129 return id_;
130 }
131
132 pid_t GetNativeId() const {
133 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134 }
135
136 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700137 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700138 }
139
140 Object* GetException() const {
141 return exception_;
142 }
143
144 void SetException(Object* new_exception) {
145 CHECK(new_exception != NULL);
146 // TODO: CHECK(exception_ == NULL);
147 exception_ = new_exception; // TODO
148 }
149
150 void ClearException() {
151 exception_ = NULL;
152 }
153
Ian Rogers45a76cb2011-07-21 22:00:15 -0700154 // Offset of exception within Thread, used by generated code
155 static ThreadOffset ExceptionOffset() {
156 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
157 }
158
Carl Shapirob5573532011-07-12 18:22:59 -0700159 void SetName(const char* name);
160
161 void Suspend();
162
163 bool IsSuspended();
164
165 void Resume();
166
167 static bool Init();
168
Carl Shapiro69759ea2011-07-21 18:13:35 -0700169 Runtime* GetRuntime() const {
170 return runtime_;
171 }
172
Elliott Hughes330304d2011-08-12 14:28:05 -0700173 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700174 return state_;
175 }
176
177 void SetState(State new_state) {
178 state_ = new_state;
179 }
180
Ian Rogers45a76cb2011-07-21 22:00:15 -0700181 static ThreadOffset SuspendCountOffset() {
182 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
183 }
184
Ian Rogersb033c752011-07-20 12:22:35 -0700185 // Offset of state within Thread, used by generated code
186 static ThreadOffset StateOffset() {
187 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
188 }
189
Ian Rogersb033c752011-07-20 12:22:35 -0700190 // JNI methods
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700191 JNIEnv* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700192 return jni_env_;
193 }
194
195 // Offset of JNI environment within Thread, used by generated code
196 static ThreadOffset JniEnvOffset() {
197 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
198 }
199
Ian Rogers45a76cb2011-07-21 22:00:15 -0700200 // Offset of top of managed stack address, used by generated code
201 static ThreadOffset TopOfManagedStackOffset() {
202 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_));
203 }
204
Ian Rogersb033c752011-07-20 12:22:35 -0700205 // Offset of top stack handle block within Thread, used by generated code
206 static ThreadOffset TopShbOffset() {
207 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
208 }
209
210 // Number of references allocated in StackHandleBlocks on this thread
211 size_t NumShbHandles() {
212 size_t count = 0;
213 for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) {
214 count += cur->NumberOfReferences();
215 }
216 return count;
217 }
218
Ian Rogers45a76cb2011-07-21 22:00:15 -0700219 // Offset of exception_entry_point_ within Thread, used by generated code
220 static ThreadOffset ExceptionEntryPointOffset() {
221 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
222 }
223
224 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
225 exception_entry_point_ = handler;
226 }
227
228 // Offset of suspend_count_entry_point_ within Thread, used by generated code
229 static ThreadOffset SuspendCountEntryPointOffset() {
230 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
231 }
232
233 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
234 suspend_count_entry_point_ = handler;
235 }
236
237 // Increasing the suspend count, will cause the thread to run to safepoint
238 void IncrementSuspendCount() { suspend_count_++; }
239 void DecrementSuspendCount() { suspend_count_--; }
240
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700241 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700242 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700243 : id_(1234),
244 top_shb_(NULL),
245 jni_env_(NULL),
246 exception_(NULL),
247 suspend_count_(0) {
Ian Rogersb033c752011-07-20 12:22:35 -0700248 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700249
Ian Rogersdf20fe02011-07-20 20:34:16 -0700250 ~Thread() {
251 delete jni_env_;
252 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700253
Ian Rogersb033c752011-07-20 12:22:35 -0700254 void InitCpu();
255
Carl Shapiro69759ea2011-07-21 18:13:35 -0700256 // Managed thread id.
257 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700258
Ian Rogers45a76cb2011-07-21 22:00:15 -0700259 // Top of the managed stack, written out prior to the state transition from
260 // kRunnable to kNative. Uses include to give the starting point for scanning
261 // a managed stack when a thread is in native code.
262 void* top_of_managed_stack_;
263
Ian Rogersb033c752011-07-20 12:22:35 -0700264 // Top of linked list of stack handle blocks or NULL for none
265 StackHandleBlock* top_shb_;
266
267 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700268 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700269
Carl Shapirob5573532011-07-12 18:22:59 -0700270 State state_;
271
Carl Shapiro69759ea2011-07-21 18:13:35 -0700272 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700273 pid_t native_id_;
274
Carl Shapiro69759ea2011-07-21 18:13:35 -0700275 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700276 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700277
Carl Shapiro69759ea2011-07-21 18:13:35 -0700278 // Initialized to "this". On certain architectures (such as x86) reading
279 // off of Thread::Current is easy but getting the address of Thread::Current
280 // is hard. This field can be read off of Thread::Current to give the address.
281 Thread* self_;
282
283 Runtime* runtime_;
284
285 // The pending exception or NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700286 Object* exception_;
287
Ian Rogers45a76cb2011-07-21 22:00:15 -0700288 // A non-zero value is used to tell the current thread to enter a safe point
289 // at the next poll.
290 int suspend_count_;
291
Carl Shapiro69759ea2011-07-21 18:13:35 -0700292 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700293 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700294
295 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700296 byte* stack_limit_;
297
Carl Shapiro69759ea2011-07-21 18:13:35 -0700298 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700299 static pthread_key_t pthread_key_self_;
300
Ian Rogers45a76cb2011-07-21 22:00:15 -0700301 // Entry point called when exception_ is set
302 void (*exception_entry_point_)(Method** frame);
303
304 // Entry point called when suspend_count_ is non-zero
305 void (*suspend_count_entry_point_)(Method** frame);
306
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700307 DISALLOW_COPY_AND_ASSIGN(Thread);
308};
Elliott Hughes330304d2011-08-12 14:28:05 -0700309std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700310std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700311
Carl Shapirob5573532011-07-12 18:22:59 -0700312class ThreadList {
313 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700314 static const int kMaxId = 0xFFFF;
315 static const int kInvalidId = 0;
316 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700317
Carl Shapiro61e019d2011-07-14 16:53:09 -0700318 static ThreadList* Create();
319
320 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700321
322 void Register(Thread* thread);
323
324 void Unregister(Thread* thread);
325
Carl Shapirob5573532011-07-12 18:22:59 -0700326 void Lock() {
327 lock_->Lock();
328 }
329
330 void Unlock() {
331 lock_->Unlock();
332 };
333
334 private:
335 ThreadList();
336
337 std::list<Thread*> list_;
338
339 Mutex* lock_;
340
341 DISALLOW_COPY_AND_ASSIGN(ThreadList);
342};
343
344class ThreadListLock {
345 public:
346 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
347 : thread_list_(thread_list) {
348 if (current_thread == NULL) { // try to get it from TLS
349 current_thread = Thread::Current();
350 }
351 Thread::State old_state;
352 if (current_thread != NULL) {
353 old_state = current_thread->GetState();
354 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
355 } else {
356 // happens during VM shutdown
357 old_state = Thread::kUnknown; // TODO: something else
358 }
359 thread_list_->Lock();
360 if (current_thread != NULL) {
361 current_thread->SetState(old_state);
362 }
363 }
364
365 ~ThreadListLock() {
366 thread_list_->Unlock();
367 }
368
Carl Shapirob5573532011-07-12 18:22:59 -0700369 private:
370 ThreadList* thread_list_;
371
372 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
373};
374
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700375} // namespace art
376
377#endif // ART_SRC_THREAD_H_