Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "src/thread.h" |
| 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 | |
| 11 | #include "src/runtime.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 12 | #include "src/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; |
| 65 | new_thread->stack_limit_ = static_cast<byte*>(stack_limit); |
| 66 | new_thread->stack_base_ = new_thread->stack_limit_ + length; |
| 67 | |
| 68 | pthread_attr_t attr; |
| 69 | int result = pthread_attr_init(&attr); |
| 70 | CHECK_EQ(result, 0); |
| 71 | |
| 72 | result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 73 | CHECK_EQ(result, 0); |
| 74 | |
| 75 | pthread_t handle; |
| 76 | result = pthread_create(&handle, &attr, ThreadStart, new_thread); |
| 77 | CHECK_EQ(result, 0); |
| 78 | |
| 79 | result = pthread_attr_destroy(&attr); |
| 80 | CHECK_EQ(result, 0); |
| 81 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame^] | 82 | InitCpu(); |
| 83 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 84 | return new_thread; |
| 85 | } |
| 86 | |
| 87 | Thread* Thread::Attach() { |
| 88 | Thread* thread = new Thread; |
| 89 | |
| 90 | thread->stack_limit_ = reinterpret_cast<byte*>(-1); // TODO: getrlimit |
| 91 | uintptr_t addr = reinterpret_cast<uintptr_t>(&thread); // TODO: ask pthreads |
| 92 | uintptr_t stack_base = RoundUp(addr, 4096); |
| 93 | thread->stack_base_ = reinterpret_cast<byte*>(stack_base); |
| 94 | // TODO: set the stack size |
| 95 | |
| 96 | thread->handle_ = pthread_self(); |
| 97 | |
| 98 | thread->state_ = kRunnable; |
| 99 | |
Elliott Hughes | a5780da | 2011-07-17 11:39:39 -0700 | [diff] [blame] | 100 | errno = pthread_setspecific(Thread::pthread_key_self_, thread); |
| 101 | if (errno != 0) { |
| 102 | PLOG(FATAL) << "pthread_setspecific failed"; |
| 103 | } |
| 104 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame^] | 105 | InitCpu(); |
| 106 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 107 | return thread; |
| 108 | } |
| 109 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 110 | static void ThreadExitCheck(void* arg) { |
| 111 | LG << "Thread exit check"; |
| 112 | } |
| 113 | |
| 114 | bool Thread::Init() { |
| 115 | // Allocate a TLS slot. |
| 116 | if (pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck) != 0) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 117 | PLOG(WARNING) << "pthread_key_create failed"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
| 121 | // Double-check the TLS slot allocation. |
| 122 | if (pthread_getspecific(pthread_key_self_) != NULL) { |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 123 | LOG(WARNING) << "newly-created pthread TLS slot is not NULL"; |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | // TODO: initialize other locks and condition variables |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame^] | 132 | static const char* kStateNames[] = { |
| 133 | "New", |
| 134 | "Runnable", |
| 135 | "Blocked", |
| 136 | "Waiting", |
| 137 | "TimedWaiting", |
| 138 | "Native", |
| 139 | "Terminated", |
| 140 | }; |
| 141 | std::ostream& operator<<(std::ostream& os, const Thread::State& state) { |
| 142 | if (state >= Thread::kNew && state <= Thread::kTerminated) { |
| 143 | os << kStateNames[state-Thread::kNew]; |
| 144 | } else { |
| 145 | os << "State[" << static_cast<int>(state) << "]"; |
| 146 | } |
| 147 | return os; |
| 148 | } |
| 149 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 150 | ThreadList* ThreadList::Create() { |
| 151 | return new ThreadList; |
| 152 | } |
| 153 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 154 | ThreadList::ThreadList() { |
| 155 | lock_ = Mutex::Create("ThreadList::Lock"); |
| 156 | } |
| 157 | |
| 158 | ThreadList::~ThreadList() { |
| 159 | // Make sure that all threads have exited and unregistered when we |
| 160 | // reach this point. This means that all daemon threads had been |
| 161 | // shutdown cleanly. |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 162 | CHECK_EQ(list_.size(), 0U); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 163 | delete lock_; |
| 164 | lock_ = NULL; |
| 165 | } |
| 166 | |
| 167 | void ThreadList::Register(Thread* thread) { |
| 168 | MutexLock mu(lock_); |
| 169 | CHECK(find(list_.begin(), list_.end(), thread) == list_.end()); |
| 170 | list_.push_front(thread); |
| 171 | } |
| 172 | |
| 173 | void ThreadList::Unregister(Thread* thread) { |
| 174 | MutexLock mu(lock_); |
| 175 | CHECK(find(list_.begin(), list_.end(), thread) != list_.end()); |
| 176 | list_.remove(thread); |
| 177 | } |
| 178 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 179 | } // namespace |