blob: 5ef8625a3c331ea6a62374134735532fe116a877 [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"
Ian Rogers66aee5c2012-08-15 17:17:47 -070024#include "mutex.h"
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070025#include "timing_logger.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"),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 suspend_all_count_(0), debug_suspend_all_count_(0),
Ian Rogersc604d732012-10-14 16:09:54 -070033 thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070034}
35
36ThreadList::~ThreadList() {
Elliott Hughese52e49b2012-04-02 16:05:44 -070037 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
Elliott Hughes6a144332012-04-03 13:07:11 -070038 // We need to detach the current thread here in case there's another thread waiting to join with
39 // us.
Elliott Hughes8daa0922011-09-11 13:46:25 -070040 if (Contains(Thread::Current())) {
41 Runtime::Current()->DetachCurrentThread();
42 }
Elliott Hughes6a144332012-04-03 13:07:11 -070043
44 WaitForOtherNonDaemonThreadsToExit();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045 // TODO: there's an unaddressed race here where a thread may attach during shutdown, see
46 // Thread::Init.
Elliott Hughes6a144332012-04-03 13:07:11 -070047 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070048}
49
50bool ThreadList::Contains(Thread* thread) {
51 return find(list_.begin(), list_.end(), thread) != list_.end();
52}
53
Elliott Hughesabbe07d2012-06-05 17:42:23 -070054bool ThreadList::Contains(pid_t tid) {
55 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
56 if ((*it)->tid_ == tid) {
57 return true;
58 }
59 }
60 return false;
61}
62
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070063pid_t ThreadList::GetLockOwner() {
Ian Rogersb726dcb2012-09-05 08:57:23 -070064 return Locks::thread_list_lock_->GetExclusiveOwnerTid();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070065}
66
Elliott Hughesc967f782012-04-16 10:23:15 -070067void ThreadList::DumpForSigQuit(std::ostream& os) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068 {
Ian Rogers50b35e22012-10-04 10:09:15 -070069 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070 DumpLocked(os);
71 }
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);
Brian Carlstromed8b7232012-06-27 17:54:47 -070078 // TODO: Reenable this when the native code in system_server can handle it.
79 // Currently "adb shell kill -3 `pid system_server`" will cause it to exit.
80 if (false) {
81 DumpNativeStack(os, tid, " native: ", false);
82 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -070083 os << "\n";
84}
85
86void ThreadList::DumpUnattachedThreads(std::ostream& os) {
87 DIR* d = opendir("/proc/self/task");
88 if (!d) {
89 return;
90 }
91
92 dirent de;
Elliott Hughes0d39c122012-06-06 16:41:17 -070093 dirent* e;
Ian Rogers50b35e22012-10-04 10:09:15 -070094 Thread* self = Thread::Current();
Elliott Hughes0d39c122012-06-06 16:41:17 -070095 while (!readdir_r(d, &de, &e) && e != NULL) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070096 char* end;
97 pid_t tid = strtol(de.d_name, &end, 10);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098 if (!*end) {
99 bool contains;
100 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700101 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700102 contains = Contains(tid);
103 }
104 if (!contains) {
105 DumpUnattachedThread(os, tid);
106 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700107 }
108 }
109 closedir(d);
Elliott Hughesff738062012-02-03 15:00:42 -0800110}
111
112void ThreadList::DumpLocked(std::ostream& os) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700113 Locks::thread_list_lock_->AssertHeld(Thread::Current());
Elliott Hughes8daa0922011-09-11 13:46:25 -0700114 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700115 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
116 (*it)->Dump(os);
117 os << "\n";
118 }
119}
120
Ian Rogers50b35e22012-10-04 10:09:15 -0700121void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
122 MutexLock mu(self, *Locks::thread_list_lock_);
123 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700124 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
125 Thread* thread = *it;
Ian Rogers01ae5802012-09-28 16:14:01 -0700126 if (thread != ignore1 || thread == ignore2) {
127 CHECK(thread->IsSuspended())
128 << "\nUnsuspended thread: <<" << *thread << "\n"
129 << "self: <<" << *Thread::Current();
130 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700131 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132}
133
Ian Rogers66aee5c2012-08-15 17:17:47 -0700134#if HAVE_TIMED_RWLOCK
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135// Attempt to rectify locks so that we dump thread list with required locks before exiting.
Ian Rogers81d425b2012-09-27 16:03:43 -0700136static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137 Runtime* runtime = Runtime::Current();
138 std::ostringstream ss;
139 ss << "Thread suspend timeout\n";
140 runtime->DumpLockHolders(ss);
141 ss << "\n";
Ian Rogers81d425b2012-09-27 16:03:43 -0700142 Locks::mutator_lock_->SharedTryLock(self);
143 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
145 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700146 Locks::thread_list_lock_->TryLock(self);
147 if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700148 LOG(WARNING) << "Dumping thread list without holding thread_list_lock_";
149 }
150 runtime->GetThreadList()->DumpLocked(ss);
151 LOG(FATAL) << ss.str();
152}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700153#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154
155void ThreadList::SuspendAll() {
156 Thread* self = Thread::Current();
157
158 VLOG(threads) << *self << " SuspendAll starting...";
159
160 if (kIsDebugBuild) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700161 Locks::mutator_lock_->AssertNotHeld(self);
162 Locks::thread_list_lock_->AssertNotHeld(self);
163 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 CHECK_NE(self->GetState(), kRunnable);
165 }
166 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700167 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700169 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 // Update global suspend all state for attaching threads.
171 ++suspend_all_count_;
172 // Increment everybody's suspend count (except our own).
173 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
174 Thread* thread = *it;
175 if (thread == self) {
176 continue;
177 }
178 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700179 thread->ModifySuspendCount(self, +1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700180 }
181 }
182 }
183
Ian Rogers66aee5c2012-08-15 17:17:47 -0700184 // Block on the mutator lock until all Runnable threads release their share of access.
185#if HAVE_TIMED_RWLOCK
186 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700187 if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0))) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700188 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700189 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700190#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700191 Locks::mutator_lock_->ExclusiveLock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700192#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700193
194 // Debug check that all threads are suspended.
Ian Rogers50b35e22012-10-04 10:09:15 -0700195 AssertThreadsAreSuspended(self, self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700196
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800197 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700198}
199
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200void ThreadList::ResumeAll() {
201 Thread* self = Thread::Current();
202
203 VLOG(threads) << *self << " ResumeAll starting";
Ian Rogers01ae5802012-09-28 16:14:01 -0700204
205 // Debug check that all threads are suspended.
Ian Rogers50b35e22012-10-04 10:09:15 -0700206 AssertThreadsAreSuspended(self, self);
Ian Rogers01ae5802012-09-28 16:14:01 -0700207
Ian Rogers81d425b2012-09-27 16:03:43 -0700208 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700210 MutexLock mu(self, *Locks::thread_list_lock_);
211 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700212 // Update global suspend all state for attaching threads.
213 --suspend_all_count_;
214 // Decrement the suspend counts for all threads.
215 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
216 Thread* thread = *it;
217 if (thread == self) {
218 continue;
219 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700220 thread->ModifySuspendCount(self, -1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 }
222
223 // Broadcast a notification to all suspended threads, some or all of
224 // which may choose to wake up. No need to wait for them.
225 VLOG(threads) << *self << " ResumeAll waking others";
Ian Rogersc604d732012-10-14 16:09:54 -0700226 Thread::resume_cond_->Broadcast(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 VLOG(threads) << *self << " ResumeAll complete";
229}
230
231void ThreadList::Resume(Thread* thread, bool for_debugger) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700232 Thread* self = Thread::Current();
233 DCHECK_NE(thread, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700235
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700236 {
237 // To check Contains.
Ian Rogers81d425b2012-09-27 16:03:43 -0700238 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239 // To check IsSuspended.
Ian Rogers81d425b2012-09-27 16:03:43 -0700240 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
241 DCHECK(thread->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 if (!Contains(thread)) {
243 return;
244 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700245 thread->ModifySuspendCount(self, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700246 }
247
248 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 VLOG(threads) << "Resume(" << *thread << ") waking others";
Ian Rogers81d425b2012-09-27 16:03:43 -0700250 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700251 Thread::resume_cond_->Broadcast(self);
Elliott Hughes01158d72011-09-19 19:47:10 -0700252 }
253
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254 VLOG(threads) << "Resume(" << *thread << ") complete";
255}
Elliott Hughes01158d72011-09-19 19:47:10 -0700256
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700257void ThreadList::SuspendAllForDebugger() {
258 Thread* self = Thread::Current();
259 Thread* debug_thread = Dbg::GetDebugThread();
260
261 VLOG(threads) << *self << " SuspendAllForDebugger starting...";
262
263 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700264 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700266 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 // Update global suspend all state for attaching threads.
268 ++suspend_all_count_;
269 ++debug_suspend_all_count_;
270 // Increment everybody's suspend count (except our own).
271 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
272 Thread* thread = *it;
273 if (thread == self || thread == debug_thread) {
274 continue;
275 }
276 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700277 thread->ModifySuspendCount(self, +1, true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 }
279 }
280 }
281
Ian Rogers66aee5c2012-08-15 17:17:47 -0700282 // Block on the mutator lock until all Runnable threads release their share of access then
283 // immediately unlock again.
284#if HAVE_TIMED_RWLOCK
285 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700286 if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700287 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700288 } else {
Ian Rogers81d425b2012-09-27 16:03:43 -0700289 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700290 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700291#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700292 Locks::mutator_lock_->ExclusiveLock(self);
293 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700294#endif
Ian Rogers50b35e22012-10-04 10:09:15 -0700295 AssertThreadsAreSuspended(self, self, debug_thread);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296
297 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700298}
299
Elliott Hughes475fc232011-10-25 15:00:35 -0700300void ThreadList::SuspendSelfForDebugger() {
301 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700302
Elliott Hughes475fc232011-10-25 15:00:35 -0700303 // The debugger thread must not suspend itself due to debugger activity!
304 Thread* debug_thread = Dbg::GetDebugThread();
305 CHECK(debug_thread != NULL);
306 CHECK(self != debug_thread);
307
308 // Collisions with other suspends aren't really interesting. We want
309 // to ensure that we're the only one fiddling with the suspend count
310 // though.
Ian Rogers81d425b2012-09-27 16:03:43 -0700311 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers01ae5802012-09-28 16:14:01 -0700312 self->ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700313
314 // Suspend ourselves.
315 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700316 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800317 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700318
319 // Tell JDWP that we've completed suspension. The JDWP thread can't
320 // tell us to resume before we're fully asleep because we hold the
321 // suspend count lock.
322 Dbg::ClearWaitForEventThread();
323
324 while (self->suspend_count_ != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700325 Thread::resume_cond_->Wait(self);
Elliott Hughes475fc232011-10-25 15:00:35 -0700326 if (self->suspend_count_ != 0) {
327 // The condition was signaled but we're still suspended. This
328 // can happen if the debugger lets go while a SIGQUIT thread
329 // dump event is pending (assuming SignalCatcher was resumed for
330 // just long enough to try to grab the thread-suspend lock).
331 LOG(DEBUG) << *self << " still suspended after undo "
332 << "(suspend count=" << self->suspend_count_ << ")";
333 }
334 }
335 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700336 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800337 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700338}
339
Elliott Hughes234ab152011-10-26 14:02:26 -0700340void ThreadList::UndoDebuggerSuspensions() {
341 Thread* self = Thread::Current();
342
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800343 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700344
345 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700346 MutexLock mu(self, *Locks::thread_list_lock_);
347 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348 // Update global suspend all state for attaching threads.
349 suspend_all_count_ -= debug_suspend_all_count_;
350 debug_suspend_all_count_ = 0;
351 // Update running threads.
Elliott Hughes234ab152011-10-26 14:02:26 -0700352 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
353 Thread* thread = *it;
354 if (thread == self || thread->debug_suspend_count_ == 0) {
355 continue;
356 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700357 thread->ModifySuspendCount(self, -thread->debug_suspend_count_, true);
Elliott Hughes234ab152011-10-26 14:02:26 -0700358 }
359 }
360
361 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700362 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700363 Thread::resume_cond_->Broadcast(self);
Elliott Hughes234ab152011-10-26 14:02:26 -0700364 }
365
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800366 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700367}
368
Elliott Hughese52e49b2012-04-02 16:05:44 -0700369void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700370 Thread* self = Thread::Current();
371 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 bool all_threads_are_daemons;
373 do {
Ian Rogers120f1c72012-09-28 17:17:10 -0700374 {
375 // No more threads can be born after we start to shutdown.
376 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
377 CHECK(Runtime::Current()->IsShuttingDown());
378 CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U);
379 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 all_threads_are_daemons = true;
Ian Rogers120f1c72012-09-28 17:17:10 -0700381 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700382 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
383 // TODO: there's a race here with thread exit that's being worked around by checking if the
384 // thread has a peer.
385 Thread* thread = *it;
Ian Rogers81d425b2012-09-27 16:03:43 -0700386 if (thread != self && thread->HasPeer() && !thread->IsDaemon()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700387 all_threads_are_daemons = false;
388 break;
389 }
390 }
391 if (!all_threads_are_daemons) {
392 // Wait for another thread to exit before re-checking.
Ian Rogersc604d732012-10-14 16:09:54 -0700393 thread_exit_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 }
395 } while(!all_threads_are_daemons);
Elliott Hughes038a8062011-09-18 14:12:41 -0700396}
397
398void ThreadList::SuspendAllDaemonThreads() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700399 Thread* self = Thread::Current();
400 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 { // Tell all the daemons it's time to suspend.
Ian Rogers81d425b2012-09-27 16:03:43 -0700402 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Elliott Hughes038a8062011-09-18 14:12:41 -0700403 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
404 Thread* thread = *it;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700405 // This is only run after all non-daemon threads have exited, so the remainder should all be
406 // daemons.
407 CHECK(thread->IsDaemon());
Ian Rogers81d425b2012-09-27 16:03:43 -0700408 if (thread != self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700409 thread->ModifySuspendCount(self, +1, false);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700410 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700411 }
412 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700413 // Give the threads a chance to suspend, complaining if they're slow.
414 bool have_complained = false;
415 for (int i = 0; i < 10; ++i) {
416 usleep(200 * 1000);
417 bool all_suspended = true;
418 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
419 Thread* thread = *it;
Ian Rogers81d425b2012-09-27 16:03:43 -0700420 if (thread != self && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700421 if (!have_complained) {
422 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
423 have_complained = true;
424 }
425 all_suspended = false;
426 }
427 }
428 if (all_suspended) {
429 return;
430 }
431 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700432 LOG(ERROR) << "suspend all daemons failed";
433}
434void ThreadList::Register(Thread* self) {
435 DCHECK_EQ(self, Thread::Current());
436
437 if (VLOG_IS_ON(threads)) {
438 std::ostringstream oss;
439 self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump.
440 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss;
441 }
442
443 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
444 // SuspendAll requests.
Ian Rogers81d425b2012-09-27 16:03:43 -0700445 MutexLock mu(self, *Locks::thread_list_lock_);
446 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700447 self->suspend_count_ = suspend_all_count_;
448 self->debug_suspend_count_ = debug_suspend_all_count_;
Ian Rogers01ae5802012-09-28 16:14:01 -0700449 if (self->suspend_count_ > 0) {
450 self->AtomicSetFlag(kSuspendRequest);
451 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 CHECK(!Contains(self));
453 list_.push_back(self);
454}
455
456void ThreadList::Unregister(Thread* self) {
457 DCHECK_EQ(self, Thread::Current());
458
459 VLOG(threads) << "ThreadList::Unregister() " << *self;
460
461 // Any time-consuming destruction, plus anything that can call back into managed code or
462 // suspend and so on, must happen at this point, and not in ~Thread.
463 self->Destroy();
464
465 {
466 // Remove this thread from the list.
Ian Rogers81d425b2012-09-27 16:03:43 -0700467 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 CHECK(Contains(self));
469 list_.remove(self);
470 }
471
472 // Delete the Thread* and release the thin lock id.
473 uint32_t thin_lock_id = self->thin_lock_id_;
474 ReleaseThreadId(thin_lock_id);
475 delete self;
476
477 // Clear the TLS data, so that the underlying native thread is recognizably detached.
478 // (It may wish to reattach later.)
479 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
480
481 // Signal that a thread just detached.
Ian Rogers81d425b2012-09-27 16:03:43 -0700482 MutexLock mu(NULL, *Locks::thread_list_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700483 thread_exit_cond_.Signal(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700484}
485
486void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
487 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
488 callback(*it, context);
489 }
490}
491
492void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Ian Rogers81d425b2012-09-27 16:03:43 -0700493 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700494 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
495 (*it)->VisitRoots(visitor, arg);
496 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700497}
498
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700499void ThreadList::VerifyRoots(Heap::VerifyRootVisitor* visitor, void* arg) const {
500 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
501 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
502 (*it)->VerifyRoots(visitor, arg);
503 }
504}
505
Elliott Hughes8daa0922011-09-11 13:46:25 -0700506uint32_t ThreadList::AllocThreadId() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700507 MutexLock mu(Thread::Current(), allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700508 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
509 if (!allocated_ids_[i]) {
510 allocated_ids_.set(i);
511 return i + 1; // Zero is reserved to mean "invalid".
512 }
513 }
514 LOG(FATAL) << "Out of internal thread ids";
515 return 0;
516}
517
518void ThreadList::ReleaseThreadId(uint32_t id) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700519 MutexLock mu(Thread::Current(), allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700520 --id; // Zero is reserved to mean "invalid".
521 DCHECK(allocated_ids_[id]) << id;
522 allocated_ids_.reset(id);
523}
524
525} // namespace art