Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2 | |
| 3 | #ifndef ART_SRC_THREAD_H_ |
| 4 | #define ART_SRC_THREAD_H_ |
| 5 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 6 | #include <pthread.h> |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 7 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 8 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "globals.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "logging.h" |
| 11 | #include "macros.h" |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 12 | #include "mem_map.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "offsets.h" |
| 14 | #include "runtime.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 15 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 16 | #include "jni.h" |
| 17 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | namespace art { |
| 19 | |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 20 | class Class; |
Elliott Hughes | edcc09c | 2011-08-21 18:47:05 -0700 | [diff] [blame] | 21 | class ClassLoader; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 22 | class Method; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 23 | class Object; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 24 | class Runtime; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 25 | class StackHandleBlock; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 26 | class Thread; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 27 | class ThreadList; |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 28 | class Throwable; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 29 | |
| 30 | class Mutex { |
| 31 | public: |
| 32 | virtual ~Mutex() {} |
| 33 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 34 | void Lock(); |
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 | bool TryLock(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 37 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 38 | void Unlock(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 39 | |
| 40 | const char* GetName() { return name_; } |
| 41 | |
| 42 | Thread* GetOwner() { return owner_; } |
| 43 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 44 | static Mutex* Create(const char* name); |
| 45 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 46 | public: // TODO: protected |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 47 | void SetOwner(Thread* thread) { owner_ = thread; } |
| 48 | |
| 49 | private: |
Elliott Hughes | 18c0753 | 2011-08-18 15:50:51 -0700 | [diff] [blame] | 50 | explicit Mutex(const char* name) : name_(name), owner_(NULL) {} |
| 51 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 52 | const char* name_; |
| 53 | |
| 54 | Thread* owner_; |
| 55 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 56 | pthread_mutex_t lock_impl_; |
| 57 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame^] | 58 | friend class SharedLibrary; // For lock_impl_. |
| 59 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 60 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 61 | }; |
| 62 | |
| 63 | class MutexLock { |
| 64 | public: |
| 65 | explicit MutexLock(Mutex *mu) : mu_(mu) { |
| 66 | mu_->Lock(); |
| 67 | } |
| 68 | ~MutexLock() { mu_->Unlock(); } |
| 69 | private: |
| 70 | Mutex* const mu_; |
| 71 | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
| 72 | }; |
| 73 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 74 | // Stack handle blocks are allocated within the bridge frame between managed |
| 75 | // and native code. |
| 76 | class StackHandleBlock { |
| 77 | public: |
| 78 | // Number of references contained within this SHB |
| 79 | size_t NumberOfReferences() { |
| 80 | return number_of_references_; |
| 81 | } |
| 82 | |
| 83 | // Link to previous SHB or NULL |
| 84 | StackHandleBlock* Link() { |
| 85 | return link_; |
| 86 | } |
| 87 | |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 88 | Object** Handles() { |
| 89 | return handles_; |
| 90 | } |
| 91 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 92 | // Offset of length within SHB, used by generated code |
| 93 | static size_t NumberOfReferencesOffset() { |
| 94 | return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_); |
| 95 | } |
| 96 | |
| 97 | // Offset of link within SHB, used by generated code |
| 98 | static size_t LinkOffset() { |
| 99 | return OFFSETOF_MEMBER(StackHandleBlock, link_); |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | StackHandleBlock() {} |
| 104 | |
| 105 | size_t number_of_references_; |
| 106 | StackHandleBlock* link_; |
| 107 | |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 108 | // Fake array, really allocated and filled in by jni_compiler. |
| 109 | Object* handles_[0]; |
| 110 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 111 | DISALLOW_COPY_AND_ASSIGN(StackHandleBlock); |
| 112 | }; |
| 113 | |
Ian Rogers | 6de0860 | 2011-08-19 14:52:39 -0700 | [diff] [blame] | 114 | struct NativeToManagedRecord { |
| 115 | NativeToManagedRecord* link; |
| 116 | void* last_top_of_managed_stack; |
| 117 | }; |
| 118 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 119 | // Iterator over managed frames up to the first native-to-managed transition |
| 120 | class Frame { |
| 121 | Frame() : sp_(NULL) {} |
| 122 | |
| 123 | const Method* GetMethod() const { |
| 124 | return *sp_; |
| 125 | } |
| 126 | |
| 127 | bool HasNext() const { |
| 128 | return NextMethod() != NULL; |
| 129 | } |
| 130 | |
| 131 | void Next(); |
| 132 | |
| 133 | void* GetPC() const; |
| 134 | |
| 135 | const Method** GetSP() const { |
| 136 | return sp_; |
| 137 | } |
| 138 | |
| 139 | // TODO: this is here for testing, remove when we have exception unit tests |
| 140 | // that use the real stack |
| 141 | void SetSP(const Method** sp) { |
| 142 | sp_ = sp; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | const Method* NextMethod() const; |
| 147 | |
| 148 | friend class Thread; |
| 149 | |
| 150 | const Method** sp_; |
| 151 | }; |
| 152 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 153 | class Thread { |
| 154 | public: |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 155 | enum State { |
| 156 | kUnknown = -1, |
| 157 | kNew, |
| 158 | kRunnable, |
| 159 | kBlocked, |
| 160 | kWaiting, |
| 161 | kTimedWaiting, |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 162 | kNative, |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 163 | kTerminated, |
| 164 | }; |
| 165 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 166 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 167 | static const size_t kDefaultStackSize = 64 * KB; |
| 168 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 169 | // TODO - needs to be redone properly, just hacked into place for now |
| 170 | // Runtime support function pointers |
| 171 | void* (*pMemcpy)(void*, const void*, size_t); |
| 172 | float (*pI2f)(int); |
| 173 | int (*pF2iz)(float); |
| 174 | float (*pD2f)(double); |
| 175 | double (*pF2d)(float); |
| 176 | double (*pI2d)(int); |
| 177 | int (*pD2iz)(double); |
| 178 | float (*pL2f)(long); |
| 179 | double (*pL2d)(long); |
| 180 | long long (*pArtF2l)(float); |
| 181 | long long (*pArtD2l)(double); |
| 182 | float (*pFadd)(float, float); |
| 183 | float (*pFsub)(float, float); |
| 184 | float (*pFdiv)(float, float); |
| 185 | float (*pFmul)(float, float); |
| 186 | float (*pFmodf)(float, float); |
| 187 | double (*pDadd)(double, double); |
| 188 | double (*pDsub)(double, double); |
| 189 | double (*pDdiv)(double, double); |
| 190 | double (*pDmul)(double, double); |
| 191 | double (*pFmod)(double, double); |
| 192 | int (*pIdivmod)(int, int); |
| 193 | int (*pIdiv)(int, int); |
| 194 | long long (*pLdivmod)(long long, long long); |
| 195 | bool (*pArtUnlockObject)(struct Thread*, struct Object*); |
| 196 | bool (*pArtCanPutArrayElementNoThrow)(const struct ClassObject*, |
| 197 | const struct ClassObject*); |
| 198 | int (*pArtInstanceofNonTrivialNoThrow) |
| 199 | (const struct ClassObject*, const struct ClassObject*); |
| 200 | int (*pArtInstanceofNonTrivial) (const struct ClassObject*, |
| 201 | const struct ClassObject*); |
| 202 | struct ArrayObject* (*pArtAllocArrayByClass)(struct ClassObject*, |
| 203 | size_t, int); |
| 204 | struct Method* (*pArtFindInterfaceMethodInCache)(ClassObject*, uint32_t, |
| 205 | const struct Method*, struct DvmDex*); |
| 206 | bool (*pArtUnlockObjectNoThrow)(struct Thread*, struct Object*); |
| 207 | void (*pArtLockObjectNoThrow)(struct Thread*, struct Object*); |
| 208 | struct Object* (*pArtAllocObjectNoThrow)(struct ClassObject*, int); |
| 209 | void (*pArtThrowException)(struct Thread*, struct Object*); |
| 210 | bool (*pArtHandleFillArrayDataNoThrow)(struct ArrayObject*, const uint16_t*); |
| 211 | |
| 212 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 213 | // Creates a new thread. |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 214 | static Thread* Create(const Runtime* runtime); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 215 | |
| 216 | // Creates a new thread from the calling thread. |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 217 | static Thread* Attach(const Runtime* runtime); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 218 | |
| 219 | static Thread* Current() { |
Carl Shapiro | d0e7e77 | 2011-07-15 14:31:01 -0700 | [diff] [blame] | 220 | void* thread = pthread_getspecific(Thread::pthread_key_self_); |
| 221 | return reinterpret_cast<Thread*>(thread); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 224 | uint32_t GetId() const { |
| 225 | return id_; |
| 226 | } |
| 227 | |
| 228 | pid_t GetNativeId() const { |
| 229 | return native_id_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | bool IsExceptionPending() const { |
Elliott Hughes | b20a554 | 2011-08-12 18:03:12 -0700 | [diff] [blame] | 233 | return exception_ != NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 236 | Throwable* GetException() const { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 237 | return exception_; |
| 238 | } |
| 239 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 240 | Frame GetTopOfStack() const { |
| 241 | return top_of_managed_stack_; |
| 242 | } |
| 243 | |
| 244 | // TODO: this is here for testing, remove when we have exception unit tests |
| 245 | // that use the real stack |
| 246 | void SetTopOfStack(void* stack) { |
| 247 | top_of_managed_stack_.SetSP(reinterpret_cast<const Method**>(stack)); |
| 248 | } |
| 249 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 250 | void SetException(Throwable* new_exception) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 251 | CHECK(new_exception != NULL); |
| 252 | // TODO: CHECK(exception_ == NULL); |
| 253 | exception_ = new_exception; // TODO |
| 254 | } |
| 255 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 256 | void ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...) |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 257 | __attribute__ ((format(printf, 3, 4))); |
| 258 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 259 | void ClearException() { |
| 260 | exception_ = NULL; |
| 261 | } |
| 262 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 263 | Frame FindExceptionHandler(void* throw_pc, void** handler_pc); |
| 264 | |
| 265 | void* FindExceptionHandlerInMethod(const Method* method, |
| 266 | void* throw_pc, |
| 267 | const DexFile& dex_file, |
| 268 | ClassLinker* class_linker); |
| 269 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 270 | // Offset of exception within Thread, used by generated code |
| 271 | static ThreadOffset ExceptionOffset() { |
| 272 | return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_)); |
| 273 | } |
| 274 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 275 | // Offset of id within Thread, used by generated code |
| 276 | static ThreadOffset IdOffset() { |
| 277 | return ThreadOffset(OFFSETOF_MEMBER(Thread, id_)); |
| 278 | } |
| 279 | |
| 280 | // Offset of card_table within Thread, used by generated code |
| 281 | static ThreadOffset CardTableOffset() { |
| 282 | return ThreadOffset(OFFSETOF_MEMBER(Thread, card_table_)); |
| 283 | } |
| 284 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 285 | void SetName(const char* name); |
| 286 | |
| 287 | void Suspend(); |
| 288 | |
| 289 | bool IsSuspended(); |
| 290 | |
| 291 | void Resume(); |
| 292 | |
| 293 | static bool Init(); |
| 294 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 295 | State GetState() const { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 296 | return state_; |
| 297 | } |
| 298 | |
| 299 | void SetState(State new_state) { |
| 300 | state_ = new_state; |
| 301 | } |
| 302 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 303 | static ThreadOffset SuspendCountOffset() { |
| 304 | return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_)); |
| 305 | } |
| 306 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 307 | // Offset of state within Thread, used by generated code |
| 308 | static ThreadOffset StateOffset() { |
| 309 | return ThreadOffset(OFFSETOF_MEMBER(Thread, state_)); |
| 310 | } |
| 311 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 312 | // JNI methods |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 313 | JNIEnv* GetJniEnv() const { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 314 | return jni_env_; |
| 315 | } |
| 316 | |
| 317 | // Offset of JNI environment within Thread, used by generated code |
| 318 | static ThreadOffset JniEnvOffset() { |
| 319 | return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_)); |
| 320 | } |
| 321 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 322 | // Offset of top of managed stack address, used by generated code |
| 323 | static ThreadOffset TopOfManagedStackOffset() { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 324 | return ThreadOffset(OFFSETOF_MEMBER(Thread, top_of_managed_stack_) + |
| 325 | OFFSETOF_MEMBER(Frame, sp_)); |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 328 | // Offset of top stack handle block within Thread, used by generated code |
| 329 | static ThreadOffset TopShbOffset() { |
| 330 | return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_)); |
| 331 | } |
| 332 | |
| 333 | // Number of references allocated in StackHandleBlocks on this thread |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 334 | size_t NumShbHandles(); |
| 335 | |
| 336 | // Is the given obj in this thread's stack handle blocks? |
| 337 | bool ShbContains(jobject obj); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 338 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 339 | // Offset of exception_entry_point_ within Thread, used by generated code |
| 340 | static ThreadOffset ExceptionEntryPointOffset() { |
| 341 | return ThreadOffset(OFFSETOF_MEMBER(Thread, exception_entry_point_)); |
| 342 | } |
| 343 | |
| 344 | void RegisterExceptionEntryPoint(void (*handler)(Method**)) { |
| 345 | exception_entry_point_ = handler; |
| 346 | } |
| 347 | |
| 348 | // Offset of suspend_count_entry_point_ within Thread, used by generated code |
| 349 | static ThreadOffset SuspendCountEntryPointOffset() { |
| 350 | return ThreadOffset(OFFSETOF_MEMBER(Thread, suspend_count_entry_point_)); |
| 351 | } |
| 352 | |
| 353 | void RegisterSuspendCountEntryPoint(void (*handler)(Method**)) { |
| 354 | suspend_count_entry_point_ = handler; |
| 355 | } |
| 356 | |
| 357 | // Increasing the suspend count, will cause the thread to run to safepoint |
| 358 | void IncrementSuspendCount() { suspend_count_++; } |
| 359 | void DecrementSuspendCount() { suspend_count_--; } |
| 360 | |
Ian Rogers | 6de0860 | 2011-08-19 14:52:39 -0700 | [diff] [blame] | 361 | // Linked list recording transitions from native to managed code |
| 362 | void PushNativeToManagedRecord(NativeToManagedRecord* record) { |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 363 | record->last_top_of_managed_stack = reinterpret_cast<void*>(top_of_managed_stack_.GetSP()); |
Ian Rogers | 6de0860 | 2011-08-19 14:52:39 -0700 | [diff] [blame] | 364 | record->link = native_to_managed_record_; |
| 365 | native_to_managed_record_ = record; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 366 | top_of_managed_stack_.SetSP(NULL); |
Ian Rogers | 6de0860 | 2011-08-19 14:52:39 -0700 | [diff] [blame] | 367 | } |
| 368 | void PopNativeToManagedRecord(const NativeToManagedRecord& record) { |
| 369 | native_to_managed_record_ = record.link; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 370 | top_of_managed_stack_.SetSP( reinterpret_cast<const Method**>(record.last_top_of_managed_stack) ); |
Ian Rogers | 6de0860 | 2011-08-19 14:52:39 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Elliott Hughes | edcc09c | 2011-08-21 18:47:05 -0700 | [diff] [blame] | 373 | ClassLoader* GetClassLoaderOverride() { |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 374 | return class_loader_override_; |
| 375 | } |
| 376 | |
Elliott Hughes | edcc09c | 2011-08-21 18:47:05 -0700 | [diff] [blame] | 377 | void SetClassLoaderOverride(ClassLoader* class_loader_override) { |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 378 | class_loader_override_ = class_loader_override; |
| 379 | } |
| 380 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 381 | private: |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 382 | Thread() |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 383 | : id_(1234), |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 384 | top_of_managed_stack_(), |
Ian Rogers | 6de0860 | 2011-08-19 14:52:39 -0700 | [diff] [blame] | 385 | native_to_managed_record_(NULL), |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 386 | top_shb_(NULL), |
| 387 | jni_env_(NULL), |
| 388 | exception_(NULL), |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 389 | suspend_count_(0), |
| 390 | class_loader_override_(NULL) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 391 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 392 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 393 | ~Thread() { |
| 394 | delete jni_env_; |
| 395 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 396 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 397 | void InitCpu(); |
| 398 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 399 | // Managed thread id. |
| 400 | uint32_t id_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 401 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 402 | // FIXME: placeholder for the gc cardTable |
| 403 | uint32_t card_table_; |
| 404 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 405 | // Top of the managed stack, written out prior to the state transition from |
| 406 | // kRunnable to kNative. Uses include to give the starting point for scanning |
| 407 | // a managed stack when a thread is in native code. |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 408 | Frame top_of_managed_stack_; |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 409 | |
Ian Rogers | 6de0860 | 2011-08-19 14:52:39 -0700 | [diff] [blame] | 410 | // A linked list (of stack allocated records) recording transitions from |
| 411 | // native to managed code. |
| 412 | NativeToManagedRecord* native_to_managed_record_; |
| 413 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 414 | // Top of linked list of stack handle blocks or NULL for none |
| 415 | StackHandleBlock* top_shb_; |
| 416 | |
| 417 | // Every thread may have an associated JNI environment |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 418 | JNIEnv* jni_env_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 419 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 420 | State state_; |
| 421 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 422 | // Native (kernel) thread id. |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 423 | pid_t native_id_; |
| 424 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 425 | // Native thread handle. |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 426 | pthread_t handle_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 427 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 428 | // Initialized to "this". On certain architectures (such as x86) reading |
| 429 | // off of Thread::Current is easy but getting the address of Thread::Current |
| 430 | // is hard. This field can be read off of Thread::Current to give the address. |
| 431 | Thread* self_; |
| 432 | |
| 433 | Runtime* runtime_; |
| 434 | |
| 435 | // The pending exception or NULL. |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 436 | Throwable* exception_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 437 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 438 | // A non-zero value is used to tell the current thread to enter a safe point |
| 439 | // at the next poll. |
| 440 | int suspend_count_; |
| 441 | |
Elliott Hughes | edcc09c | 2011-08-21 18:47:05 -0700 | [diff] [blame] | 442 | // Needed to get the right ClassLoader in JNI_OnLoad, but also |
| 443 | // useful for testing. |
| 444 | ClassLoader* class_loader_override_; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 445 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 446 | // The memory mapping of the stack for non-attached threads. |
| 447 | scoped_ptr<MemMap> stack_; |
| 448 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 449 | // The inclusive base of the control stack. |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 450 | byte* stack_base_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 451 | |
| 452 | // The exclusive limit of the control stack. |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 453 | byte* stack_limit_; |
| 454 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 455 | // TLS key used to retrieve the VM thread object. |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 456 | static pthread_key_t pthread_key_self_; |
| 457 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 458 | // Entry point called when exception_ is set |
| 459 | void (*exception_entry_point_)(Method** frame); |
| 460 | |
| 461 | // Entry point called when suspend_count_ is non-zero |
| 462 | void (*suspend_count_entry_point_)(Method** frame); |
| 463 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 464 | DISALLOW_COPY_AND_ASSIGN(Thread); |
| 465 | }; |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 466 | std::ostream& operator<<(std::ostream& os, const Thread& thread); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 467 | std::ostream& operator<<(std::ostream& os, const Thread::State& state); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 468 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 469 | class ThreadList { |
| 470 | public: |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 471 | static const int kMaxId = 0xFFFF; |
| 472 | static const int kInvalidId = 0; |
| 473 | static const int kMainId = 1; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 474 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 475 | static ThreadList* Create(); |
| 476 | |
| 477 | ~ThreadList(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 478 | |
| 479 | void Register(Thread* thread); |
| 480 | |
| 481 | void Unregister(Thread* thread); |
| 482 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 483 | void Lock() { |
| 484 | lock_->Lock(); |
| 485 | } |
| 486 | |
| 487 | void Unlock() { |
| 488 | lock_->Unlock(); |
| 489 | }; |
| 490 | |
| 491 | private: |
| 492 | ThreadList(); |
| 493 | |
| 494 | std::list<Thread*> list_; |
| 495 | |
| 496 | Mutex* lock_; |
| 497 | |
| 498 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 499 | }; |
| 500 | |
| 501 | class ThreadListLock { |
| 502 | public: |
| 503 | ThreadListLock(ThreadList* thread_list, Thread* current_thread) |
| 504 | : thread_list_(thread_list) { |
| 505 | if (current_thread == NULL) { // try to get it from TLS |
| 506 | current_thread = Thread::Current(); |
| 507 | } |
| 508 | Thread::State old_state; |
| 509 | if (current_thread != NULL) { |
| 510 | old_state = current_thread->GetState(); |
| 511 | current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT |
| 512 | } else { |
| 513 | // happens during VM shutdown |
| 514 | old_state = Thread::kUnknown; // TODO: something else |
| 515 | } |
| 516 | thread_list_->Lock(); |
| 517 | if (current_thread != NULL) { |
| 518 | current_thread->SetState(old_state); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | ~ThreadListLock() { |
| 523 | thread_list_->Unlock(); |
| 524 | } |
| 525 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 526 | private: |
| 527 | ThreadList* thread_list_; |
| 528 | |
| 529 | DISALLOW_COPY_AND_ASSIGN(ThreadListLock); |
| 530 | }; |
| 531 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 532 | } // namespace art |
| 533 | |
| 534 | #endif // ART_SRC_THREAD_H_ |