blob: ab0bacc55e491b2def9abaa1dc8e7ee6bd0394af [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: cshapiro@google.com (Carl Shapiro)
3
4#ifndef ART_SRC_THREAD_H_
5#define ART_SRC_THREAD_H_
6
Carl Shapirob5573532011-07-12 18:22:59 -07007#include <pthread.h>
Ian Rogersb033c752011-07-20 12:22:35 -07008#include <list>
Carl Shapirob5573532011-07-12 18:22:59 -07009
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070010#include "src/globals.h"
Ian Rogersb033c752011-07-20 12:22:35 -070011#include "src/heap.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070012#include "src/logging.h"
13#include "src/macros.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070014#include "src/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
Carl Shapiro61e019d2011-07-14 16:53:09 -070020class Heap;
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 {
137 return false; // TODO exception_ != NULL;
138 }
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
Carl Shapirob5573532011-07-12 18:22:59 -0700154 void SetName(const char* name);
155
156 void Suspend();
157
158 bool IsSuspended();
159
160 void Resume();
161
162 static bool Init();
163
Carl Shapirob5573532011-07-12 18:22:59 -0700164 State GetState() {
165 return state_;
166 }
167
168 void SetState(State new_state) {
169 state_ = new_state;
170 }
171
Ian Rogersb033c752011-07-20 12:22:35 -0700172 // Offset of state within Thread, used by generated code
173 static ThreadOffset StateOffset() {
174 return ThreadOffset(OFFSETOF_MEMBER(Thread, state_));
175 }
176
177 Heap* GetHeap() {
178 return heap_;
179 }
180
181 // JNI methods
182 JNIEnv* GetJniEnv() const {
183 return jni_env_;
184 }
185
186 // Offset of JNI environment within Thread, used by generated code
187 static ThreadOffset JniEnvOffset() {
188 return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_));
189 }
190
191 // Offset of top stack handle block within Thread, used by generated code
192 static ThreadOffset TopShbOffset() {
193 return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_));
194 }
195
196 // Number of references allocated in StackHandleBlocks on this thread
197 size_t NumShbHandles() {
198 size_t count = 0;
199 for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) {
200 count += cur->NumberOfReferences();
201 }
202 return count;
203 }
204
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700205 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700206 Thread() :
207 thread_id_(1234), top_shb_(NULL),
208 jni_env_(reinterpret_cast<JNIEnv*>(0xEBADC0DE)), exception_(NULL) {
209 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700210 ~Thread() {}
211
Ian Rogersb033c752011-07-20 12:22:35 -0700212 void InitCpu();
213
214 // Initialized to "this". On certain architectures (such as x86) reading
215 // off of Thread::Current is easy but getting the address of Thread::Current
216 // is hard. This field can be read off of Thread::Current to give the address.
217 Thread* self_;
218
219 uint32_t thread_id_;
220
221 Heap* heap_;
222
223 // Top of linked list of stack handle blocks or NULL for none
224 StackHandleBlock* top_shb_;
225
226 // Every thread may have an associated JNI environment
227 JNIEnv* jni_env_;
228
Carl Shapirob5573532011-07-12 18:22:59 -0700229 State state_;
230
231 uint32_t id_;
232
233 pid_t native_id_;
234
Carl Shapiro61e019d2011-07-14 16:53:09 -0700235 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236
237 Object* exception_;
238
Carl Shapiro61e019d2011-07-14 16:53:09 -0700239 byte* stack_base_;
240 byte* stack_limit_;
241
Carl Shapirob5573532011-07-12 18:22:59 -0700242 static pthread_key_t pthread_key_self_;
243
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700244 DISALLOW_COPY_AND_ASSIGN(Thread);
245};
Ian Rogersb033c752011-07-20 12:22:35 -0700246std::ostream& operator<<(std::ostream& os, const Thread::State& state);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700247
Carl Shapirob5573532011-07-12 18:22:59 -0700248class ThreadList {
249 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700250 static const int kMaxId = 0xFFFF;
251 static const int kInvalidId = 0;
252 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700253
Carl Shapiro61e019d2011-07-14 16:53:09 -0700254 static ThreadList* Create();
255
256 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700257
258 void Register(Thread* thread);
259
260 void Unregister(Thread* thread);
261
Carl Shapirob5573532011-07-12 18:22:59 -0700262 void Lock() {
263 lock_->Lock();
264 }
265
266 void Unlock() {
267 lock_->Unlock();
268 };
269
270 private:
271 ThreadList();
272
273 std::list<Thread*> list_;
274
275 Mutex* lock_;
276
277 DISALLOW_COPY_AND_ASSIGN(ThreadList);
278};
279
280class ThreadListLock {
281 public:
282 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
283 : thread_list_(thread_list) {
284 if (current_thread == NULL) { // try to get it from TLS
285 current_thread = Thread::Current();
286 }
287 Thread::State old_state;
288 if (current_thread != NULL) {
289 old_state = current_thread->GetState();
290 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
291 } else {
292 // happens during VM shutdown
293 old_state = Thread::kUnknown; // TODO: something else
294 }
295 thread_list_->Lock();
296 if (current_thread != NULL) {
297 current_thread->SetState(old_state);
298 }
299 }
300
301 ~ThreadListLock() {
302 thread_list_->Unlock();
303 }
304
Carl Shapirob5573532011-07-12 18:22:59 -0700305 private:
306 ThreadList* thread_list_;
307
308 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
309};
310
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700311} // namespace art
312
313#endif // ART_SRC_THREAD_H_