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