blob: e92f23c2531a814d398abed72265413ad8063575 [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 Hughes37f7a402011-08-22 18:56:01 -0700159void Thread::ThrowNewException(Class* exception_class, const char* msg) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700160 Object* exception = exception_class->NewInstance();
161 CHECK(exception != NULL);
162
Elliott Hughesbfaadc82011-08-18 17:36:58 -0700163 String* java_msg = String::AllocFromModifiedUtf8(msg);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700164 CHECK(java_msg != NULL);
165
166 // TODO: what if there's already a pending exception?
167 // TODO: support the other constructors.
168 Method* ctor = exception_class->FindDirectMethod("<init>", "(Ljava/lang/String;)V");
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700169 CHECK(ctor != NULL);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700170
171 // TODO: need to *call* the constructor!
172 UNIMPLEMENTED(WARNING) << "can't call "
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700173 << exception_class->GetDescriptor()->ToModifiedUtf8() << ".<init> "
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700174 << "\"" << msg << "\"";
175
Elliott Hughes37f7a402011-08-22 18:56:01 -0700176 SetException(exception);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700177}
178
179void Thread::ThrowNewException(const char* exception_class_name, const char* fmt, ...) {
Elliott Hughes37f7a402011-08-22 18:56:01 -0700180 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
181 Class* exception_class = class_linker->FindSystemClass(exception_class_name);
182 CHECK(exception_class != NULL);
183
184 std::string msg;
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700185 va_list args;
186 va_start(args, fmt);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700187 StringAppendV(&msg, fmt, args);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700188 va_end(args);
Elliott Hughes37f7a402011-08-22 18:56:01 -0700189
190 ThrowNewException(exception_class, msg.c_str());
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700191}
192
Ian Rogersb033c752011-07-20 12:22:35 -0700193static const char* kStateNames[] = {
194 "New",
195 "Runnable",
196 "Blocked",
197 "Waiting",
198 "TimedWaiting",
199 "Native",
200 "Terminated",
201};
202std::ostream& operator<<(std::ostream& os, const Thread::State& state) {
203 if (state >= Thread::kNew && state <= Thread::kTerminated) {
204 os << kStateNames[state-Thread::kNew];
205 } else {
206 os << "State[" << static_cast<int>(state) << "]";
207 }
208 return os;
209}
210
Elliott Hughes330304d2011-08-12 14:28:05 -0700211std::ostream& operator<<(std::ostream& os, const Thread& thread) {
212 os << "Thread[" << &thread
213 << ",id=" << thread.GetId()
214 << ",tid=" << thread.GetNativeId()
215 << ",state=" << thread.GetState() << "]";
216 return os;
217}
218
Carl Shapiro61e019d2011-07-14 16:53:09 -0700219ThreadList* ThreadList::Create() {
220 return new ThreadList;
221}
222
Carl Shapirob5573532011-07-12 18:22:59 -0700223ThreadList::ThreadList() {
224 lock_ = Mutex::Create("ThreadList::Lock");
225}
226
227ThreadList::~ThreadList() {
228 // Make sure that all threads have exited and unregistered when we
229 // reach this point. This means that all daemon threads had been
230 // shutdown cleanly.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700231 CHECK_LE(list_.size(), 1U);
Carl Shapiro7a909592011-07-24 19:21:59 -0700232 // TODO: wait for all other threads to unregister
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700233 CHECK(list_.size() == 0 || list_.front() == Thread::Current());
Carl Shapiro7a909592011-07-24 19:21:59 -0700234 // TODO: detach the current thread
Carl Shapirob5573532011-07-12 18:22:59 -0700235 delete lock_;
236 lock_ = NULL;
237}
238
239void ThreadList::Register(Thread* thread) {
240 MutexLock mu(lock_);
241 CHECK(find(list_.begin(), list_.end(), thread) == list_.end());
242 list_.push_front(thread);
243}
244
245void ThreadList::Unregister(Thread* thread) {
246 MutexLock mu(lock_);
247 CHECK(find(list_.begin(), list_.end(), thread) != list_.end());
248 list_.remove(thread);
249}
250
Carl Shapirob5573532011-07-12 18:22:59 -0700251} // namespace