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