blob: edbc7709561128fc15ae9f49c573136e68a34dd6 [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"
12#include "object.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "runtime.h"
14#include "utils.h"
Carl Shapirob5573532011-07-12 18:22:59 -070015
16namespace art {
17
18pthread_key_t Thread::pthread_key_self_;
19
20Mutex* Mutex::Create(const char* name) {
21 Mutex* mu = new Mutex(name);
22 int result = pthread_mutex_init(&mu->lock_impl_, NULL);
Ian Rogersb033c752011-07-20 12:22:35 -070023 CHECK_EQ(0, result);
Carl Shapirob5573532011-07-12 18:22:59 -070024 return mu;
25}
26
27void Mutex::Lock() {
28 int result = pthread_mutex_lock(&lock_impl_);
29 CHECK_EQ(result, 0);
30 SetOwner(Thread::Current());
31}
32
33bool Mutex::TryLock() {
34 int result = pthread_mutex_lock(&lock_impl_);
35 if (result == EBUSY) {
36 return false;
37 } else {
38 CHECK_EQ(result, 0);
39 SetOwner(Thread::Current());
40 return true;
41 }
42}
43
44void Mutex::Unlock() {
45 CHECK(GetOwner() == Thread::Current());
46 int result = pthread_mutex_unlock(&lock_impl_);
47 CHECK_EQ(result, 0);
48 SetOwner(Thread::Current());
49}
50
Carl Shapiro61e019d2011-07-14 16:53:09 -070051void* ThreadStart(void *arg) {
Elliott Hughes53b61312011-08-12 18:28:20 -070052 UNIMPLEMENTED(FATAL);
Carl Shapirob5573532011-07-12 18:22:59 -070053 return NULL;
54}
55
Carl Shapiro61e019d2011-07-14 16:53:09 -070056Thread* Thread::Create(size_t stack_size) {
57 int prot = PROT_READ | PROT_WRITE;
58 // TODO: require the stack size to be page aligned?
59 size_t length = RoundUp(stack_size, 0x1000);
60 void* stack_limit = mmap(NULL, length, prot, MAP_PRIVATE, -1, 0);
61 if (stack_limit == MAP_FAILED) {
62 LOG(FATAL) << "mmap";
63 return false;
64 }
65
66 Thread* new_thread = new Thread;
Ian Rogers176f59c2011-07-20 13:14:11 -070067 new_thread->InitCpu();
Carl Shapiro61e019d2011-07-14 16:53:09 -070068 new_thread->stack_limit_ = static_cast<byte*>(stack_limit);
69 new_thread->stack_base_ = new_thread->stack_limit_ + length;
70
71 pthread_attr_t attr;
72 int result = pthread_attr_init(&attr);
73 CHECK_EQ(result, 0);
74
75 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
76 CHECK_EQ(result, 0);
77
78 pthread_t handle;
79 result = pthread_create(&handle, &attr, ThreadStart, new_thread);
80 CHECK_EQ(result, 0);
81
82 result = pthread_attr_destroy(&attr);
83 CHECK_EQ(result, 0);
84
85 return new_thread;
86}
87
Elliott Hughes515a5bc2011-08-17 11:08:34 -070088Thread* Thread::Attach(const Runtime* runtime) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070089 Thread* thread = new Thread;
Ian Rogers176f59c2011-07-20 13:14:11 -070090 thread->InitCpu();
Carl Shapiro61e019d2011-07-14 16:53:09 -070091 thread->stack_limit_ = reinterpret_cast<byte*>(-1); // TODO: getrlimit
92 uintptr_t addr = reinterpret_cast<uintptr_t>(&thread); // TODO: ask pthreads
Brian Carlstromb0460ea2011-07-29 10:08:05 -070093 uintptr_t stack_base = RoundUp(addr, kPageSize);
Carl Shapiro61e019d2011-07-14 16:53:09 -070094 thread->stack_base_ = reinterpret_cast<byte*>(stack_base);
95 // TODO: set the stack size
96
97 thread->handle_ = pthread_self();
98
99 thread->state_ = kRunnable;
100
Elliott Hughesa5780da2011-07-17 11:39:39 -0700101 errno = pthread_setspecific(Thread::pthread_key_self_, thread);
102 if (errno != 0) {
103 PLOG(FATAL) << "pthread_setspecific failed";
104 }
105
Elliott Hughes0af55432011-08-17 18:37:28 -0700106 JavaVMExt* vm = runtime->GetJavaVM();
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700107 CHECK(vm != NULL);
108 bool check_jni = vm->check_jni;
109 thread->jni_env_ = reinterpret_cast<JNIEnv*>(new JNIEnvExt(thread, check_jni));
Elliott Hughes330304d2011-08-12 14:28:05 -0700110
Carl Shapiro61e019d2011-07-14 16:53:09 -0700111 return thread;
112}
113
Carl Shapirob5573532011-07-12 18:22:59 -0700114static void ThreadExitCheck(void* arg) {
115 LG << "Thread exit check";
116}
117
118bool Thread::Init() {
119 // Allocate a TLS slot.
120 if (pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck) != 0) {
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700121 PLOG(WARNING) << "pthread_key_create failed";
Carl Shapirob5573532011-07-12 18:22:59 -0700122 return false;
123 }
124
125 // Double-check the TLS slot allocation.
126 if (pthread_getspecific(pthread_key_self_) != NULL) {
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700127 LOG(WARNING) << "newly-created pthread TLS slot is not NULL";
Carl Shapirob5573532011-07-12 18:22:59 -0700128 return false;
129 }
130
131 // TODO: initialize other locks and condition variables
132
133 return true;
134}
135
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700136void ThrowNewException(Thread* thread, const char* exception_class_name, const char* msg) {
137 CHECK(thread != NULL);
138
139 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
140 Class* exception_class = class_linker->FindSystemClass(exception_class_name);
141 CHECK(exception_class != NULL);
142
143 Object* exception = exception_class->NewInstance();
144 CHECK(exception != NULL);
145
146 size_t char_count = String::ModifiedUtf8Len(msg);
147 String* java_msg = String::AllocFromModifiedUtf8(char_count, msg);
148 CHECK(java_msg != NULL);
149
150 // TODO: what if there's already a pending exception?
151 // TODO: support the other constructors.
152 Method* ctor = exception_class->FindDirectMethod("<init>", "(Ljava/lang/String;)V");
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700153 CHECK(ctor != NULL);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700154
155 // TODO: need to *call* the constructor!
156 UNIMPLEMENTED(WARNING) << "can't call "
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700157 << exception_class->GetDescriptor() << ".<init> "
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700158 << "\"" << msg << "\"";
159
160 thread->SetException(exception);
161}
162
163void ThrowNewExceptionV(Thread* thread, const char* exception_class_name, const char* fmt, va_list args) {
164 char msg[512];
165 vsnprintf(msg, sizeof(msg), fmt, args);
166 ThrowNewException(thread, exception_class_name, msg);
167}
168
169void Thread::ThrowNewException(const char* exception_class_name, const char* fmt, ...) {
170 va_list args;
171 va_start(args, fmt);
172 ThrowNewExceptionV(this, exception_class_name, fmt, args);
173 va_end(args);
174}
175
Ian Rogersb033c752011-07-20 12:22:35 -0700176static const char* kStateNames[] = {
177 "New",
178 "Runnable",
179 "Blocked",
180 "Waiting",
181 "TimedWaiting",
182 "Native",
183 "Terminated",
184};
185std::ostream& operator<<(std::ostream& os, const Thread::State& state) {
186 if (state >= Thread::kNew && state <= Thread::kTerminated) {
187 os << kStateNames[state-Thread::kNew];
188 } else {
189 os << "State[" << static_cast<int>(state) << "]";
190 }
191 return os;
192}
193
Elliott Hughes330304d2011-08-12 14:28:05 -0700194std::ostream& operator<<(std::ostream& os, const Thread& thread) {
195 os << "Thread[" << &thread
196 << ",id=" << thread.GetId()
197 << ",tid=" << thread.GetNativeId()
198 << ",state=" << thread.GetState() << "]";
199 return os;
200}
201
Carl Shapiro61e019d2011-07-14 16:53:09 -0700202ThreadList* ThreadList::Create() {
203 return new ThreadList;
204}
205
Carl Shapirob5573532011-07-12 18:22:59 -0700206ThreadList::ThreadList() {
207 lock_ = Mutex::Create("ThreadList::Lock");
208}
209
210ThreadList::~ThreadList() {
211 // Make sure that all threads have exited and unregistered when we
212 // reach this point. This means that all daemon threads had been
213 // shutdown cleanly.
Carl Shapiro7a909592011-07-24 19:21:59 -0700214 CHECK_EQ(list_.size(), 1U);
215 // TODO: wait for all other threads to unregister
216 CHECK_EQ(list_.front(), Thread::Current());
217 // TODO: detach the current thread
Carl Shapirob5573532011-07-12 18:22:59 -0700218 delete lock_;
219 lock_ = NULL;
220}
221
222void ThreadList::Register(Thread* thread) {
223 MutexLock mu(lock_);
224 CHECK(find(list_.begin(), list_.end(), thread) == list_.end());
225 list_.push_front(thread);
226}
227
228void ThreadList::Unregister(Thread* thread) {
229 MutexLock mu(lock_);
230 CHECK(find(list_.begin(), list_.end(), thread) != list_.end());
231 list_.remove(thread);
232}
233
Carl Shapirob5573532011-07-12 18:22:59 -0700234} // namespace