blob: 44cf8101782ce599c7e5e2863920ab40059bb213 [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 Hughes76b61672012-12-12 17:47:30 -080023#include "base/mutex.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070024#include "base/mutex-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080025#include "base/timing_logger.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070026#include "debugger.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "thread.h"
Elliott Hughesabbe07d2012-06-05 17:42:23 -070028#include "utils.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070029
Elliott Hughes8daa0922011-09-11 13:46:25 -070030namespace art {
31
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080032ThreadList::ThreadList()
Elliott Hughese52e49b2012-04-02 16:05:44 -070033 : allocated_ids_lock_("allocated thread ids lock"),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034 suspend_all_count_(0), debug_suspend_all_count_(0),
Ian Rogersc604d732012-10-14 16:09:54 -070035 thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070036}
37
38ThreadList::~ThreadList() {
Elliott Hughese52e49b2012-04-02 16:05:44 -070039 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
Elliott Hughes6a144332012-04-03 13:07:11 -070040 // We need to detach the current thread here in case there's another thread waiting to join with
41 // us.
Elliott Hughes8daa0922011-09-11 13:46:25 -070042 if (Contains(Thread::Current())) {
43 Runtime::Current()->DetachCurrentThread();
44 }
Elliott Hughes6a144332012-04-03 13:07:11 -070045
46 WaitForOtherNonDaemonThreadsToExit();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 // TODO: there's an unaddressed race here where a thread may attach during shutdown, see
48 // Thread::Init.
Elliott Hughes6a144332012-04-03 13:07:11 -070049 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070050}
51
52bool ThreadList::Contains(Thread* thread) {
53 return find(list_.begin(), list_.end(), thread) != list_.end();
54}
55
Elliott Hughesabbe07d2012-06-05 17:42:23 -070056bool ThreadList::Contains(pid_t tid) {
Mathieu Chartier02e25112013-08-14 16:14:24 -070057 for (const auto& thread : list_) {
58 if (thread->tid_ == tid) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070059 return true;
60 }
61 }
62 return false;
63}
64
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070065pid_t ThreadList::GetLockOwner() {
Ian Rogersb726dcb2012-09-05 08:57:23 -070066 return Locks::thread_list_lock_->GetExclusiveOwnerTid();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070067}
68
Elliott Hughesc967f782012-04-16 10:23:15 -070069void ThreadList::DumpForSigQuit(std::ostream& os) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070 {
Ian Rogers50b35e22012-10-04 10:09:15 -070071 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 DumpLocked(os);
73 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -070074 DumpUnattachedThreads(os);
75}
76
Ian Rogerscfaa4552012-11-26 21:00:08 -080077static void DumpUnattachedThread(std::ostream& os, pid_t tid) NO_THREAD_SAFETY_ANALYSIS {
78 // TODO: No thread safety analysis as DumpState with a NULL thread won't access fields, should
79 // refactor DumpState to avoid skipping analysis.
Elliott Hughesabbe07d2012-06-05 17:42:23 -070080 Thread::DumpState(os, NULL, tid);
81 DumpKernelStack(os, tid, " kernel: ", false);
Brian Carlstromed8b7232012-06-27 17:54:47 -070082 // TODO: Reenable this when the native code in system_server can handle it.
83 // Currently "adb shell kill -3 `pid system_server`" will cause it to exit.
84 if (false) {
85 DumpNativeStack(os, tid, " native: ", false);
86 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -070087 os << "\n";
88}
89
90void ThreadList::DumpUnattachedThreads(std::ostream& os) {
91 DIR* d = opendir("/proc/self/task");
92 if (!d) {
93 return;
94 }
95
Ian Rogers50b35e22012-10-04 10:09:15 -070096 Thread* self = Thread::Current();
Elliott Hughes4696b5b2012-10-30 10:35:10 -070097 dirent* e;
98 while ((e = readdir(d)) != NULL) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070099 char* end;
Elliott Hughes4696b5b2012-10-30 10:35:10 -0700100 pid_t tid = strtol(e->d_name, &end, 10);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 if (!*end) {
102 bool contains;
103 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700104 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 contains = Contains(tid);
106 }
107 if (!contains) {
108 DumpUnattachedThread(os, tid);
109 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700110 }
111 }
112 closedir(d);
Elliott Hughesff738062012-02-03 15:00:42 -0800113}
114
115void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700116 os << "DALVIK THREADS (" << list_.size() << "):\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -0700117 for (const auto& thread : list_) {
118 thread->Dump(os);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700119 os << "\n";
120 }
121}
122
Ian Rogers50b35e22012-10-04 10:09:15 -0700123void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
124 MutexLock mu(self, *Locks::thread_list_lock_);
125 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700126 for (const auto& thread : list_) {
jeffhao725a9572012-11-13 18:20:12 -0800127 if (thread != ignore1 && thread != ignore2) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700128 CHECK(thread->IsSuspended())
129 << "\nUnsuspended thread: <<" << *thread << "\n"
130 << "self: <<" << *Thread::Current();
131 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700132 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133}
134
Ian Rogers66aee5c2012-08-15 17:17:47 -0700135#if HAVE_TIMED_RWLOCK
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136// Attempt to rectify locks so that we dump thread list with required locks before exiting.
Ian Rogers81d425b2012-09-27 16:03:43 -0700137static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 Runtime* runtime = Runtime::Current();
139 std::ostringstream ss;
140 ss << "Thread suspend timeout\n";
141 runtime->DumpLockHolders(ss);
142 ss << "\n";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 runtime->GetThreadList()->DumpLocked(ss);
144 LOG(FATAL) << ss.str();
145}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700146#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700148size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700149 Thread* self = Thread::Current();
150 if (kIsDebugBuild) {
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800151 Locks::mutator_lock_->AssertNotExclusiveHeld(self);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700152 Locks::thread_list_lock_->AssertNotHeld(self);
153 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
154 CHECK_NE(self->GetState(), kRunnable);
155 }
156
157 std::vector<Thread*> suspended_count_modified_threads;
158 size_t count = 0;
159 {
160 // Call a checkpoint function for each thread, threads which are suspend get their checkpoint
161 // manually called.
162 MutexLock mu(self, *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700163 for (const auto& thread : list_) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700164 if (thread != self) {
165 for (;;) {
166 if (thread->RequestCheckpoint(checkpoint_function)) {
167 // This thread will run it's checkpoint some time in the near future.
168 count++;
169 break;
170 } else {
171 // We are probably suspended, try to make sure that we stay suspended.
172 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
173 // The thread switched back to runnable.
174 if (thread->GetState() == kRunnable) {
175 continue;
176 }
177 thread->ModifySuspendCount(self, +1, false);
178 suspended_count_modified_threads.push_back(thread);
179 break;
180 }
181 }
182 }
183 }
184 }
185
186 // Run the checkpoint on ourself while we wait for threads to suspend.
187 checkpoint_function->Run(self);
188
189 // Run the checkpoint on the suspended threads.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700190 for (const auto& thread : suspended_count_modified_threads) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700191 if (!thread->IsSuspended()) {
192 // Wait until the thread is suspended.
193 uint64_t start = NanoTime();
194 do {
195 // Sleep for 100us.
196 usleep(100);
197 } while (!thread->IsSuspended());
198 uint64_t end = NanoTime();
199 // Shouldn't need to wait for longer than 1 millisecond.
200 const uint64_t threshold = 1;
201 if (NsToMs(end - start) > threshold) {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800202 LOG(INFO) << "Warning: waited longer than " << threshold
203 << " ms for thread suspend\n";
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700204 }
205 }
206 // We know for sure that the thread is suspended at this point.
207 thread->RunCheckpointFunction();
208 {
209 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
210 thread->ModifySuspendCount(self, -1, false);
211 }
212 }
213
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800214 {
215 // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their
216 // suspend count. Now the suspend_count_ is lowered so we must do the broadcast.
217 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
218 Thread::resume_cond_->Broadcast(self);
219 }
220
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700221 // Add one for self.
222 return count + suspended_count_modified_threads.size() + 1;
223}
224
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225void ThreadList::SuspendAll() {
226 Thread* self = Thread::Current();
227
228 VLOG(threads) << *self << " SuspendAll starting...";
229
230 if (kIsDebugBuild) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700231 Locks::mutator_lock_->AssertNotHeld(self);
232 Locks::thread_list_lock_->AssertNotHeld(self);
233 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234 CHECK_NE(self->GetState(), kRunnable);
235 }
236 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700237 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700239 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700240 // Update global suspend all state for attaching threads.
241 ++suspend_all_count_;
242 // Increment everybody's suspend count (except our own).
Mathieu Chartier02e25112013-08-14 16:14:24 -0700243 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244 if (thread == self) {
245 continue;
246 }
247 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700248 thread->ModifySuspendCount(self, +1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 }
250 }
251 }
252
Ian Rogers66aee5c2012-08-15 17:17:47 -0700253 // Block on the mutator lock until all Runnable threads release their share of access.
254#if HAVE_TIMED_RWLOCK
255 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700256 if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0))) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700257 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700258 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700259#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700260 Locks::mutator_lock_->ExclusiveLock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700261#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262
263 // Debug check that all threads are suspended.
Ian Rogers50b35e22012-10-04 10:09:15 -0700264 AssertThreadsAreSuspended(self, self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700265
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800266 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700267}
268
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269void ThreadList::ResumeAll() {
270 Thread* self = Thread::Current();
271
272 VLOG(threads) << *self << " ResumeAll starting";
Ian Rogers01ae5802012-09-28 16:14:01 -0700273
274 // Debug check that all threads are suspended.
Ian Rogers50b35e22012-10-04 10:09:15 -0700275 AssertThreadsAreSuspended(self, self);
Ian Rogers01ae5802012-09-28 16:14:01 -0700276
Ian Rogers81d425b2012-09-27 16:03:43 -0700277 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700279 MutexLock mu(self, *Locks::thread_list_lock_);
280 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700281 // Update global suspend all state for attaching threads.
282 --suspend_all_count_;
283 // Decrement the suspend counts for all threads.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700284 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 if (thread == self) {
286 continue;
287 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700288 thread->ModifySuspendCount(self, -1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 }
290
291 // Broadcast a notification to all suspended threads, some or all of
292 // which may choose to wake up. No need to wait for them.
293 VLOG(threads) << *self << " ResumeAll waking others";
Ian Rogersc604d732012-10-14 16:09:54 -0700294 Thread::resume_cond_->Broadcast(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 VLOG(threads) << *self << " ResumeAll complete";
297}
298
299void ThreadList::Resume(Thread* thread, bool for_debugger) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700300 Thread* self = Thread::Current();
301 DCHECK_NE(thread, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700303
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 {
305 // To check Contains.
Ian Rogers81d425b2012-09-27 16:03:43 -0700306 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 // To check IsSuspended.
Ian Rogers81d425b2012-09-27 16:03:43 -0700308 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
309 DCHECK(thread->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700310 if (!Contains(thread)) {
311 return;
312 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700313 thread->ModifySuspendCount(self, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700314 }
315
316 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700317 VLOG(threads) << "Resume(" << *thread << ") waking others";
Ian Rogers81d425b2012-09-27 16:03:43 -0700318 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700319 Thread::resume_cond_->Broadcast(self);
Elliott Hughes01158d72011-09-19 19:47:10 -0700320 }
321
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700322 VLOG(threads) << "Resume(" << *thread << ") complete";
323}
Elliott Hughes01158d72011-09-19 19:47:10 -0700324
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700325void ThreadList::SuspendAllForDebugger() {
326 Thread* self = Thread::Current();
327 Thread* debug_thread = Dbg::GetDebugThread();
328
329 VLOG(threads) << *self << " SuspendAllForDebugger starting...";
330
331 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700332 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700334 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 // Update global suspend all state for attaching threads.
336 ++suspend_all_count_;
337 ++debug_suspend_all_count_;
338 // Increment everybody's suspend count (except our own).
Mathieu Chartier02e25112013-08-14 16:14:24 -0700339 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700340 if (thread == self || thread == debug_thread) {
341 continue;
342 }
343 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700344 thread->ModifySuspendCount(self, +1, true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 }
346 }
347 }
348
Ian Rogers66aee5c2012-08-15 17:17:47 -0700349 // Block on the mutator lock until all Runnable threads release their share of access then
350 // immediately unlock again.
351#if HAVE_TIMED_RWLOCK
352 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700353 if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700354 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 } else {
Ian Rogers81d425b2012-09-27 16:03:43 -0700356 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700358#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700359 Locks::mutator_lock_->ExclusiveLock(self);
360 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700361#endif
Ian Rogers50b35e22012-10-04 10:09:15 -0700362 AssertThreadsAreSuspended(self, self, debug_thread);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700363
364 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700365}
366
Elliott Hughes475fc232011-10-25 15:00:35 -0700367void ThreadList::SuspendSelfForDebugger() {
368 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700369
Elliott Hughes475fc232011-10-25 15:00:35 -0700370 // The debugger thread must not suspend itself due to debugger activity!
371 Thread* debug_thread = Dbg::GetDebugThread();
372 CHECK(debug_thread != NULL);
373 CHECK(self != debug_thread);
jeffhaoa77f0f62012-12-05 17:19:31 -0800374 CHECK_NE(self->GetState(), kRunnable);
375 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes475fc232011-10-25 15:00:35 -0700376
jeffhaoa77f0f62012-12-05 17:19:31 -0800377 {
378 // Collisions with other suspends aren't really interesting. We want
379 // to ensure that we're the only one fiddling with the suspend count
380 // though.
381 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
382 self->ModifySuspendCount(self, +1, true);
383 CHECK_GT(self->suspend_count_, 0);
384 }
Elliott Hughes475fc232011-10-25 15:00:35 -0700385
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800386 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700387
388 // Tell JDWP that we've completed suspension. The JDWP thread can't
389 // tell us to resume before we're fully asleep because we hold the
390 // suspend count lock.
391 Dbg::ClearWaitForEventThread();
392
jeffhaoa77f0f62012-12-05 17:19:31 -0800393 {
394 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
395 while (self->suspend_count_ != 0) {
396 Thread::resume_cond_->Wait(self);
397 if (self->suspend_count_ != 0) {
398 // The condition was signaled but we're still suspended. This
399 // can happen if the debugger lets go while a SIGQUIT thread
400 // dump event is pending (assuming SignalCatcher was resumed for
401 // just long enough to try to grab the thread-suspend lock).
402 LOG(DEBUG) << *self << " still suspended after undo "
403 << "(suspend count=" << self->suspend_count_ << ")";
404 }
Elliott Hughes475fc232011-10-25 15:00:35 -0700405 }
jeffhaoa77f0f62012-12-05 17:19:31 -0800406 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes475fc232011-10-25 15:00:35 -0700407 }
jeffhaoa77f0f62012-12-05 17:19:31 -0800408
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800409 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700410}
411
Elliott Hughes234ab152011-10-26 14:02:26 -0700412void ThreadList::UndoDebuggerSuspensions() {
413 Thread* self = Thread::Current();
414
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800415 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700416
417 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700418 MutexLock mu(self, *Locks::thread_list_lock_);
419 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700420 // Update global suspend all state for attaching threads.
421 suspend_all_count_ -= debug_suspend_all_count_;
422 debug_suspend_all_count_ = 0;
423 // Update running threads.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700424 for (const auto& thread : list_) {
Elliott Hughes234ab152011-10-26 14:02:26 -0700425 if (thread == self || thread->debug_suspend_count_ == 0) {
426 continue;
427 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700428 thread->ModifySuspendCount(self, -thread->debug_suspend_count_, true);
Elliott Hughes234ab152011-10-26 14:02:26 -0700429 }
430 }
431
432 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700433 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700434 Thread::resume_cond_->Broadcast(self);
Elliott Hughes234ab152011-10-26 14:02:26 -0700435 }
436
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800437 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700438}
439
Elliott Hughese52e49b2012-04-02 16:05:44 -0700440void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700441 Thread* self = Thread::Current();
442 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700443 bool all_threads_are_daemons;
444 do {
Ian Rogers120f1c72012-09-28 17:17:10 -0700445 {
446 // No more threads can be born after we start to shutdown.
447 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
448 CHECK(Runtime::Current()->IsShuttingDown());
449 CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U);
450 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700451 all_threads_are_daemons = true;
Ian Rogers120f1c72012-09-28 17:17:10 -0700452 MutexLock mu(self, *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700453 for (const auto& thread : list_) {
Anwar Ghuloum97543682013-06-14 12:58:16 -0700454 if (thread != self && !thread->IsDaemon()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700455 all_threads_are_daemons = false;
456 break;
457 }
458 }
459 if (!all_threads_are_daemons) {
460 // Wait for another thread to exit before re-checking.
Ian Rogersc604d732012-10-14 16:09:54 -0700461 thread_exit_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700463 } while (!all_threads_are_daemons);
Elliott Hughes038a8062011-09-18 14:12:41 -0700464}
465
466void ThreadList::SuspendAllDaemonThreads() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700467 Thread* self = Thread::Current();
468 MutexLock mu(self, *Locks::thread_list_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700469 { // Tell all the daemons it's time to suspend.
Ian Rogers81d425b2012-09-27 16:03:43 -0700470 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700471 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472 // This is only run after all non-daemon threads have exited, so the remainder should all be
473 // daemons.
Ian Rogers7e762862012-10-22 15:45:08 -0700474 CHECK(thread->IsDaemon()) << *thread;
Ian Rogers81d425b2012-09-27 16:03:43 -0700475 if (thread != self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700476 thread->ModifySuspendCount(self, +1, false);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700477 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700478 }
479 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700480 // Give the threads a chance to suspend, complaining if they're slow.
481 bool have_complained = false;
482 for (int i = 0; i < 10; ++i) {
483 usleep(200 * 1000);
484 bool all_suspended = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700485 for (const auto& thread : list_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700486 if (thread != self && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700487 if (!have_complained) {
488 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
489 have_complained = true;
490 }
491 all_suspended = false;
492 }
493 }
494 if (all_suspended) {
495 return;
496 }
497 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700498 LOG(ERROR) << "suspend all daemons failed";
499}
500void ThreadList::Register(Thread* self) {
501 DCHECK_EQ(self, Thread::Current());
502
503 if (VLOG_IS_ON(threads)) {
504 std::ostringstream oss;
505 self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump.
506 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss;
507 }
508
509 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
510 // SuspendAll requests.
Ian Rogers81d425b2012-09-27 16:03:43 -0700511 MutexLock mu(self, *Locks::thread_list_lock_);
512 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700513 self->suspend_count_ = suspend_all_count_;
514 self->debug_suspend_count_ = debug_suspend_all_count_;
Ian Rogers01ae5802012-09-28 16:14:01 -0700515 if (self->suspend_count_ > 0) {
516 self->AtomicSetFlag(kSuspendRequest);
517 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700518 CHECK(!Contains(self));
519 list_.push_back(self);
520}
521
522void ThreadList::Unregister(Thread* self) {
523 DCHECK_EQ(self, Thread::Current());
524
525 VLOG(threads) << "ThreadList::Unregister() " << *self;
526
527 // Any time-consuming destruction, plus anything that can call back into managed code or
528 // suspend and so on, must happen at this point, and not in ~Thread.
529 self->Destroy();
530
Ian Rogerscfaa4552012-11-26 21:00:08 -0800531 uint32_t thin_lock_id = self->thin_lock_id_;
532 self->thin_lock_id_ = 0;
533 ReleaseThreadId(self, thin_lock_id);
534 while (self != NULL) {
535 // Remove and delete the Thread* while holding the thread_list_lock_ and
536 // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended.
Ian Rogers0878d652013-04-18 17:38:35 -0700537 // Note: deliberately not using MutexLock that could hold a stale self pointer.
538 Locks::thread_list_lock_->ExclusiveLock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700539 CHECK(Contains(self));
Ian Rogerscfaa4552012-11-26 21:00:08 -0800540 // Note: we don't take the thread_suspend_count_lock_ here as to be suspending a thread other
541 // than yourself you need to hold the thread_list_lock_ (see Thread::ModifySuspendCount).
542 if (!self->IsSuspended()) {
543 list_.remove(self);
544 delete self;
545 self = NULL;
546 }
Ian Rogers0878d652013-04-18 17:38:35 -0700547 Locks::thread_list_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700548 }
549
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700550 // Clear the TLS data, so that the underlying native thread is recognizably detached.
551 // (It may wish to reattach later.)
552 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
553
554 // Signal that a thread just detached.
Ian Rogers81d425b2012-09-27 16:03:43 -0700555 MutexLock mu(NULL, *Locks::thread_list_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700556 thread_exit_cond_.Signal(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700557}
558
559void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700560 for (const auto& thread : list_) {
561 callback(thread, context);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700562 }
563}
564
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565void ThreadList::VisitRoots(RootVisitor* visitor, void* arg) const {
Ian Rogers81d425b2012-09-27 16:03:43 -0700566 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700567 for (const auto& thread : list_) {
568 thread->VisitRoots(visitor, arg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700569 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700570}
571
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700572struct VerifyRootWrapperArg {
573 VerifyRootVisitor* visitor;
574 void* arg;
575};
576
577static mirror::Object* VerifyRootWrapperCallback(mirror::Object* root, void* arg) {
578 VerifyRootWrapperArg* wrapperArg = reinterpret_cast<VerifyRootWrapperArg*>(arg);
579 wrapperArg->visitor(root, wrapperArg->arg, 0, NULL);
580 return root;
581}
582
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800583void ThreadList::VerifyRoots(VerifyRootVisitor* visitor, void* arg) const {
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700584 VerifyRootWrapperArg wrapper;
585 wrapper.visitor = visitor;
586 wrapper.arg = arg;
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700587 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700588 for (const auto& thread : list_) {
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700589 thread->VisitRoots(VerifyRootWrapperCallback, &wrapper);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700590 }
591}
592
Ian Rogerscfaa4552012-11-26 21:00:08 -0800593uint32_t ThreadList::AllocThreadId(Thread* self) {
594 MutexLock mu(self, allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700595 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
596 if (!allocated_ids_[i]) {
597 allocated_ids_.set(i);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700598 return i + 1; // Zero is reserved to mean "invalid".
Elliott Hughes8daa0922011-09-11 13:46:25 -0700599 }
600 }
601 LOG(FATAL) << "Out of internal thread ids";
602 return 0;
603}
604
Ian Rogerscfaa4552012-11-26 21:00:08 -0800605void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) {
606 MutexLock mu(self, allocated_ids_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700607 --id; // Zero is reserved to mean "invalid".
Elliott Hughes8daa0922011-09-11 13:46:25 -0700608 DCHECK(allocated_ids_[id]) << id;
609 allocated_ids_.reset(id);
610}
611
Elliott Hughesf327e072013-01-09 16:01:26 -0800612Thread* ThreadList::FindThreadByThinLockId(uint32_t thin_lock_id) {
613 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700614 for (const auto& thread : list_) {
615 if (thread->GetThinLockId() == thin_lock_id) {
616 return thread;
Elliott Hughesf327e072013-01-09 16:01:26 -0800617 }
618 }
619 return NULL;
620}
621
Elliott Hughes8daa0922011-09-11 13:46:25 -0700622} // namespace art