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