blob: 68f456681ab7e6e038cb1bf23627d61b56fcfe22 [file] [log] [blame]
Carl Shapirob5573532011-07-12 18:22:59 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "thread.h"
Carl Shapirob5573532011-07-12 18:22:59 -07004
Ian Rogersb033c752011-07-20 12:22:35 -07005#include <pthread.h>
6#include <sys/mman.h>
Carl Shapirob5573532011-07-12 18:22:59 -07007#include <algorithm>
Elliott Hugheseb4f6142011-07-15 17:43:51 -07008#include <cerrno>
Carl Shapirob5573532011-07-12 18:22:59 -07009#include <list>
Carl Shapirob5573532011-07-12 18:22:59 -070010
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "runtime.h"
12#include "utils.h"
Carl Shapirob5573532011-07-12 18:22:59 -070013
14namespace art {
15
16pthread_key_t Thread::pthread_key_self_;
17
18Mutex* Mutex::Create(const char* name) {
19 Mutex* mu = new Mutex(name);
20 int result = pthread_mutex_init(&mu->lock_impl_, NULL);
Ian Rogersb033c752011-07-20 12:22:35 -070021 CHECK_EQ(0, result);
Carl Shapirob5573532011-07-12 18:22:59 -070022 return mu;
23}
24
25void Mutex::Lock() {
26 int result = pthread_mutex_lock(&lock_impl_);
27 CHECK_EQ(result, 0);
28 SetOwner(Thread::Current());
29}
30
31bool 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
42void 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 Shapiro61e019d2011-07-14 16:53:09 -070049void* ThreadStart(void *arg) {
Carl Shapirob5573532011-07-12 18:22:59 -070050 LOG(FATAL) << "Unimplemented";
51 return NULL;
52}
53
Carl Shapiro61e019d2011-07-14 16:53:09 -070054Thread* 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 Rogers176f59c2011-07-20 13:14:11 -070065 new_thread->InitCpu();
Carl Shapiro61e019d2011-07-14 16:53:09 -070066 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
86Thread* Thread::Attach() {
87 Thread* thread = new Thread;
Ian Rogers176f59c2011-07-20 13:14:11 -070088 thread->InitCpu();
Carl Shapiro61e019d2011-07-14 16:53:09 -070089 thread->stack_limit_ = reinterpret_cast<byte*>(-1); // TODO: getrlimit
90 uintptr_t addr = reinterpret_cast<uintptr_t>(&thread); // TODO: ask pthreads
Brian Carlstromb0460ea2011-07-29 10:08:05 -070091 uintptr_t stack_base = RoundUp(addr, kPageSize);
Carl Shapiro61e019d2011-07-14 16:53:09 -070092 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 Hughesa5780da2011-07-17 11:39:39 -070099 errno = pthread_setspecific(Thread::pthread_key_self_, thread);
100 if (errno != 0) {
101 PLOG(FATAL) << "pthread_setspecific failed";
102 }
103
Carl Shapiro61e019d2011-07-14 16:53:09 -0700104 return thread;
105}
106
Carl Shapirob5573532011-07-12 18:22:59 -0700107static void ThreadExitCheck(void* arg) {
108 LG << "Thread exit check";
109}
110
111bool Thread::Init() {
112 // Allocate a TLS slot.
113 if (pthread_key_create(&Thread::pthread_key_self_, ThreadExitCheck) != 0) {
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700114 PLOG(WARNING) << "pthread_key_create failed";
Carl Shapirob5573532011-07-12 18:22:59 -0700115 return false;
116 }
117
118 // Double-check the TLS slot allocation.
119 if (pthread_getspecific(pthread_key_self_) != NULL) {
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700120 LOG(WARNING) << "newly-created pthread TLS slot is not NULL";
Carl Shapirob5573532011-07-12 18:22:59 -0700121 return false;
122 }
123
124 // TODO: initialize other locks and condition variables
125
126 return true;
127}
128
Ian Rogersb033c752011-07-20 12:22:35 -0700129static const char* kStateNames[] = {
130 "New",
131 "Runnable",
132 "Blocked",
133 "Waiting",
134 "TimedWaiting",
135 "Native",
136 "Terminated",
137};
138std::ostream& operator<<(std::ostream& os, const Thread::State& state) {
139 if (state >= Thread::kNew && state <= Thread::kTerminated) {
140 os << kStateNames[state-Thread::kNew];
141 } else {
142 os << "State[" << static_cast<int>(state) << "]";
143 }
144 return os;
145}
146
Carl Shapiro61e019d2011-07-14 16:53:09 -0700147ThreadList* ThreadList::Create() {
148 return new ThreadList;
149}
150
Carl Shapirob5573532011-07-12 18:22:59 -0700151ThreadList::ThreadList() {
152 lock_ = Mutex::Create("ThreadList::Lock");
153}
154
155ThreadList::~ThreadList() {
156 // Make sure that all threads have exited and unregistered when we
157 // reach this point. This means that all daemon threads had been
158 // shutdown cleanly.
Carl Shapiro7a909592011-07-24 19:21:59 -0700159 CHECK_EQ(list_.size(), 1U);
160 // TODO: wait for all other threads to unregister
161 CHECK_EQ(list_.front(), Thread::Current());
162 // TODO: detach the current thread
Carl Shapirob5573532011-07-12 18:22:59 -0700163 delete lock_;
164 lock_ = NULL;
165}
166
167void 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
173void 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 Shapirob5573532011-07-12 18:22:59 -0700179} // namespace