blob: 609ae41cd25861a6b39f493ff0062517568000d6 [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
88Thread* Thread::Attach() {
89 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 Hughes330304d2011-08-12 14:28:05 -0700106 thread->jni_env_ = CreateJNIEnv();
107
Carl Shapiro61e019d2011-07-14 16:53:09 -0700108 return thread;
109}
110
Carl Shapirob5573532011-07-12 18:22:59 -0700111static void ThreadExitCheck(void* arg) {
112 LG << "Thread exit check";
113}
114
115bool Thread::Init() {
116 // Allocate a TLS slot.
117 if (pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck) != 0) {
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700118 PLOG(WARNING) << "pthread_key_create failed";
Carl Shapirob5573532011-07-12 18:22:59 -0700119 return false;
120 }
121
122 // Double-check the TLS slot allocation.
123 if (pthread_getspecific(pthread_key_self_) != NULL) {
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700124 LOG(WARNING) << "newly-created pthread TLS slot is not NULL";
Carl Shapirob5573532011-07-12 18:22:59 -0700125 return false;
126 }
127
128 // TODO: initialize other locks and condition variables
129
130 return true;
131}
132
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700133void ThrowNewException(Thread* thread, const char* exception_class_name, const char* msg) {
134 CHECK(thread != NULL);
135
136 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
137 Class* exception_class = class_linker->FindSystemClass(exception_class_name);
138 CHECK(exception_class != NULL);
139
140 Object* exception = exception_class->NewInstance();
141 CHECK(exception != NULL);
142
143 size_t char_count = String::ModifiedUtf8Len(msg);
144 String* java_msg = String::AllocFromModifiedUtf8(char_count, msg);
145 CHECK(java_msg != NULL);
146
147 // TODO: what if there's already a pending exception?
148 // TODO: support the other constructors.
149 Method* ctor = exception_class->FindDirectMethod("<init>", "(Ljava/lang/String;)V");
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700150 CHECK(ctor != NULL);
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700151
152 // TODO: need to *call* the constructor!
153 UNIMPLEMENTED(WARNING) << "can't call "
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700154 << exception_class->GetDescriptor() << ".<init> "
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700155 << "\"" << msg << "\"";
156
157 thread->SetException(exception);
158}
159
160void ThrowNewExceptionV(Thread* thread, const char* exception_class_name, const char* fmt, va_list args) {
161 char msg[512];
162 vsnprintf(msg, sizeof(msg), fmt, args);
163 ThrowNewException(thread, exception_class_name, msg);
164}
165
166void Thread::ThrowNewException(const char* exception_class_name, const char* fmt, ...) {
167 va_list args;
168 va_start(args, fmt);
169 ThrowNewExceptionV(this, exception_class_name, fmt, args);
170 va_end(args);
171}
172
Ian Rogersb033c752011-07-20 12:22:35 -0700173static const char* kStateNames[] = {
174 "New",
175 "Runnable",
176 "Blocked",
177 "Waiting",
178 "TimedWaiting",
179 "Native",
180 "Terminated",
181};
182std::ostream& operator<<(std::ostream& os, const Thread::State& state) {
183 if (state >= Thread::kNew && state <= Thread::kTerminated) {
184 os << kStateNames[state-Thread::kNew];
185 } else {
186 os << "State[" << static_cast<int>(state) << "]";
187 }
188 return os;
189}
190
Elliott Hughes330304d2011-08-12 14:28:05 -0700191std::ostream& operator<<(std::ostream& os, const Thread& thread) {
192 os << "Thread[" << &thread
193 << ",id=" << thread.GetId()
194 << ",tid=" << thread.GetNativeId()
195 << ",state=" << thread.GetState() << "]";
196 return os;
197}
198
Carl Shapiro61e019d2011-07-14 16:53:09 -0700199ThreadList* ThreadList::Create() {
200 return new ThreadList;
201}
202
Carl Shapirob5573532011-07-12 18:22:59 -0700203ThreadList::ThreadList() {
204 lock_ = Mutex::Create("ThreadList::Lock");
205}
206
207ThreadList::~ThreadList() {
208 // Make sure that all threads have exited and unregistered when we
209 // reach this point. This means that all daemon threads had been
210 // shutdown cleanly.
Carl Shapiro7a909592011-07-24 19:21:59 -0700211 CHECK_EQ(list_.size(), 1U);
212 // TODO: wait for all other threads to unregister
213 CHECK_EQ(list_.front(), Thread::Current());
214 // TODO: detach the current thread
Carl Shapirob5573532011-07-12 18:22:59 -0700215 delete lock_;
216 lock_ = NULL;
217}
218
219void ThreadList::Register(Thread* thread) {
220 MutexLock mu(lock_);
221 CHECK(find(list_.begin(), list_.end(), thread) == list_.end());
222 list_.push_front(thread);
223}
224
225void ThreadList::Unregister(Thread* thread) {
226 MutexLock mu(lock_);
227 CHECK(find(list_.begin(), list_.end(), thread) != list_.end());
228 list_.remove(thread);
229}
230
Carl Shapirob5573532011-07-12 18:22:59 -0700231} // namespace