blob: 3d0e0be4cdf635dec5460c23fb85cbe9abea52cf [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 Hughesabbe07d2012-06-05 17:42:23 -070019#include <dirent.h>
20#include <sys/types.h>
Elliott Hughes038a8062011-09-18 14:12:41 -070021#include <unistd.h>
22
Elliott Hughes475fc232011-10-25 15:00:35 -070023#include "debugger.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080024#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070025#include "scoped_thread_list_lock.h"
Elliott Hughesabbe07d2012-06-05 17:42:23 -070026#include "utils.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070027
Elliott Hughes8daa0922011-09-11 13:46:25 -070028namespace art {
29
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080030ThreadList::ThreadList()
Elliott Hughese52e49b2012-04-02 16:05:44 -070031 : allocated_ids_lock_("allocated thread ids lock"),
32 thread_list_lock_("thread list lock", kThreadListLock),
Elliott Hughese62934d2012-04-09 11:24:29 -070033 thread_start_cond_("thread start condition variable"),
34 thread_exit_cond_("thread exit condition variable"),
Elliott Hughesffb465f2012-03-01 18:46:05 -080035 thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock),
Elliott Hughese62934d2012-04-09 11:24:29 -070036 thread_suspend_count_cond_("thread suspend count condition variable") {
Elliott Hughes409d2732012-04-03 13:34:44 -070037 VLOG(threads) << "Default stack size: " << PrettySize(Runtime::Current()->GetDefaultStackSize());
Elliott Hughes8daa0922011-09-11 13:46:25 -070038}
39
40ThreadList::~ThreadList() {
Elliott Hughese52e49b2012-04-02 16:05:44 -070041 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
Elliott Hughes6a144332012-04-03 13:07:11 -070042 // We need to detach the current thread here in case there's another thread waiting to join with
43 // us.
Elliott Hughes8daa0922011-09-11 13:46:25 -070044 if (Contains(Thread::Current())) {
45 Runtime::Current()->DetachCurrentThread();
46 }
Elliott Hughes6a144332012-04-03 13:07:11 -070047
48 WaitForOtherNonDaemonThreadsToExit();
49 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070050}
51
52bool ThreadList::Contains(Thread* thread) {
53 return find(list_.begin(), list_.end(), thread) != list_.end();
54}
55
Elliott Hughesabbe07d2012-06-05 17:42:23 -070056bool ThreadList::Contains(pid_t tid) {
57 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
58 if ((*it)->tid_ == tid) {
59 return true;
60 }
61 }
62 return false;
63}
64
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070065pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070066 return thread_list_lock_.GetOwner();
67}
68
Elliott Hughesc967f782012-04-16 10:23:15 -070069void ThreadList::DumpForSigQuit(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080070 ScopedThreadListLock thread_list_lock;
Elliott Hughesff738062012-02-03 15:00:42 -080071 DumpLocked(os);
Elliott Hughesabbe07d2012-06-05 17:42:23 -070072 DumpUnattachedThreads(os);
73}
74
75static void DumpUnattachedThread(std::ostream& os, pid_t tid) {
76 Thread::DumpState(os, NULL, tid);
77 DumpKernelStack(os, tid, " kernel: ", false);
78 DumpNativeStack(os, tid, " native: ", false);
79 os << "\n";
80}
81
82void ThreadList::DumpUnattachedThreads(std::ostream& os) {
83 DIR* d = opendir("/proc/self/task");
84 if (!d) {
85 return;
86 }
87
88 dirent de;
89 dirent* result;
90 while (!readdir_r(d, &de, &result) && result != NULL) {
91 char* end;
92 pid_t tid = strtol(de.d_name, &end, 10);
93 if (!*end && !Contains(tid)) {
94 DumpUnattachedThread(os, tid);
95 }
96 }
97 closedir(d);
Elliott Hughesff738062012-02-03 15:00:42 -080098}
99
100void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700101 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700102 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
103 (*it)->Dump(os);
104 os << "\n";
105 }
106}
107
Elliott Hughes234ab152011-10-26 14:02:26 -0700108void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
109#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800110 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
111 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -0700112 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700113#endif
Elliott Hughes47179f72011-10-27 16:44:39 -0700114 if (delta == -1 && thread->suspend_count_ <= 0) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700115 // This is expected if you attach a thread during a GC.
Ian Rogersd237a382012-06-01 08:53:29 -0700116 if (UNLIKELY(!thread->IsStillStarting())) {
117 std::ostringstream ss;
118 Runtime::Current()->GetThreadList()->DumpLocked(ss);
119 LOG(FATAL) << *thread << " suspend count already zero.\n" << ss.str();
Elliott Hughes34e06962012-04-09 13:55:55 -0700120 }
Elliott Hughes47179f72011-10-27 16:44:39 -0700121 return;
122 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700123 thread->suspend_count_ += delta;
124 if (for_debugger) {
125 thread->debug_suspend_count_ += delta;
126 }
127}
128
Elliott Hughes8d768a92011-09-14 16:35:25 -0700129void ThreadList::FullSuspendCheck(Thread* thread) {
130 CHECK(thread != NULL);
131 CHECK_GE(thread->suspend_count_, 0);
132
133 MutexLock mu(thread_suspend_count_lock_);
134 if (thread->suspend_count_ == 0) {
135 return;
136 }
137
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800138 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700139 {
Elliott Hughes34e06962012-04-09 13:55:55 -0700140 ScopedThreadStateChange tsc(thread, kSuspended);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700141 while (thread->suspend_count_ != 0) {
142 /*
143 * Wait for wakeup signal, releasing lock. The act of releasing
144 * and re-acquiring the lock provides the memory barriers we
145 * need for correct behavior on SMP.
146 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700147 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700148 }
149 CHECK_EQ(thread->suspend_count_, 0);
150 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800151 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700152}
153
Elliott Hughes475fc232011-10-25 15:00:35 -0700154void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700155 Thread* self = Thread::Current();
156
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800157 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700158
Elliott Hughes34e06962012-04-09 13:55:55 -0700159 CHECK_EQ(self->GetState(), kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800160 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700161 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700162
163 {
164 // Increment everybody's suspend count (except our own).
165 MutexLock mu(thread_suspend_count_lock_);
166 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
167 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700168 if (thread == self || (for_debugger && thread == debug_thread)) {
169 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700170 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800171 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700172 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700173 }
174 }
175
176 /*
177 * Wait for everybody in kRunnable state to stop. Other states
178 * indicate the code is either running natively or sleeping quietly.
179 * Any attempt to transition back to kRunnable will cause a check
180 * for suspension, so it should be impossible for anything to execute
181 * interpreted code or modify objects (assuming native code plays nicely).
182 *
183 * It's also okay if the thread transitions to a non-kRunnable state.
184 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700185 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700186 * so if another thread is fiddling with its suspend count (perhaps
187 * self-suspending for the debugger) it won't block while we're waiting
188 * in here.
189 */
190 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
191 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700192 if (thread == self || (for_debugger && thread == debug_thread)) {
193 continue;
194 }
195 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800196 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700197 }
198
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800199 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700200}
201
Elliott Hughes4e235312011-12-02 11:34:15 -0800202void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700203 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700204 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700205
206 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
207
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800208 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700209
Elliott Hughes01158d72011-09-19 19:47:10 -0700210 if (!Contains(thread)) {
211 return;
212 }
213
214 {
215 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800216 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700217 }
218
219 thread->WaitUntilSuspended();
220
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800221 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700222}
223
Elliott Hughes475fc232011-10-25 15:00:35 -0700224void ThreadList::SuspendSelfForDebugger() {
225 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700226
Elliott Hughes475fc232011-10-25 15:00:35 -0700227 // The debugger thread must not suspend itself due to debugger activity!
228 Thread* debug_thread = Dbg::GetDebugThread();
229 CHECK(debug_thread != NULL);
230 CHECK(self != debug_thread);
231
232 // Collisions with other suspends aren't really interesting. We want
233 // to ensure that we're the only one fiddling with the suspend count
234 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700235 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700236 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700237
238 // Suspend ourselves.
239 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700240 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800241 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700242
243 // Tell JDWP that we've completed suspension. The JDWP thread can't
244 // tell us to resume before we're fully asleep because we hold the
245 // suspend count lock.
246 Dbg::ClearWaitForEventThread();
247
248 while (self->suspend_count_ != 0) {
249 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
250 if (self->suspend_count_ != 0) {
251 // The condition was signaled but we're still suspended. This
252 // can happen if the debugger lets go while a SIGQUIT thread
253 // dump event is pending (assuming SignalCatcher was resumed for
254 // just long enough to try to grab the thread-suspend lock).
255 LOG(DEBUG) << *self << " still suspended after undo "
256 << "(suspend count=" << self->suspend_count_ << ")";
257 }
258 }
259 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700260 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800261 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700262}
263
264void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700265 Thread* self = Thread::Current();
266
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800267 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700268
269 // Decrement the suspend counts for all threads. No need for atomic
270 // writes, since nobody should be moving until we decrement the count.
271 // We do need to hold the thread list because of JNI attaches.
272 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800273 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700274 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700275 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700276 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
277 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700278 if (thread == self || (for_debugger && thread == debug_thread)) {
279 continue;
280 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700281 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700282 }
283 }
284
285 // Broadcast a notification to all suspended threads, some or all of
286 // which may choose to wake up. No need to wait for them.
287 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800288 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700289 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700290 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700291 }
292
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800293 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700294}
295
Elliott Hughes4e235312011-12-02 11:34:15 -0800296void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700297 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800298
299 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
300 thread_list_lock_.AssertHeld();
301 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700302
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800303 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700304
305 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700306 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700307 if (!Contains(thread)) {
308 return;
309 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800310 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700311 }
312
313 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800314 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700315 MutexLock mu(thread_suspend_count_lock_);
316 thread_suspend_count_cond_.Broadcast();
317 }
318
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800319 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700320}
321
Elliott Hughes398f64b2012-03-26 18:05:48 -0700322void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) { // NOLINT
Elliott Hughes01158d72011-09-19 19:47:10 -0700323 DCHECK(thread != NULL);
324 Thread* self = Thread::Current();
325 if (thread != self) {
326 Suspend(thread);
327 }
328 callback(arg);
329 if (thread != self) {
330 Resume(thread);
331 }
332}
333
Elliott Hughes234ab152011-10-26 14:02:26 -0700334void ThreadList::UndoDebuggerSuspensions() {
335 Thread* self = Thread::Current();
336
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800337 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700338
339 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800340 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700341 MutexLock mu(thread_suspend_count_lock_);
342 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
343 Thread* thread = *it;
344 if (thread == self || thread->debug_suspend_count_ == 0) {
345 continue;
346 }
347 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
348 }
349 }
350
351 {
352 MutexLock mu(thread_suspend_count_lock_);
353 thread_suspend_count_cond_.Broadcast();
354 }
355
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800356 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700357}
358
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700359void ThreadList::Register() {
360 Thread* self = Thread::Current();
361
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800362 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700363
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800364 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700365 CHECK(!Contains(self));
366 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700367}
368
369void ThreadList::Unregister() {
370 Thread* self = Thread::Current();
371
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800372 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700373
Elliott Hughesc0f09332012-03-26 13:27:06 -0700374 // Any time-consuming destruction, plus anything that can call back into managed code or
375 // suspend and so on, must happen at this point, and not in ~Thread.
376 self->Destroy();
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700377
Elliott Hughesc0f09332012-03-26 13:27:06 -0700378 {
379 // Remove this thread from the list.
380 ScopedThreadListLock thread_list_lock;
381 CHECK(Contains(self));
382 list_.remove(self);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700383 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700384
Elliott Hughese52e49b2012-04-02 16:05:44 -0700385 // Delete the Thread* and release the thin lock id.
386 uint32_t thin_lock_id = self->thin_lock_id_;
387 delete self;
388 ReleaseThreadId(thin_lock_id);
389
Elliott Hughesc0f09332012-03-26 13:27:06 -0700390 // Clear the TLS data, so that the underlying native thread is recognizably detached.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700391 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700392 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700393
394 // Signal that a thread just detached.
395 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700396}
397
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700398void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700399 thread_list_lock_.AssertHeld();
400 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700401 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700402 }
403}
404
Elliott Hughes8daa0922011-09-11 13:46:25 -0700405void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800406 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700407 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
408 (*it)->VisitRoots(visitor, arg);
409 }
410}
411
Elliott Hughes93e74e82011-09-13 11:07:03 -0700412/*
413 * Tell a new thread it's safe to start.
414 *
415 * We must hold the thread list lock before messing with another thread.
416 * In the general case we would also need to verify that the new thread was
417 * still in the thread list, but in our case the thread has not started
418 * executing user code and therefore has not had a chance to exit.
419 *
420 * We move it to kVmWait, and it then shifts itself to kRunning, which
421 * comes with a suspend-pending check. We do this after
422 */
423void ThreadList::SignalGo(Thread* child) {
424 Thread* self = Thread::Current();
425 CHECK(child != self);
426
427 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800428 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800429 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700430
431 // We wait for the child to tell us that it's in the thread list.
Elliott Hughes34e06962012-04-09 13:55:55 -0700432 while (child->GetState() != kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700433 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700434 }
435 }
436
437 // If we switch out of runnable and then back in, we know there's no pending suspend.
Elliott Hughes34e06962012-04-09 13:55:55 -0700438 self->SetState(kVmWait);
439 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700440
441 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800442 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800443 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700444 child->SetState(kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700445 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700446}
447
448void ThreadList::WaitForGo() {
449 Thread* self = Thread::Current();
450 DCHECK(Contains(self));
451
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700452 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800453 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700454
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700455 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800456 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700457 self->SetState(kStarting);
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700458 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700459
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700460 // Wait until our parent tells us there's no suspend still pending
461 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800462 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700463 while (self->GetState() != kVmWait) {
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700464 thread_start_cond_.Wait(thread_list_lock_);
465 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700466 }
467
468 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800469 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700470 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
471 // started, we wait until it's over. Which means that if there's now another GC pending, our
472 // suspend count is non-zero, so switching to the runnable state will suspend us.
473 // TODO: find a better solution!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800474 {
475 ScopedHeapLock heap_lock;
476 }
Elliott Hughes34e06962012-04-09 13:55:55 -0700477 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700478}
479
Elliott Hughese52e49b2012-04-02 16:05:44 -0700480bool ThreadList::AllOtherThreadsAreDaemons() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700481 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700482 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
483 // is null.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700484 Thread* thread = *it;
485 if (thread != Thread::Current() && thread->GetPeer() != NULL && !thread->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700486 return false;
487 }
488 }
489 return true;
490}
491
Elliott Hughese52e49b2012-04-02 16:05:44 -0700492void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800493 ScopedThreadListLock thread_list_lock;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700494 while (!AllOtherThreadsAreDaemons()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700495 thread_exit_cond_.Wait(thread_list_lock_);
496 }
497}
498
499void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800500 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700501
Elliott Hughese52e49b2012-04-02 16:05:44 -0700502 // Tell all the daemons it's time to suspend.
Elliott Hughes038a8062011-09-18 14:12:41 -0700503 {
504 MutexLock mu(thread_suspend_count_lock_);
505 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
506 Thread* thread = *it;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700507 if (thread != Thread::Current()) {
508 ++thread->suspend_count_;
509 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700510 }
511 }
512
513 // Give the threads a chance to suspend, complaining if they're slow.
514 bool have_complained = false;
515 for (int i = 0; i < 10; ++i) {
516 usleep(200 * 1000);
517 bool all_suspended = true;
518 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
519 Thread* thread = *it;
Elliott Hughes34e06962012-04-09 13:55:55 -0700520 if (thread != Thread::Current() && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700521 if (!have_complained) {
522 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
523 have_complained = true;
524 }
525 all_suspended = false;
526 }
527 }
528 if (all_suspended) {
529 return;
530 }
531 }
532}
533
Elliott Hughes8daa0922011-09-11 13:46:25 -0700534uint32_t ThreadList::AllocThreadId() {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700535 MutexLock mu(allocated_ids_lock_);
536 //ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700537 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
538 if (!allocated_ids_[i]) {
539 allocated_ids_.set(i);
540 return i + 1; // Zero is reserved to mean "invalid".
541 }
542 }
543 LOG(FATAL) << "Out of internal thread ids";
544 return 0;
545}
546
547void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700548 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700549 --id; // Zero is reserved to mean "invalid".
550 DCHECK(allocated_ids_[id]) << id;
551 allocated_ids_.reset(id);
552}
553
554} // namespace art