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 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "runtime.h" |
| 12 | #include "utils.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 13 | |
| 14 | namespace art { |
| 15 | |
| 16 | pthread_key_t Thread::pthread_key_self_; |
| 17 | |
| 18 | Mutex* Mutex::Create(const char* name) { |
| 19 | Mutex* mu = new Mutex(name); |
| 20 | int result = pthread_mutex_init(&mu->lock_impl_, NULL); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 21 | CHECK_EQ(0, result); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 22 | return mu; |
| 23 | } |
| 24 | |
| 25 | void Mutex::Lock() { |
| 26 | int result = pthread_mutex_lock(&lock_impl_); |
| 27 | CHECK_EQ(result, 0); |
| 28 | SetOwner(Thread::Current()); |
| 29 | } |
| 30 | |
| 31 | bool Mutex::TryLock() { |
| 32 | int result = pthread_mutex_lock(&lock_impl_); |
| 33 | if (result == EBUSY) { |
| 34 | return false; |
| 35 | } else { |
| 36 | CHECK_EQ(result, 0); |
| 37 | SetOwner(Thread::Current()); |
| 38 | return true; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void Mutex::Unlock() { |
| 43 | CHECK(GetOwner() == Thread::Current()); |
| 44 | int result = pthread_mutex_unlock(&lock_impl_); |
| 45 | CHECK_EQ(result, 0); |
| 46 | SetOwner(Thread::Current()); |
| 47 | } |
| 48 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 49 | void* ThreadStart(void *arg) { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 50 | LOG(FATAL) << "Unimplemented"; |
| 51 | return NULL; |
| 52 | } |
| 53 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 54 | Thread* Thread::Create(size_t stack_size) { |
| 55 | int prot = PROT_READ | PROT_WRITE; |
| 56 | // TODO: require the stack size to be page aligned? |
| 57 | size_t length = RoundUp(stack_size, 0x1000); |
| 58 | void* stack_limit = mmap(NULL, length, prot, MAP_PRIVATE, -1, 0); |
| 59 | if (stack_limit == MAP_FAILED) { |
| 60 | LOG(FATAL) << "mmap"; |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | Thread* new_thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 65 | new_thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 66 | new_thread->stack_limit_ = static_cast<byte*>(stack_limit); |
| 67 | new_thread->stack_base_ = new_thread->stack_limit_ + length; |
| 68 | |
| 69 | pthread_attr_t attr; |
| 70 | int result = pthread_attr_init(&attr); |
| 71 | CHECK_EQ(result, 0); |
| 72 | |
| 73 | result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 74 | CHECK_EQ(result, 0); |
| 75 | |
| 76 | pthread_t handle; |
| 77 | result = pthread_create(&handle, &attr, ThreadStart, new_thread); |
| 78 | CHECK_EQ(result, 0); |
| 79 | |
| 80 | result = pthread_attr_destroy(&attr); |
| 81 | CHECK_EQ(result, 0); |
| 82 | |
| 83 | return new_thread; |
| 84 | } |
| 85 | |
| 86 | Thread* Thread::Attach() { |
| 87 | Thread* thread = new Thread; |
Ian Rogers | 176f59c | 2011-07-20 13:14:11 -0700 | [diff] [blame] | 88 | thread->InitCpu(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 89 | thread->stack_limit_ = reinterpret_cast<byte*>(-1); // TODO: getrlimit |
| 90 | uintptr_t addr = reinterpret_cast<uintptr_t>(&thread); // TODO: ask pthreads |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 91 | uintptr_t stack_base = RoundUp(addr, kPageSize); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 92 | thread->stack_base_ = reinterpret_cast<byte*>(stack_base); |
| 93 | // TODO: set the stack size |
| 94 | |
| 95 | thread->handle_ = pthread_self(); |
| 96 | |
| 97 | thread->state_ = kRunnable; |
| 98 | |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 99 | errno = pthread_setspecific(Thread::pthread_key_self_, thread); |
| 100 | if (errno != 0) { |
| 101 | PLOG(FATAL) << "pthread_setspecific failed"; |
| 102 | } |
| 103 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame^] | 104 | thread->jni_env_ = CreateJNIEnv(); |
| 105 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 106 | return thread; |
| 107 | } |
| 108 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 109 | static void ThreadExitCheck(void* arg) { |
| 110 | LG << "Thread exit check"; |
| 111 | } |
| 112 | |
| 113 | bool Thread::Init() { |
| 114 | // Allocate a TLS slot. |
| 115 | if (pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck) != 0) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 116 | PLOG(WARNING) << "pthread_key_create failed"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
| 120 | // Double-check the TLS slot allocation. |
| 121 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 122 | LOG(WARNING) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
| 126 | // TODO: initialize other locks and condition variables |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 131 | static const char* kStateNames[] = { |
| 132 | "New", |
| 133 | "Runnable", |
| 134 | "Blocked", |
| 135 | "Waiting", |
| 136 | "TimedWaiting", |
| 137 | "Native", |
| 138 | "Terminated", |
| 139 | }; |
| 140 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
| 141 | if (state >= Thread::kNew && state <= Thread::kTerminated) { |
| 142 | os << kStateNames[state-Thread::kNew]; |
| 143 | } else { |
| 144 | os << "State[" << static_cast<int>(state) << "]"; |
| 145 | } |
| 146 | return os; |
| 147 | } |
| 148 | |
Elliott Hughes | 330304d | 2011-08-12 14:28:05 -0700 | [diff] [blame^] | 149 | std::ostream& operator<<(std::ostream& os, const Thread& thread) { |
| 150 | os << "Thread[" << &thread |
| 151 | << ",id=" << thread.GetId() |
| 152 | << ",tid=" << thread.GetNativeId() |
| 153 | << ",state=" << thread.GetState() << "]"; |
| 154 | return os; |
| 155 | } |
| 156 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 157 | ThreadList* ThreadList::Create() { |
| 158 | return new ThreadList; |
| 159 | } |
| 160 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 161 | ThreadList::ThreadList() { |
| 162 | lock_ = Mutex::Create("ThreadList::Lock"); |
| 163 | } |
| 164 | |
| 165 | ThreadList::~ThreadList() { |
| 166 | // Make sure that all threads have exited and unregistered when we |
| 167 | // reach this point. This means that all daemon threads had been |
| 168 | // shutdown cleanly. |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 169 | CHECK_EQ(list_.size(), 1U); |
| 170 | // TODO: wait for all other threads to unregister |
| 171 | CHECK_EQ(list_.front(), Thread::Current()); |
| 172 | // TODO: detach the current thread |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 173 | delete lock_; |
| 174 | lock_ = NULL; |
| 175 | } |
| 176 | |
| 177 | void ThreadList::Register(Thread* thread) { |
| 178 | MutexLock mu(lock_); |
| 179 | CHECK(find(list_.begin(), list_.end(), thread) == list_.end()); |
| 180 | list_.push_front(thread); |
| 181 | } |
| 182 | |
| 183 | void ThreadList::Unregister(Thread* thread) { |
| 184 | MutexLock mu(lock_); |
| 185 | CHECK(find(list_.begin(), list_.end(), thread) != list_.end()); |
| 186 | list_.remove(thread); |
| 187 | } |
| 188 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 189 | } // namespace |