blob: 082d7af0daf51d149dae34bdc0782fae88ca9d78 [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),
33 thread_exit_cond_("thread exit condition variable") {
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 Rogersb726dcb2012-09-05 08:57:23 -070069 MutexLock mu(*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;
94 while (!readdir_r(d, &de, &e) && e != NULL) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070095 char* end;
96 pid_t tid = strtol(de.d_name, &end, 10);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 if (!*end) {
98 bool contains;
99 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700100 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 contains = Contains(tid);
102 }
103 if (!contains) {
104 DumpUnattachedThread(os, tid);
105 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700106 }
107 }
108 closedir(d);
Elliott Hughesff738062012-02-03 15:00:42 -0800109}
110
111void ThreadList::DumpLocked(std::ostream& os) {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700112 Locks::thread_list_lock_->AssertHeld();
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 Rogers00f7d0e2012-07-19 15:28:27 -0700120void ThreadList::AssertThreadsAreSuspended() {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700121 MutexLock mu(*Locks::thread_list_lock_);
122 MutexLock mu2(*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 Rogers00f7d0e2012-07-19 15:28:27 -0700125 CHECK_NE(thread->GetState(), kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700126 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127}
128
Ian Rogers66aee5c2012-08-15 17:17:47 -0700129#if HAVE_TIMED_RWLOCK
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130// Attempt to rectify locks so that we dump thread list with required locks before exiting.
Ian Rogers81d425b2012-09-27 16:03:43 -0700131static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 Runtime* runtime = Runtime::Current();
133 std::ostringstream ss;
134 ss << "Thread suspend timeout\n";
135 runtime->DumpLockHolders(ss);
136 ss << "\n";
Ian Rogers81d425b2012-09-27 16:03:43 -0700137 Locks::mutator_lock_->SharedTryLock(self);
138 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
140 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700141 Locks::thread_list_lock_->TryLock(self);
142 if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 LOG(WARNING) << "Dumping thread list without holding thread_list_lock_";
144 }
145 runtime->GetThreadList()->DumpLocked(ss);
146 LOG(FATAL) << ss.str();
147}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700148#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149
150void ThreadList::SuspendAll() {
151 Thread* self = Thread::Current();
152
153 VLOG(threads) << *self << " SuspendAll starting...";
154
155 if (kIsDebugBuild) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700156 Locks::mutator_lock_->AssertNotHeld(self);
157 Locks::thread_list_lock_->AssertNotHeld(self);
158 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 CHECK_NE(self->GetState(), kRunnable);
160 }
161 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700162 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700164 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165 // Update global suspend all state for attaching threads.
166 ++suspend_all_count_;
167 // Increment everybody's suspend count (except our own).
168 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
169 Thread* thread = *it;
170 if (thread == self) {
171 continue;
172 }
173 VLOG(threads) << "requesting thread suspend: " << *thread;
174 thread->ModifySuspendCount(+1, false);
175 }
176 }
177 }
178
Ian Rogers66aee5c2012-08-15 17:17:47 -0700179 // Block on the mutator lock until all Runnable threads release their share of access.
180#if HAVE_TIMED_RWLOCK
181 // Timeout if we wait more than 30 seconds.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700182 timespec timeout;
183 clock_gettime(CLOCK_REALTIME, &timeout);
184 timeout.tv_sec += 30;
Ian Rogers81d425b2012-09-27 16:03:43 -0700185 if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, timeout))) {
186 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700187 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700188#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700189 Locks::mutator_lock_->ExclusiveLock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700190#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700191
192 // Debug check that all threads are suspended.
193 AssertThreadsAreSuspended();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700194
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800195 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700196}
197
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198void ThreadList::ResumeAll() {
199 Thread* self = Thread::Current();
200
201 VLOG(threads) << *self << " ResumeAll starting";
Ian Rogers81d425b2012-09-27 16:03:43 -0700202 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700204 MutexLock mu(self, *Locks::thread_list_lock_);
205 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 // Update global suspend all state for attaching threads.
207 --suspend_all_count_;
208 // Decrement the suspend counts for all threads.
209 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
210 Thread* thread = *it;
211 if (thread == self) {
212 continue;
213 }
214 thread->ModifySuspendCount(-1, false);
215 }
216
217 // Broadcast a notification to all suspended threads, some or all of
218 // which may choose to wake up. No need to wait for them.
219 VLOG(threads) << *self << " ResumeAll waking others";
220 Thread::resume_cond_->Broadcast();
221 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 VLOG(threads) << *self << " ResumeAll complete";
223}
224
225void ThreadList::Resume(Thread* thread, bool for_debugger) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700226 Thread* self = Thread::Current();
227 DCHECK_NE(thread, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700228 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700229
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 {
231 // To check Contains.
Ian Rogers81d425b2012-09-27 16:03:43 -0700232 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 // To check IsSuspended.
Ian Rogers81d425b2012-09-27 16:03:43 -0700234 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
235 DCHECK(thread->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700236 if (!Contains(thread)) {
237 return;
238 }
239 thread->ModifySuspendCount(-1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700240 }
241
242 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 VLOG(threads) << "Resume(" << *thread << ") waking others";
Ian Rogers81d425b2012-09-27 16:03:43 -0700244 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245 Thread::resume_cond_->Broadcast();
Elliott Hughes01158d72011-09-19 19:47:10 -0700246 }
247
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 VLOG(threads) << "Resume(" << *thread << ") complete";
249}
Elliott Hughes01158d72011-09-19 19:47:10 -0700250
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251void ThreadList::SuspendAllForDebugger() {
252 Thread* self = Thread::Current();
253 Thread* debug_thread = Dbg::GetDebugThread();
254
255 VLOG(threads) << *self << " SuspendAllForDebugger starting...";
256
257 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700258 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700260 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 // Update global suspend all state for attaching threads.
262 ++suspend_all_count_;
263 ++debug_suspend_all_count_;
264 // Increment everybody's suspend count (except our own).
265 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
266 Thread* thread = *it;
267 if (thread == self || thread == debug_thread) {
268 continue;
269 }
270 VLOG(threads) << "requesting thread suspend: " << *thread;
271 thread->ModifySuspendCount(+1, true);
272 }
273 }
274 }
275
Ian Rogers66aee5c2012-08-15 17:17:47 -0700276 // Block on the mutator lock until all Runnable threads release their share of access then
277 // immediately unlock again.
278#if HAVE_TIMED_RWLOCK
279 // Timeout if we wait more than 30 seconds.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 timespec timeout;
281 clock_gettime(CLOCK_REALTIME, &timeout);
282 timeout.tv_sec += 30;
Ian Rogers81d425b2012-09-27 16:03:43 -0700283 if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, timeout)) {
284 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 } else {
Ian Rogers81d425b2012-09-27 16:03:43 -0700286 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700288#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700289 Locks::mutator_lock_->ExclusiveLock(self);
290 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700291#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 AssertThreadsAreSuspended();
293
294 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700295}
296
Elliott Hughes475fc232011-10-25 15:00:35 -0700297void ThreadList::SuspendSelfForDebugger() {
298 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700299
Elliott Hughes475fc232011-10-25 15:00:35 -0700300 // The debugger thread must not suspend itself due to debugger activity!
301 Thread* debug_thread = Dbg::GetDebugThread();
302 CHECK(debug_thread != NULL);
303 CHECK(self != debug_thread);
304
305 // Collisions with other suspends aren't really interesting. We want
306 // to ensure that we're the only one fiddling with the suspend count
307 // though.
Ian Rogers81d425b2012-09-27 16:03:43 -0700308 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700309 self->ModifySuspendCount(+1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700310
311 // Suspend ourselves.
312 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700313 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800314 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700315
316 // Tell JDWP that we've completed suspension. The JDWP thread can't
317 // tell us to resume before we're fully asleep because we hold the
318 // suspend count lock.
319 Dbg::ClearWaitForEventThread();
320
321 while (self->suspend_count_ != 0) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700322 Thread::resume_cond_->Wait(self, *Locks::thread_suspend_count_lock_);
Elliott Hughes475fc232011-10-25 15:00:35 -0700323 if (self->suspend_count_ != 0) {
324 // The condition was signaled but we're still suspended. This
325 // can happen if the debugger lets go while a SIGQUIT thread
326 // dump event is pending (assuming SignalCatcher was resumed for
327 // just long enough to try to grab the thread-suspend lock).
328 LOG(DEBUG) << *self << " still suspended after undo "
329 << "(suspend count=" << self->suspend_count_ << ")";
330 }
331 }
332 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700333 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800334 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700335}
336
Elliott Hughes234ab152011-10-26 14:02:26 -0700337void ThreadList::UndoDebuggerSuspensions() {
338 Thread* self = Thread::Current();
339
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800340 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700341
342 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700343 MutexLock mu(self, *Locks::thread_list_lock_);
344 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 // Update global suspend all state for attaching threads.
346 suspend_all_count_ -= debug_suspend_all_count_;
347 debug_suspend_all_count_ = 0;
348 // Update running threads.
Elliott Hughes234ab152011-10-26 14:02:26 -0700349 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
350 Thread* thread = *it;
351 if (thread == self || thread->debug_suspend_count_ == 0) {
352 continue;
353 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354 thread->ModifySuspendCount(-thread->debug_suspend_count_, true);
Elliott Hughes234ab152011-10-26 14:02:26 -0700355 }
356 }
357
358 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700359 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700360 Thread::resume_cond_->Broadcast();
Elliott Hughes234ab152011-10-26 14:02:26 -0700361 }
362
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800363 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700364}
365
Elliott Hughese52e49b2012-04-02 16:05:44 -0700366void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700367 Thread* self = Thread::Current();
368 Locks::mutator_lock_->AssertNotHeld(self);
369 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 bool all_threads_are_daemons;
371 do {
372 all_threads_are_daemons = true;
373 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
374 // TODO: there's a race here with thread exit that's being worked around by checking if the
375 // thread has a peer.
376 Thread* thread = *it;
Ian Rogers81d425b2012-09-27 16:03:43 -0700377 if (thread != self && thread->HasPeer() && !thread->IsDaemon()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 all_threads_are_daemons = false;
379 break;
380 }
381 }
382 if (!all_threads_are_daemons) {
383 // Wait for another thread to exit before re-checking.
Ian Rogers81d425b2012-09-27 16:03:43 -0700384 thread_exit_cond_.Wait(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385 }
386 } while(!all_threads_are_daemons);
Elliott Hughes038a8062011-09-18 14:12:41 -0700387}
388
389void ThreadList::SuspendAllDaemonThreads() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700390 Thread* self = Thread::Current();
391 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700392 { // Tell all the daemons it's time to suspend.
Ian Rogers81d425b2012-09-27 16:03:43 -0700393 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Elliott Hughes038a8062011-09-18 14:12:41 -0700394 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
395 Thread* thread = *it;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 // This is only run after all non-daemon threads have exited, so the remainder should all be
397 // daemons.
398 CHECK(thread->IsDaemon());
Ian Rogers81d425b2012-09-27 16:03:43 -0700399 if (thread != self) {
Ian Rogers474b6da2012-09-25 00:20:38 -0700400 thread->ModifySuspendCount(+1, false);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700401 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700402 }
403 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700404 // Give the threads a chance to suspend, complaining if they're slow.
405 bool have_complained = false;
406 for (int i = 0; i < 10; ++i) {
407 usleep(200 * 1000);
408 bool all_suspended = true;
409 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
410 Thread* thread = *it;
Ian Rogers81d425b2012-09-27 16:03:43 -0700411 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
412 if (thread != self && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700413 if (!have_complained) {
414 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
415 have_complained = true;
416 }
417 all_suspended = false;
418 }
419 }
420 if (all_suspended) {
421 return;
422 }
423 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700424 LOG(ERROR) << "suspend all daemons failed";
425}
426void ThreadList::Register(Thread* self) {
427 DCHECK_EQ(self, Thread::Current());
428
429 if (VLOG_IS_ON(threads)) {
430 std::ostringstream oss;
431 self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump.
432 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss;
433 }
434
435 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
436 // SuspendAll requests.
Ian Rogers81d425b2012-09-27 16:03:43 -0700437 MutexLock mu(self, *Locks::thread_list_lock_);
438 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700439 self->suspend_count_ = suspend_all_count_;
440 self->debug_suspend_count_ = debug_suspend_all_count_;
441 CHECK(!Contains(self));
442 list_.push_back(self);
443}
444
445void ThreadList::Unregister(Thread* self) {
446 DCHECK_EQ(self, Thread::Current());
447
448 VLOG(threads) << "ThreadList::Unregister() " << *self;
449
450 // Any time-consuming destruction, plus anything that can call back into managed code or
451 // suspend and so on, must happen at this point, and not in ~Thread.
452 self->Destroy();
453
454 {
455 // Remove this thread from the list.
Ian Rogers81d425b2012-09-27 16:03:43 -0700456 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700457 CHECK(Contains(self));
458 list_.remove(self);
459 }
460
461 // Delete the Thread* and release the thin lock id.
462 uint32_t thin_lock_id = self->thin_lock_id_;
463 ReleaseThreadId(thin_lock_id);
464 delete self;
465
466 // Clear the TLS data, so that the underlying native thread is recognizably detached.
467 // (It may wish to reattach later.)
468 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
469
470 // Signal that a thread just detached.
Ian Rogers81d425b2012-09-27 16:03:43 -0700471 MutexLock mu(NULL, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472 thread_exit_cond_.Signal();
473}
474
475void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
476 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
477 callback(*it, context);
478 }
479}
480
481void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Ian Rogers81d425b2012-09-27 16:03:43 -0700482 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700483 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
484 (*it)->VisitRoots(visitor, arg);
485 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700486}
487
Elliott Hughes8daa0922011-09-11 13:46:25 -0700488uint32_t ThreadList::AllocThreadId() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700489 MutexLock mu(Thread::Current(), allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700490 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
491 if (!allocated_ids_[i]) {
492 allocated_ids_.set(i);
493 return i + 1; // Zero is reserved to mean "invalid".
494 }
495 }
496 LOG(FATAL) << "Out of internal thread ids";
497 return 0;
498}
499
500void ThreadList::ReleaseThreadId(uint32_t id) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700501 MutexLock mu(Thread::Current(), allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700502 --id; // Zero is reserved to mean "invalid".
503 DCHECK(allocated_ids_[id]) << id;
504 allocated_ids_.reset(id);
505}
506
507} // namespace art