blob: c1db387e0f88afe0f3ea278cb0b2d83f6b4d26cd [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.
131static void UnsafeLogFatalForThreadSuspendAllTimeout() NO_THREAD_SAFETY_ANALYSIS {
132 Runtime* runtime = Runtime::Current();
133 std::ostringstream ss;
134 ss << "Thread suspend timeout\n";
135 runtime->DumpLockHolders(ss);
136 ss << "\n";
Ian Rogersb726dcb2012-09-05 08:57:23 -0700137 Locks::mutator_lock_->SharedTryLock();
138 if (!Locks::mutator_lock_->IsSharedHeld()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
140 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700141 Locks::thread_list_lock_->TryLock();
142 if (!Locks::thread_list_lock_->IsExclusiveHeld()) {
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 Rogersb726dcb2012-09-05 08:57:23 -0700156 Locks::mutator_lock_->AssertNotHeld();
157 Locks::thread_list_lock_->AssertNotHeld();
158 Locks::thread_suspend_count_lock_->AssertNotHeld();
159 MutexLock mu(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 CHECK_NE(self->GetState(), kRunnable);
161 }
162 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700163 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700165 MutexLock mu2(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700166 // Update global suspend all state for attaching threads.
167 ++suspend_all_count_;
168 // Increment everybody's suspend count (except our own).
169 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
170 Thread* thread = *it;
171 if (thread == self) {
172 continue;
173 }
174 VLOG(threads) << "requesting thread suspend: " << *thread;
175 thread->ModifySuspendCount(+1, false);
176 }
177 }
178 }
179
Ian Rogers66aee5c2012-08-15 17:17:47 -0700180 // Block on the mutator lock until all Runnable threads release their share of access.
181#if HAVE_TIMED_RWLOCK
182 // Timeout if we wait more than 30 seconds.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700183 timespec timeout;
184 clock_gettime(CLOCK_REALTIME, &timeout);
185 timeout.tv_sec += 30;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(timeout))) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700187 UnsafeLogFatalForThreadSuspendAllTimeout();
188 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700189#else
Ian Rogersb726dcb2012-09-05 08:57:23 -0700190 Locks::mutator_lock_->ExclusiveLock();
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.
194 AssertThreadsAreSuspended();
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";
203 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700204 MutexLock mu(*Locks::thread_list_lock_);
205 MutexLock mu2(*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 Rogersb726dcb2012-09-05 08:57:23 -0700222 Locks::mutator_lock_->ExclusiveUnlock();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223 VLOG(threads) << *self << " ResumeAll complete";
224}
225
226void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700227 DCHECK(thread != Thread::Current());
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 Rogersb726dcb2012-09-05 08:57:23 -0700232 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 // To check IsSuspended.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 MutexLock mu2(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235 CHECK(thread->IsSuspended());
236 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 Rogersb726dcb2012-09-05 08:57:23 -0700244 MutexLock mu(*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 Rogersb726dcb2012-09-05 08:57:23 -0700258 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700260 MutexLock mu(*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 Rogersb726dcb2012-09-05 08:57:23 -0700283 if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(timeout)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 UnsafeLogFatalForThreadSuspendAllTimeout();
285 } else {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700286 Locks::mutator_lock_->ExclusiveUnlock();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700287 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700288#else
Ian Rogersb726dcb2012-09-05 08:57:23 -0700289 Locks::mutator_lock_->ExclusiveLock();
290 Locks::mutator_lock_->ExclusiveUnlock();
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 Rogersb726dcb2012-09-05 08:57:23 -0700308 MutexLock mu(*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 Rogersb726dcb2012-09-05 08:57:23 -0700322 Thread::resume_cond_->Wait(*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 Rogersb726dcb2012-09-05 08:57:23 -0700343 MutexLock mu(*Locks::thread_list_lock_);
344 MutexLock mu2(*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 Rogersb726dcb2012-09-05 08:57:23 -0700359 MutexLock mu(*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 Rogersb726dcb2012-09-05 08:57:23 -0700367 Locks::mutator_lock_->AssertNotHeld();
368 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 bool all_threads_are_daemons;
370 do {
371 all_threads_are_daemons = true;
372 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
373 // TODO: there's a race here with thread exit that's being worked around by checking if the
374 // thread has a peer.
375 Thread* thread = *it;
376 if (thread != Thread::Current() && thread->HasPeer() && !thread->IsDaemon()) {
377 all_threads_are_daemons = false;
378 break;
379 }
380 }
381 if (!all_threads_are_daemons) {
382 // Wait for another thread to exit before re-checking.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700383 thread_exit_cond_.Wait(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 }
385 } while(!all_threads_are_daemons);
Elliott Hughes038a8062011-09-18 14:12:41 -0700386}
387
388void ThreadList::SuspendAllDaemonThreads() {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700389 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 { // Tell all the daemons it's time to suspend.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700391 MutexLock mu2(*Locks::thread_suspend_count_lock_);
Elliott Hughes038a8062011-09-18 14:12:41 -0700392 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
393 Thread* thread = *it;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700394 // This is only run after all non-daemon threads have exited, so the remainder should all be
395 // daemons.
396 CHECK(thread->IsDaemon());
Elliott Hughese52e49b2012-04-02 16:05:44 -0700397 if (thread != Thread::Current()) {
398 ++thread->suspend_count_;
399 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700400 }
401 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700402 // Give the threads a chance to suspend, complaining if they're slow.
403 bool have_complained = false;
404 for (int i = 0; i < 10; ++i) {
405 usleep(200 * 1000);
406 bool all_suspended = true;
407 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
408 Thread* thread = *it;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700409 MutexLock mu2(*Locks::thread_suspend_count_lock_);
Elliott Hughes34e06962012-04-09 13:55:55 -0700410 if (thread != Thread::Current() && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700411 if (!have_complained) {
412 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
413 have_complained = true;
414 }
415 all_suspended = false;
416 }
417 }
418 if (all_suspended) {
419 return;
420 }
421 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700422 LOG(ERROR) << "suspend all daemons failed";
423}
424void ThreadList::Register(Thread* self) {
425 DCHECK_EQ(self, Thread::Current());
426
427 if (VLOG_IS_ON(threads)) {
428 std::ostringstream oss;
429 self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump.
430 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss;
431 }
432
433 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
434 // SuspendAll requests.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700435 MutexLock mu(*Locks::thread_list_lock_);
436 MutexLock mu2(*Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700437 self->suspend_count_ = suspend_all_count_;
438 self->debug_suspend_count_ = debug_suspend_all_count_;
439 CHECK(!Contains(self));
440 list_.push_back(self);
441}
442
443void ThreadList::Unregister(Thread* self) {
444 DCHECK_EQ(self, Thread::Current());
445
446 VLOG(threads) << "ThreadList::Unregister() " << *self;
447
448 // Any time-consuming destruction, plus anything that can call back into managed code or
449 // suspend and so on, must happen at this point, and not in ~Thread.
450 self->Destroy();
451
452 {
453 // Remove this thread from the list.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700454 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700455 CHECK(Contains(self));
456 list_.remove(self);
457 }
458
459 // Delete the Thread* and release the thin lock id.
460 uint32_t thin_lock_id = self->thin_lock_id_;
461 ReleaseThreadId(thin_lock_id);
462 delete self;
463
464 // Clear the TLS data, so that the underlying native thread is recognizably detached.
465 // (It may wish to reattach later.)
466 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
467
468 // Signal that a thread just detached.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700469 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700470 thread_exit_cond_.Signal();
471}
472
473void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
474 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
475 callback(*it, context);
476 }
477}
478
479void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Ian Rogersb726dcb2012-09-05 08:57:23 -0700480 MutexLock mu(*Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700481 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
482 (*it)->VisitRoots(visitor, arg);
483 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700484}
485
Elliott Hughes8daa0922011-09-11 13:46:25 -0700486uint32_t ThreadList::AllocThreadId() {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700487 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700488 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
489 if (!allocated_ids_[i]) {
490 allocated_ids_.set(i);
491 return i + 1; // Zero is reserved to mean "invalid".
492 }
493 }
494 LOG(FATAL) << "Out of internal thread ids";
495 return 0;
496}
497
498void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700499 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700500 --id; // Zero is reserved to mean "invalid".
501 DCHECK(allocated_ids_[id]) << id;
502 allocated_ids_.reset(id);
503}
504
505} // namespace art