blob: 63cbf40d89dc1ebe006306dd8e6666f0ddcff211 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "thread_list.h"
18
19namespace art {
20
Elliott Hughes8d768a92011-09-14 16:35:25 -070021ThreadList::ThreadList()
22 : thread_list_lock_("thread list lock"),
23 thread_suspend_count_lock_("thread suspend count lock") {
24 CHECK_PTHREAD_CALL(pthread_cond_init, (&thread_start_cond_, NULL), "thread_start_cond_");
25 CHECK_PTHREAD_CALL(pthread_cond_init, (&thread_suspend_count_cond_, NULL), "thread_suspend_count_cond_");
Elliott Hughes8daa0922011-09-11 13:46:25 -070026}
27
28ThreadList::~ThreadList() {
29 if (Contains(Thread::Current())) {
30 Runtime::Current()->DetachCurrentThread();
31 }
32
Elliott Hughes8d768a92011-09-14 16:35:25 -070033 CHECK_PTHREAD_CALL(pthread_cond_destroy, (&thread_start_cond_), "thread_start_cond_");
34 CHECK_PTHREAD_CALL(pthread_cond_destroy, (&thread_suspend_count_cond_), "thread_suspend_count_cond_");
35
Elliott Hughes8daa0922011-09-11 13:46:25 -070036 // All threads should have exited and unregistered when we
37 // reach this point. This means that all daemon threads had been
38 // shutdown cleanly.
39 // TODO: dump ThreadList if non-empty.
40 CHECK_EQ(list_.size(), 0U);
41}
42
43bool ThreadList::Contains(Thread* thread) {
44 return find(list_.begin(), list_.end(), thread) != list_.end();
45}
46
47void ThreadList::Dump(std::ostream& os) {
Elliott Hughes8d768a92011-09-14 16:35:25 -070048 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070049 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070050 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
51 (*it)->Dump(os);
52 os << "\n";
53 }
54}
55
Elliott Hughes8d768a92011-09-14 16:35:25 -070056void ThreadList::FullSuspendCheck(Thread* thread) {
57 CHECK(thread != NULL);
58 CHECK_GE(thread->suspend_count_, 0);
59
60 MutexLock mu(thread_suspend_count_lock_);
61 if (thread->suspend_count_ == 0) {
62 return;
63 }
64
65 //LOG(INFO) << *thread << " self-suspending";
66 {
67 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
68 while (thread->suspend_count_ != 0) {
69 /*
70 * Wait for wakeup signal, releasing lock. The act of releasing
71 * and re-acquiring the lock provides the memory barriers we
72 * need for correct behavior on SMP.
73 */
74 CHECK_PTHREAD_CALL(pthread_cond_wait, (&thread_suspend_count_cond_, thread_suspend_count_lock_.GetImpl()), __FUNCTION__);
75 }
76 CHECK_EQ(thread->suspend_count_, 0);
77 }
78 //LOG(INFO) << *thread << " self-reviving";
79}
80
81void ThreadList::SuspendAll() {
82 Thread* self = Thread::Current();
83
84 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
85
86 //LOG(INFO) << *self << " SuspendAll starting...";
87
88 MutexLock mu(thread_list_lock_);
89
90 {
91 // Increment everybody's suspend count (except our own).
92 MutexLock mu(thread_suspend_count_lock_);
93 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
94 Thread* thread = *it;
95 if (thread != self) {
96 //LOG(INFO) << "requesting thread suspend: " << *thread;
97 ++thread->suspend_count_;
98 }
99 }
100 }
101
102 /*
103 * Wait for everybody in kRunnable state to stop. Other states
104 * indicate the code is either running natively or sleeping quietly.
105 * Any attempt to transition back to kRunnable will cause a check
106 * for suspension, so it should be impossible for anything to execute
107 * interpreted code or modify objects (assuming native code plays nicely).
108 *
109 * It's also okay if the thread transitions to a non-kRunnable state.
110 *
111 * Note we released the threadSuspendCountLock before getting here,
112 * so if another thread is fiddling with its suspend count (perhaps
113 * self-suspending for the debugger) it won't block while we're waiting
114 * in here.
115 */
116 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
117 Thread* thread = *it;
118 if (thread != self) {
119 thread->WaitUntilSuspended();
120 //LOG(INFO) << "thread suspended: " << *thread;
121 }
122 }
123
124 //LOG(INFO) << *self << " SuspendAll complete";
125}
126
127void ThreadList::ResumeAll() {
128 Thread* self = Thread::Current();
129
130 //LOG(INFO) << *self << " ResumeAll starting";
131
132 // Decrement the suspend counts for all threads. No need for atomic
133 // writes, since nobody should be moving until we decrement the count.
134 // We do need to hold the thread list because of JNI attaches.
135 {
136 MutexLock mu1(thread_list_lock_);
137 MutexLock mu2(thread_suspend_count_lock_);
138 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
139 Thread* thread = *it;
140 if (thread != self) {
141 if (thread->suspend_count_ > 0) {
142 --thread->suspend_count_;
143 } else {
144 LOG(WARNING) << *thread << " suspend count already zero";
145 }
146 }
147 }
148 }
149
150 // Broadcast a notification to all suspended threads, some or all of
151 // which may choose to wake up. No need to wait for them.
152 {
153 //LOG(INFO) << *self << " ResumeAll waking others";
154 MutexLock mu(thread_suspend_count_lock_);
155 CHECK_PTHREAD_CALL(pthread_cond_broadcast, (&thread_suspend_count_cond_), "thread_suspend_count_cond_");
156 }
157
158 //LOG(INFO) << *self << " ResumeAll complete";
159}
160
Elliott Hughes8daa0922011-09-11 13:46:25 -0700161void ThreadList::Register(Thread* thread) {
162 //LOG(INFO) << "ThreadList::Register() " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700163 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700164 CHECK(!Contains(thread));
165 list_.push_back(thread);
166}
167
168void ThreadList::Unregister() {
169 Thread* self = Thread::Current();
170
Elliott Hughes93e74e82011-09-13 11:07:03 -0700171 //LOG(INFO) << "ThreadList::Unregister() " << *self;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700172 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700173
174 // Remove this thread from the list.
175 CHECK(Contains(self));
176 list_.remove(self);
177
178 // Delete the Thread* and release the thin lock id.
179 uint32_t thin_lock_id = self->thin_lock_id_;
180 delete self;
181 ReleaseThreadId(thin_lock_id);
182
183 // Clear the TLS data, so that thread is recognizably detached.
184 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700185 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes8daa0922011-09-11 13:46:25 -0700186}
187
188void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700189 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700190 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
191 (*it)->VisitRoots(visitor, arg);
192 }
193}
194
Elliott Hughes93e74e82011-09-13 11:07:03 -0700195/*
196 * Tell a new thread it's safe to start.
197 *
198 * We must hold the thread list lock before messing with another thread.
199 * In the general case we would also need to verify that the new thread was
200 * still in the thread list, but in our case the thread has not started
201 * executing user code and therefore has not had a chance to exit.
202 *
203 * We move it to kVmWait, and it then shifts itself to kRunning, which
204 * comes with a suspend-pending check. We do this after
205 */
206void ThreadList::SignalGo(Thread* child) {
207 Thread* self = Thread::Current();
208 CHECK(child != self);
209
210 {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700211 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700212
213 // We wait for the child to tell us that it's in the thread list.
214 while (child->GetState() != Thread::kStarting) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700215 CHECK_PTHREAD_CALL(pthread_cond_wait, (&thread_start_cond_, thread_list_lock_.GetImpl()), __FUNCTION__);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700216 }
217 }
218
219 // If we switch out of runnable and then back in, we know there's no pending suspend.
220 self->SetState(Thread::kVmWait);
221 self->SetState(Thread::kRunnable);
222
223 // Tell the child that it's safe: it will see any future suspend request.
224 child->SetState(Thread::kVmWait);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700225 CHECK_PTHREAD_CALL(pthread_cond_broadcast, (&thread_start_cond_), __FUNCTION__);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700226}
227
228void ThreadList::WaitForGo() {
229 Thread* self = Thread::Current();
230 DCHECK(Contains(self));
231
Elliott Hughes8d768a92011-09-14 16:35:25 -0700232 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700233
234 // Tell our parent that we're in the thread list.
235 self->SetState(Thread::kStarting);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700236 CHECK_PTHREAD_CALL(pthread_cond_broadcast, (&thread_start_cond_), __FUNCTION__);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700237
238 // Wait until our parent tells us there's no suspend still pending
239 // from before we were on the thread list.
240 while (self->GetState() != Thread::kVmWait) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700241 CHECK_PTHREAD_CALL(pthread_cond_wait, (&thread_start_cond_, thread_list_lock_.GetImpl()), __FUNCTION__);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700242 }
243
244 // Enter the runnable state. We know that any pending suspend will affect us now.
245 self->SetState(Thread::kRunnable);
246}
247
Elliott Hughes8daa0922011-09-11 13:46:25 -0700248uint32_t ThreadList::AllocThreadId() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700249 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700250 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
251 if (!allocated_ids_[i]) {
252 allocated_ids_.set(i);
253 return i + 1; // Zero is reserved to mean "invalid".
254 }
255 }
256 LOG(FATAL) << "Out of internal thread ids";
257 return 0;
258}
259
260void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700261 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700262 --id; // Zero is reserved to mean "invalid".
263 DCHECK(allocated_ids_[id]) << id;
264 allocated_ids_.reset(id);
265}
266
267} // namespace art