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 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 52 | void* ThreadStart(void *arg) { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 53 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
| 56 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 57 | Thread* Thread::Create(const Runtime* runtime) { |
| 58 | size_t stack_size = runtime->GetStackSize(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 59 | scoped_ptr<MemMap> stack(MemMap::Map(stack_size, PROT_READ | PROT_WRITE)); |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 60 | if (stack == NULL) { |
| 61 | LOG(FATAL) << "failed to allocate thread stack"; |
| 62 | // notreached |
| 63 | return NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | Thread* new_thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 67 | new_thread->InitCpu(); |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 68 | 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 Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 72 | |
| 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 Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 90 | Thread* Thread::Attach(const Runtime* runtime) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 91 | Thread* thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 92 | thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 93 | thread->stack_limit_ = reinterpret_cast<byte*>(-1); // TODO: getrlimit |
| 94 | uintptr_t addr = reinterpret_cast<uintptr_t>(&thread); // TODO: ask pthreads |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 95 | uintptr_t stack_base = RoundUp(addr, kPageSize); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 96 | 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 Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 103 | errno = pthread_setspecific(Thread::pthread_key_self_, thread); |
| 104 | if (errno != 0) { |
| 105 | PLOG(FATAL) << "pthread_setspecific failed"; |
| 106 | } |
| 107 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 108 | JavaVMExt* vm = runtime->GetJavaVM(); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 109 | CHECK(vm != NULL); |
| 110 | bool check_jni = vm->check_jni; |
| 111 | thread->jni_env_ = reinterpret_cast<JNIEnv*>(new JNIEnvExt(thread, check_jni)); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 112 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 113 | return thread; |
| 114 | } |
| 115 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 116 | static void ThreadExitCheck(void* arg) { |
| 117 | LG << "Thread exit check"; |
| 118 | } |
| 119 | |
| 120 | bool Thread::Init() { |
| 121 | // Allocate a TLS slot. |
| 122 | if (pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck) != 0) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 123 | PLOG(WARNING) << "pthread_key_create failed"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | // Double-check the TLS slot allocation. |
| 128 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 129 | LOG(WARNING) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
| 133 | // TODO: initialize other locks and condition variables |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 138 | size_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 | |
| 146 | bool Thread::ShbContains(jobject obj) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 147 | Object** shb_entry = reinterpret_cast<Object**>(obj); |
Ian Rogers | a8cd9f4 | 2011-08-19 16:43:41 -0700 | [diff] [blame] | 148 | 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 Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 159 | void ThrowNewException(Thread* thread, const char* exception_class_name, const char* msg) { |
| 160 | CHECK(thread != NULL); |
| 161 | |
| 162 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 163 | Class* exception_class = class_linker->FindSystemClass(exception_class_name); |
| 164 | CHECK(exception_class != NULL); |
| 165 | |
| 166 | Object* exception = exception_class->NewInstance(); |
| 167 | CHECK(exception != NULL); |
| 168 | |
Elliott Hughes | bfaadc8 | 2011-08-18 17:36:58 -0700 | [diff] [blame] | 169 | String* java_msg = String::AllocFromModifiedUtf8(msg); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 170 | CHECK(java_msg != NULL); |
| 171 | |
| 172 | // TODO: what if there's already a pending exception? |
| 173 | // TODO: support the other constructors. |
| 174 | Method* ctor = exception_class->FindDirectMethod("<init>", "(Ljava/lang/String;)V"); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 175 | CHECK(ctor != NULL); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 176 | |
| 177 | // TODO: need to *call* the constructor! |
| 178 | UNIMPLEMENTED(WARNING) << "can't call " |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 179 | << exception_class->GetDescriptor()->ToModifiedUtf8() << ".<init> " |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 180 | << "\"" << msg << "\""; |
| 181 | |
| 182 | thread->SetException(exception); |
| 183 | } |
| 184 | |
| 185 | void ThrowNewExceptionV(Thread* thread, const char* exception_class_name, const char* fmt, va_list args) { |
| 186 | char msg[512]; |
| 187 | vsnprintf(msg, sizeof(msg), fmt, args); |
| 188 | ThrowNewException(thread, exception_class_name, msg); |
| 189 | } |
| 190 | |
| 191 | void Thread::ThrowNewException(const char* exception_class_name, const char* fmt, ...) { |
| 192 | va_list args; |
| 193 | va_start(args, fmt); |
| 194 | ThrowNewExceptionV(this, exception_class_name, fmt, args); |
| 195 | va_end(args); |
| 196 | } |
| 197 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 198 | static const char* kStateNames[] = { |
| 199 | "New", |
| 200 | "Runnable", |
| 201 | "Blocked", |
| 202 | "Waiting", |
| 203 | "TimedWaiting", |
| 204 | "Native", |
| 205 | "Terminated", |
| 206 | }; |
| 207 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
| 208 | if (state >= Thread::kNew && state <= Thread::kTerminated) { |
| 209 | os << kStateNames[state-Thread::kNew]; |
| 210 | } else { |
| 211 | os << "State[" << static_cast<int>(state) << "]"; |
| 212 | } |
| 213 | return os; |
| 214 | } |
| 215 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 216 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
| 217 | os << "Thread[" << &thread |
| 218 | << ",id=" << thread.GetId() |
| 219 | << ",tid=" << thread.GetNativeId() |
| 220 | << ",state=" << thread.GetState() << "]"; |
| 221 | return os; |
| 222 | } |
| 223 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 224 | ThreadList* ThreadList::Create() { |
| 225 | return new ThreadList; |
| 226 | } |
| 227 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 228 | ThreadList::ThreadList() { |
| 229 | lock_ = Mutex::Create("ThreadList::Lock"); |
| 230 | } |
| 231 | |
| 232 | ThreadList::~ThreadList() { |
| 233 | // Make sure that all threads have exited and unregistered when we |
| 234 | // reach this point. This means that all daemon threads had been |
| 235 | // shutdown cleanly. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 236 | CHECK_LE(list_.size(), 1U); |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 237 | // TODO: wait for all other threads to unregister |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 238 | CHECK(list_.size() == 0 || list_.front() == Thread::Current()); |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 239 | // TODO: detach the current thread |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 240 | delete lock_; |
| 241 | lock_ = NULL; |
| 242 | } |
| 243 | |
| 244 | void ThreadList::Register(Thread* thread) { |
| 245 | MutexLock mu(lock_); |
| 246 | CHECK(find(list_.begin(), list_.end(), thread) == list_.end()); |
| 247 | list_.push_front(thread); |
| 248 | } |
| 249 | |
| 250 | void ThreadList::Unregister(Thread* thread) { |
| 251 | MutexLock mu(lock_); |
| 252 | CHECK(find(list_.begin(), list_.end(), thread) != list_.end()); |
| 253 | list_.remove(thread); |
| 254 | } |
| 255 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 256 | } // namespace |