blob: d4b700850aeb26db0fc3a36c2c5f8c38f2ddca7c [file] [log] [blame]
Carl Shapirob5573532011-07-12 18:22:59 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "thread.h"
Carl Shapirob5573532011-07-12 18:22:59 -07004
Ian Rogersb033c752011-07-20 12:22:35 -07005#include <pthread.h>
6#include <sys/mman.h>
Carl Shapirob5573532011-07-12 18:22:59 -07007#include <algorithm>
Elliott Hugheseb4f6142011-07-15 17:43:51 -07008#include <cerrno>
Carl Shapirob5573532011-07-12 18:22:59 -07009#include <list>
Carl Shapirob5573532011-07-12 18:22:59 -070010
Elliott Hughesa5b897e2011-08-16 11:33:06 -070011#include "class_linker.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070012#include "jni_internal.h"
Elliott Hughesa5b897e2011-08-16 11:33:06 -070013#include "object.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "runtime.h"
15#include "utils.h"
Carl Shapirob5573532011-07-12 18:22:59 -070016
17namespace art {
18
19pthread_key_t Thread::pthread_key_self_;
20
21Mutex* Mutex::Create(const char* name) {
22 Mutex* mu = new Mutex(name);
23 int result = pthread_mutex_init(&mu->lock_impl_, NULL);
Ian Rogersb033c752011-07-20 12:22:35 -070024 CHECK_EQ(0, result);
Carl Shapirob5573532011-07-12 18:22:59 -070025 return mu;
26}
27
28void Mutex::Lock() {
29 int result = pthread_mutex_lock(&lock_impl_);
30 CHECK_EQ(result, 0);
31 SetOwner(Thread::Current());
32}
33
34bool Mutex::TryLock() {
35 int result = pthread_mutex_lock(&lock_impl_);
36 if (result == EBUSY) {
37 return false;
38 } else {
39 CHECK_EQ(result, 0);
40 SetOwner(Thread::Current());
41 return true;
42 }
43}
44
45void Mutex::Unlock() {
46 CHECK(GetOwner() == Thread::Current());
47 int result = pthread_mutex_unlock(&lock_impl_);
48 CHECK_EQ(result, 0);
Elliott Hughesf4c21c92011-08-19 17:31:31 -070049 SetOwner(NULL);
Carl Shapirob5573532011-07-12 18:22:59 -070050}
51
Carl Shapiro61e019d2011-07-14 16:53:09 -070052void* ThreadStart(void *arg) {
Elliott Hughes53b61312011-08-12 18:28:20 -070053 UNIMPLEMENTED(FATAL);
Carl Shapirob5573532011-07-12 18:22:59 -070054 return NULL;
55}
56
Brian Carlstromb765be02011-08-17 23:54:10 -070057Thread* Thread::Create(const Runtime* runtime) {
58 size_t stack_size = runtime->GetStackSize();
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070059 scoped_ptr<MemMap> stack(MemMap::Map(stack_size, PROT_READ | PROT_WRITE));
Brian Carlstromb765be02011-08-17 23:54:10 -070060 if (stack == NULL) {
61 LOG(FATAL) << "failed to allocate thread stack";
62 // notreached
63 return NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070064 }
65
66 Thread* new_thread = new Thread;
Ian Rogers176f59c2011-07-20 13:14:11 -070067 new_thread->InitCpu();
Brian Carlstromb765be02011-08-17 23:54:10 -070068 new_thread->stack_.reset(stack.release());
69 // Since stacks are assumed to grown downward the base is the limit and the limit is the base.
70 new_thread->stack_limit_ = stack->GetAddress();
71 new_thread->stack_base_ = stack->GetLimit();
Carl Shapiro61e019d2011-07-14 16:53:09 -070072
73 pthread_attr_t attr;
74 int result = pthread_attr_init(&attr);
75 CHECK_EQ(result, 0);
76
77 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
78 CHECK_EQ(result, 0);
79
80 pthread_t handle;
81 result = pthread_create(&handle, &attr, ThreadStart, new_thread);
82 CHECK_EQ(result, 0);
83
84 result = pthread_attr_destroy(&attr);
85 CHECK_EQ(result, 0);
86
87 return new_thread;
88}
89
Elliott Hughes515a5bc2011-08-17 11:08:34 -070090Thread* Thread::Attach(const Runtime* runtime) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070091 Thread* thread = new Thread;
Ian Rogers176f59c2011-07-20 13:14:11 -070092 thread->InitCpu();
Carl Shapiro61e019d2011-07-14 16:53:09 -070093 thread->stack_limit_ = reinterpret_cast<byte*>(-1); // TODO: getrlimit
94 uintptr_t addr = reinterpret_cast<uintptr_t>(&thread); // TODO: ask pthreads
Brian Carlstromb0460ea2011-07-29 10:08:05 -070095 uintptr_t stack_base = RoundUp(addr, kPageSize);
Carl Shapiro61e019d2011-07-14 16:53:09 -070096 thread->stack_base_ = reinterpret_cast<byte*>(stack_base);
97 // TODO: set the stack size
98
99 thread->handle_ = pthread_self();
100
101 thread->state_ = kRunnable;
102
Elliott Hughesa5780da2011-07-17 11:39:39 -0700103 errno = pthread_setspecific(Thread::pthread_key_self_, thread);
104 if (errno != 0) {
105 PLOG(FATAL) << "pthread_setspecific failed";
106 }
107
Elliott Hughes0af55432011-08-17 18:37:28 -0700108 JavaVMExt* vm = runtime->GetJavaVM();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700109 CHECK(vm != NULL);
110 bool check_jni = vm->check_jni;
111 thread->jni_env_ = reinterpret_cast<JNIEnv*>(new JNIEnvExt(thread, check_jni));
Elliott Hughes330304d2011-08-12 14:28:05 -0700112
Carl Shapiro61e019d2011-07-14 16:53:09 -0700113 return thread;
114}
115
Carl Shapirob5573532011-07-12 18:22:59 -0700116static void ThreadExitCheck(void* arg) {
117 LG << "Thread exit check";
118}
119
120bool Thread::Init() {
121 // Allocate a TLS slot.
122 if (pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck) != 0) {
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700123 PLOG(WARNING) << "pthread_key_create failed";
Carl Shapirob5573532011-07-12 18:22:59 -0700124 return false;
125 }
126
127 // Double-check the TLS slot allocation.
128 if (pthread_getspecific(pthread_key_self_) != NULL) {
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700129 LOG(WARNING) << "newly-created pthread TLS slot is not NULL";
Carl Shapirob5573532011-07-12 18:22:59 -0700130 return false;
131 }
132
133 // TODO: initialize other locks and condition variables
134
135 return true;
136}
137
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700138size_t Thread::NumShbHandles() {
139 size_t count = 0;
140 for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) {
141 count += cur->NumberOfReferences();
142 }
143 return count;
144}
145
146bool Thread::ShbContains(jobject obj) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700147 Object** shb_entry = reinterpret_cast<Object**>(obj);
Ian Rogersa8cd9f42011-08-19 16:43:41 -0700148 for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) {
149 size_t num_refs = cur->NumberOfReferences();
150 DCHECK_GT(num_refs, 0u); // A SHB should always have a jobject/jclass
151 if ((&cur->Handles()[0] >= shb_entry) &&
152 (shb_entry <= (&cur->Handles()[num_refs-1]))) {
153 return true;
154 }
155 }
156 return false;
157}
158
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700159void ThrowNewException(Thread* thread, const char* exception_class_name, const char* msg) {
160 CHECK(thread != NULL);
161
162 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
163 Class* exception_class = class_linker->FindSystemClass(exception_class_name);
164 CHECK(exception_class != NULL);
165
166 Object* exception = exception_class->NewInstance();
167 CHECK(exception != NULL);
168
Elliott Hughesbfaadc82011-08-18 17:36:58 -0700169 String* java_msg = String::AllocFromModifiedUtf8(msg);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700170 CHECK(java_msg != NULL);
171
172 // TODO: what if there's already a pending exception?
173 // TODO: support the other constructors.
174 Method* ctor = exception_class->FindDirectMethod("<init>", "(Ljava/lang/String;)V");
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700175 CHECK(ctor != NULL);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700176
177 // TODO: need to *call* the constructor!
178 UNIMPLEMENTED(WARNING) << "can't call "
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700179 << exception_class->GetDescriptor()->ToModifiedUtf8() << ".<init> "
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700180 << "\"" << msg << "\"";
181
182 thread->SetException(exception);
183}
184
185void ThrowNewExceptionV(Thread* thread, const char* exception_class_name, const char* fmt, va_list args) {
186 char msg[512];
187 vsnprintf(msg, sizeof(msg), fmt, args);
188 ThrowNewException(thread, exception_class_name, msg);
189}
190
191void Thread::ThrowNewException(const char* exception_class_name, const char* fmt, ...) {
192 va_list args;
193 va_start(args, fmt);
194 ThrowNewExceptionV(this, exception_class_name, fmt, args);
195 va_end(args);
196}
197
Ian Rogersb033c752011-07-20 12:22:35 -0700198static const char* kStateNames[] = {
199 "New",
200 "Runnable",
201 "Blocked",
202 "Waiting",
203 "TimedWaiting",
204 "Native",
205 "Terminated",
206};
207std::ostream& operator<<(std::ostream& os, const Thread::State& state) {
208 if (state >= Thread::kNew && state <= Thread::kTerminated) {
209 os << kStateNames[state-Thread::kNew];
210 } else {
211 os << "State[" << static_cast<int>(state) << "]";
212 }
213 return os;
214}
215
Elliott Hughes330304d2011-08-12 14:28:05 -0700216std::ostream& operator<<(std::ostream& os, const Thread& thread) {
217 os << "Thread[" << &thread
218 << ",id=" << thread.GetId()
219 << ",tid=" << thread.GetNativeId()
220 << ",state=" << thread.GetState() << "]";
221 return os;
222}
223
Carl Shapiro61e019d2011-07-14 16:53:09 -0700224ThreadList* ThreadList::Create() {
225 return new ThreadList;
226}
227
Carl Shapirob5573532011-07-12 18:22:59 -0700228ThreadList::ThreadList() {
229 lock_ = Mutex::Create("ThreadList::Lock");
230}
231
232ThreadList::~ThreadList() {
233 // Make sure that all threads have exited and unregistered when we
234 // reach this point. This means that all daemon threads had been
235 // shutdown cleanly.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700236 CHECK_LE(list_.size(), 1U);
Carl Shapiro7a909592011-07-24 19:21:59 -0700237 // TODO: wait for all other threads to unregister
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700238 CHECK(list_.size() == 0 || list_.front() == Thread::Current());
Carl Shapiro7a909592011-07-24 19:21:59 -0700239 // TODO: detach the current thread
Carl Shapirob5573532011-07-12 18:22:59 -0700240 delete lock_;
241 lock_ = NULL;
242}
243
244void ThreadList::Register(Thread* thread) {
245 MutexLock mu(lock_);
246 CHECK(find(list_.begin(), list_.end(), thread) == list_.end());
247 list_.push_front(thread);
248}
249
250void ThreadList::Unregister(Thread* thread) {
251 MutexLock mu(lock_);
252 CHECK(find(list_.begin(), list_.end(), thread) != list_.end());
253 list_.remove(thread);
254}
255
Carl Shapirob5573532011-07-12 18:22:59 -0700256} // namespace