blob: 7745a19123470520d6e20aa10a74a711dbf2d284 [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>
Ian Rogersd9c4fc92013-10-01 19:45:43 -070020#include <ScopedLocalRef.h>
21#include <ScopedUtfChars.h>
Elliott Hughesabbe07d2012-06-05 17:42:23 -070022#include <sys/types.h>
Elliott Hughes038a8062011-09-18 14:12:41 -070023#include <unistd.h>
24
Elliott Hughes76b61672012-12-12 17:47:30 -080025#include "base/mutex.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070026#include "base/mutex-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080027#include "base/timing_logger.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070028#include "debugger.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070029#include "jni_internal.h"
30#include "lock_word.h"
31#include "monitor.h"
32#include "scoped_thread_state_change.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "thread.h"
Elliott Hughesabbe07d2012-06-05 17:42:23 -070034#include "utils.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070035#include "well_known_classes.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070036
Elliott Hughes8daa0922011-09-11 13:46:25 -070037namespace art {
38
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080039ThreadList::ThreadList()
Elliott Hughese52e49b2012-04-02 16:05:44 -070040 : allocated_ids_lock_("allocated thread ids lock"),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 suspend_all_count_(0), debug_suspend_all_count_(0),
Ian Rogersc604d732012-10-14 16:09:54 -070042 thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -070043 CHECK(Monitor::IsValidLockWord(LockWord::FromThinLockId(kMaxThreadId, 1)));
Elliott Hughes8daa0922011-09-11 13:46:25 -070044}
45
46ThreadList::~ThreadList() {
Elliott Hughese52e49b2012-04-02 16:05:44 -070047 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
Elliott Hughes6a144332012-04-03 13:07:11 -070048 // We need to detach the current thread here in case there's another thread waiting to join with
49 // us.
Elliott Hughes8daa0922011-09-11 13:46:25 -070050 if (Contains(Thread::Current())) {
51 Runtime::Current()->DetachCurrentThread();
52 }
Elliott Hughes6a144332012-04-03 13:07:11 -070053
54 WaitForOtherNonDaemonThreadsToExit();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 // TODO: there's an unaddressed race here where a thread may attach during shutdown, see
56 // Thread::Init.
Elliott Hughes6a144332012-04-03 13:07:11 -070057 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070058}
59
60bool ThreadList::Contains(Thread* thread) {
61 return find(list_.begin(), list_.end(), thread) != list_.end();
62}
63
Elliott Hughesabbe07d2012-06-05 17:42:23 -070064bool ThreadList::Contains(pid_t tid) {
Mathieu Chartier02e25112013-08-14 16:14:24 -070065 for (const auto& thread : list_) {
66 if (thread->tid_ == tid) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070067 return true;
68 }
69 }
70 return false;
71}
72
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070073pid_t ThreadList::GetLockOwner() {
Ian Rogersb726dcb2012-09-05 08:57:23 -070074 return Locks::thread_list_lock_->GetExclusiveOwnerTid();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070075}
76
Mathieu Chartier590fee92013-09-13 13:46:47 -070077void ThreadList::DumpNativeStacks(std::ostream& os) {
78 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
79 for (const auto& thread : list_) {
80 os << "DUMPING THREAD " << thread->tid_ << "\n";
81 DumpNativeStack(os, thread->tid_, "\t", true);
82 os << "\n";
83 }
84}
85
Elliott Hughesc967f782012-04-16 10:23:15 -070086void ThreadList::DumpForSigQuit(std::ostream& os) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 {
Ian Rogers50b35e22012-10-04 10:09:15 -070088 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 DumpLocked(os);
90 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -070091 DumpUnattachedThreads(os);
92}
93
Ian Rogerscfaa4552012-11-26 21:00:08 -080094static void DumpUnattachedThread(std::ostream& os, pid_t tid) NO_THREAD_SAFETY_ANALYSIS {
95 // TODO: No thread safety analysis as DumpState with a NULL thread won't access fields, should
96 // refactor DumpState to avoid skipping analysis.
Elliott Hughesabbe07d2012-06-05 17:42:23 -070097 Thread::DumpState(os, NULL, tid);
98 DumpKernelStack(os, tid, " kernel: ", false);
Brian Carlstromed8b7232012-06-27 17:54:47 -070099 // TODO: Reenable this when the native code in system_server can handle it.
100 // Currently "adb shell kill -3 `pid system_server`" will cause it to exit.
101 if (false) {
102 DumpNativeStack(os, tid, " native: ", false);
103 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700104 os << "\n";
105}
106
107void ThreadList::DumpUnattachedThreads(std::ostream& os) {
108 DIR* d = opendir("/proc/self/task");
109 if (!d) {
110 return;
111 }
112
Ian Rogers50b35e22012-10-04 10:09:15 -0700113 Thread* self = Thread::Current();
Elliott Hughes4696b5b2012-10-30 10:35:10 -0700114 dirent* e;
115 while ((e = readdir(d)) != NULL) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700116 char* end;
Elliott Hughes4696b5b2012-10-30 10:35:10 -0700117 pid_t tid = strtol(e->d_name, &end, 10);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 if (!*end) {
119 bool contains;
120 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700121 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122 contains = Contains(tid);
123 }
124 if (!contains) {
125 DumpUnattachedThread(os, tid);
126 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700127 }
128 }
129 closedir(d);
Elliott Hughesff738062012-02-03 15:00:42 -0800130}
131
132void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700133 os << "DALVIK THREADS (" << list_.size() << "):\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -0700134 for (const auto& thread : list_) {
135 thread->Dump(os);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700136 os << "\n";
137 }
138}
139
Ian Rogers50b35e22012-10-04 10:09:15 -0700140void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
141 MutexLock mu(self, *Locks::thread_list_lock_);
142 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700143 for (const auto& thread : list_) {
jeffhao725a9572012-11-13 18:20:12 -0800144 if (thread != ignore1 && thread != ignore2) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700145 CHECK(thread->IsSuspended())
146 << "\nUnsuspended thread: <<" << *thread << "\n"
147 << "self: <<" << *Thread::Current();
148 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700149 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700150}
151
Ian Rogers66aee5c2012-08-15 17:17:47 -0700152#if HAVE_TIMED_RWLOCK
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153// Attempt to rectify locks so that we dump thread list with required locks before exiting.
Ian Rogers81d425b2012-09-27 16:03:43 -0700154static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 Runtime* runtime = Runtime::Current();
156 std::ostringstream ss;
157 ss << "Thread suspend timeout\n";
158 runtime->DumpLockHolders(ss);
159 ss << "\n";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 runtime->GetThreadList()->DumpLocked(ss);
161 LOG(FATAL) << ss.str();
162}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700163#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800165// Unlike suspending all threads where we can wait to acquire the mutator_lock_, suspending an
166// individual thread requires polling. delay_us is the requested sleep and total_delay_us
167// accumulates the total time spent sleeping for timeouts. The first sleep is just a yield,
168// subsequently sleeps increase delay_us from 1ms to 500ms by doubling.
169static void ThreadSuspendSleep(Thread* self, useconds_t* delay_us, useconds_t* total_delay_us,
170 bool holding_locks) {
171 if (!holding_locks) {
172 for (int i = kLockLevelCount - 1; i >= 0; --i) {
173 BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i));
174 if (held_mutex != NULL) {
175 LOG(FATAL) << "Holding " << held_mutex->GetName() << " while sleeping for thread suspension";
176 }
177 }
178 }
179 useconds_t new_delay_us = (*delay_us) * 2;
180 CHECK_GE(new_delay_us, *delay_us);
181 if (new_delay_us < 500000) { // Don't allow sleeping to be more than 0.5s.
182 *delay_us = new_delay_us;
183 }
184 if (*delay_us == 0) {
185 sched_yield();
186 // Default to 1 milliseconds (note that this gets multiplied by 2 before the first sleep).
187 *delay_us = 500;
188 } else {
189 usleep(*delay_us);
190 *total_delay_us += *delay_us;
191 }
192}
193
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700194size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700195 Thread* self = Thread::Current();
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800196 Locks::mutator_lock_->AssertNotExclusiveHeld(self);
197 Locks::thread_list_lock_->AssertNotHeld(self);
198 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
199 if (kDebugLocking) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700200 CHECK_NE(self->GetState(), kRunnable);
201 }
202
203 std::vector<Thread*> suspended_count_modified_threads;
204 size_t count = 0;
205 {
206 // Call a checkpoint function for each thread, threads which are suspend get their checkpoint
207 // manually called.
208 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700209 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700210 for (const auto& thread : list_) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700211 if (thread != self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700212 while (true) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700213 if (thread->RequestCheckpoint(checkpoint_function)) {
Dave Allison0aded082013-11-07 13:15:11 -0800214 // This thread will run its checkpoint some time in the near future.
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700215 count++;
216 break;
217 } else {
218 // We are probably suspended, try to make sure that we stay suspended.
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700219 // The thread switched back to runnable.
220 if (thread->GetState() == kRunnable) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700221 // Spurious fail, try again.
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700222 continue;
223 }
224 thread->ModifySuspendCount(self, +1, false);
225 suspended_count_modified_threads.push_back(thread);
226 break;
227 }
228 }
229 }
230 }
231 }
232
233 // Run the checkpoint on ourself while we wait for threads to suspend.
234 checkpoint_function->Run(self);
235
236 // Run the checkpoint on the suspended threads.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700237 for (const auto& thread : suspended_count_modified_threads) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700238 if (!thread->IsSuspended()) {
239 // Wait until the thread is suspended.
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800240 useconds_t total_delay_us = 0;
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700241 do {
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800242 useconds_t delay_us = 100;
243 ThreadSuspendSleep(self, &delay_us, &total_delay_us, true);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700244 } while (!thread->IsSuspended());
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800245 // Shouldn't need to wait for longer than 1000 microseconds.
246 constexpr useconds_t kLongWaitThresholdUS = 1000;
247 if (UNLIKELY(total_delay_us > kLongWaitThresholdUS)) {
248 LOG(WARNING) << "Waited " << total_delay_us << " us for thread suspend!";
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700249 }
250 }
251 // We know for sure that the thread is suspended at this point.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700252 checkpoint_function->Run(thread);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700253 {
254 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
255 thread->ModifySuspendCount(self, -1, false);
256 }
257 }
258
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800259 {
260 // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their
261 // suspend count. Now the suspend_count_ is lowered so we must do the broadcast.
262 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
263 Thread::resume_cond_->Broadcast(self);
264 }
265
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700266 // Add one for self.
267 return count + suspended_count_modified_threads.size() + 1;
268}
269
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270void ThreadList::SuspendAll() {
271 Thread* self = Thread::Current();
Brian Carlstrom6449c622014-02-10 23:48:36 -0800272 DCHECK(self != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273
274 VLOG(threads) << *self << " SuspendAll starting...";
275
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800276 Locks::mutator_lock_->AssertNotHeld(self);
277 Locks::thread_list_lock_->AssertNotHeld(self);
278 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
279 if (kDebugLocking) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 CHECK_NE(self->GetState(), kRunnable);
281 }
282 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700283 MutexLock mu(self, *Locks::thread_list_lock_);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800284 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
285 // Update global suspend all state for attaching threads.
286 ++suspend_all_count_;
287 // Increment everybody's suspend count (except our own).
288 for (const auto& thread : list_) {
289 if (thread == self) {
290 continue;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 }
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800292 VLOG(threads) << "requesting thread suspend: " << *thread;
293 thread->ModifySuspendCount(self, +1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 }
295 }
296
Ian Rogers66aee5c2012-08-15 17:17:47 -0700297 // Block on the mutator lock until all Runnable threads release their share of access.
298#if HAVE_TIMED_RWLOCK
299 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700300 if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0))) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700301 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700303#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700304 Locks::mutator_lock_->ExclusiveLock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700305#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700306
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800307 if (kDebugLocking) {
308 // Debug check that all threads are suspended.
309 AssertThreadsAreSuspended(self, self);
310 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700311
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800312 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700313}
314
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315void ThreadList::ResumeAll() {
316 Thread* self = Thread::Current();
317
318 VLOG(threads) << *self << " ResumeAll starting";
Ian Rogers01ae5802012-09-28 16:14:01 -0700319
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800320 if (kDebugLocking) {
321 // Debug check that all threads are suspended.
322 AssertThreadsAreSuspended(self, self);
323 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700324
Ian Rogers81d425b2012-09-27 16:03:43 -0700325 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700326 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700327 MutexLock mu(self, *Locks::thread_list_lock_);
328 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700329 // Update global suspend all state for attaching threads.
330 --suspend_all_count_;
331 // Decrement the suspend counts for all threads.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700332 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700333 if (thread == self) {
334 continue;
335 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700336 thread->ModifySuspendCount(self, -1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700337 }
338
339 // Broadcast a notification to all suspended threads, some or all of
340 // which may choose to wake up. No need to wait for them.
341 VLOG(threads) << *self << " ResumeAll waking others";
Ian Rogersc604d732012-10-14 16:09:54 -0700342 Thread::resume_cond_->Broadcast(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700344 VLOG(threads) << *self << " ResumeAll complete";
345}
346
347void ThreadList::Resume(Thread* thread, bool for_debugger) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700348 Thread* self = Thread::Current();
349 DCHECK_NE(thread, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700351
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700352 {
353 // To check Contains.
Ian Rogers81d425b2012-09-27 16:03:43 -0700354 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 // To check IsSuspended.
Ian Rogers81d425b2012-09-27 16:03:43 -0700356 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
357 DCHECK(thread->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 if (!Contains(thread)) {
359 return;
360 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700361 thread->ModifySuspendCount(self, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700362 }
363
364 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700365 VLOG(threads) << "Resume(" << *thread << ") waking others";
Ian Rogers81d425b2012-09-27 16:03:43 -0700366 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700367 Thread::resume_cond_->Broadcast(self);
Elliott Hughes01158d72011-09-19 19:47:10 -0700368 }
369
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370 VLOG(threads) << "Resume(" << *thread << ") complete";
371}
Elliott Hughes01158d72011-09-19 19:47:10 -0700372
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700373static void ThreadSuspendByPeerWarning(Thread* self, int level, const char* message, jobject peer) {
374 JNIEnvExt* env = self->GetJniEnv();
375 ScopedLocalRef<jstring>
376 scoped_name_string(env, (jstring)env->GetObjectField(peer,
377 WellKnownClasses::java_lang_Thread_name));
378 ScopedUtfChars scoped_name_chars(env, scoped_name_string.get());
379 if (scoped_name_chars.c_str() == NULL) {
380 LOG(level) << message << ": " << peer;
381 env->ExceptionClear();
382 } else {
383 LOG(level) << message << ": " << peer << ":" << scoped_name_chars.c_str();
384 }
385}
386
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700387Thread* ThreadList::SuspendThreadByPeer(jobject peer, bool request_suspension,
388 bool debug_suspension, bool* timed_out) {
389 static const useconds_t kTimeoutUs = 30 * 1000000; // 30s.
390 useconds_t total_delay_us = 0;
391 useconds_t delay_us = 0;
392 bool did_suspend_request = false;
393 *timed_out = false;
394 Thread* self = Thread::Current();
395 while (true) {
396 Thread* thread;
397 {
398 ScopedObjectAccess soa(self);
399 MutexLock mu(self, *Locks::thread_list_lock_);
400 thread = Thread::FromManagedThread(soa, peer);
401 if (thread == NULL) {
402 ThreadSuspendByPeerWarning(self, WARNING, "No such thread for suspend", peer);
403 return NULL;
404 }
405 {
406 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
407 if (request_suspension) {
408 thread->ModifySuspendCount(self, +1, debug_suspension);
409 request_suspension = false;
410 did_suspend_request = true;
411 } else {
412 // If the caller isn't requesting suspension, a suspension should have already occurred.
413 CHECK_GT(thread->GetSuspendCount(), 0);
414 }
415 // IsSuspended on the current thread will fail as the current thread is changed into
416 // Runnable above. As the suspend count is now raised if this is the current thread
417 // it will self suspend on transition to Runnable, making it hard to work with. It's simpler
418 // to just explicitly handle the current thread in the callers to this code.
419 CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
420 // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
421 // count, or else we've waited and it has self suspended) or is the current thread, we're
422 // done.
423 if (thread->IsSuspended()) {
424 return thread;
425 }
426 if (total_delay_us >= kTimeoutUs) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700427 ThreadSuspendByPeerWarning(self, FATAL, "Thread suspension timed out", peer);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700428 if (did_suspend_request) {
429 thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
430 }
431 *timed_out = true;
432 return NULL;
433 }
434 }
435 // Release locks and come out of runnable state.
436 }
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800437 ThreadSuspendSleep(self, &delay_us, &total_delay_us, false);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700438 }
439}
440
441static void ThreadSuspendByThreadIdWarning(int level, const char* message, uint32_t thread_id) {
442 LOG(level) << StringPrintf("%s: %d", message, thread_id);
443}
444
445Thread* ThreadList::SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension,
446 bool* timed_out) {
447 static const useconds_t kTimeoutUs = 30 * 1000000; // 30s.
448 useconds_t total_delay_us = 0;
449 useconds_t delay_us = 0;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700450 *timed_out = false;
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800451 Thread* suspended_thread = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700452 Thread* self = Thread::Current();
453 CHECK_NE(thread_id, kInvalidThreadId);
454 while (true) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700455 {
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800456 Thread* thread = NULL;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700457 ScopedObjectAccess soa(self);
458 MutexLock mu(self, *Locks::thread_list_lock_);
459 for (const auto& it : list_) {
460 if (it->GetThreadId() == thread_id) {
461 thread = it;
462 break;
463 }
464 }
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800465 if (thread == nullptr) {
466 CHECK(suspended_thread == nullptr) << "Suspended thread " << suspended_thread
467 << " no longer in thread list";
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700468 // There's a race in inflating a lock and the owner giving up ownership and then dying.
469 ThreadSuspendByThreadIdWarning(WARNING, "No such thread id for suspend", thread_id);
470 return NULL;
471 }
472 {
473 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800474 if (suspended_thread == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700475 thread->ModifySuspendCount(self, +1, debug_suspension);
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800476 suspended_thread = thread;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700477 } else {
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800478 CHECK_EQ(suspended_thread, thread);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700479 // If the caller isn't requesting suspension, a suspension should have already occurred.
480 CHECK_GT(thread->GetSuspendCount(), 0);
481 }
482 // IsSuspended on the current thread will fail as the current thread is changed into
483 // Runnable above. As the suspend count is now raised if this is the current thread
484 // it will self suspend on transition to Runnable, making it hard to work with. It's simpler
485 // to just explicitly handle the current thread in the callers to this code.
486 CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
487 // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
488 // count, or else we've waited and it has self suspended) or is the current thread, we're
489 // done.
490 if (thread->IsSuspended()) {
491 return thread;
492 }
493 if (total_delay_us >= kTimeoutUs) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700494 ThreadSuspendByThreadIdWarning(WARNING, "Thread suspension timed out", thread_id);
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800495 if (suspended_thread != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700496 thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
497 }
498 *timed_out = true;
499 return NULL;
500 }
501 }
502 // Release locks and come out of runnable state.
503 }
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800504 ThreadSuspendSleep(self, &delay_us, &total_delay_us, false);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700505 }
506}
507
508Thread* ThreadList::FindThreadByThreadId(uint32_t thin_lock_id) {
509 Thread* self = Thread::Current();
510 MutexLock mu(self, *Locks::thread_list_lock_);
511 for (const auto& thread : list_) {
512 if (thread->GetThreadId() == thin_lock_id) {
513 CHECK(thread == self || thread->IsSuspended());
514 return thread;
515 }
516 }
517 return NULL;
518}
519
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700520void ThreadList::SuspendAllForDebugger() {
521 Thread* self = Thread::Current();
522 Thread* debug_thread = Dbg::GetDebugThread();
523
524 VLOG(threads) << *self << " SuspendAllForDebugger starting...";
525
526 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700527 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700528 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700529 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 // Update global suspend all state for attaching threads.
531 ++suspend_all_count_;
532 ++debug_suspend_all_count_;
533 // Increment everybody's suspend count (except our own).
Mathieu Chartier02e25112013-08-14 16:14:24 -0700534 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700535 if (thread == self || thread == debug_thread) {
536 continue;
537 }
538 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700539 thread->ModifySuspendCount(self, +1, true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700540 }
541 }
542 }
543
Ian Rogers66aee5c2012-08-15 17:17:47 -0700544 // Block on the mutator lock until all Runnable threads release their share of access then
545 // immediately unlock again.
546#if HAVE_TIMED_RWLOCK
547 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700548 if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700549 UnsafeLogFatalForThreadSuspendAllTimeout(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700550 } else {
Ian Rogers81d425b2012-09-27 16:03:43 -0700551 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700552 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700553#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700554 Locks::mutator_lock_->ExclusiveLock(self);
555 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700556#endif
Ian Rogers50b35e22012-10-04 10:09:15 -0700557 AssertThreadsAreSuspended(self, self, debug_thread);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558
559 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700560}
561
Elliott Hughes475fc232011-10-25 15:00:35 -0700562void ThreadList::SuspendSelfForDebugger() {
563 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700564
Elliott Hughes475fc232011-10-25 15:00:35 -0700565 // The debugger thread must not suspend itself due to debugger activity!
566 Thread* debug_thread = Dbg::GetDebugThread();
567 CHECK(debug_thread != NULL);
568 CHECK(self != debug_thread);
jeffhaoa77f0f62012-12-05 17:19:31 -0800569 CHECK_NE(self->GetState(), kRunnable);
570 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes475fc232011-10-25 15:00:35 -0700571
jeffhaoa77f0f62012-12-05 17:19:31 -0800572 {
573 // Collisions with other suspends aren't really interesting. We want
574 // to ensure that we're the only one fiddling with the suspend count
575 // though.
576 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
577 self->ModifySuspendCount(self, +1, true);
578 CHECK_GT(self->suspend_count_, 0);
579 }
Elliott Hughes475fc232011-10-25 15:00:35 -0700580
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800581 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700582
Sebastien Hertz21e729c2014-02-18 14:16:00 +0100583 // Tell JDWP we've completed invocation and are ready to suspend.
584 DebugInvokeReq* pReq = self->GetInvokeReq();
585 DCHECK(pReq != NULL);
586 if (pReq->invoke_needed) {
587 // Clear this before signaling.
588 pReq->invoke_needed = false;
589
590 VLOG(jdwp) << "invoke complete, signaling";
591 MutexLock mu(self, pReq->lock);
592 pReq->cond.Signal(self);
593 }
594
Elliott Hughes475fc232011-10-25 15:00:35 -0700595 // Tell JDWP that we've completed suspension. The JDWP thread can't
596 // tell us to resume before we're fully asleep because we hold the
597 // suspend count lock.
598 Dbg::ClearWaitForEventThread();
599
jeffhaoa77f0f62012-12-05 17:19:31 -0800600 {
601 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
602 while (self->suspend_count_ != 0) {
603 Thread::resume_cond_->Wait(self);
604 if (self->suspend_count_ != 0) {
605 // The condition was signaled but we're still suspended. This
606 // can happen if the debugger lets go while a SIGQUIT thread
607 // dump event is pending (assuming SignalCatcher was resumed for
608 // just long enough to try to grab the thread-suspend lock).
609 LOG(DEBUG) << *self << " still suspended after undo "
610 << "(suspend count=" << self->suspend_count_ << ")";
611 }
Elliott Hughes475fc232011-10-25 15:00:35 -0700612 }
jeffhaoa77f0f62012-12-05 17:19:31 -0800613 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes475fc232011-10-25 15:00:35 -0700614 }
jeffhaoa77f0f62012-12-05 17:19:31 -0800615
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800616 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700617}
618
Elliott Hughes234ab152011-10-26 14:02:26 -0700619void ThreadList::UndoDebuggerSuspensions() {
620 Thread* self = Thread::Current();
621
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800622 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700623
624 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700625 MutexLock mu(self, *Locks::thread_list_lock_);
626 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700627 // Update global suspend all state for attaching threads.
628 suspend_all_count_ -= debug_suspend_all_count_;
629 debug_suspend_all_count_ = 0;
630 // Update running threads.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700631 for (const auto& thread : list_) {
Elliott Hughes234ab152011-10-26 14:02:26 -0700632 if (thread == self || thread->debug_suspend_count_ == 0) {
633 continue;
634 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700635 thread->ModifySuspendCount(self, -thread->debug_suspend_count_, true);
Elliott Hughes234ab152011-10-26 14:02:26 -0700636 }
637 }
638
639 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700640 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700641 Thread::resume_cond_->Broadcast(self);
Elliott Hughes234ab152011-10-26 14:02:26 -0700642 }
643
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800644 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700645}
646
Elliott Hughese52e49b2012-04-02 16:05:44 -0700647void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700648 Thread* self = Thread::Current();
649 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 bool all_threads_are_daemons;
651 do {
Ian Rogers120f1c72012-09-28 17:17:10 -0700652 {
653 // No more threads can be born after we start to shutdown.
654 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700655 CHECK(Runtime::Current()->IsShuttingDownLocked());
Ian Rogers120f1c72012-09-28 17:17:10 -0700656 CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U);
657 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700658 all_threads_are_daemons = true;
Ian Rogers120f1c72012-09-28 17:17:10 -0700659 MutexLock mu(self, *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700660 for (const auto& thread : list_) {
Anwar Ghuloum97543682013-06-14 12:58:16 -0700661 if (thread != self && !thread->IsDaemon()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 all_threads_are_daemons = false;
663 break;
664 }
665 }
666 if (!all_threads_are_daemons) {
667 // Wait for another thread to exit before re-checking.
Ian Rogersc604d732012-10-14 16:09:54 -0700668 thread_exit_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 }
Brian Carlstromdf629502013-07-17 22:39:56 -0700670 } while (!all_threads_are_daemons);
Elliott Hughes038a8062011-09-18 14:12:41 -0700671}
672
673void ThreadList::SuspendAllDaemonThreads() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700674 Thread* self = Thread::Current();
675 MutexLock mu(self, *Locks::thread_list_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700676 { // Tell all the daemons it's time to suspend.
Ian Rogers81d425b2012-09-27 16:03:43 -0700677 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700678 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 // This is only run after all non-daemon threads have exited, so the remainder should all be
680 // daemons.
Ian Rogers7e762862012-10-22 15:45:08 -0700681 CHECK(thread->IsDaemon()) << *thread;
Ian Rogers81d425b2012-09-27 16:03:43 -0700682 if (thread != self) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700683 thread->ModifySuspendCount(self, +1, false);
Elliott Hughese52e49b2012-04-02 16:05:44 -0700684 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700685 }
686 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700687 // Give the threads a chance to suspend, complaining if they're slow.
688 bool have_complained = false;
689 for (int i = 0; i < 10; ++i) {
690 usleep(200 * 1000);
691 bool all_suspended = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -0700692 for (const auto& thread : list_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700693 if (thread != self && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700694 if (!have_complained) {
695 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
696 have_complained = true;
697 }
698 all_suspended = false;
699 }
700 }
701 if (all_suspended) {
702 return;
703 }
704 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700705 LOG(ERROR) << "suspend all daemons failed";
706}
707void ThreadList::Register(Thread* self) {
708 DCHECK_EQ(self, Thread::Current());
709
710 if (VLOG_IS_ON(threads)) {
711 std::ostringstream oss;
712 self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump.
713 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss;
714 }
715
716 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
717 // SuspendAll requests.
Ian Rogers81d425b2012-09-27 16:03:43 -0700718 MutexLock mu(self, *Locks::thread_list_lock_);
719 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700720 self->suspend_count_ = suspend_all_count_;
721 self->debug_suspend_count_ = debug_suspend_all_count_;
Ian Rogers01ae5802012-09-28 16:14:01 -0700722 if (self->suspend_count_ > 0) {
723 self->AtomicSetFlag(kSuspendRequest);
724 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700725 CHECK(!Contains(self));
726 list_.push_back(self);
727}
728
729void ThreadList::Unregister(Thread* self) {
730 DCHECK_EQ(self, Thread::Current());
731
732 VLOG(threads) << "ThreadList::Unregister() " << *self;
733
734 // Any time-consuming destruction, plus anything that can call back into managed code or
735 // suspend and so on, must happen at this point, and not in ~Thread.
736 self->Destroy();
737
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700738 uint32_t thin_lock_id = self->thin_lock_thread_id_;
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800739 while (self != nullptr) {
Ian Rogerscfaa4552012-11-26 21:00:08 -0800740 // Remove and delete the Thread* while holding the thread_list_lock_ and
741 // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended.
Ian Rogers0878d652013-04-18 17:38:35 -0700742 // Note: deliberately not using MutexLock that could hold a stale self pointer.
743 Locks::thread_list_lock_->ExclusiveLock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700744 CHECK(Contains(self));
Ian Rogerscfaa4552012-11-26 21:00:08 -0800745 // Note: we don't take the thread_suspend_count_lock_ here as to be suspending a thread other
746 // than yourself you need to hold the thread_list_lock_ (see Thread::ModifySuspendCount).
747 if (!self->IsSuspended()) {
748 list_.remove(self);
749 delete self;
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800750 self = nullptr;
Ian Rogerscfaa4552012-11-26 21:00:08 -0800751 }
Ian Rogers0878d652013-04-18 17:38:35 -0700752 Locks::thread_list_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700753 }
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800754 // Release the thread ID after the thread is finished and deleted to avoid cases where we can
755 // temporarily have multiple threads with the same thread id. When this occurs, it causes
756 // problems in FindThreadByThreadId / SuspendThreadByThreadId.
757 ReleaseThreadId(nullptr, thin_lock_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700758
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700759 // Clear the TLS data, so that the underlying native thread is recognizably detached.
760 // (It may wish to reattach later.)
761 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
762
763 // Signal that a thread just detached.
Ian Rogers81d425b2012-09-27 16:03:43 -0700764 MutexLock mu(NULL, *Locks::thread_list_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700765 thread_exit_cond_.Signal(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700766}
767
768void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700769 for (const auto& thread : list_) {
770 callback(thread, context);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700771 }
772}
773
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800774void ThreadList::VisitRoots(RootCallback* callback, void* arg) const {
Ian Rogers81d425b2012-09-27 16:03:43 -0700775 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700776 for (const auto& thread : list_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800777 thread->VisitRoots(callback, arg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700778 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700779}
780
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800781class VerifyRootWrapperArg {
782 public:
783 VerifyRootWrapperArg(VerifyRootCallback* callback, void* arg) : callback_(callback), arg_(arg) {
784 }
785 VerifyRootCallback* const callback_;
786 void* const arg_;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700787};
788
Mathieu Chartier815873e2014-02-13 18:02:13 -0800789static void VerifyRootWrapperCallback(mirror::Object** root, void* arg, uint32_t /*thread_id*/,
790 RootType /*root_type*/) {
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700791 VerifyRootWrapperArg* wrapperArg = reinterpret_cast<VerifyRootWrapperArg*>(arg);
Mathieu Chartier815873e2014-02-13 18:02:13 -0800792 wrapperArg->callback_(*root, wrapperArg->arg_, 0, NULL);
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700793}
794
Mathieu Chartier83c8ee02014-01-28 14:50:23 -0800795void ThreadList::VerifyRoots(VerifyRootCallback* callback, void* arg) const {
796 VerifyRootWrapperArg wrapper(callback, arg);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700797 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700798 for (const auto& thread : list_) {
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700799 thread->VisitRoots(VerifyRootWrapperCallback, &wrapper);
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700800 }
801}
802
Ian Rogerscfaa4552012-11-26 21:00:08 -0800803uint32_t ThreadList::AllocThreadId(Thread* self) {
804 MutexLock mu(self, allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700805 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
806 if (!allocated_ids_[i]) {
807 allocated_ids_.set(i);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700808 return i + 1; // Zero is reserved to mean "invalid".
Elliott Hughes8daa0922011-09-11 13:46:25 -0700809 }
810 }
811 LOG(FATAL) << "Out of internal thread ids";
812 return 0;
813}
814
Ian Rogerscfaa4552012-11-26 21:00:08 -0800815void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) {
816 MutexLock mu(self, allocated_ids_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700817 --id; // Zero is reserved to mean "invalid".
Elliott Hughes8daa0922011-09-11 13:46:25 -0700818 DCHECK(allocated_ids_[id]) << id;
819 allocated_ids_.reset(id);
820}
821
Elliott Hughes8daa0922011-09-11 13:46:25 -0700822} // namespace art