blob: 1a2fd47dfa04f9369a822396ad5cbc75e6081cc8 [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
Ian Rogers50b35e22012-10-04 10:09:15 -070092 Thread* self = Thread::Current();
Elliott Hughes4696b5b2012-10-30 10:35:10 -070093 dirent* e;
94 while ((e = readdir(d)) != NULL) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070095 char* end;
Elliott Hughes4696b5b2012-10-30 10:35:10 -070096 pid_t tid = strtol(e->d_name, &end, 10);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 if (!*end) {
98 bool contains;
99 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700100 MutexLock mu(self, *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) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700112 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700113 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
114 (*it)->Dump(os);
115 os << "\n";
116 }
117}
118
Ian Rogers50b35e22012-10-04 10:09:15 -0700119void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
120 MutexLock mu(self, *Locks::thread_list_lock_);
121 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700122 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
123 Thread* thread = *it;
Ian Rogers01ae5802012-09-28 16:14:01 -0700124 if (thread != ignore1 || thread == ignore2) {
125 CHECK(thread->IsSuspended())
126 << "\nUnsuspended thread: <<" << *thread << "\n"
127 << "self: <<" << *Thread::Current();
128 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700129 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130}
131
Ian Rogers66aee5c2012-08-15 17:17:47 -0700132#if HAVE_TIMED_RWLOCK
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133// Attempt to rectify locks so that we dump thread list with required locks before exiting.
Ian Rogers81d425b2012-09-27 16:03:43 -0700134static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 Runtime* runtime = Runtime::Current();
136 std::ostringstream ss;
137 ss << "Thread suspend timeout\n";
138 runtime->DumpLockHolders(ss);
139 ss << "\n";
Ian Rogers81d425b2012-09-27 16:03:43 -0700140 Locks::mutator_lock_->SharedTryLock(self);
141 if (!Locks::mutator_lock_->IsSharedHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142 LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
143 }
Ian Rogers81d425b2012-09-27 16:03:43 -0700144 Locks::thread_list_lock_->TryLock(self);
145 if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 LOG(WARNING) << "Dumping thread list without holding thread_list_lock_";
147 }
148 runtime->GetThreadList()->DumpLocked(ss);
149 LOG(FATAL) << ss.str();
150}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700151#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700153size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700154 Thread* self = Thread::Current();
155 if (kIsDebugBuild) {
156 Locks::mutator_lock_->AssertNotHeld(self);
157 Locks::thread_list_lock_->AssertNotHeld(self);
158 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
159 CHECK_NE(self->GetState(), kRunnable);
160 }
161
162 std::vector<Thread*> suspended_count_modified_threads;
163 size_t count = 0;
164 {
165 // Call a checkpoint function for each thread, threads which are suspend get their checkpoint
166 // manually called.
167 MutexLock mu(self, *Locks::thread_list_lock_);
168 // TODO: C++0x auto.
169 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
170 Thread* thread = *it;
171 if (thread != self) {
172 for (;;) {
173 if (thread->RequestCheckpoint(checkpoint_function)) {
174 // This thread will run it's checkpoint some time in the near future.
175 count++;
176 break;
177 } else {
178 // We are probably suspended, try to make sure that we stay suspended.
179 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
180 // The thread switched back to runnable.
181 if (thread->GetState() == kRunnable) {
182 continue;
183 }
184 thread->ModifySuspendCount(self, +1, false);
185 suspended_count_modified_threads.push_back(thread);
186 break;
187 }
188 }
189 }
190 }
191 }
192
193 // Run the checkpoint on ourself while we wait for threads to suspend.
194 checkpoint_function->Run(self);
195
196 // Run the checkpoint on the suspended threads.
197 for (size_t i = 0; i < suspended_count_modified_threads.size(); ++i) {
198 Thread* thread = suspended_count_modified_threads[i];
199 if (!thread->IsSuspended()) {
200 // Wait until the thread is suspended.
201 uint64_t start = NanoTime();
202 do {
203 // Sleep for 100us.
204 usleep(100);
205 } while (!thread->IsSuspended());
206 uint64_t end = NanoTime();
207 // Shouldn't need to wait for longer than 1 millisecond.
208 const uint64_t threshold = 1;
209 if (NsToMs(end - start) > threshold) {
Brian Carlstrombcc29262012-11-02 11:36:03 -0700210 LOG(INFO) << "Warning: waited longer than " << threshold << " ms for thread suspend"
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700211 << std::endl;
212 }
213 }
214 // We know for sure that the thread is suspended at this point.
215 thread->RunCheckpointFunction();
216 {
217 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
218 thread->ModifySuspendCount(self, -1, false);
219 }
220 }
221
222 // Add one for self.
223 return count + suspended_count_modified_threads.size() + 1;
224}
225
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700226void ThreadList::SuspendAll() {
227 Thread* self = Thread::Current();
228
229 VLOG(threads) << *self << " SuspendAll starting...";
230
231 if (kIsDebugBuild) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700232 Locks::mutator_lock_->AssertNotHeld(self);
233 Locks::thread_list_lock_->AssertNotHeld(self);
234 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700235 CHECK_NE(self->GetState(), kRunnable);
236 }
237 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700238 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700240 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241 // Update global suspend all state for attaching threads.
242 ++suspend_all_count_;
243 // Increment everybody's suspend count (except our own).
244 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
245 Thread* thread = *it;
246 if (thread == self) {
247 continue;
248 }
249 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700250 thread->ModifySuspendCount(self, +1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 }
252 }
253 }
254
Ian Rogers66aee5c2012-08-15 17:17:47 -0700255 // Block on the mutator lock until all Runnable threads release their share of access.
256#if HAVE_TIMED_RWLOCK
257 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700258 if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0))) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700259 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700261#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700262 Locks::mutator_lock_->ExclusiveLock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700263#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264
265 // Debug check that all threads are suspended.
Ian Rogers50b35e22012-10-04 10:09:15 -0700266 AssertThreadsAreSuspended(self, self);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700267
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800268 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700269}
270
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271void ThreadList::ResumeAll() {
272 Thread* self = Thread::Current();
273
274 VLOG(threads) << *self << " ResumeAll starting";
Ian Rogers01ae5802012-09-28 16:14:01 -0700275
276 // Debug check that all threads are suspended.
Ian Rogers50b35e22012-10-04 10:09:15 -0700277 AssertThreadsAreSuspended(self, self);
Ian Rogers01ae5802012-09-28 16:14:01 -0700278
Ian Rogers81d425b2012-09-27 16:03:43 -0700279 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700281 MutexLock mu(self, *Locks::thread_list_lock_);
282 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700283 // Update global suspend all state for attaching threads.
284 --suspend_all_count_;
285 // Decrement the suspend counts for all threads.
286 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
287 Thread* thread = *it;
288 if (thread == self) {
289 continue;
290 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700291 thread->ModifySuspendCount(self, -1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 }
293
294 // Broadcast a notification to all suspended threads, some or all of
295 // which may choose to wake up. No need to wait for them.
296 VLOG(threads) << *self << " ResumeAll waking others";
Ian Rogersc604d732012-10-14 16:09:54 -0700297 Thread::resume_cond_->Broadcast(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299 VLOG(threads) << *self << " ResumeAll complete";
300}
301
302void ThreadList::Resume(Thread* thread, bool for_debugger) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700303 Thread* self = Thread::Current();
304 DCHECK_NE(thread, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700306
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700307 {
308 // To check Contains.
Ian Rogers81d425b2012-09-27 16:03:43 -0700309 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700310 // To check IsSuspended.
Ian Rogers81d425b2012-09-27 16:03:43 -0700311 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
312 DCHECK(thread->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 if (!Contains(thread)) {
314 return;
315 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700316 thread->ModifySuspendCount(self, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700317 }
318
319 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 VLOG(threads) << "Resume(" << *thread << ") waking others";
Ian Rogers81d425b2012-09-27 16:03:43 -0700321 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700322 Thread::resume_cond_->Broadcast(self);
Elliott Hughes01158d72011-09-19 19:47:10 -0700323 }
324
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700325 VLOG(threads) << "Resume(" << *thread << ") complete";
326}
Elliott Hughes01158d72011-09-19 19:47:10 -0700327
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700328void ThreadList::SuspendAllForDebugger() {
329 Thread* self = Thread::Current();
330 Thread* debug_thread = Dbg::GetDebugThread();
331
332 VLOG(threads) << *self << " SuspendAllForDebugger starting...";
333
334 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700335 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700337 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700338 // Update global suspend all state for attaching threads.
339 ++suspend_all_count_;
340 ++debug_suspend_all_count_;
341 // Increment everybody's suspend count (except our own).
342 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
343 Thread* thread = *it;
344 if (thread == self || thread == debug_thread) {
345 continue;
346 }
347 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700348 thread->ModifySuspendCount(self, +1, true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700349 }
350 }
351 }
352
Ian Rogers66aee5c2012-08-15 17:17:47 -0700353 // Block on the mutator lock until all Runnable threads release their share of access then
354 // immediately unlock again.
355#if HAVE_TIMED_RWLOCK
356 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700357 if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700358 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700359 } else {
Ian Rogers81d425b2012-09-27 16:03:43 -0700360 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700361 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700362#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700363 Locks::mutator_lock_->ExclusiveLock(self);
364 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700365#endif
Ian Rogers50b35e22012-10-04 10:09:15 -0700366 AssertThreadsAreSuspended(self, self, debug_thread);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700367
368 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700369}
370
Elliott Hughes475fc232011-10-25 15:00:35 -0700371void ThreadList::SuspendSelfForDebugger() {
372 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700373
Elliott Hughes475fc232011-10-25 15:00:35 -0700374 // The debugger thread must not suspend itself due to debugger activity!
375 Thread* debug_thread = Dbg::GetDebugThread();
376 CHECK(debug_thread != NULL);
377 CHECK(self != debug_thread);
378
379 // Collisions with other suspends aren't really interesting. We want
380 // to ensure that we're the only one fiddling with the suspend count
381 // though.
Ian Rogers81d425b2012-09-27 16:03:43 -0700382 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers01ae5802012-09-28 16:14:01 -0700383 self->ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700384
385 // Suspend ourselves.
386 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700387 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800388 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700389
390 // Tell JDWP that we've completed suspension. The JDWP thread can't
391 // tell us to resume before we're fully asleep because we hold the
392 // suspend count lock.
393 Dbg::ClearWaitForEventThread();
394
395 while (self->suspend_count_ != 0) {
Ian Rogersc604d732012-10-14 16:09:54 -0700396 Thread::resume_cond_->Wait(self);
Elliott Hughes475fc232011-10-25 15:00:35 -0700397 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 }
405 }
406 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700407 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800408 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700409}
410
Elliott Hughes234ab152011-10-26 14:02:26 -0700411void ThreadList::UndoDebuggerSuspensions() {
412 Thread* self = Thread::Current();
413
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800414 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700415
416 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700417 MutexLock mu(self, *Locks::thread_list_lock_);
418 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700419 // Update global suspend all state for attaching threads.
420 suspend_all_count_ -= debug_suspend_all_count_;
421 debug_suspend_all_count_ = 0;
422 // Update running threads.
Elliott Hughes234ab152011-10-26 14:02:26 -0700423 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
424 Thread* thread = *it;
425 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_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700453 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
454 // TODO: there's a race here with thread exit that's being worked around by checking if the
455 // thread has a peer.
456 Thread* thread = *it;
Ian Rogers81d425b2012-09-27 16:03:43 -0700457 if (thread != self && thread->HasPeer() && !thread->IsDaemon()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700458 all_threads_are_daemons = false;
459 break;
460 }
461 }
462 if (!all_threads_are_daemons) {
463 // Wait for another thread to exit before re-checking.
Ian Rogersc604d732012-10-14 16:09:54 -0700464 thread_exit_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700465 }
466 } while(!all_threads_are_daemons);
Elliott Hughes038a8062011-09-18 14:12:41 -0700467}
468
469void ThreadList::SuspendAllDaemonThreads() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700470 Thread* self = Thread::Current();
471 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472 { // Tell all the daemons it's time to suspend.
Ian Rogers81d425b2012-09-27 16:03:43 -0700473 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Elliott Hughes038a8062011-09-18 14:12:41 -0700474 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
475 Thread* thread = *it;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700476 // This is only run after all non-daemon threads have exited, so the remainder should all be
477 // daemons.
Ian Rogers7e762862012-10-22 15:45:08 -0700478 CHECK(thread->IsDaemon()) << *thread;
Ian Rogers81d425b2012-09-27 16:03:43 -0700479 if (thread != self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700480 thread->ModifySuspendCount(self, +1, false);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700481 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700482 }
483 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700484 // Give the threads a chance to suspend, complaining if they're slow.
485 bool have_complained = false;
486 for (int i = 0; i < 10; ++i) {
487 usleep(200 * 1000);
488 bool all_suspended = true;
489 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
490 Thread* thread = *it;
Ian Rogers81d425b2012-09-27 16:03:43 -0700491 if (thread != self && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700492 if (!have_complained) {
493 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
494 have_complained = true;
495 }
496 all_suspended = false;
497 }
498 }
499 if (all_suspended) {
500 return;
501 }
502 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700503 LOG(ERROR) << "suspend all daemons failed";
504}
505void ThreadList::Register(Thread* self) {
506 DCHECK_EQ(self, Thread::Current());
507
508 if (VLOG_IS_ON(threads)) {
509 std::ostringstream oss;
510 self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump.
511 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss;
512 }
513
514 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
515 // SuspendAll requests.
Ian Rogers81d425b2012-09-27 16:03:43 -0700516 MutexLock mu(self, *Locks::thread_list_lock_);
517 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700518 self->suspend_count_ = suspend_all_count_;
519 self->debug_suspend_count_ = debug_suspend_all_count_;
Ian Rogers01ae5802012-09-28 16:14:01 -0700520 if (self->suspend_count_ > 0) {
521 self->AtomicSetFlag(kSuspendRequest);
522 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700523 CHECK(!Contains(self));
524 list_.push_back(self);
525}
526
527void ThreadList::Unregister(Thread* self) {
528 DCHECK_EQ(self, Thread::Current());
529
530 VLOG(threads) << "ThreadList::Unregister() " << *self;
531
532 // Any time-consuming destruction, plus anything that can call back into managed code or
533 // suspend and so on, must happen at this point, and not in ~Thread.
534 self->Destroy();
535
536 {
537 // Remove this thread from the list.
Ian Rogers81d425b2012-09-27 16:03:43 -0700538 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700539 CHECK(Contains(self));
540 list_.remove(self);
541 }
542
543 // Delete the Thread* and release the thin lock id.
544 uint32_t thin_lock_id = self->thin_lock_id_;
545 ReleaseThreadId(thin_lock_id);
546 delete self;
547
548 // Clear the TLS data, so that the underlying native thread is recognizably detached.
549 // (It may wish to reattach later.)
550 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
551
552 // Signal that a thread just detached.
Ian Rogers81d425b2012-09-27 16:03:43 -0700553 MutexLock mu(NULL, *Locks::thread_list_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700554 thread_exit_cond_.Signal(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700555}
556
557void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
558 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
559 callback(*it, context);
560 }
561}
562
563void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Ian Rogers81d425b2012-09-27 16:03:43 -0700564 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700565 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
566 (*it)->VisitRoots(visitor, arg);
567 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700568}
569
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700570void ThreadList::VerifyRoots(Heap::VerifyRootVisitor* visitor, void* arg) const {
571 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
572 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
573 (*it)->VerifyRoots(visitor, arg);
574 }
575}
576
Elliott Hughes8daa0922011-09-11 13:46:25 -0700577uint32_t ThreadList::AllocThreadId() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700578 MutexLock mu(Thread::Current(), allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700579 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
580 if (!allocated_ids_[i]) {
581 allocated_ids_.set(i);
582 return i + 1; // Zero is reserved to mean "invalid".
583 }
584 }
585 LOG(FATAL) << "Out of internal thread ids";
586 return 0;
587}
588
589void ThreadList::ReleaseThreadId(uint32_t id) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700590 MutexLock mu(Thread::Current(), allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700591 --id; // Zero is reserved to mean "invalid".
592 DCHECK(allocated_ids_[id]) << id;
593 allocated_ids_.reset(id);
594}
595
596} // namespace art