Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // 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 Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 7 | #include <pthread.h> |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 8 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 9 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "globals.h" |
| 11 | #include "jni_internal.h" |
| 12 | #include "logging.h" |
| 13 | #include "macros.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 14 | #include "offsets.h" |
| 15 | #include "runtime.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 17 | #include "jni.h" |
| 18 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 19 | namespace art { |
| 20 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 21 | class Method; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 22 | class Object; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 23 | class Runtime; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 24 | class StackHandleBlock; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | class Thread; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 26 | class ThreadList; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 27 | |
| 28 | class Mutex { |
| 29 | public: |
| 30 | virtual ~Mutex() {} |
| 31 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 32 | void Lock(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 33 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 34 | bool TryLock(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 35 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 36 | void Unlock(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 37 | |
| 38 | const char* GetName() { return name_; } |
| 39 | |
| 40 | Thread* GetOwner() { return owner_; } |
| 41 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 42 | static Mutex* Create(const char* name); |
| 43 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 44 | 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 Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 54 | pthread_mutex_t lock_impl_; |
| 55 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 56 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 57 | }; |
| 58 | |
| 59 | class 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 Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 70 | // Stack handle blocks are allocated within the bridge frame between managed |
| 71 | // and native code. |
| 72 | class 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 Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 103 | class Thread { |
| 104 | public: |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 105 | enum State { |
| 106 | kUnknown = -1, |
| 107 | kNew, |
| 108 | kRunnable, |
| 109 | kBlocked, |
| 110 | kWaiting, |
| 111 | kTimedWaiting, |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 112 | kNative, |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 113 | kTerminated, |
| 114 | }; |
| 115 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 116 | static const size_t kDefaultStackSize = 64 * KB; |
| 117 | |
| 118 | // Creates a new thread. |
| 119 | static Thread* Create(size_t stack_size); |
| 120 | |
| 121 | // Creates a new thread from the calling thread. |
| 122 | static Thread* Attach(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 123 | |
| 124 | static Thread* Current() { |
Carl Shapiro | d0e7e77 | 2011-07-15 14:31:01 -0700 | [diff] [blame] | 125 | void* thread = pthread_getspecific(Thread::pthread_key_self_); |
| 126 | return reinterpret_cast<Thread*>(thread); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 129 | uint32_t GetId() const { |
| 130 | return id_; |
| 131 | } |
| 132 | |
| 133 | pid_t GetNativeId() const { |
| 134 | return native_id_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool IsExceptionPending() const { |
Elliott Hughes | b20a554 | 2011-08-12 18:03:12 -0700 | [diff] [blame^] | 138 | return exception_ != NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 139 | } |
| 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 | |
| 151 | void ClearException() { |
| 152 | exception_ = NULL; |
| 153 | } |
| 154 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 155 | // Offset of exception within Thread, used by generated code |
| 156 | static ThreadOffset ExceptionOffset() { |
| 157 | return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_)); |
| 158 | } |
| 159 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 160 | void SetName(const char* name); |
| 161 | |
| 162 | void Suspend(); |
| 163 | |
| 164 | bool IsSuspended(); |
| 165 | |
| 166 | void Resume(); |
| 167 | |
| 168 | static bool Init(); |
| 169 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 170 | Runtime* GetRuntime() const { |
| 171 | return runtime_; |
| 172 | } |
| 173 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 174 | State GetState() const { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 175 | return state_; |
| 176 | } |
| 177 | |
| 178 | void SetState(State new_state) { |
| 179 | state_ = new_state; |
| 180 | } |
| 181 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 182 | static ThreadOffset SuspendCountOffset() { |
| 183 | return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_)); |
| 184 | } |
| 185 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 186 | // Offset of state within Thread, used by generated code |
| 187 | static ThreadOffset StateOffset() { |
| 188 | return ThreadOffset(OFFSETOF_MEMBER(Thread, state_)); |
| 189 | } |
| 190 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 191 | // JNI methods |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 192 | JNIEnv* GetJniEnv() const { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 193 | return jni_env_; |
| 194 | } |
| 195 | |
| 196 | // Offset of JNI environment within Thread, used by generated code |
| 197 | static ThreadOffset JniEnvOffset() { |
| 198 | return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_)); |
| 199 | } |
| 200 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 201 | // Offset of top of managed stack address, used by generated code |
| 202 | static ThreadOffset TopOfManagedStackOffset() { |
| 203 | return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_)); |
| 204 | } |
| 205 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 206 | // Offset of top stack handle block within Thread, used by generated code |
| 207 | static ThreadOffset TopShbOffset() { |
| 208 | return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_)); |
| 209 | } |
| 210 | |
| 211 | // Number of references allocated in StackHandleBlocks on this thread |
| 212 | size_t NumShbHandles() { |
| 213 | size_t count = 0; |
| 214 | for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) { |
| 215 | count += cur->NumberOfReferences(); |
| 216 | } |
| 217 | return count; |
| 218 | } |
| 219 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 220 | // Offset of exception_entry_point_ within Thread, used by generated code |
| 221 | static ThreadOffset ExceptionEntryPointOffset() { |
| 222 | return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_)); |
| 223 | } |
| 224 | |
| 225 | void RegisterExceptionEntryPoint(void (*handler)(Method**)) { |
| 226 | exception_entry_point_ = handler; |
| 227 | } |
| 228 | |
| 229 | // Offset of suspend_count_entry_point_ within Thread, used by generated code |
| 230 | static ThreadOffset SuspendCountEntryPointOffset() { |
| 231 | return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_)); |
| 232 | } |
| 233 | |
| 234 | void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) { |
| 235 | suspend_count_entry_point_ = handler; |
| 236 | } |
| 237 | |
| 238 | // Increasing the suspend count, will cause the thread to run to safepoint |
| 239 | void IncrementSuspendCount() { suspend_count_++; } |
| 240 | void DecrementSuspendCount() { suspend_count_--; } |
| 241 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 242 | private: |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 243 | Thread() |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 244 | : id_(1234), |
| 245 | top_shb_(NULL), |
| 246 | jni_env_(NULL), |
| 247 | exception_(NULL), |
| 248 | suspend_count_(0) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 249 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 250 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 251 | ~Thread() { |
| 252 | delete jni_env_; |
| 253 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 254 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 255 | void InitCpu(); |
| 256 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 257 | // Managed thread id. |
| 258 | uint32_t id_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 259 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 260 | // Top of the managed stack, written out prior to the state transition from |
| 261 | // kRunnable to kNative. Uses include to give the starting point for scanning |
| 262 | // a managed stack when a thread is in native code. |
| 263 | void* top_of_managed_stack_; |
| 264 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 265 | // Top of linked list of stack handle blocks or NULL for none |
| 266 | StackHandleBlock* top_shb_; |
| 267 | |
| 268 | // Every thread may have an associated JNI environment |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 269 | JNIEnv* jni_env_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 270 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 271 | State state_; |
| 272 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 273 | // Native (kernel) thread id. |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 274 | pid_t native_id_; |
| 275 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 276 | // Native thread handle. |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 277 | pthread_t handle_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 278 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 279 | // Initialized to "this". On certain architectures (such as x86) reading |
| 280 | // off of Thread::Current is easy but getting the address of Thread::Current |
| 281 | // is hard. This field can be read off of Thread::Current to give the address. |
| 282 | Thread* self_; |
| 283 | |
| 284 | Runtime* runtime_; |
| 285 | |
| 286 | // The pending exception or NULL. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 287 | Object* exception_; |
| 288 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 289 | // A non-zero value is used to tell the current thread to enter a safe point |
| 290 | // at the next poll. |
| 291 | int suspend_count_; |
| 292 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 293 | // The inclusive base of the control stack. |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 294 | byte* stack_base_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 295 | |
| 296 | // The exclusive limit of the control stack. |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 297 | byte* stack_limit_; |
| 298 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 299 | // TLS key used to retrieve the VM thread object. |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 300 | static pthread_key_t pthread_key_self_; |
| 301 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 302 | // Entry point called when exception_ is set |
| 303 | void (*exception_entry_point_)(Method** frame); |
| 304 | |
| 305 | // Entry point called when suspend_count_ is non-zero |
| 306 | void (*suspend_count_entry_point_)(Method** frame); |
| 307 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 308 | DISALLOW_COPY_AND_ASSIGN(Thread); |
| 309 | }; |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 310 | std::ostream& operator<<(std::ostream& os, const Thread& thread); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 311 | std::ostream& operator<<(std::ostream& os, const Thread::State& state); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 312 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 313 | class ThreadList { |
| 314 | public: |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 315 | static const int kMaxId = 0xFFFF; |
| 316 | static const int kInvalidId = 0; |
| 317 | static const int kMainId = 1; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 318 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 319 | static ThreadList* Create(); |
| 320 | |
| 321 | ~ThreadList(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 322 | |
| 323 | void Register(Thread* thread); |
| 324 | |
| 325 | void Unregister(Thread* thread); |
| 326 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 327 | void Lock() { |
| 328 | lock_->Lock(); |
| 329 | } |
| 330 | |
| 331 | void Unlock() { |
| 332 | lock_->Unlock(); |
| 333 | }; |
| 334 | |
| 335 | private: |
| 336 | ThreadList(); |
| 337 | |
| 338 | std::list<Thread*> list_; |
| 339 | |
| 340 | Mutex* lock_; |
| 341 | |
| 342 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 343 | }; |
| 344 | |
| 345 | class ThreadListLock { |
| 346 | public: |
| 347 | ThreadListLock(ThreadList* thread_list, Thread* current_thread) |
| 348 | : thread_list_(thread_list) { |
| 349 | if (current_thread == NULL) { // try to get it from TLS |
| 350 | current_thread = Thread::Current(); |
| 351 | } |
| 352 | Thread::State old_state; |
| 353 | if (current_thread != NULL) { |
| 354 | old_state = current_thread->GetState(); |
| 355 | current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT |
| 356 | } else { |
| 357 | // happens during VM shutdown |
| 358 | old_state = Thread::kUnknown; // TODO: something else |
| 359 | } |
| 360 | thread_list_->Lock(); |
| 361 | if (current_thread != NULL) { |
| 362 | current_thread->SetState(old_state); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | ~ThreadListLock() { |
| 367 | thread_list_->Unlock(); |
| 368 | } |
| 369 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 370 | private: |
| 371 | ThreadList* thread_list_; |
| 372 | |
| 373 | DISALLOW_COPY_AND_ASSIGN(ThreadListLock); |
| 374 | }; |
| 375 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 376 | } // namespace art |
| 377 | |
| 378 | #endif // ART_SRC_THREAD_H_ |