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