blob: f17414cb34aec62e78c36272363b34afd650168f [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
Elliott Hughes038a8062011-09-18 14:12:41 -070019#include <unistd.h>
20
Elliott Hughes8daa0922011-09-11 13:46:25 -070021namespace art {
22
Elliott Hughes8d768a92011-09-14 16:35:25 -070023ThreadList::ThreadList()
24 : thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070025 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070026 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070027 thread_suspend_count_lock_("thread suspend count lock"),
28 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes8daa0922011-09-11 13:46:25 -070029}
30
31ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070032 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070033 if (Contains(Thread::Current())) {
34 Runtime::Current()->DetachCurrentThread();
35 }
36
Elliott Hughes038a8062011-09-18 14:12:41 -070037 WaitForNonDaemonThreadsToExit();
38 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070039}
40
41bool ThreadList::Contains(Thread* thread) {
42 return find(list_.begin(), list_.end(), thread) != list_.end();
43}
44
45void ThreadList::Dump(std::ostream& os) {
Elliott Hughes8d768a92011-09-14 16:35:25 -070046 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070047 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070048 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
49 (*it)->Dump(os);
50 os << "\n";
51 }
52}
53
Elliott Hughes8d768a92011-09-14 16:35:25 -070054void ThreadList::FullSuspendCheck(Thread* thread) {
55 CHECK(thread != NULL);
56 CHECK_GE(thread->suspend_count_, 0);
57
58 MutexLock mu(thread_suspend_count_lock_);
59 if (thread->suspend_count_ == 0) {
60 return;
61 }
62
63 //LOG(INFO) << *thread << " self-suspending";
64 {
65 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
66 while (thread->suspend_count_ != 0) {
67 /*
68 * Wait for wakeup signal, releasing lock. The act of releasing
69 * and re-acquiring the lock provides the memory barriers we
70 * need for correct behavior on SMP.
71 */
Elliott Hughes5f791332011-09-15 17:45:30 -070072 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -070073 }
74 CHECK_EQ(thread->suspend_count_, 0);
75 }
76 //LOG(INFO) << *thread << " self-reviving";
77}
78
79void ThreadList::SuspendAll() {
80 Thread* self = Thread::Current();
81
82 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
83
84 //LOG(INFO) << *self << " SuspendAll starting...";
85
86 MutexLock mu(thread_list_lock_);
87
88 {
89 // Increment everybody's suspend count (except our own).
90 MutexLock mu(thread_suspend_count_lock_);
91 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
92 Thread* thread = *it;
93 if (thread != self) {
94 //LOG(INFO) << "requesting thread suspend: " << *thread;
95 ++thread->suspend_count_;
96 }
97 }
98 }
99
100 /*
101 * Wait for everybody in kRunnable state to stop. Other states
102 * indicate the code is either running natively or sleeping quietly.
103 * Any attempt to transition back to kRunnable will cause a check
104 * for suspension, so it should be impossible for anything to execute
105 * interpreted code or modify objects (assuming native code plays nicely).
106 *
107 * It's also okay if the thread transitions to a non-kRunnable state.
108 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700109 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700110 * so if another thread is fiddling with its suspend count (perhaps
111 * self-suspending for the debugger) it won't block while we're waiting
112 * in here.
113 */
114 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
115 Thread* thread = *it;
116 if (thread != self) {
117 thread->WaitUntilSuspended();
118 //LOG(INFO) << "thread suspended: " << *thread;
119 }
120 }
121
122 //LOG(INFO) << *self << " SuspendAll complete";
123}
124
125void ThreadList::ResumeAll() {
126 Thread* self = Thread::Current();
127
128 //LOG(INFO) << *self << " ResumeAll starting";
129
130 // Decrement the suspend counts for all threads. No need for atomic
131 // writes, since nobody should be moving until we decrement the count.
132 // We do need to hold the thread list because of JNI attaches.
133 {
134 MutexLock mu1(thread_list_lock_);
135 MutexLock mu2(thread_suspend_count_lock_);
136 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
137 Thread* thread = *it;
138 if (thread != self) {
139 if (thread->suspend_count_ > 0) {
140 --thread->suspend_count_;
141 } else {
142 LOG(WARNING) << *thread << " suspend count already zero";
143 }
144 }
145 }
146 }
147
148 // Broadcast a notification to all suspended threads, some or all of
149 // which may choose to wake up. No need to wait for them.
150 {
151 //LOG(INFO) << *self << " ResumeAll waking others";
152 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700153 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700154 }
155
156 //LOG(INFO) << *self << " ResumeAll complete";
157}
158
Elliott Hughes8daa0922011-09-11 13:46:25 -0700159void ThreadList::Register(Thread* thread) {
160 //LOG(INFO) << "ThreadList::Register() " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700161 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700162 CHECK(!Contains(thread));
163 list_.push_back(thread);
164}
165
166void ThreadList::Unregister() {
167 Thread* self = Thread::Current();
168
Elliott Hughes93e74e82011-09-13 11:07:03 -0700169 //LOG(INFO) << "ThreadList::Unregister() " << *self;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700170 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700171
172 // Remove this thread from the list.
173 CHECK(Contains(self));
174 list_.remove(self);
175
176 // Delete the Thread* and release the thin lock id.
177 uint32_t thin_lock_id = self->thin_lock_id_;
178 delete self;
179 ReleaseThreadId(thin_lock_id);
180
181 // Clear the TLS data, so that thread is recognizably detached.
182 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700183 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700184
185 // Signal that a thread just detached.
186 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700187}
188
189void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700190 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700191 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
192 (*it)->VisitRoots(visitor, arg);
193 }
194}
195
Elliott Hughes93e74e82011-09-13 11:07:03 -0700196/*
197 * Tell a new thread it's safe to start.
198 *
199 * We must hold the thread list lock before messing with another thread.
200 * In the general case we would also need to verify that the new thread was
201 * still in the thread list, but in our case the thread has not started
202 * executing user code and therefore has not had a chance to exit.
203 *
204 * We move it to kVmWait, and it then shifts itself to kRunning, which
205 * comes with a suspend-pending check. We do this after
206 */
207void ThreadList::SignalGo(Thread* child) {
208 Thread* self = Thread::Current();
209 CHECK(child != self);
210
211 {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700212 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700213
214 // We wait for the child to tell us that it's in the thread list.
215 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700216 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700217 }
218 }
219
220 // If we switch out of runnable and then back in, we know there's no pending suspend.
221 self->SetState(Thread::kVmWait);
222 self->SetState(Thread::kRunnable);
223
224 // Tell the child that it's safe: it will see any future suspend request.
225 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700226 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700227}
228
229void ThreadList::WaitForGo() {
230 Thread* self = Thread::Current();
231 DCHECK(Contains(self));
232
Elliott Hughes8d768a92011-09-14 16:35:25 -0700233 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700234
235 // Tell our parent that we're in the thread list.
236 self->SetState(Thread::kStarting);
Elliott Hughes5f791332011-09-15 17:45:30 -0700237 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700238
239 // Wait until our parent tells us there's no suspend still pending
240 // from before we were on the thread list.
241 while (self->GetState() != Thread::kVmWait) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700242 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700243 }
244
245 // Enter the runnable state. We know that any pending suspend will affect us now.
246 self->SetState(Thread::kRunnable);
247}
248
Elliott Hughes038a8062011-09-18 14:12:41 -0700249bool ThreadList::AllThreadsAreDaemons() {
250 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
251 if (!(*it)->IsDaemon()) {
252 return false;
253 }
254 }
255 return true;
256}
257
258void ThreadList::WaitForNonDaemonThreadsToExit() {
259 MutexLock mu(thread_list_lock_);
260 while (!AllThreadsAreDaemons()) {
261 thread_exit_cond_.Wait(thread_list_lock_);
262 }
263}
264
265void ThreadList::SuspendAllDaemonThreads() {
266 MutexLock mu(thread_list_lock_);
267
268 // Tell all the daemons it's time to suspend. (At this point, we know
269 // all threads are daemons.)
270 {
271 MutexLock mu(thread_suspend_count_lock_);
272 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
273 Thread* thread = *it;
274 ++thread->suspend_count_;
275 }
276 }
277
278 // Give the threads a chance to suspend, complaining if they're slow.
279 bool have_complained = false;
280 for (int i = 0; i < 10; ++i) {
281 usleep(200 * 1000);
282 bool all_suspended = true;
283 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
284 Thread* thread = *it;
285 if (thread->GetState() == Thread::kRunnable) {
286 if (!have_complained) {
287 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
288 have_complained = true;
289 }
290 all_suspended = false;
291 }
292 }
293 if (all_suspended) {
294 return;
295 }
296 }
297}
298
Elliott Hughes8daa0922011-09-11 13:46:25 -0700299uint32_t ThreadList::AllocThreadId() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700300 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700301 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
302 if (!allocated_ids_[i]) {
303 allocated_ids_.set(i);
304 return i + 1; // Zero is reserved to mean "invalid".
305 }
306 }
307 LOG(FATAL) << "Out of internal thread ids";
308 return 0;
309}
310
311void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700312 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700313 --id; // Zero is reserved to mean "invalid".
314 DCHECK(allocated_ids_[id]) << id;
315 allocated_ids_.reset(id);
316}
317
318} // namespace art