Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: cshapiro@google.com (Carl Shapiro) |
| 3 | |
| 4 | #ifndef ART_SRC_THREAD_H_ |
| 5 | #define ART_SRC_THREAD_H_ |
| 6 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 7 | #include <pthread.h> |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 8 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 9 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 10 | #include "src/globals.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 11 | #include "src/heap.h" |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame^] | 12 | #include "src/jni_internal.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 13 | #include "src/logging.h" |
| 14 | #include "src/macros.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 15 | #include "src/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 | #include "jni.h" |
| 18 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 19 | namespace art { |
| 20 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 21 | class Heap; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 22 | class Object; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 23 | class Runtime; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 24 | class StackHandleBlock; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | class Thread; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 26 | class ThreadList; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 27 | |
| 28 | class Mutex { |
| 29 | public: |
| 30 | virtual ~Mutex() {} |
| 31 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 32 | void Lock(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 33 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 34 | bool TryLock(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 35 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 36 | void Unlock(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 37 | |
| 38 | const char* GetName() { return name_; } |
| 39 | |
| 40 | Thread* GetOwner() { return owner_; } |
| 41 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 42 | static Mutex* Create(const char* name); |
| 43 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 44 | public: // TODO: protected |
| 45 | explicit Mutex(const char* name) : name_(name), owner_(NULL) {} |
| 46 | |
| 47 | void SetOwner(Thread* thread) { owner_ = thread; } |
| 48 | |
| 49 | private: |
| 50 | const char* name_; |
| 51 | |
| 52 | Thread* owner_; |
| 53 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 54 | pthread_mutex_t lock_impl_; |
| 55 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 56 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 57 | }; |
| 58 | |
| 59 | class MutexLock { |
| 60 | public: |
| 61 | explicit MutexLock(Mutex *mu) : mu_(mu) { |
| 62 | mu_->Lock(); |
| 63 | } |
| 64 | ~MutexLock() { mu_->Unlock(); } |
| 65 | private: |
| 66 | Mutex* const mu_; |
| 67 | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
| 68 | }; |
| 69 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 70 | // Stack handle blocks are allocated within the bridge frame between managed |
| 71 | // and native code. |
| 72 | class StackHandleBlock { |
| 73 | public: |
| 74 | // Number of references contained within this SHB |
| 75 | size_t NumberOfReferences() { |
| 76 | return number_of_references_; |
| 77 | } |
| 78 | |
| 79 | // Link to previous SHB or NULL |
| 80 | StackHandleBlock* Link() { |
| 81 | return link_; |
| 82 | } |
| 83 | |
| 84 | // Offset of length within SHB, used by generated code |
| 85 | static size_t NumberOfReferencesOffset() { |
| 86 | return OFFSETOF_MEMBER(StackHandleBlock, number_of_references_); |
| 87 | } |
| 88 | |
| 89 | // Offset of link within SHB, used by generated code |
| 90 | static size_t LinkOffset() { |
| 91 | return OFFSETOF_MEMBER(StackHandleBlock, link_); |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | StackHandleBlock() {} |
| 96 | |
| 97 | size_t number_of_references_; |
| 98 | StackHandleBlock* link_; |
| 99 | |
| 100 | DISALLOW_COPY_AND_ASSIGN(StackHandleBlock); |
| 101 | }; |
| 102 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 103 | class Thread { |
| 104 | public: |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 105 | enum State { |
| 106 | kUnknown = -1, |
| 107 | kNew, |
| 108 | kRunnable, |
| 109 | kBlocked, |
| 110 | kWaiting, |
| 111 | kTimedWaiting, |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 112 | kNative, |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 113 | kTerminated, |
| 114 | }; |
| 115 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 116 | static const size_t kDefaultStackSize = 64 * KB; |
| 117 | |
| 118 | // Creates a new thread. |
| 119 | static Thread* Create(size_t stack_size); |
| 120 | |
| 121 | // Creates a new thread from the calling thread. |
| 122 | static Thread* Attach(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 123 | |
| 124 | static Thread* Current() { |
Carl Shapiro | d0e7e77 | 2011-07-15 14:31:01 -0700 | [diff] [blame] | 125 | void* thread = pthread_getspecific(Thread::pthread_key_self_); |
| 126 | return reinterpret_cast<Thread*>(thread); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 129 | uint32_t GetId() const { |
| 130 | return id_; |
| 131 | } |
| 132 | |
| 133 | pid_t GetNativeId() const { |
| 134 | return native_id_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool IsExceptionPending() const { |
| 138 | return false; // TODO exception_ != NULL; |
| 139 | } |
| 140 | |
| 141 | Object* GetException() const { |
| 142 | return exception_; |
| 143 | } |
| 144 | |
| 145 | void SetException(Object* new_exception) { |
| 146 | CHECK(new_exception != NULL); |
| 147 | // TODO: CHECK(exception_ == NULL); |
| 148 | exception_ = new_exception; // TODO |
| 149 | } |
| 150 | |
| 151 | void ClearException() { |
| 152 | exception_ = NULL; |
| 153 | } |
| 154 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 155 | void SetName(const char* name); |
| 156 | |
| 157 | void Suspend(); |
| 158 | |
| 159 | bool IsSuspended(); |
| 160 | |
| 161 | void Resume(); |
| 162 | |
| 163 | static bool Init(); |
| 164 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 165 | State GetState() { |
| 166 | return state_; |
| 167 | } |
| 168 | |
| 169 | void SetState(State new_state) { |
| 170 | state_ = new_state; |
| 171 | } |
| 172 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 173 | // Offset of state within Thread, used by generated code |
| 174 | static ThreadOffset StateOffset() { |
| 175 | return ThreadOffset(OFFSETOF_MEMBER(Thread, state_)); |
| 176 | } |
| 177 | |
| 178 | Heap* GetHeap() { |
| 179 | return heap_; |
| 180 | } |
| 181 | |
| 182 | // JNI methods |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame^] | 183 | JniEnvironment* GetJniEnv() const { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 184 | return jni_env_; |
| 185 | } |
| 186 | |
| 187 | // Offset of JNI environment within Thread, used by generated code |
| 188 | static ThreadOffset JniEnvOffset() { |
| 189 | return ThreadOffset(OFFSETOF_MEMBER(Thread, jni_env_)); |
| 190 | } |
| 191 | |
| 192 | // Offset of top stack handle block within Thread, used by generated code |
| 193 | static ThreadOffset TopShbOffset() { |
| 194 | return ThreadOffset(OFFSETOF_MEMBER(Thread, top_shb_)); |
| 195 | } |
| 196 | |
| 197 | // Number of references allocated in StackHandleBlocks on this thread |
| 198 | size_t NumShbHandles() { |
| 199 | size_t count = 0; |
| 200 | for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) { |
| 201 | count += cur->NumberOfReferences(); |
| 202 | } |
| 203 | return count; |
| 204 | } |
| 205 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 206 | private: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 207 | Thread() : |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame^] | 208 | thread_id_(1234), top_shb_(NULL), exception_(NULL) { |
| 209 | jni_env_ = new JniEnvironment(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 210 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame^] | 211 | ~Thread() { |
| 212 | delete jni_env_; |
| 213 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 214 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 215 | void InitCpu(); |
| 216 | |
| 217 | // Initialized to "this". On certain architectures (such as x86) reading |
| 218 | // off of Thread::Current is easy but getting the address of Thread::Current |
| 219 | // is hard. This field can be read off of Thread::Current to give the address. |
| 220 | Thread* self_; |
| 221 | |
| 222 | uint32_t thread_id_; |
| 223 | |
| 224 | Heap* heap_; |
| 225 | |
| 226 | // Top of linked list of stack handle blocks or NULL for none |
| 227 | StackHandleBlock* top_shb_; |
| 228 | |
| 229 | // Every thread may have an associated JNI environment |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame^] | 230 | JniEnvironment* jni_env_; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 231 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 232 | State state_; |
| 233 | |
| 234 | uint32_t id_; |
| 235 | |
| 236 | pid_t native_id_; |
| 237 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 238 | pthread_t handle_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 239 | |
| 240 | Object* exception_; |
| 241 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 242 | byte* stack_base_; |
| 243 | byte* stack_limit_; |
| 244 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 245 | static pthread_key_t pthread_key_self_; |
| 246 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 247 | DISALLOW_COPY_AND_ASSIGN(Thread); |
| 248 | }; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 249 | std::ostream& operator<<(std::ostream& os, const Thread::State& state); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 250 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 251 | class ThreadList { |
| 252 | public: |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 253 | static const int kMaxId = 0xFFFF; |
| 254 | static const int kInvalidId = 0; |
| 255 | static const int kMainId = 1; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 256 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 257 | static ThreadList* Create(); |
| 258 | |
| 259 | ~ThreadList(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 260 | |
| 261 | void Register(Thread* thread); |
| 262 | |
| 263 | void Unregister(Thread* thread); |
| 264 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 265 | void Lock() { |
| 266 | lock_->Lock(); |
| 267 | } |
| 268 | |
| 269 | void Unlock() { |
| 270 | lock_->Unlock(); |
| 271 | }; |
| 272 | |
| 273 | private: |
| 274 | ThreadList(); |
| 275 | |
| 276 | std::list<Thread*> list_; |
| 277 | |
| 278 | Mutex* lock_; |
| 279 | |
| 280 | DISALLOW_COPY_AND_ASSIGN(ThreadList); |
| 281 | }; |
| 282 | |
| 283 | class ThreadListLock { |
| 284 | public: |
| 285 | ThreadListLock(ThreadList* thread_list, Thread* current_thread) |
| 286 | : thread_list_(thread_list) { |
| 287 | if (current_thread == NULL) { // try to get it from TLS |
| 288 | current_thread = Thread::Current(); |
| 289 | } |
| 290 | Thread::State old_state; |
| 291 | if (current_thread != NULL) { |
| 292 | old_state = current_thread->GetState(); |
| 293 | current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT |
| 294 | } else { |
| 295 | // happens during VM shutdown |
| 296 | old_state = Thread::kUnknown; // TODO: something else |
| 297 | } |
| 298 | thread_list_->Lock(); |
| 299 | if (current_thread != NULL) { |
| 300 | current_thread->SetState(old_state); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | ~ThreadListLock() { |
| 305 | thread_list_->Unlock(); |
| 306 | } |
| 307 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 308 | private: |
| 309 | ThreadList* thread_list_; |
| 310 | |
| 311 | DISALLOW_COPY_AND_ASSIGN(ThreadListLock); |
| 312 | }; |
| 313 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 314 | } // namespace art |
| 315 | |
| 316 | #endif // ART_SRC_THREAD_H_ |