blob: 9cefc82ccde8f4f411b6eaa0805de637ab9c9563 [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 Hughes475fc232011-10-25 15:00:35 -070021#include "debugger.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080022#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070023#include "scoped_thread_list_lock.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070024
Elliott Hughes8daa0922011-09-11 13:46:25 -070025namespace art {
26
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080027ThreadList::ThreadList()
Elliott Hughesffb465f2012-03-01 18:46:05 -080028 : thread_list_lock_("thread list lock", kThreadListLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070029 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070030 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughesffb465f2012-03-01 18:46:05 -080031 thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070032 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080033 VLOG(threads) << "Default stack size: " << Runtime::Current()->GetDefaultStackSize() / KB << "KiB";
Elliott Hughes8daa0922011-09-11 13:46:25 -070034}
35
36ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070037 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070038 if (Contains(Thread::Current())) {
39 Runtime::Current()->DetachCurrentThread();
40 }
41
Elliott Hughes038a8062011-09-18 14:12:41 -070042 WaitForNonDaemonThreadsToExit();
43 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070044}
45
46bool ThreadList::Contains(Thread* thread) {
47 return find(list_.begin(), list_.end(), thread) != list_.end();
48}
49
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070050pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070051 return thread_list_lock_.GetOwner();
52}
53
Elliott Hughes8daa0922011-09-11 13:46:25 -070054void ThreadList::Dump(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080055 ScopedThreadListLock thread_list_lock;
Elliott Hughesff738062012-02-03 15:00:42 -080056 DumpLocked(os);
57}
58
59void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070060 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070061 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
62 (*it)->Dump(os);
63 os << "\n";
64 }
65}
66
Elliott Hughes234ab152011-10-26 14:02:26 -070067void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
68#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080069 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
70 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -070071 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -070072#endif
Elliott Hughes47179f72011-10-27 16:44:39 -070073 if (delta == -1 && thread->suspend_count_ <= 0) {
74 // This can happen if you attach a thread during a GC.
75 LOG(WARNING) << *thread << " suspend count already zero";
76 return;
77 }
Elliott Hughes234ab152011-10-26 14:02:26 -070078 thread->suspend_count_ += delta;
79 if (for_debugger) {
80 thread->debug_suspend_count_ += delta;
81 }
82}
83
Elliott Hughes8d768a92011-09-14 16:35:25 -070084void ThreadList::FullSuspendCheck(Thread* thread) {
85 CHECK(thread != NULL);
86 CHECK_GE(thread->suspend_count_, 0);
87
88 MutexLock mu(thread_suspend_count_lock_);
89 if (thread->suspend_count_ == 0) {
90 return;
91 }
92
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080093 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -070094 {
95 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
96 while (thread->suspend_count_ != 0) {
97 /*
98 * Wait for wakeup signal, releasing lock. The act of releasing
99 * and re-acquiring the lock provides the memory barriers we
100 * need for correct behavior on SMP.
101 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700102 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700103 }
104 CHECK_EQ(thread->suspend_count_, 0);
105 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800106 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700107}
108
Elliott Hughes475fc232011-10-25 15:00:35 -0700109void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700110 Thread* self = Thread::Current();
111
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800112 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700113
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700114 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800115 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700116 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700117
118 {
119 // Increment everybody's suspend count (except our own).
120 MutexLock mu(thread_suspend_count_lock_);
121 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
122 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700123 if (thread == self || (for_debugger && thread == debug_thread)) {
124 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700125 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800126 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700127 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700128 }
129 }
130
131 /*
132 * Wait for everybody in kRunnable state to stop. Other states
133 * indicate the code is either running natively or sleeping quietly.
134 * Any attempt to transition back to kRunnable will cause a check
135 * for suspension, so it should be impossible for anything to execute
136 * interpreted code or modify objects (assuming native code plays nicely).
137 *
138 * It's also okay if the thread transitions to a non-kRunnable state.
139 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700140 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700141 * so if another thread is fiddling with its suspend count (perhaps
142 * self-suspending for the debugger) it won't block while we're waiting
143 * in here.
144 */
145 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
146 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700147 if (thread == self || (for_debugger && thread == debug_thread)) {
148 continue;
149 }
150 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800151 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700152 }
153
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800154 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700155}
156
Elliott Hughes4e235312011-12-02 11:34:15 -0800157void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700158 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700159 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700160
161 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
162
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800163 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700164
Elliott Hughes01158d72011-09-19 19:47:10 -0700165 if (!Contains(thread)) {
166 return;
167 }
168
169 {
170 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800171 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700172 }
173
174 thread->WaitUntilSuspended();
175
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800176 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700177}
178
Elliott Hughes475fc232011-10-25 15:00:35 -0700179void ThreadList::SuspendSelfForDebugger() {
180 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700181
Elliott Hughes475fc232011-10-25 15:00:35 -0700182 // The debugger thread must not suspend itself due to debugger activity!
183 Thread* debug_thread = Dbg::GetDebugThread();
184 CHECK(debug_thread != NULL);
185 CHECK(self != debug_thread);
186
187 // Collisions with other suspends aren't really interesting. We want
188 // to ensure that we're the only one fiddling with the suspend count
189 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700190 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700191 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700192
193 // Suspend ourselves.
194 CHECK_GT(self->suspend_count_, 0);
195 self->SetState(Thread::kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800196 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700197
198 // Tell JDWP that we've completed suspension. The JDWP thread can't
199 // tell us to resume before we're fully asleep because we hold the
200 // suspend count lock.
201 Dbg::ClearWaitForEventThread();
202
203 while (self->suspend_count_ != 0) {
204 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
205 if (self->suspend_count_ != 0) {
206 // The condition was signaled but we're still suspended. This
207 // can happen if the debugger lets go while a SIGQUIT thread
208 // dump event is pending (assuming SignalCatcher was resumed for
209 // just long enough to try to grab the thread-suspend lock).
210 LOG(DEBUG) << *self << " still suspended after undo "
211 << "(suspend count=" << self->suspend_count_ << ")";
212 }
213 }
214 CHECK_EQ(self->suspend_count_, 0);
215 self->SetState(Thread::kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800216 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700217}
218
219void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700220 Thread* self = Thread::Current();
221
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800222 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700223
224 // Decrement the suspend counts for all threads. No need for atomic
225 // writes, since nobody should be moving until we decrement the count.
226 // We do need to hold the thread list because of JNI attaches.
227 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800228 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700229 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700230 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700231 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
232 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700233 if (thread == self || (for_debugger && thread == debug_thread)) {
234 continue;
235 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700236 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700237 }
238 }
239
240 // Broadcast a notification to all suspended threads, some or all of
241 // which may choose to wake up. No need to wait for them.
242 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800243 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700244 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700245 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700246 }
247
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800248 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700249}
250
Elliott Hughes4e235312011-12-02 11:34:15 -0800251void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700252 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800253
254 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
255 thread_list_lock_.AssertHeld();
256 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700257
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800258 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700259
260 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700261 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700262 if (!Contains(thread)) {
263 return;
264 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800265 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700266 }
267
268 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800269 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700270 MutexLock mu(thread_suspend_count_lock_);
271 thread_suspend_count_cond_.Broadcast();
272 }
273
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800274 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700275}
276
277void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
278 DCHECK(thread != NULL);
279 Thread* self = Thread::Current();
280 if (thread != self) {
281 Suspend(thread);
282 }
283 callback(arg);
284 if (thread != self) {
285 Resume(thread);
286 }
287}
288
Elliott Hughes234ab152011-10-26 14:02:26 -0700289void ThreadList::UndoDebuggerSuspensions() {
290 Thread* self = Thread::Current();
291
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800292 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700293
294 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800295 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700296 MutexLock mu(thread_suspend_count_lock_);
297 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
298 Thread* thread = *it;
299 if (thread == self || thread->debug_suspend_count_ == 0) {
300 continue;
301 }
302 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
303 }
304 }
305
306 {
307 MutexLock mu(thread_suspend_count_lock_);
308 thread_suspend_count_cond_.Broadcast();
309 }
310
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800311 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700312}
313
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700314void ThreadList::Register() {
315 Thread* self = Thread::Current();
316
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800317 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700318
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800319 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700320 CHECK(!Contains(self));
321 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700322}
323
324void ThreadList::Unregister() {
325 Thread* self = Thread::Current();
326
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800327 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700328
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700329 if (self->GetPeer() != NULL) {
330 self->SetState(Thread::kRunnable);
331
332 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
333 // down the Thread* and removing it from the thread list (or start taking any locks).
334 self->HandleUncaughtExceptions();
335
336 // Make sure we remove from ThreadGroup before taking the
337 // thread_list_lock_ since it allocates an Iterator which can cause
338 // a GC which will want to suspend.
339 self->RemoveFromThreadGroup();
340 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700341
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800342 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700343
344 // Remove this thread from the list.
345 CHECK(Contains(self));
346 list_.remove(self);
347
348 // Delete the Thread* and release the thin lock id.
349 uint32_t thin_lock_id = self->thin_lock_id_;
350 delete self;
351 ReleaseThreadId(thin_lock_id);
352
353 // Clear the TLS data, so that thread is recognizably detached.
354 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700355 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700356
357 // Signal that a thread just detached.
358 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700359}
360
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700361void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700362 thread_list_lock_.AssertHeld();
363 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700364 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700365 }
366}
367
Elliott Hughes8daa0922011-09-11 13:46:25 -0700368void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800369 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700370 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
371 (*it)->VisitRoots(visitor, arg);
372 }
373}
374
Elliott Hughes93e74e82011-09-13 11:07:03 -0700375/*
376 * Tell a new thread it's safe to start.
377 *
378 * We must hold the thread list lock before messing with another thread.
379 * In the general case we would also need to verify that the new thread was
380 * still in the thread list, but in our case the thread has not started
381 * executing user code and therefore has not had a chance to exit.
382 *
383 * We move it to kVmWait, and it then shifts itself to kRunning, which
384 * comes with a suspend-pending check. We do this after
385 */
386void ThreadList::SignalGo(Thread* child) {
387 Thread* self = Thread::Current();
388 CHECK(child != self);
389
390 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800391 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800392 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700393
394 // We wait for the child to tell us that it's in the thread list.
395 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700396 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700397 }
398 }
399
400 // If we switch out of runnable and then back in, we know there's no pending suspend.
401 self->SetState(Thread::kVmWait);
402 self->SetState(Thread::kRunnable);
403
404 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800405 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800406 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700407 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700408 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700409}
410
411void ThreadList::WaitForGo() {
412 Thread* self = Thread::Current();
413 DCHECK(Contains(self));
414
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700415 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800416 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700417
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700418 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800419 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700420 self->SetState(Thread::kStarting);
421 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700422
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700423 // Wait until our parent tells us there's no suspend still pending
424 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800425 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700426 while (self->GetState() != Thread::kVmWait) {
427 thread_start_cond_.Wait(thread_list_lock_);
428 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700429 }
430
431 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800432 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700433 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
434 // started, we wait until it's over. Which means that if there's now another GC pending, our
435 // suspend count is non-zero, so switching to the runnable state will suspend us.
436 // TODO: find a better solution!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800437 {
438 ScopedHeapLock heap_lock;
439 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700440 self->SetState(Thread::kRunnable);
441}
442
Elliott Hughes038a8062011-09-18 14:12:41 -0700443bool ThreadList::AllThreadsAreDaemons() {
444 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700445 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
446 // is null.
447 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700448 return false;
449 }
450 }
451 return true;
452}
453
454void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800455 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700456 while (!AllThreadsAreDaemons()) {
457 thread_exit_cond_.Wait(thread_list_lock_);
458 }
459}
460
461void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800462 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700463
464 // Tell all the daemons it's time to suspend. (At this point, we know
465 // all threads are daemons.)
466 {
467 MutexLock mu(thread_suspend_count_lock_);
468 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
469 Thread* thread = *it;
470 ++thread->suspend_count_;
471 }
472 }
473
474 // Give the threads a chance to suspend, complaining if they're slow.
475 bool have_complained = false;
476 for (int i = 0; i < 10; ++i) {
477 usleep(200 * 1000);
478 bool all_suspended = true;
479 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
480 Thread* thread = *it;
481 if (thread->GetState() == Thread::kRunnable) {
482 if (!have_complained) {
483 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
484 have_complained = true;
485 }
486 all_suspended = false;
487 }
488 }
489 if (all_suspended) {
490 return;
491 }
492 }
493}
494
Elliott Hughes8daa0922011-09-11 13:46:25 -0700495uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800496 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700497 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
498 if (!allocated_ids_[i]) {
499 allocated_ids_.set(i);
500 return i + 1; // Zero is reserved to mean "invalid".
501 }
502 }
503 LOG(FATAL) << "Out of internal thread ids";
504 return 0;
505}
506
507void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700508 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700509 --id; // Zero is reserved to mean "invalid".
510 DCHECK(allocated_ids_[id]) << id;
511 allocated_ids_.reset(id);
512}
513
514} // namespace art