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