blob: 601a93f77bcbe0c309835aa942f6fb695a9c8945 [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) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700113 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700114 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
115 (*it)->Dump(os);
116 os << "\n";
117 }
118}
119
Ian Rogers50b35e22012-10-04 10:09:15 -0700120void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
121 MutexLock mu(self, *Locks::thread_list_lock_);
122 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700123 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
124 Thread* thread = *it;
Ian Rogers01ae5802012-09-28 16:14:01 -0700125 if (thread != ignore1 || thread == ignore2) {
126 CHECK(thread->IsSuspended())
127 << "\nUnsuspended thread: <<" << *thread << "\n"
128 << "self: <<" << *Thread::Current();
129 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700130 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131}
132
Ian Rogers66aee5c2012-08-15 17:17:47 -0700133#if HAVE_TIMED_RWLOCK
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134// Attempt to rectify locks so that we dump thread list with required locks before exiting.
Ian Rogers81d425b2012-09-27 16:03:43 -0700135static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136 Runtime* runtime = Runtime::Current();
137 std::ostringstream ss;
138 ss << "Thread suspend timeout\n";
139 runtime->DumpLockHolders(ss);
140 ss << "\n";
Ian Rogers81d425b2012-09-27 16:03:43 -0700141 Locks::mutator_lock_->SharedTryLock(self);
142 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
144 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700145 Locks::thread_list_lock_->TryLock(self);
146 if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 LOG(WARNING) << "Dumping thread list without holding thread_list_lock_";
148 }
149 runtime->GetThreadList()->DumpLocked(ss);
150 LOG(FATAL) << ss.str();
151}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700152#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153
154void ThreadList::SuspendAll() {
155 Thread* self = Thread::Current();
156
157 VLOG(threads) << *self << " SuspendAll starting...";
158
159 if (kIsDebugBuild) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700160 Locks::mutator_lock_->AssertNotHeld(self);
161 Locks::thread_list_lock_->AssertNotHeld(self);
162 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 CHECK_NE(self->GetState(), kRunnable);
164 }
165 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700166 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700168 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169 // Update global suspend all state for attaching threads.
170 ++suspend_all_count_;
171 // Increment everybody's suspend count (except our own).
172 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
173 Thread* thread = *it;
174 if (thread == self) {
175 continue;
176 }
177 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700178 thread->ModifySuspendCount(self, +1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700179 }
180 }
181 }
182
Ian Rogers66aee5c2012-08-15 17:17:47 -0700183 // Block on the mutator lock until all Runnable threads release their share of access.
184#if HAVE_TIMED_RWLOCK
185 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700186 if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0))) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700187 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700188 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700189#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700190 Locks::mutator_lock_->ExclusiveLock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700191#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700192
193 // Debug check that all threads are suspended.
Ian Rogers50b35e22012-10-04 10:09:15 -0700194 AssertThreadsAreSuspended(self, self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700195
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800196 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700197}
198
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199void ThreadList::ResumeAll() {
200 Thread* self = Thread::Current();
201
202 VLOG(threads) << *self << " ResumeAll starting";
Ian Rogers01ae5802012-09-28 16:14:01 -0700203
204 // Debug check that all threads are suspended.
Ian Rogers50b35e22012-10-04 10:09:15 -0700205 AssertThreadsAreSuspended(self, self);
Ian Rogers01ae5802012-09-28 16:14:01 -0700206
Ian Rogers81d425b2012-09-27 16:03:43 -0700207 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700208 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700209 MutexLock mu(self, *Locks::thread_list_lock_);
210 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700211 // Update global suspend all state for attaching threads.
212 --suspend_all_count_;
213 // Decrement the suspend counts for all threads.
214 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
215 Thread* thread = *it;
216 if (thread == self) {
217 continue;
218 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700219 thread->ModifySuspendCount(self, -1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700220 }
221
222 // Broadcast a notification to all suspended threads, some or all of
223 // which may choose to wake up. No need to wait for them.
224 VLOG(threads) << *self << " ResumeAll waking others";
Ian Rogersc604d732012-10-14 16:09:54 -0700225 Thread::resume_cond_->Broadcast(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700227 VLOG(threads) << *self << " ResumeAll complete";
228}
229
230void ThreadList::Resume(Thread* thread, bool for_debugger) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700231 Thread* self = Thread::Current();
232 DCHECK_NE(thread, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700234
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235 {
236 // To check Contains.
Ian Rogers81d425b2012-09-27 16:03:43 -0700237 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238 // To check IsSuspended.
Ian Rogers81d425b2012-09-27 16:03:43 -0700239 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
240 DCHECK(thread->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241 if (!Contains(thread)) {
242 return;
243 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700244 thread->ModifySuspendCount(self, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700245 }
246
247 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 VLOG(threads) << "Resume(" << *thread << ") waking others";
Ian Rogers81d425b2012-09-27 16:03:43 -0700249 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700250 Thread::resume_cond_->Broadcast(self);
Elliott Hughes01158d72011-09-19 19:47:10 -0700251 }
252
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 VLOG(threads) << "Resume(" << *thread << ") complete";
254}
Elliott Hughes01158d72011-09-19 19:47:10 -0700255
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256void ThreadList::SuspendAllForDebugger() {
257 Thread* self = Thread::Current();
258 Thread* debug_thread = Dbg::GetDebugThread();
259
260 VLOG(threads) << *self << " SuspendAllForDebugger starting...";
261
262 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700263 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700265 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 // Update global suspend all state for attaching threads.
267 ++suspend_all_count_;
268 ++debug_suspend_all_count_;
269 // Increment everybody's suspend count (except our own).
270 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
271 Thread* thread = *it;
272 if (thread == self || thread == debug_thread) {
273 continue;
274 }
275 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700276 thread->ModifySuspendCount(self, +1, true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700277 }
278 }
279 }
280
Ian Rogers66aee5c2012-08-15 17:17:47 -0700281 // Block on the mutator lock until all Runnable threads release their share of access then
282 // immediately unlock again.
283#if HAVE_TIMED_RWLOCK
284 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700285 if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700286 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 } else {
Ian Rogers81d425b2012-09-27 16:03:43 -0700288 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700290#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700291 Locks::mutator_lock_->ExclusiveLock(self);
292 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700293#endif
Ian Rogers50b35e22012-10-04 10:09:15 -0700294 AssertThreadsAreSuspended(self, self, debug_thread);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295
296 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700297}
298
Elliott Hughes475fc232011-10-25 15:00:35 -0700299void ThreadList::SuspendSelfForDebugger() {
300 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700301
Elliott Hughes475fc232011-10-25 15:00:35 -0700302 // The debugger thread must not suspend itself due to debugger activity!
303 Thread* debug_thread = Dbg::GetDebugThread();
304 CHECK(debug_thread != NULL);
305 CHECK(self != debug_thread);
306
307 // Collisions with other suspends aren't really interesting. We want
308 // to ensure that we're the only one fiddling with the suspend count
309 // though.
Ian Rogers81d425b2012-09-27 16:03:43 -0700310 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers01ae5802012-09-28 16:14:01 -0700311 self->ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700312
313 // Suspend ourselves.
314 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700315 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800316 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700317
318 // Tell JDWP that we've completed suspension. The JDWP thread can't
319 // tell us to resume before we're fully asleep because we hold the
320 // suspend count lock.
321 Dbg::ClearWaitForEventThread();
322
323 while (self->suspend_count_ != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700324 Thread::resume_cond_->Wait(self);
Elliott Hughes475fc232011-10-25 15:00:35 -0700325 if (self->suspend_count_ != 0) {
326 // The condition was signaled but we're still suspended. This
327 // can happen if the debugger lets go while a SIGQUIT thread
328 // dump event is pending (assuming SignalCatcher was resumed for
329 // just long enough to try to grab the thread-suspend lock).
330 LOG(DEBUG) << *self << " still suspended after undo "
331 << "(suspend count=" << self->suspend_count_ << ")";
332 }
333 }
334 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700335 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800336 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700337}
338
Elliott Hughes234ab152011-10-26 14:02:26 -0700339void ThreadList::UndoDebuggerSuspensions() {
340 Thread* self = Thread::Current();
341
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800342 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700343
344 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700345 MutexLock mu(self, *Locks::thread_list_lock_);
346 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700347 // Update global suspend all state for attaching threads.
348 suspend_all_count_ -= debug_suspend_all_count_;
349 debug_suspend_all_count_ = 0;
350 // Update running threads.
Elliott Hughes234ab152011-10-26 14:02:26 -0700351 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
352 Thread* thread = *it;
353 if (thread == self || thread->debug_suspend_count_ == 0) {
354 continue;
355 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700356 thread->ModifySuspendCount(self, -thread->debug_suspend_count_, true);
Elliott Hughes234ab152011-10-26 14:02:26 -0700357 }
358 }
359
360 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700361 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700362 Thread::resume_cond_->Broadcast(self);
Elliott Hughes234ab152011-10-26 14:02:26 -0700363 }
364
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800365 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700366}
367
Elliott Hughese52e49b2012-04-02 16:05:44 -0700368void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700369 Thread* self = Thread::Current();
370 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700371 bool all_threads_are_daemons;
372 do {
Ian Rogers120f1c72012-09-28 17:17:10 -0700373 {
374 // No more threads can be born after we start to shutdown.
375 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
376 CHECK(Runtime::Current()->IsShuttingDown());
377 CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U);
378 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700379 all_threads_are_daemons = true;
Ian Rogers120f1c72012-09-28 17:17:10 -0700380 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700381 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
382 // TODO: there's a race here with thread exit that's being worked around by checking if the
383 // thread has a peer.
384 Thread* thread = *it;
Ian Rogers81d425b2012-09-27 16:03:43 -0700385 if (thread != self && thread->HasPeer() && !thread->IsDaemon()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700386 all_threads_are_daemons = false;
387 break;
388 }
389 }
390 if (!all_threads_are_daemons) {
391 // Wait for another thread to exit before re-checking.
Ian Rogersc604d732012-10-14 16:09:54 -0700392 thread_exit_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700393 }
394 } while(!all_threads_are_daemons);
Elliott Hughes038a8062011-09-18 14:12:41 -0700395}
396
397void ThreadList::SuspendAllDaemonThreads() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700398 Thread* self = Thread::Current();
399 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700400 { // Tell all the daemons it's time to suspend.
Ian Rogers81d425b2012-09-27 16:03:43 -0700401 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Elliott Hughes038a8062011-09-18 14:12:41 -0700402 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
403 Thread* thread = *it;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700404 // This is only run after all non-daemon threads have exited, so the remainder should all be
405 // daemons.
Ian Rogers7e762862012-10-22 15:45:08 -0700406 CHECK(thread->IsDaemon()) << *thread;
Ian Rogers81d425b2012-09-27 16:03:43 -0700407 if (thread != self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700408 thread->ModifySuspendCount(self, +1, false);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700409 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700410 }
411 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700412 // Give the threads a chance to suspend, complaining if they're slow.
413 bool have_complained = false;
414 for (int i = 0; i < 10; ++i) {
415 usleep(200 * 1000);
416 bool all_suspended = true;
417 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
418 Thread* thread = *it;
Ian Rogers81d425b2012-09-27 16:03:43 -0700419 if (thread != self && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700420 if (!have_complained) {
421 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
422 have_complained = true;
423 }
424 all_suspended = false;
425 }
426 }
427 if (all_suspended) {
428 return;
429 }
430 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700431 LOG(ERROR) << "suspend all daemons failed";
432}
433void ThreadList::Register(Thread* self) {
434 DCHECK_EQ(self, Thread::Current());
435
436 if (VLOG_IS_ON(threads)) {
437 std::ostringstream oss;
438 self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump.
439 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss;
440 }
441
442 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
443 // SuspendAll requests.
Ian Rogers81d425b2012-09-27 16:03:43 -0700444 MutexLock mu(self, *Locks::thread_list_lock_);
445 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446 self->suspend_count_ = suspend_all_count_;
447 self->debug_suspend_count_ = debug_suspend_all_count_;
Ian Rogers01ae5802012-09-28 16:14:01 -0700448 if (self->suspend_count_ > 0) {
449 self->AtomicSetFlag(kSuspendRequest);
450 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700451 CHECK(!Contains(self));
452 list_.push_back(self);
453}
454
455void ThreadList::Unregister(Thread* self) {
456 DCHECK_EQ(self, Thread::Current());
457
458 VLOG(threads) << "ThreadList::Unregister() " << *self;
459
460 // Any time-consuming destruction, plus anything that can call back into managed code or
461 // suspend and so on, must happen at this point, and not in ~Thread.
462 self->Destroy();
463
464 {
465 // Remove this thread from the list.
Ian Rogers81d425b2012-09-27 16:03:43 -0700466 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700467 CHECK(Contains(self));
468 list_.remove(self);
469 }
470
471 // Delete the Thread* and release the thin lock id.
472 uint32_t thin_lock_id = self->thin_lock_id_;
473 ReleaseThreadId(thin_lock_id);
474 delete self;
475
476 // Clear the TLS data, so that the underlying native thread is recognizably detached.
477 // (It may wish to reattach later.)
478 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
479
480 // Signal that a thread just detached.
Ian Rogers81d425b2012-09-27 16:03:43 -0700481 MutexLock mu(NULL, *Locks::thread_list_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700482 thread_exit_cond_.Signal(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700483}
484
485void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
486 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
487 callback(*it, context);
488 }
489}
490
491void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Ian Rogers81d425b2012-09-27 16:03:43 -0700492 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700493 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
494 (*it)->VisitRoots(visitor, arg);
495 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700496}
497
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700498void ThreadList::VerifyRoots(Heap::VerifyRootVisitor* visitor, void* arg) const {
499 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
500 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
501 (*it)->VerifyRoots(visitor, arg);
502 }
503}
504
Elliott Hughes8daa0922011-09-11 13:46:25 -0700505uint32_t ThreadList::AllocThreadId() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700506 MutexLock mu(Thread::Current(), allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700507 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
508 if (!allocated_ids_[i]) {
509 allocated_ids_.set(i);
510 return i + 1; // Zero is reserved to mean "invalid".
511 }
512 }
513 LOG(FATAL) << "Out of internal thread ids";
514 return 0;
515}
516
517void ThreadList::ReleaseThreadId(uint32_t id) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700518 MutexLock mu(Thread::Current(), allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700519 --id; // Zero is reserved to mean "invalid".
520 DCHECK(allocated_ids_[id]) << id;
521 allocated_ids_.reset(id);
522}
523
524} // namespace art