Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "thread.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 4 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 5 | #include <pthread.h> |
| 6 | #include <sys/mman.h> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 7 | #include <algorithm> |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 8 | #include <cerrno> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 9 | #include <list> |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 10 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 11 | #include "class_linker.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 12 | #include "jni_internal.h" |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 13 | #include "object.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 14 | #include "runtime.h" |
| 15 | #include "utils.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 16 | |
| 17 | namespace art { |
| 18 | |
| 19 | pthread_key_t Thread::pthread_key_self_; |
| 20 | |
| 21 | Mutex* Mutex::Create(const char* name) { |
| 22 | Mutex* mu = new Mutex(name); |
| 23 | int result = pthread_mutex_init(&mu->lock_impl_, NULL); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 24 | CHECK_EQ(0, result); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 25 | return mu; |
| 26 | } |
| 27 | |
| 28 | void Mutex::Lock() { |
| 29 | int result = pthread_mutex_lock(&lock_impl_); |
| 30 | CHECK_EQ(result, 0); |
| 31 | SetOwner(Thread::Current()); |
| 32 | } |
| 33 | |
| 34 | bool 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 | |
| 45 | void Mutex::Unlock() { |
| 46 | CHECK(GetOwner() == Thread::Current()); |
| 47 | int result = pthread_mutex_unlock(&lock_impl_); |
| 48 | CHECK_EQ(result, 0); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 49 | SetOwner(NULL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 52 | void Frame::Next() { |
| 53 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
| 54 | GetMethod()->GetFrameSize(); |
| 55 | sp_ = reinterpret_cast<const Method**>(next_sp); |
| 56 | } |
| 57 | |
| 58 | void* Frame::GetPC() const { |
| 59 | byte* pc_addr = reinterpret_cast<byte*>(sp_) + |
Ian Rogers | 762400c | 2011-08-23 12:14:16 -0700 | [diff] [blame] | 60 | GetMethod()->GetReturnPcOffset(); |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 61 | return reinterpret_cast<void*>(pc_addr); |
| 62 | } |
| 63 | |
| 64 | const Method* Frame::NextMethod() const { |
| 65 | byte* next_sp = reinterpret_cast<byte*>(sp_) + |
| 66 | GetMethod()->GetFrameSize(); |
| 67 | return reinterpret_cast<const Method*>(next_sp); |
| 68 | } |
| 69 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 70 | void* ThreadStart(void *arg) { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 71 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
| 74 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 75 | Thread* Thread::Create(const Runtime* runtime) { |
| 76 | size_t stack_size = runtime->GetStackSize(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 77 | scoped_ptr<MemMap> stack(MemMap::Map(stack_size, PROT_READ | PROT_WRITE)); |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 78 | if (stack == NULL) { |
| 79 | LOG(FATAL) << "failed to allocate thread stack"; |
| 80 | // notreached |
| 81 | return NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | Thread* new_thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 85 | new_thread->InitCpu(); |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 86 | new_thread->stack_.reset(stack.release()); |
| 87 | // Since stacks are assumed to grown downward the base is the limit and the limit is the base. |
| 88 | new_thread->stack_limit_ = stack->GetAddress(); |
| 89 | new_thread->stack_base_ = stack->GetLimit(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 90 | |
| 91 | pthread_attr_t attr; |
| 92 | int result = pthread_attr_init(&attr); |
| 93 | CHECK_EQ(result, 0); |
| 94 | |
| 95 | result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 96 | CHECK_EQ(result, 0); |
| 97 | |
| 98 | pthread_t handle; |
| 99 | result = pthread_create(&handle, &attr, ThreadStart, new_thread); |
| 100 | CHECK_EQ(result, 0); |
| 101 | |
| 102 | result = pthread_attr_destroy(&attr); |
| 103 | CHECK_EQ(result, 0); |
| 104 | |
| 105 | return new_thread; |
| 106 | } |
| 107 | |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 108 | Thread* Thread::Attach(const Runtime* runtime) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 109 | Thread* thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 110 | thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 111 | thread->stack_limit_ = reinterpret_cast<byte*>(-1); // TODO: getrlimit |
| 112 | uintptr_t addr = reinterpret_cast<uintptr_t>(&thread); // TODO: ask pthreads |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 113 | uintptr_t stack_base = RoundUp(addr, kPageSize); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 114 | thread->stack_base_ = reinterpret_cast<byte*>(stack_base); |
| 115 | // TODO: set the stack size |
| 116 | |
| 117 | thread->handle_ = pthread_self(); |
| 118 | |
| 119 | thread->state_ = kRunnable; |
| 120 | |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 121 | errno = pthread_setspecific(Thread::pthread_key_self_, thread); |
| 122 | if (errno != 0) { |
| 123 | PLOG(FATAL) << "pthread_setspecific failed"; |
| 124 | } |
| 125 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 126 | JavaVMExt* vm = runtime->GetJavaVM(); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 127 | CHECK(vm != NULL); |
| 128 | bool check_jni = vm->check_jni; |
| 129 | thread->jni_env_ = reinterpret_cast<JNIEnv*>(new JNIEnvExt(thread, check_jni)); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 130 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 131 | return thread; |
| 132 | } |
| 133 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 134 | static void ThreadExitCheck(void* arg) { |
| 135 | LG << "Thread exit check"; |
| 136 | } |
| 137 | |
| 138 | bool Thread::Init() { |
| 139 | // Allocate a TLS slot. |
| 140 | if (pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck) != 0) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 141 | PLOG(WARNING) << "pthread_key_create failed"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | |
| 145 | // Double-check the TLS slot allocation. |
| 146 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 147 | LOG(WARNING) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
| 151 | // TODO: initialize other locks and condition variables |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 156 | size_t Thread::NumShbHandles() { |
| 157 | size_t count = 0; |
| 158 | for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) { |
| 159 | count += cur->NumberOfReferences(); |
| 160 | } |
| 161 | return count; |
| 162 | } |
| 163 | |
| 164 | bool Thread::ShbContains(jobject obj) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 165 | Object** shb_entry = reinterpret_cast<Object**>(obj); |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 166 | for (StackHandleBlock* cur = top_shb_; cur; cur = cur->Link()) { |
| 167 | size_t num_refs = cur->NumberOfReferences(); |
| 168 | DCHECK_GT(num_refs, 0u); // A SHB should always have a jobject/jclass |
| 169 | if ((&cur->Handles()[0] >= shb_entry) && |
| 170 | (shb_entry <= (&cur->Handles()[num_refs-1]))) { |
| 171 | return true; |
| 172 | } |
| 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 177 | void Thread::ThrowNewException(const char* exception_class_descriptor, const char* fmt, ...) { |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 178 | std::string msg; |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 179 | va_list args; |
| 180 | va_start(args, fmt); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 181 | StringAppendV(&msg, fmt, args); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 182 | va_end(args); |
Elliott Hughes | 37f7a40 | 2011-08-22 18:56:01 -0700 | [diff] [blame] | 183 | |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 184 | // Convert "Ljava/lang/Exception;" into JNI-style "java/lang/Exception". |
| 185 | CHECK(exception_class_descriptor[0] == 'L'); |
| 186 | std::string descriptor(exception_class_descriptor + 1); |
| 187 | CHECK(descriptor[descriptor.length() - 1] == ';'); |
| 188 | descriptor.erase(descriptor.length() - 1); |
| 189 | |
| 190 | JNIEnv* env = GetJniEnv(); |
| 191 | jclass exception_class = env->FindClass(descriptor.c_str()); |
| 192 | CHECK(exception_class != NULL) << "descriptor=\"" << descriptor << "\""; |
| 193 | int rc = env->ThrowNew(exception_class, msg.c_str()); |
| 194 | CHECK_EQ(rc, JNI_OK); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 197 | Frame Thread::FindExceptionHandler(void* throw_pc, void** handler_pc) { |
| 198 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 199 | DCHECK(class_linker != NULL); |
| 200 | |
| 201 | Frame cur_frame = GetTopOfStack(); |
| 202 | for (int unwind_depth = 0; ; unwind_depth++) { |
| 203 | const Method* cur_method = cur_frame.GetMethod(); |
| 204 | DexCache* dex_cache = cur_method->GetDeclaringClass()->GetDexCache(); |
| 205 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
| 206 | |
| 207 | void* handler_addr = FindExceptionHandlerInMethod(cur_method, |
| 208 | throw_pc, |
| 209 | dex_file, |
| 210 | class_linker); |
| 211 | if (handler_addr) { |
| 212 | *handler_pc = handler_addr; |
| 213 | return cur_frame; |
| 214 | } else { |
| 215 | // Check if we are at the last frame |
| 216 | if (cur_frame.HasNext()) { |
| 217 | cur_frame.Next(); |
| 218 | } else { |
| 219 | // Either at the top of stack or next frame is native. |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | *handler_pc = NULL; |
| 225 | return Frame(); |
| 226 | } |
| 227 | |
| 228 | void* Thread::FindExceptionHandlerInMethod(const Method* method, |
| 229 | void* throw_pc, |
| 230 | const DexFile& dex_file, |
| 231 | ClassLinker* class_linker) { |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 232 | Throwable* exception_obj = exception_; |
Shih-wei Liao | 1a18c8c | 2011-08-14 17:47:36 -0700 | [diff] [blame] | 233 | exception_ = NULL; |
| 234 | |
| 235 | intptr_t dex_pc = -1; |
| 236 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->code_off_); |
| 237 | DexFile::CatchHandlerIterator iter; |
| 238 | for (iter = dex_file.dexFindCatchHandler(*code_item, |
| 239 | method->ToDexPC(reinterpret_cast<intptr_t>(throw_pc))); |
| 240 | !iter.HasNext(); |
| 241 | iter.Next()) { |
| 242 | Class* klass = class_linker->FindSystemClass(dex_file.dexStringByTypeIdx(iter.Get().type_idx_)); |
| 243 | DCHECK(klass != NULL); |
| 244 | if (exception_obj->InstanceOf(klass)) { |
| 245 | dex_pc = iter.Get().address_; |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | exception_ = exception_obj; |
| 251 | if (iter.HasNext()) { |
| 252 | return NULL; |
| 253 | } else { |
| 254 | return reinterpret_cast<void*>( method->ToNativePC(dex_pc) ); |
| 255 | } |
| 256 | } |
| 257 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 258 | static const char* kStateNames[] = { |
| 259 | "New", |
| 260 | "Runnable", |
| 261 | "Blocked", |
| 262 | "Waiting", |
| 263 | "TimedWaiting", |
| 264 | "Native", |
| 265 | "Terminated", |
| 266 | }; |
| 267 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
| 268 | if (state >= Thread::kNew && state <= Thread::kTerminated) { |
| 269 | os << kStateNames[state-Thread::kNew]; |
| 270 | } else { |
| 271 | os << "State[" << static_cast<int>(state) << "]"; |
| 272 | } |
| 273 | return os; |
| 274 | } |
| 275 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 276 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
| 277 | os << "Thread[" << &thread |
| 278 | << ",id=" << thread.GetId() |
| 279 | << ",tid=" << thread.GetNativeId() |
| 280 | << ",state=" << thread.GetState() << "]"; |
| 281 | return os; |
| 282 | } |
| 283 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 284 | ThreadList* ThreadList::Create() { |
| 285 | return new ThreadList; |
| 286 | } |
| 287 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 288 | ThreadList::ThreadList() { |
| 289 | lock_ = Mutex::Create("ThreadList::Lock"); |
| 290 | } |
| 291 | |
| 292 | ThreadList::~ThreadList() { |
| 293 | // Make sure that all threads have exited and unregistered when we |
| 294 | // reach this point. This means that all daemon threads had been |
| 295 | // shutdown cleanly. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 296 | CHECK_LE(list_.size(), 1U); |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 297 | // TODO: wait for all other threads to unregister |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 298 | CHECK(list_.size() == 0 || list_.front() == Thread::Current()); |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 299 | // TODO: detach the current thread |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 300 | delete lock_; |
| 301 | lock_ = NULL; |
| 302 | } |
| 303 | |
| 304 | void ThreadList::Register(Thread* thread) { |
| 305 | MutexLock mu(lock_); |
| 306 | CHECK(find(list_.begin(), list_.end(), thread) == list_.end()); |
| 307 | list_.push_front(thread); |
| 308 | } |
| 309 | |
| 310 | void ThreadList::Unregister(Thread* thread) { |
| 311 | MutexLock mu(lock_); |
| 312 | CHECK(find(list_.begin(), list_.end(), thread) != list_.end()); |
| 313 | list_.remove(thread); |
| 314 | } |
| 315 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 316 | } // namespace |