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