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" |
| 12 | #include "object.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 13 | #include "runtime.h" |
| 14 | #include "utils.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 15 | |
| 16 | namespace art { |
| 17 | |
| 18 | pthread_key_t Thread::pthread_key_self_; |
| 19 | |
| 20 | Mutex* Mutex::Create(const char* name) { |
| 21 | Mutex* mu = new Mutex(name); |
| 22 | int result = pthread_mutex_init(&mu->lock_impl_, NULL); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 23 | CHECK_EQ(0, result); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 24 | return mu; |
| 25 | } |
| 26 | |
| 27 | void Mutex::Lock() { |
| 28 | int result = pthread_mutex_lock(&lock_impl_); |
| 29 | CHECK_EQ(result, 0); |
| 30 | SetOwner(Thread::Current()); |
| 31 | } |
| 32 | |
| 33 | bool 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 | |
| 44 | void 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 Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 51 | void* ThreadStart(void *arg) { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 52 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 53 | return NULL; |
| 54 | } |
| 55 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 56 | Thread* Thread::Create(size_t stack_size) { |
| 57 | int prot = PROT_READ | PROT_WRITE; |
| 58 | // TODO: require the stack size to be page aligned? |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame^] | 59 | size_t length = RoundUp(stack_size, kPageSize); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 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 Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 67 | new_thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 68 | 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 Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 88 | Thread* Thread::Attach(const Runtime* runtime) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 89 | Thread* thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 90 | thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 91 | thread->stack_limit_ = reinterpret_cast<byte*>(-1); // TODO: getrlimit |
| 92 | uintptr_t addr = reinterpret_cast<uintptr_t>(&thread); // TODO: ask pthreads |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 93 | uintptr_t stack_base = RoundUp(addr, kPageSize); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 94 | 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 Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 101 | errno = pthread_setspecific(Thread::pthread_key_self_, thread); |
| 102 | if (errno != 0) { |
| 103 | PLOG(FATAL) << "pthread_setspecific failed"; |
| 104 | } |
| 105 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 106 | JavaVMExt* vm = runtime->GetJavaVM(); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 107 | CHECK(vm != NULL); |
| 108 | bool check_jni = vm->check_jni; |
| 109 | thread->jni_env_ = reinterpret_cast<JNIEnv*>(new JNIEnvExt(thread, check_jni)); |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 110 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 111 | return thread; |
| 112 | } |
| 113 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 114 | static void ThreadExitCheck(void* arg) { |
| 115 | LG << "Thread exit check"; |
| 116 | } |
| 117 | |
| 118 | bool Thread::Init() { |
| 119 | // Allocate a TLS slot. |
| 120 | if (pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck) != 0) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 121 | PLOG(WARNING) << "pthread_key_create failed"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
| 125 | // Double-check the TLS slot allocation. |
| 126 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 127 | LOG(WARNING) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
| 131 | // TODO: initialize other locks and condition variables |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 136 | void 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 Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 153 | CHECK(ctor != NULL); |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 154 | |
| 155 | // TODO: need to *call* the constructor! |
| 156 | UNIMPLEMENTED(WARNING) << "can't call " |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 157 | << exception_class->GetDescriptor() << ".<init> " |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 158 | << "\"" << msg << "\""; |
| 159 | |
| 160 | thread->SetException(exception); |
| 161 | } |
| 162 | |
| 163 | void 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 | |
| 169 | void 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 Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 176 | static const char* kStateNames[] = { |
| 177 | "New", |
| 178 | "Runnable", |
| 179 | "Blocked", |
| 180 | "Waiting", |
| 181 | "TimedWaiting", |
| 182 | "Native", |
| 183 | "Terminated", |
| 184 | }; |
| 185 | std::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 Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame] | 194 | std::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 Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 202 | ThreadList* ThreadList::Create() { |
| 203 | return new ThreadList; |
| 204 | } |
| 205 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 206 | ThreadList::ThreadList() { |
| 207 | lock_ = Mutex::Create("ThreadList::Lock"); |
| 208 | } |
| 209 | |
| 210 | ThreadList::~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 Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 214 | 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 Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 218 | delete lock_; |
| 219 | lock_ = NULL; |
| 220 | } |
| 221 | |
| 222 | void 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 | |
| 228 | void 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 Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 234 | } // namespace |