blob: 4264a1afe5165165bab34dfc4dc5354c10d1f106 [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 Carlstromb765be02011-08-17 23:54:10 -070013#include "mem_map.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "offsets.h"
15#include "runtime.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogersb033c752011-07-20 12:22:35 -070017#include "jni.h"
18
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019namespace art {
20
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
45 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
46
47 void SetOwner(Thread* thread) { owner_ = thread; }
48
49 private:
50 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
84 // Offset of length within SHB, used by generated code
85 static size_t NumberOfReferencesOffset() {
86 return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_);
87 }
88
89 // Offset of link within SHB, used by generated code
90 static size_t LinkOffset() {
91 return OFFSETOF_MEMBER(StackHandleBlock, link_);
92 }
93
94 private:
95 StackHandleBlock() {}
96
97 size_t number_of_references_;
98 StackHandleBlock* link_;
99
100 DISALLOW_COPY_AND_ASSIGN(StackHandleBlock);
101};
102
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700103class Thread {
104 public:
Carl Shapirob5573532011-07-12 18:22:59 -0700105 enum State {
106 kUnknown = -1,
107 kNew,
108 kRunnable,
109 kBlocked,
110 kWaiting,
111 kTimedWaiting,
Ian Rogersb033c752011-07-20 12:22:35 -0700112 kNative,
Carl Shapirob5573532011-07-12 18:22:59 -0700113 kTerminated,
114 };
115
Carl Shapiro61e019d2011-07-14 16:53:09 -0700116 static const size_t kDefaultStackSize = 64 * KB;
117
118 // Creates a new thread.
Brian Carlstromb765be02011-08-17 23:54:10 -0700119 static Thread* Create(const Runtime* runtime);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700120
121 // Creates a new thread from the calling thread.
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700122 static Thread* Attach(const Runtime* runtime);
Carl Shapirob5573532011-07-12 18:22:59 -0700123
124 static Thread* Current() {
Carl Shapirod0e7e772011-07-15 14:31:01 -0700125 void* thread = pthread_getspecific(Thread::pthread_key_self_);
126 return reinterpret_cast<Thread*>(thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700127 }
128
Carl Shapirob5573532011-07-12 18:22:59 -0700129 uint32_t GetId() const {
130 return id_;
131 }
132
133 pid_t GetNativeId() const {
134 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700135 }
136
137 bool IsExceptionPending() const {
Elliott Hughesb20a5542011-08-12 18:03:12 -0700138 return exception_ != NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700139 }
140
141 Object* GetException() const {
142 return exception_;
143 }
144
145 void SetException(Object* new_exception) {
146 CHECK(new_exception != NULL);
147 // TODO: CHECK(exception_ == NULL);
148 exception_ = new_exception; // TODO
149 }
150
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700151 void ThrowNewException(const char* exception_class_name, const char* fmt, ...)
152 __attribute__ ((format(printf, 3, 4)));
153
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700154 void ClearException() {
155 exception_ = NULL;
156 }
157
Ian Rogers45a76cb2011-07-21 22:00:15 -0700158 // Offset of exception within Thread, used by generated code
159 static ThreadOffset ExceptionOffset() {
160 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_));
161 }
162
Carl Shapirob5573532011-07-12 18:22:59 -0700163 void SetName(const char* name);
164
165 void Suspend();
166
167 bool IsSuspended();
168
169 void Resume();
170
171 static bool Init();
172
Carl Shapiro69759ea2011-07-21 18:13:35 -0700173 Runtime* GetRuntime() const {
174 return runtime_;
175 }
176
Elliott Hughes330304d2011-08-12 14:28:05 -0700177 State GetState() const {
Carl Shapirob5573532011-07-12 18:22:59 -0700178 return state_;
179 }
180
181 void SetState(State new_state) {
182 state_ = new_state;
183 }
184
Ian Rogers45a76cb2011-07-21 22:00:15 -0700185 static ThreadOffset SuspendCountOffset() {
186 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_));
187 }
188
Ian Rogersb033c752011-07-20 12:22:35 -0700189 // Offset of state within Thread, used by generated code
190 static ThreadOffset StateOffset() {
191 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
192 }
193
Ian Rogersb033c752011-07-20 12:22:35 -0700194 // JNI methods
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700195 JNIEnv* GetJniEnv() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700196 return jni_env_;
197 }
198
199 // Offset of JNI environment within Thread, used by generated code
200 static ThreadOffset JniEnvOffset() {
201 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
202 }
203
Ian Rogers45a76cb2011-07-21 22:00:15 -0700204 // Offset of top of managed stack address, used by generated code
205 static ThreadOffset TopOfManagedStackOffset() {
206 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_));
207 }
208
Ian Rogersb033c752011-07-20 12:22:35 -0700209 // Offset of top stack handle block within Thread, used by generated code
210 static ThreadOffset TopShbOffset() {
211 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
212 }
213
214 // Number of references allocated in StackHandleBlocks on this thread
215 size_t NumShbHandles() {
216 size_t count = 0;
217 for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) {
218 count += cur->NumberOfReferences();
219 }
220 return count;
221 }
222
Ian Rogers45a76cb2011-07-21 22:00:15 -0700223 // Offset of exception_entry_point_ within Thread, used by generated code
224 static ThreadOffset ExceptionEntryPointOffset() {
225 return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_));
226 }
227
228 void RegisterExceptionEntryPoint(void (*handler)(Method**)) {
229 exception_entry_point_ = handler;
230 }
231
232 // Offset of suspend_count_entry_point_ within Thread, used by generated code
233 static ThreadOffset SuspendCountEntryPointOffset() {
234 return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_));
235 }
236
237 void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) {
238 suspend_count_entry_point_ = handler;
239 }
240
241 // Increasing the suspend count, will cause the thread to run to safepoint
242 void IncrementSuspendCount() { suspend_count_++; }
243 void DecrementSuspendCount() { suspend_count_--; }
244
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700245 private:
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700246 Thread()
Elliott Hughes330304d2011-08-12 14:28:05 -0700247 : id_(1234),
248 top_shb_(NULL),
249 jni_env_(NULL),
250 exception_(NULL),
251 suspend_count_(0) {
Ian Rogersb033c752011-07-20 12:22:35 -0700252 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700253
Ian Rogersdf20fe02011-07-20 20:34:16 -0700254 ~Thread() {
255 delete jni_env_;
256 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700257
Ian Rogersb033c752011-07-20 12:22:35 -0700258 void InitCpu();
259
Carl Shapiro69759ea2011-07-21 18:13:35 -0700260 // Managed thread id.
261 uint32_t id_;
Ian Rogersb033c752011-07-20 12:22:35 -0700262
Ian Rogers45a76cb2011-07-21 22:00:15 -0700263 // Top of the managed stack, written out prior to the state transition from
264 // kRunnable to kNative. Uses include to give the starting point for scanning
265 // a managed stack when a thread is in native code.
266 void* top_of_managed_stack_;
267
Ian Rogersb033c752011-07-20 12:22:35 -0700268 // Top of linked list of stack handle blocks or NULL for none
269 StackHandleBlock* top_shb_;
270
271 // Every thread may have an associated JNI environment
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700272 JNIEnv* jni_env_;
Ian Rogersb033c752011-07-20 12:22:35 -0700273
Carl Shapirob5573532011-07-12 18:22:59 -0700274 State state_;
275
Carl Shapiro69759ea2011-07-21 18:13:35 -0700276 // Native (kernel) thread id.
Carl Shapirob5573532011-07-12 18:22:59 -0700277 pid_t native_id_;
278
Carl Shapiro69759ea2011-07-21 18:13:35 -0700279 // Native thread handle.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700280 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700281
Carl Shapiro69759ea2011-07-21 18:13:35 -0700282 // Initialized to "this". On certain architectures (such as x86) reading
283 // off of Thread::Current is easy but getting the address of Thread::Current
284 // is hard. This field can be read off of Thread::Current to give the address.
285 Thread* self_;
286
287 Runtime* runtime_;
288
289 // The pending exception or NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700290 Object* exception_;
291
Ian Rogers45a76cb2011-07-21 22:00:15 -0700292 // A non-zero value is used to tell the current thread to enter a safe point
293 // at the next poll.
294 int suspend_count_;
295
Brian Carlstromb765be02011-08-17 23:54:10 -0700296 // The memory mapping of the stack for non-attached threads.
297 scoped_ptr<MemMap> stack_;
298
Carl Shapiro69759ea2011-07-21 18:13:35 -0700299 // The inclusive base of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700300 byte* stack_base_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700301
302 // The exclusive limit of the control stack.
Carl Shapiro61e019d2011-07-14 16:53:09 -0700303 byte* stack_limit_;
304
Carl Shapiro69759ea2011-07-21 18:13:35 -0700305 // TLS key used to retrieve the VM thread object.
Carl Shapirob5573532011-07-12 18:22:59 -0700306 static pthread_key_t pthread_key_self_;
307
Ian Rogers45a76cb2011-07-21 22:00:15 -0700308 // Entry point called when exception_ is set
309 void (*exception_entry_point_)(Method** frame);
310
311 // Entry point called when suspend_count_ is non-zero
312 void (*suspend_count_entry_point_)(Method** frame);
313
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700314 DISALLOW_COPY_AND_ASSIGN(Thread);
315};
Elliott Hughes330304d2011-08-12 14:28:05 -0700316std::ostream& operator<<(std::ostream& os, const Thread& thread);
Ian Rogersb033c752011-07-20 12:22:35 -0700317std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700318
Carl Shapirob5573532011-07-12 18:22:59 -0700319class ThreadList {
320 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700321 static const int kMaxId = 0xFFFF;
322 static const int kInvalidId = 0;
323 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700324
Carl Shapiro61e019d2011-07-14 16:53:09 -0700325 static ThreadList* Create();
326
327 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700328
329 void Register(Thread* thread);
330
331 void Unregister(Thread* thread);
332
Carl Shapirob5573532011-07-12 18:22:59 -0700333 void Lock() {
334 lock_->Lock();
335 }
336
337 void Unlock() {
338 lock_->Unlock();
339 };
340
341 private:
342 ThreadList();
343
344 std::list<Thread*> list_;
345
346 Mutex* lock_;
347
348 DISALLOW_COPY_AND_ASSIGN(ThreadList);
349};
350
351class ThreadListLock {
352 public:
353 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
354 : thread_list_(thread_list) {
355 if (current_thread == NULL) { // try to get it from TLS
356 current_thread = Thread::Current();
357 }
358 Thread::State old_state;
359 if (current_thread != NULL) {
360 old_state = current_thread->GetState();
361 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
362 } else {
363 // happens during VM shutdown
364 old_state = Thread::kUnknown; // TODO: something else
365 }
366 thread_list_->Lock();
367 if (current_thread != NULL) {
368 current_thread->SetState(old_state);
369 }
370 }
371
372 ~ThreadListLock() {
373 thread_list_->Unlock();
374 }
375
Carl Shapirob5573532011-07-12 18:22:59 -0700376 private:
377 ThreadList* thread_list_;
378
379 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
380};
381
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700382} // namespace art
383
384#endif // ART_SRC_THREAD_H_