blob: 55c1b1110def3a0db68158e9a4ee5cf2e8d956d9 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// 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 Shapirob5573532011-07-12 18:22:59 -07007#include <list>
8#include <pthread.h>
9
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070010#include "src/globals.h"
11#include "src/logging.h"
12#include "src/macros.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070013#include "src/runtime.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070014
15namespace art {
16
Carl Shapiro61e019d2011-07-14 16:53:09 -070017class Heap;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018class Object;
Carl Shapirob5573532011-07-12 18:22:59 -070019class Runtime;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020class Thread;
Carl Shapirob5573532011-07-12 18:22:59 -070021class ThreadList;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070022
23class Mutex {
24 public:
25 virtual ~Mutex() {}
26
Carl Shapirob5573532011-07-12 18:22:59 -070027 void Lock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070028
Carl Shapirob5573532011-07-12 18:22:59 -070029 bool TryLock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070030
Carl Shapirob5573532011-07-12 18:22:59 -070031 void Unlock();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070032
33 const char* GetName() { return name_; }
34
35 Thread* GetOwner() { return owner_; }
36
Carl Shapirob5573532011-07-12 18:22:59 -070037 static Mutex* Create(const char* name);
38
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070039 public: // TODO: protected
40 explicit Mutex(const char* name) : name_(name), owner_(NULL) {}
41
42 void SetOwner(Thread* thread) { owner_ = thread; }
43
44 private:
45 const char* name_;
46
47 Thread* owner_;
48
Carl Shapirob5573532011-07-12 18:22:59 -070049 pthread_mutex_t lock_impl_;
50
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051 DISALLOW_COPY_AND_ASSIGN(Mutex);
52};
53
54class MutexLock {
55 public:
56 explicit MutexLock(Mutex *mu) : mu_(mu) {
57 mu_->Lock();
58 }
59 ~MutexLock() { mu_->Unlock(); }
60 private:
61 Mutex* const mu_;
62 DISALLOW_COPY_AND_ASSIGN(MutexLock);
63};
64
65class Thread {
66 public:
Carl Shapirob5573532011-07-12 18:22:59 -070067 enum State {
68 kUnknown = -1,
69 kNew,
70 kRunnable,
71 kBlocked,
72 kWaiting,
73 kTimedWaiting,
74 kTerminated,
75 };
76
Carl Shapiro61e019d2011-07-14 16:53:09 -070077 static const size_t kDefaultStackSize = 64 * KB;
78
79 // Creates a new thread.
80 static Thread* Create(size_t stack_size);
81
82 // Creates a new thread from the calling thread.
83 static Thread* Attach();
Carl Shapirob5573532011-07-12 18:22:59 -070084
85 static Thread* Current() {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070086 static Thread self;
87 return &self; // TODO
88 }
89
Carl Shapirob5573532011-07-12 18:22:59 -070090 uint32_t GetId() const {
91 return id_;
92 }
93
94 pid_t GetNativeId() const {
95 return native_id_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070096 }
97
98 bool IsExceptionPending() const {
99 return false; // TODO exception_ != NULL;
100 }
101
102 Object* GetException() const {
103 return exception_;
104 }
105
106 void SetException(Object* new_exception) {
107 CHECK(new_exception != NULL);
108 // TODO: CHECK(exception_ == NULL);
109 exception_ = new_exception; // TODO
110 }
111
112 void ClearException() {
113 exception_ = NULL;
114 }
115
Carl Shapirob5573532011-07-12 18:22:59 -0700116 void SetName(const char* name);
117
118 void Suspend();
119
120 bool IsSuspended();
121
122 void Resume();
123
124 static bool Init();
125
Carl Shapirob5573532011-07-12 18:22:59 -0700126 State GetState() {
127 return state_;
128 }
129
130 void SetState(State new_state) {
131 state_ = new_state;
132 }
133
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134 private:
Carl Shapirob5573532011-07-12 18:22:59 -0700135 Thread() : id_(1234), exception_(NULL) {}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700136 ~Thread() {}
137
Carl Shapirob5573532011-07-12 18:22:59 -0700138 State state_;
139
140 uint32_t id_;
141
142 pid_t native_id_;
143
Carl Shapiro61e019d2011-07-14 16:53:09 -0700144 pthread_t handle_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700145
146 Object* exception_;
147
Carl Shapiro61e019d2011-07-14 16:53:09 -0700148 byte* stack_base_;
149 byte* stack_limit_;
150
Carl Shapirob5573532011-07-12 18:22:59 -0700151 static pthread_key_t pthread_key_self_;
152
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700153 DISALLOW_COPY_AND_ASSIGN(Thread);
154};
155
Carl Shapirob5573532011-07-12 18:22:59 -0700156class ThreadList {
157 public:
Carl Shapiro61e019d2011-07-14 16:53:09 -0700158 static const int kMaxId = 0xFFFF;
159 static const int kInvalidId = 0;
160 static const int kMainId = 1;
Carl Shapirob5573532011-07-12 18:22:59 -0700161
Carl Shapiro61e019d2011-07-14 16:53:09 -0700162 static ThreadList* Create();
163
164 ~ThreadList();
Carl Shapirob5573532011-07-12 18:22:59 -0700165
166 void Register(Thread* thread);
167
168 void Unregister(Thread* thread);
169
Carl Shapirob5573532011-07-12 18:22:59 -0700170 void Lock() {
171 lock_->Lock();
172 }
173
174 void Unlock() {
175 lock_->Unlock();
176 };
177
178 private:
179 ThreadList();
180
181 std::list<Thread*> list_;
182
183 Mutex* lock_;
184
185 DISALLOW_COPY_AND_ASSIGN(ThreadList);
186};
187
188class ThreadListLock {
189 public:
190 ThreadListLock(ThreadList* thread_list, Thread* current_thread)
191 : thread_list_(thread_list) {
192 if (current_thread == NULL) { // try to get it from TLS
193 current_thread = Thread::Current();
194 }
195 Thread::State old_state;
196 if (current_thread != NULL) {
197 old_state = current_thread->GetState();
198 current_thread->SetState(Thread::kWaiting); // TODO: VMWAIT
199 } else {
200 // happens during VM shutdown
201 old_state = Thread::kUnknown; // TODO: something else
202 }
203 thread_list_->Lock();
204 if (current_thread != NULL) {
205 current_thread->SetState(old_state);
206 }
207 }
208
209 ~ThreadListLock() {
210 thread_list_->Unlock();
211 }
212
Carl Shapirob5573532011-07-12 18:22:59 -0700213 private:
214 ThreadList* thread_list_;
215
216 DISALLOW_COPY_AND_ASSIGN(ThreadListLock);
217};
218
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700219} // namespace art
220
221#endif // ART_SRC_THREAD_H_