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