blob: 58e5b9d5c967b2819377241fb146a2927e68509c [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
Mathieu Chartier6f365cc2014-04-23 12:42:27 -070019#define ATRACE_TAG ATRACE_TAG_DALVIK
20
21#include <cutils/trace.h>
Elliott Hughesabbe07d2012-06-05 17:42:23 -070022#include <dirent.h>
Ian Rogersd9c4fc92013-10-01 19:45:43 -070023#include <ScopedLocalRef.h>
24#include <ScopedUtfChars.h>
Elliott Hughesabbe07d2012-06-05 17:42:23 -070025#include <sys/types.h>
Elliott Hughes038a8062011-09-18 14:12:41 -070026#include <unistd.h>
27
Ian Rogersc7dd2952014-10-21 23:31:19 -070028#include <sstream>
29
Mathieu Chartier70a596d2014-12-17 14:56:47 -080030#include "base/histogram-inl.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080031#include "base/mutex.h"
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -070032#include "base/mutex-inl.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080033#include "base/timing_logger.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070034#include "debugger.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070035#include "jni_internal.h"
36#include "lock_word.h"
37#include "monitor.h"
38#include "scoped_thread_state_change.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "thread.h"
Jeff Haoe094b872014-10-14 13:12:01 -070040#include "trace.h"
Elliott Hughesabbe07d2012-06-05 17:42:23 -070041#include "utils.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070042#include "well_known_classes.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070043
Elliott Hughes8daa0922011-09-11 13:46:25 -070044namespace art {
45
Mathieu Chartier251755c2014-07-15 18:10:25 -070046static constexpr uint64_t kLongThreadSuspendThreshold = MsToNs(5);
47
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080048ThreadList::ThreadList()
Chao-ying Fu9e369312014-05-21 11:20:52 -070049 : suspend_all_count_(0), debug_suspend_all_count_(0),
Mathieu Chartier70a596d2014-12-17 14:56:47 -080050 thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_),
51 suspend_all_historam_("suspend all histogram", 16, 64) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -070052 CHECK(Monitor::IsValidLockWord(LockWord::FromThinLockId(kMaxThreadId, 1)));
Elliott Hughes8daa0922011-09-11 13:46:25 -070053}
54
55ThreadList::~ThreadList() {
Elliott Hughese52e49b2012-04-02 16:05:44 -070056 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
Elliott Hughes6a144332012-04-03 13:07:11 -070057 // We need to detach the current thread here in case there's another thread waiting to join with
58 // us.
Mathieu Chartierfec72f42014-10-09 12:57:58 -070059 bool contains = false;
60 {
61 Thread* self = Thread::Current();
62 MutexLock mu(self, *Locks::thread_list_lock_);
63 contains = Contains(self);
64 }
65 if (contains) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070066 Runtime::Current()->DetachCurrentThread();
67 }
Elliott Hughes6a144332012-04-03 13:07:11 -070068
69 WaitForOtherNonDaemonThreadsToExit();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070 // TODO: there's an unaddressed race here where a thread may attach during shutdown, see
71 // Thread::Init.
Elliott Hughes6a144332012-04-03 13:07:11 -070072 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070073}
74
75bool ThreadList::Contains(Thread* thread) {
76 return find(list_.begin(), list_.end(), thread) != list_.end();
77}
78
Elliott Hughesabbe07d2012-06-05 17:42:23 -070079bool ThreadList::Contains(pid_t tid) {
Mathieu Chartier02e25112013-08-14 16:14:24 -070080 for (const auto& thread : list_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -070081 if (thread->GetTid() == tid) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070082 return true;
83 }
84 }
85 return false;
86}
87
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070088pid_t ThreadList::GetLockOwner() {
Ian Rogersb726dcb2012-09-05 08:57:23 -070089 return Locks::thread_list_lock_->GetExclusiveOwnerTid();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070090}
91
Mathieu Chartier590fee92013-09-13 13:46:47 -070092void ThreadList::DumpNativeStacks(std::ostream& os) {
93 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
94 for (const auto& thread : list_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -070095 os << "DUMPING THREAD " << thread->GetTid() << "\n";
Christopher Ferrisa2cee182014-04-16 19:13:59 -070096 DumpNativeStack(os, thread->GetTid(), "\t");
Mathieu Chartier590fee92013-09-13 13:46:47 -070097 os << "\n";
98 }
99}
100
Elliott Hughesc967f782012-04-16 10:23:15 -0700101void ThreadList::DumpForSigQuit(std::ostream& os) {
Mathieu Chartier70a596d2014-12-17 14:56:47 -0800102 {
103 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier23f6e692014-12-18 18:24:39 -0800104 // Only print if we have samples.
105 if (suspend_all_historam_.SampleSize() > 0) {
106 Histogram<uint64_t>::CumulativeData data;
107 suspend_all_historam_.CreateHistogram(&data);
108 suspend_all_historam_.PrintConfidenceIntervals(os, 0.99, data); // Dump time to suspend.
109 }
Mathieu Chartier70a596d2014-12-17 14:56:47 -0800110 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700111 Dump(os);
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700112 DumpUnattachedThreads(os);
113}
114
Ian Rogerscfaa4552012-11-26 21:00:08 -0800115static void DumpUnattachedThread(std::ostream& os, pid_t tid) NO_THREAD_SAFETY_ANALYSIS {
116 // TODO: No thread safety analysis as DumpState with a NULL thread won't access fields, should
117 // refactor DumpState to avoid skipping analysis.
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700118 Thread::DumpState(os, NULL, tid);
119 DumpKernelStack(os, tid, " kernel: ", false);
Brian Carlstromed8b7232012-06-27 17:54:47 -0700120 // TODO: Reenable this when the native code in system_server can handle it.
121 // Currently "adb shell kill -3 `pid system_server`" will cause it to exit.
122 if (false) {
Christopher Ferrisa2cee182014-04-16 19:13:59 -0700123 DumpNativeStack(os, tid, " native: ");
Brian Carlstromed8b7232012-06-27 17:54:47 -0700124 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700125 os << "\n";
126}
127
128void ThreadList::DumpUnattachedThreads(std::ostream& os) {
129 DIR* d = opendir("/proc/self/task");
130 if (!d) {
131 return;
132 }
133
Ian Rogers50b35e22012-10-04 10:09:15 -0700134 Thread* self = Thread::Current();
Elliott Hughes4696b5b2012-10-30 10:35:10 -0700135 dirent* e;
136 while ((e = readdir(d)) != NULL) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700137 char* end;
Elliott Hughes4696b5b2012-10-30 10:35:10 -0700138 pid_t tid = strtol(e->d_name, &end, 10);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 if (!*end) {
140 bool contains;
141 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700142 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 contains = Contains(tid);
144 }
145 if (!contains) {
146 DumpUnattachedThread(os, tid);
147 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700148 }
149 }
150 closedir(d);
Elliott Hughesff738062012-02-03 15:00:42 -0800151}
152
Andreas Gampe4a3d19b2015-01-09 17:54:51 -0800153// Dump checkpoint timeout in milliseconds. Larger amount on the host, as dumping will invoke
154// addr2line when available.
Andreas Gampe1e4b0ca2015-01-14 09:06:32 -0800155static constexpr uint32_t kDumpWaitTimeout = kIsTargetBuild ? 10000 : 20000;
Andreas Gampe4a3d19b2015-01-09 17:54:51 -0800156
Ian Rogers7b078e82014-09-10 14:44:24 -0700157// A closure used by Thread::Dump.
158class DumpCheckpoint FINAL : public Closure {
159 public:
160 explicit DumpCheckpoint(std::ostream* os) : os_(os), barrier_(0) {}
161
162 void Run(Thread* thread) OVERRIDE {
163 // Note thread and self may not be equal if thread was already suspended at the point of the
164 // request.
165 Thread* self = Thread::Current();
166 std::ostringstream local_os;
167 {
168 ScopedObjectAccess soa(self);
169 thread->Dump(local_os);
170 }
171 local_os << "\n";
172 {
173 // Use the logging lock to ensure serialization when writing to the common ostream.
174 MutexLock mu(self, *Locks::logging_lock_);
175 *os_ << local_os.str();
176 }
177 barrier_.Pass(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700178 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700179
180 void WaitForThreadsToRunThroughCheckpoint(size_t threads_running_checkpoint) {
181 Thread* self = Thread::Current();
182 ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun);
Andreas Gampe1e4b0ca2015-01-14 09:06:32 -0800183 bool timed_out = barrier_.Increment(self, threads_running_checkpoint, kDumpWaitTimeout);
Ian Rogers2156ff12014-09-13 19:20:54 -0700184 if (timed_out) {
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000185 // Avoid a recursive abort.
186 LOG((kIsDebugBuild && (gAborting == 0)) ? FATAL : ERROR)
187 << "Unexpected time out during dump checkpoint.";
Ian Rogers2156ff12014-09-13 19:20:54 -0700188 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700189 }
190
191 private:
192 // The common stream that will accumulate all the dumps.
193 std::ostream* const os_;
194 // The barrier to be passed through and for the requestor to wait upon.
195 Barrier barrier_;
196};
197
198void ThreadList::Dump(std::ostream& os) {
199 {
200 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
201 os << "DALVIK THREADS (" << list_.size() << "):\n";
202 }
203 DumpCheckpoint checkpoint(&os);
204 size_t threads_running_checkpoint = RunCheckpoint(&checkpoint);
205 checkpoint.WaitForThreadsToRunThroughCheckpoint(threads_running_checkpoint);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700206}
207
Ian Rogers50b35e22012-10-04 10:09:15 -0700208void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
209 MutexLock mu(self, *Locks::thread_list_lock_);
210 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700211 for (const auto& thread : list_) {
jeffhao725a9572012-11-13 18:20:12 -0800212 if (thread != ignore1 && thread != ignore2) {
Ian Rogers01ae5802012-09-28 16:14:01 -0700213 CHECK(thread->IsSuspended())
214 << "\nUnsuspended thread: <<" << *thread << "\n"
215 << "self: <<" << *Thread::Current();
216 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700217 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218}
219
Ian Rogers66aee5c2012-08-15 17:17:47 -0700220#if HAVE_TIMED_RWLOCK
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221// Attempt to rectify locks so that we dump thread list with required locks before exiting.
Ian Rogers7b078e82014-09-10 14:44:24 -0700222static void UnsafeLogFatalForThreadSuspendAllTimeout() __attribute__((noreturn));
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100223static void UnsafeLogFatalForThreadSuspendAllTimeout() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700224 Runtime* runtime = Runtime::Current();
225 std::ostringstream ss;
226 ss << "Thread suspend timeout\n";
Mathieu Chartier5869a2c2014-10-08 14:26:23 -0700227 Locks::mutator_lock_->Dump(ss);
228 ss << "\n";
Ian Rogers7b078e82014-09-10 14:44:24 -0700229 runtime->GetThreadList()->Dump(ss);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 LOG(FATAL) << ss.str();
Ian Rogers719d1a32014-03-06 12:13:39 -0800231 exit(0);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232}
Ian Rogers66aee5c2012-08-15 17:17:47 -0700233#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700234
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800235// Unlike suspending all threads where we can wait to acquire the mutator_lock_, suspending an
236// individual thread requires polling. delay_us is the requested sleep and total_delay_us
237// accumulates the total time spent sleeping for timeouts. The first sleep is just a yield,
238// subsequently sleeps increase delay_us from 1ms to 500ms by doubling.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700239static void ThreadSuspendSleep(useconds_t* delay_us, useconds_t* total_delay_us) {
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800240 useconds_t new_delay_us = (*delay_us) * 2;
241 CHECK_GE(new_delay_us, *delay_us);
242 if (new_delay_us < 500000) { // Don't allow sleeping to be more than 0.5s.
243 *delay_us = new_delay_us;
244 }
245 if (*delay_us == 0) {
246 sched_yield();
247 // Default to 1 milliseconds (note that this gets multiplied by 2 before the first sleep).
248 *delay_us = 500;
249 } else {
250 usleep(*delay_us);
251 *total_delay_us += *delay_us;
252 }
253}
254
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700255size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700256 Thread* self = Thread::Current();
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800257 Locks::mutator_lock_->AssertNotExclusiveHeld(self);
258 Locks::thread_list_lock_->AssertNotHeld(self);
259 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
Nicolas Geoffraydb978712014-12-09 13:33:38 +0000260 if (kDebugLocking && gAborting == 0) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700261 CHECK_NE(self->GetState(), kRunnable);
262 }
263
264 std::vector<Thread*> suspended_count_modified_threads;
265 size_t count = 0;
266 {
267 // Call a checkpoint function for each thread, threads which are suspend get their checkpoint
268 // manually called.
269 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700270 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700271 for (const auto& thread : list_) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700272 if (thread != self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700273 while (true) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700274 if (thread->RequestCheckpoint(checkpoint_function)) {
Dave Allison0aded082013-11-07 13:15:11 -0800275 // This thread will run its checkpoint some time in the near future.
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700276 count++;
277 break;
278 } else {
279 // We are probably suspended, try to make sure that we stay suspended.
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700280 // The thread switched back to runnable.
281 if (thread->GetState() == kRunnable) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700282 // Spurious fail, try again.
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700283 continue;
284 }
285 thread->ModifySuspendCount(self, +1, false);
286 suspended_count_modified_threads.push_back(thread);
287 break;
288 }
289 }
290 }
291 }
292 }
293
294 // Run the checkpoint on ourself while we wait for threads to suspend.
295 checkpoint_function->Run(self);
296
297 // Run the checkpoint on the suspended threads.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700298 for (const auto& thread : suspended_count_modified_threads) {
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700299 if (!thread->IsSuspended()) {
300 // Wait until the thread is suspended.
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800301 useconds_t total_delay_us = 0;
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700302 do {
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800303 useconds_t delay_us = 100;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700304 ThreadSuspendSleep(&delay_us, &total_delay_us);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700305 } while (!thread->IsSuspended());
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800306 // Shouldn't need to wait for longer than 1000 microseconds.
307 constexpr useconds_t kLongWaitThresholdUS = 1000;
308 if (UNLIKELY(total_delay_us > kLongWaitThresholdUS)) {
309 LOG(WARNING) << "Waited " << total_delay_us << " us for thread suspend!";
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700310 }
311 }
312 // We know for sure that the thread is suspended at this point.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700313 checkpoint_function->Run(thread);
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700314 {
315 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
316 thread->ModifySuspendCount(self, -1, false);
317 }
318 }
319
Mathieu Chartier664bebf2012-11-12 16:54:11 -0800320 {
321 // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their
322 // suspend count. Now the suspend_count_ is lowered so we must do the broadcast.
323 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
324 Thread::resume_cond_->Broadcast(self);
325 }
326
Mathieu Chartier858f1c52012-10-17 17:45:55 -0700327 // Add one for self.
328 return count + suspended_count_modified_threads.size() + 1;
329}
330
Dave Allison39c3bfb2014-01-28 18:33:52 -0800331// Request that a checkpoint function be run on all active (non-suspended)
332// threads. Returns the number of successful requests.
333size_t ThreadList::RunCheckpointOnRunnableThreads(Closure* checkpoint_function) {
334 Thread* self = Thread::Current();
Ian Rogers7b078e82014-09-10 14:44:24 -0700335 Locks::mutator_lock_->AssertNotExclusiveHeld(self);
336 Locks::thread_list_lock_->AssertNotHeld(self);
337 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
338 CHECK_NE(self->GetState(), kRunnable);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800339
340 size_t count = 0;
341 {
342 // Call a checkpoint function for each non-suspended thread.
343 MutexLock mu(self, *Locks::thread_list_lock_);
344 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
345 for (const auto& thread : list_) {
346 if (thread != self) {
347 if (thread->RequestCheckpoint(checkpoint_function)) {
348 // This thread will run its checkpoint some time in the near future.
349 count++;
350 }
351 }
352 }
353 }
354
355 // Return the number of threads that will run the checkpoint function.
356 return count;
357}
358
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800359// A checkpoint/suspend-all hybrid to switch thread roots from
360// from-space to to-space refs. Used to synchronize threads at a point
361// to mark the initiation of marking while maintaining the to-space
362// invariant.
363size_t ThreadList::FlipThreadRoots(Closure* thread_flip_visitor, Closure* flip_callback,
364 gc::collector::GarbageCollector* collector) {
365 TimingLogger::ScopedTiming split("ThreadListFlip", collector->GetTimings());
366 const uint64_t start_time = NanoTime();
367 Thread* self = Thread::Current();
368 Locks::mutator_lock_->AssertNotHeld(self);
369 Locks::thread_list_lock_->AssertNotHeld(self);
370 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
371 CHECK_NE(self->GetState(), kRunnable);
372
373 std::vector<Thread*> runnable_threads;
374 std::vector<Thread*> other_threads;
375
376 // Suspend all threads once.
377 {
378 MutexLock mu(self, *Locks::thread_list_lock_);
379 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
380 // Update global suspend all state for attaching threads.
381 ++suspend_all_count_;
382 // Increment everybody's suspend count (except our own).
383 for (const auto& thread : list_) {
384 if (thread == self) {
385 continue;
386 }
387 thread->ModifySuspendCount(self, +1, false);
388 }
389 }
390
391 // Run the flip callback for the collector.
392 Locks::mutator_lock_->ExclusiveLock(self);
393 flip_callback->Run(self);
394 Locks::mutator_lock_->ExclusiveUnlock(self);
395 collector->RegisterPause(NanoTime() - start_time);
396
397 // Resume runnable threads.
398 {
399 MutexLock mu(self, *Locks::thread_list_lock_);
400 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
401 --suspend_all_count_;
402 for (const auto& thread : list_) {
403 if (thread == self) {
404 continue;
405 }
406 // Set the flip function for both runnable and suspended threads
407 // because Thread::DumpState/DumpJavaStack() (invoked by a
408 // checkpoint) may cause the flip function to be run for a
409 // runnable/suspended thread before a runnable threads runs it
410 // for itself or we run it for a suspended thread below.
411 thread->SetFlipFunction(thread_flip_visitor);
412 if (thread->IsSuspendedAtSuspendCheck()) {
413 // The thread will resume right after the broadcast.
414 thread->ModifySuspendCount(self, -1, false);
415 runnable_threads.push_back(thread);
416 } else {
417 other_threads.push_back(thread);
418 }
419 }
420 Thread::resume_cond_->Broadcast(self);
421 }
422
423 // Run the closure on the other threads and let them resume.
424 {
425 ReaderMutexLock mu(self, *Locks::mutator_lock_);
426 for (const auto& thread : other_threads) {
427 Closure* flip_func = thread->GetFlipFunction();
428 if (flip_func != nullptr) {
429 flip_func->Run(thread);
430 }
431 }
432 // Run it for self.
433 thread_flip_visitor->Run(self);
434 }
435
436 // Resume other threads.
437 {
438 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
439 for (const auto& thread : other_threads) {
440 thread->ModifySuspendCount(self, -1, false);
441 }
442 Thread::resume_cond_->Broadcast(self);
443 }
444
445 return runnable_threads.size() + other_threads.size() + 1; // +1 for self.
446}
447
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448void ThreadList::SuspendAll() {
449 Thread* self = Thread::Current();
450
Jeff Haoc5d824a2014-07-28 18:35:38 -0700451 if (self != nullptr) {
452 VLOG(threads) << *self << " SuspendAll starting...";
453 } else {
454 VLOG(threads) << "Thread[null] SuspendAll starting...";
455 }
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700456 ATRACE_BEGIN("Suspending mutator threads");
Mathieu Chartier70a596d2014-12-17 14:56:47 -0800457 const uint64_t start_time = NanoTime();
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700458
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800459 Locks::mutator_lock_->AssertNotHeld(self);
460 Locks::thread_list_lock_->AssertNotHeld(self);
461 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
Jeff Haoc5d824a2014-07-28 18:35:38 -0700462 if (kDebugLocking && self != nullptr) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700463 CHECK_NE(self->GetState(), kRunnable);
464 }
465 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700466 MutexLock mu(self, *Locks::thread_list_lock_);
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800467 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
468 // Update global suspend all state for attaching threads.
469 ++suspend_all_count_;
470 // Increment everybody's suspend count (except our own).
471 for (const auto& thread : list_) {
472 if (thread == self) {
473 continue;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 }
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800475 VLOG(threads) << "requesting thread suspend: " << *thread;
476 thread->ModifySuspendCount(self, +1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700477 }
478 }
479
Ian Rogers66aee5c2012-08-15 17:17:47 -0700480 // Block on the mutator lock until all Runnable threads release their share of access.
481#if HAVE_TIMED_RWLOCK
482 // Timeout if we wait more than 30 seconds.
Ian Rogers719d1a32014-03-06 12:13:39 -0800483 if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100484 UnsafeLogFatalForThreadSuspendAllTimeout();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700485 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700486#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700487 Locks::mutator_lock_->ExclusiveLock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700488#endif
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700489
Mathieu Chartier70a596d2014-12-17 14:56:47 -0800490 const uint64_t end_time = NanoTime();
491 const uint64_t suspend_time = end_time - start_time;
492 suspend_all_historam_.AdjustAndAddValue(suspend_time);
493 if (suspend_time > kLongThreadSuspendThreshold) {
494 LOG(WARNING) << "Suspending all threads took: " << PrettyDuration(suspend_time);
Mathieu Chartier251755c2014-07-15 18:10:25 -0700495 }
496
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800497 if (kDebugLocking) {
498 // Debug check that all threads are suspended.
499 AssertThreadsAreSuspended(self, self);
500 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700501
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700502 ATRACE_END();
503 ATRACE_BEGIN("Mutator threads suspended");
504
Jeff Haoc5d824a2014-07-28 18:35:38 -0700505 if (self != nullptr) {
506 VLOG(threads) << *self << " SuspendAll complete";
507 } else {
508 VLOG(threads) << "Thread[null] SuspendAll complete";
509 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700510}
511
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700512void ThreadList::ResumeAll() {
513 Thread* self = Thread::Current();
514
Jeff Haoc5d824a2014-07-28 18:35:38 -0700515 if (self != nullptr) {
516 VLOG(threads) << *self << " ResumeAll starting";
517 } else {
518 VLOG(threads) << "Thread[null] ResumeAll starting";
519 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700520
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700521 ATRACE_END();
522 ATRACE_BEGIN("Resuming mutator threads");
523
Mathieu Chartier6dda8982014-03-06 11:11:48 -0800524 if (kDebugLocking) {
525 // Debug check that all threads are suspended.
526 AssertThreadsAreSuspended(self, self);
527 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700528
Ian Rogers81d425b2012-09-27 16:03:43 -0700529 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700530 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700531 MutexLock mu(self, *Locks::thread_list_lock_);
532 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700533 // Update global suspend all state for attaching threads.
534 --suspend_all_count_;
535 // Decrement the suspend counts for all threads.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700536 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700537 if (thread == self) {
538 continue;
539 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700540 thread->ModifySuspendCount(self, -1, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700541 }
542
543 // Broadcast a notification to all suspended threads, some or all of
544 // which may choose to wake up. No need to wait for them.
Jeff Haoc5d824a2014-07-28 18:35:38 -0700545 if (self != nullptr) {
546 VLOG(threads) << *self << " ResumeAll waking others";
547 } else {
548 VLOG(threads) << "Thread[null] ResumeAll waking others";
549 }
Ian Rogersc604d732012-10-14 16:09:54 -0700550 Thread::resume_cond_->Broadcast(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700551 }
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700552 ATRACE_END();
Jeff Haoc5d824a2014-07-28 18:35:38 -0700553
554 if (self != nullptr) {
555 VLOG(threads) << *self << " ResumeAll complete";
556 } else {
557 VLOG(threads) << "Thread[null] ResumeAll complete";
558 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700559}
560
561void ThreadList::Resume(Thread* thread, bool for_debugger) {
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800562 // This assumes there was an ATRACE_BEGIN when we suspended the thread.
563 ATRACE_END();
564
Ian Rogers81d425b2012-09-27 16:03:43 -0700565 Thread* self = Thread::Current();
566 DCHECK_NE(thread, self);
Brian Carlstromba32de42014-08-27 23:43:46 -0700567 VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") starting..."
568 << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700569
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700570 {
571 // To check Contains.
Ian Rogers81d425b2012-09-27 16:03:43 -0700572 MutexLock mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700573 // To check IsSuspended.
Ian Rogers81d425b2012-09-27 16:03:43 -0700574 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
575 DCHECK(thread->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700576 if (!Contains(thread)) {
Brian Carlstromba32de42014-08-27 23:43:46 -0700577 // We only expect threads within the thread-list to have been suspended otherwise we can't
578 // stop such threads from delete-ing themselves.
579 LOG(ERROR) << "Resume(" << reinterpret_cast<void*>(thread)
580 << ") thread not within thread list";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700581 return;
582 }
Ian Rogers01ae5802012-09-28 16:14:01 -0700583 thread->ModifySuspendCount(self, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700584 }
585
586 {
Brian Carlstromba32de42014-08-27 23:43:46 -0700587 VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") waking others";
Ian Rogers81d425b2012-09-27 16:03:43 -0700588 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700589 Thread::resume_cond_->Broadcast(self);
Elliott Hughes01158d72011-09-19 19:47:10 -0700590 }
591
Brian Carlstromba32de42014-08-27 23:43:46 -0700592 VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") complete";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700593}
Elliott Hughes01158d72011-09-19 19:47:10 -0700594
Ian Rogersc7dd2952014-10-21 23:31:19 -0700595static void ThreadSuspendByPeerWarning(Thread* self, LogSeverity severity, const char* message,
596 jobject peer) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700597 JNIEnvExt* env = self->GetJniEnv();
598 ScopedLocalRef<jstring>
599 scoped_name_string(env, (jstring)env->GetObjectField(peer,
600 WellKnownClasses::java_lang_Thread_name));
601 ScopedUtfChars scoped_name_chars(env, scoped_name_string.get());
602 if (scoped_name_chars.c_str() == NULL) {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700603 LOG(severity) << message << ": " << peer;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700604 env->ExceptionClear();
605 } else {
Ian Rogersc7dd2952014-10-21 23:31:19 -0700606 LOG(severity) << message << ": " << peer << ":" << scoped_name_chars.c_str();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700607 }
608}
609
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700610Thread* ThreadList::SuspendThreadByPeer(jobject peer, bool request_suspension,
611 bool debug_suspension, bool* timed_out) {
612 static const useconds_t kTimeoutUs = 30 * 1000000; // 30s.
613 useconds_t total_delay_us = 0;
614 useconds_t delay_us = 0;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700615 *timed_out = false;
616 Thread* self = Thread::Current();
Mathieu Chartier82a800d2014-12-15 15:59:49 -0800617 Thread* suspended_thread = nullptr;
Brian Carlstromba32de42014-08-27 23:43:46 -0700618 VLOG(threads) << "SuspendThreadByPeer starting";
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700619 while (true) {
620 Thread* thread;
621 {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700622 // Note: this will transition to runnable and potentially suspend. We ensure only one thread
623 // is requesting another suspend, to avoid deadlock, by requiring this function be called
624 // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
625 // than request thread suspension, to avoid potential cycles in threads requesting each other
626 // suspend.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700627 ScopedObjectAccess soa(self);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800628 MutexLock thread_list_mu(self, *Locks::thread_list_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700629 thread = Thread::FromManagedThread(soa, peer);
Brian Carlstromba32de42014-08-27 23:43:46 -0700630 if (thread == nullptr) {
Mathieu Chartier82a800d2014-12-15 15:59:49 -0800631 if (suspended_thread != nullptr) {
632 MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_);
633 // If we incremented the suspend count but the thread reset its peer, we need to
634 // re-decrement it since it is shutting down and may deadlock the runtime in
635 // ThreadList::WaitForOtherNonDaemonThreadsToExit.
636 suspended_thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
637 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700638 ThreadSuspendByPeerWarning(self, WARNING, "No such thread for suspend", peer);
Brian Carlstromba32de42014-08-27 23:43:46 -0700639 return nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700640 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700641 if (!Contains(thread)) {
Mathieu Chartier82a800d2014-12-15 15:59:49 -0800642 CHECK(suspended_thread == nullptr);
Brian Carlstromba32de42014-08-27 23:43:46 -0700643 VLOG(threads) << "SuspendThreadByPeer failed for unattached thread: "
644 << reinterpret_cast<void*>(thread);
645 return nullptr;
646 }
647 VLOG(threads) << "SuspendThreadByPeer found thread: " << *thread;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700648 {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800649 MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700650 if (request_suspension) {
Ian Rogers4ad5cd32014-11-11 23:08:07 -0800651 if (self->GetSuspendCount() > 0) {
652 // We hold the suspend count lock but another thread is trying to suspend us. Its not
653 // safe to try to suspend another thread in case we get a cycle. Start the loop again
654 // which will allow this thread to be suspended.
655 continue;
656 }
Mathieu Chartier82a800d2014-12-15 15:59:49 -0800657 CHECK(suspended_thread == nullptr);
658 suspended_thread = thread;
659 suspended_thread->ModifySuspendCount(self, +1, debug_suspension);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700660 request_suspension = false;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700661 } else {
662 // If the caller isn't requesting suspension, a suspension should have already occurred.
663 CHECK_GT(thread->GetSuspendCount(), 0);
664 }
665 // IsSuspended on the current thread will fail as the current thread is changed into
666 // Runnable above. As the suspend count is now raised if this is the current thread
667 // it will self suspend on transition to Runnable, making it hard to work with. It's simpler
668 // to just explicitly handle the current thread in the callers to this code.
669 CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
670 // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
671 // count, or else we've waited and it has self suspended) or is the current thread, we're
672 // done.
673 if (thread->IsSuspended()) {
Brian Carlstromba32de42014-08-27 23:43:46 -0700674 VLOG(threads) << "SuspendThreadByPeer thread suspended: " << *thread;
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800675 if (ATRACE_ENABLED()) {
676 std::string name;
677 thread->GetThreadName(name);
678 ATRACE_BEGIN(StringPrintf("SuspendThreadByPeer suspended %s for peer=%p", name.c_str(),
679 peer).c_str());
680 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700681 return thread;
682 }
683 if (total_delay_us >= kTimeoutUs) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700684 ThreadSuspendByPeerWarning(self, FATAL, "Thread suspension timed out", peer);
Mathieu Chartier82a800d2014-12-15 15:59:49 -0800685 if (suspended_thread != nullptr) {
686 CHECK_EQ(suspended_thread, thread);
687 suspended_thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700688 }
689 *timed_out = true;
Brian Carlstromba32de42014-08-27 23:43:46 -0700690 return nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700691 }
692 }
693 // Release locks and come out of runnable state.
694 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700695 VLOG(threads) << "SuspendThreadByPeer sleeping to allow thread chance to suspend";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700696 ThreadSuspendSleep(&delay_us, &total_delay_us);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700697 }
698}
699
Ian Rogersc7dd2952014-10-21 23:31:19 -0700700static void ThreadSuspendByThreadIdWarning(LogSeverity severity, const char* message,
701 uint32_t thread_id) {
702 LOG(severity) << StringPrintf("%s: %d", message, thread_id);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700703}
704
705Thread* ThreadList::SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension,
706 bool* timed_out) {
707 static const useconds_t kTimeoutUs = 30 * 1000000; // 30s.
708 useconds_t total_delay_us = 0;
709 useconds_t delay_us = 0;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700710 *timed_out = false;
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800711 Thread* suspended_thread = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700712 Thread* self = Thread::Current();
713 CHECK_NE(thread_id, kInvalidThreadId);
Brian Carlstromba32de42014-08-27 23:43:46 -0700714 VLOG(threads) << "SuspendThreadByThreadId starting";
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700715 while (true) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700716 {
Ian Rogersf3d874c2014-07-17 18:52:42 -0700717 // Note: this will transition to runnable and potentially suspend. We ensure only one thread
718 // is requesting another suspend, to avoid deadlock, by requiring this function be called
719 // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
720 // than request thread suspension, to avoid potential cycles in threads requesting each other
721 // suspend.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700722 ScopedObjectAccess soa(self);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800723 MutexLock thread_list_mu(self, *Locks::thread_list_lock_);
Ian Rogersf3d874c2014-07-17 18:52:42 -0700724 Thread* thread = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700725 for (const auto& it : list_) {
726 if (it->GetThreadId() == thread_id) {
727 thread = it;
728 break;
729 }
730 }
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800731 if (thread == nullptr) {
732 CHECK(suspended_thread == nullptr) << "Suspended thread " << suspended_thread
733 << " no longer in thread list";
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700734 // There's a race in inflating a lock and the owner giving up ownership and then dying.
735 ThreadSuspendByThreadIdWarning(WARNING, "No such thread id for suspend", thread_id);
Brian Carlstromba32de42014-08-27 23:43:46 -0700736 return nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700737 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700738 VLOG(threads) << "SuspendThreadByThreadId found thread: " << *thread;
739 DCHECK(Contains(thread));
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700740 {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800741 MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800742 if (suspended_thread == nullptr) {
Ian Rogers4ad5cd32014-11-11 23:08:07 -0800743 if (self->GetSuspendCount() > 0) {
744 // We hold the suspend count lock but another thread is trying to suspend us. Its not
745 // safe to try to suspend another thread in case we get a cycle. Start the loop again
746 // which will allow this thread to be suspended.
747 continue;
748 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700749 thread->ModifySuspendCount(self, +1, debug_suspension);
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800750 suspended_thread = thread;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700751 } else {
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800752 CHECK_EQ(suspended_thread, thread);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700753 // If the caller isn't requesting suspension, a suspension should have already occurred.
754 CHECK_GT(thread->GetSuspendCount(), 0);
755 }
756 // IsSuspended on the current thread will fail as the current thread is changed into
757 // Runnable above. As the suspend count is now raised if this is the current thread
758 // it will self suspend on transition to Runnable, making it hard to work with. It's simpler
759 // to just explicitly handle the current thread in the callers to this code.
760 CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
761 // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
762 // count, or else we've waited and it has self suspended) or is the current thread, we're
763 // done.
764 if (thread->IsSuspended()) {
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800765 if (ATRACE_ENABLED()) {
766 std::string name;
767 thread->GetThreadName(name);
768 ATRACE_BEGIN(StringPrintf("SuspendThreadByThreadId suspended %s id=%d",
769 name.c_str(), thread_id).c_str());
770 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700771 VLOG(threads) << "SuspendThreadByThreadId thread suspended: " << *thread;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700772 return thread;
773 }
774 if (total_delay_us >= kTimeoutUs) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700775 ThreadSuspendByThreadIdWarning(WARNING, "Thread suspension timed out", thread_id);
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800776 if (suspended_thread != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700777 thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
778 }
779 *timed_out = true;
Brian Carlstromba32de42014-08-27 23:43:46 -0700780 return nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700781 }
782 }
783 // Release locks and come out of runnable state.
784 }
Brian Carlstromba32de42014-08-27 23:43:46 -0700785 VLOG(threads) << "SuspendThreadByThreadId sleeping to allow thread chance to suspend";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700786 ThreadSuspendSleep(&delay_us, &total_delay_us);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700787 }
788}
789
790Thread* ThreadList::FindThreadByThreadId(uint32_t thin_lock_id) {
791 Thread* self = Thread::Current();
792 MutexLock mu(self, *Locks::thread_list_lock_);
793 for (const auto& thread : list_) {
794 if (thread->GetThreadId() == thin_lock_id) {
795 CHECK(thread == self || thread->IsSuspended());
796 return thread;
797 }
798 }
799 return NULL;
800}
801
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700802void ThreadList::SuspendAllForDebugger() {
803 Thread* self = Thread::Current();
804 Thread* debug_thread = Dbg::GetDebugThread();
805
806 VLOG(threads) << *self << " SuspendAllForDebugger starting...";
807
808 {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800809 MutexLock thread_list_mu(self, *Locks::thread_list_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700810 {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800811 MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700812 // Update global suspend all state for attaching threads.
Sebastien Hertz253fa552014-10-14 17:27:15 +0200813 DCHECK_GE(suspend_all_count_, debug_suspend_all_count_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700814 ++suspend_all_count_;
815 ++debug_suspend_all_count_;
816 // Increment everybody's suspend count (except our own).
Mathieu Chartier02e25112013-08-14 16:14:24 -0700817 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 if (thread == self || thread == debug_thread) {
819 continue;
820 }
821 VLOG(threads) << "requesting thread suspend: " << *thread;
Ian Rogers01ae5802012-09-28 16:14:01 -0700822 thread->ModifySuspendCount(self, +1, true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823 }
824 }
825 }
826
Ian Rogers66aee5c2012-08-15 17:17:47 -0700827 // Block on the mutator lock until all Runnable threads release their share of access then
828 // immediately unlock again.
829#if HAVE_TIMED_RWLOCK
830 // Timeout if we wait more than 30 seconds.
Ian Rogersc604d732012-10-14 16:09:54 -0700831 if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
Sebastien Hertzbae182c2013-12-17 10:42:03 +0100832 UnsafeLogFatalForThreadSuspendAllTimeout();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700833 } else {
Ian Rogers81d425b2012-09-27 16:03:43 -0700834 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700835 }
Ian Rogers66aee5c2012-08-15 17:17:47 -0700836#else
Ian Rogers81d425b2012-09-27 16:03:43 -0700837 Locks::mutator_lock_->ExclusiveLock(self);
838 Locks::mutator_lock_->ExclusiveUnlock(self);
Ian Rogers66aee5c2012-08-15 17:17:47 -0700839#endif
Ian Rogers50b35e22012-10-04 10:09:15 -0700840 AssertThreadsAreSuspended(self, self, debug_thread);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700841
Sebastien Hertzed2be172014-08-19 15:33:43 +0200842 VLOG(threads) << *self << " SuspendAllForDebugger complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700843}
844
Elliott Hughes475fc232011-10-25 15:00:35 -0700845void ThreadList::SuspendSelfForDebugger() {
846 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700847
Elliott Hughes475fc232011-10-25 15:00:35 -0700848 // The debugger thread must not suspend itself due to debugger activity!
849 Thread* debug_thread = Dbg::GetDebugThread();
850 CHECK(debug_thread != NULL);
851 CHECK(self != debug_thread);
jeffhaoa77f0f62012-12-05 17:19:31 -0800852 CHECK_NE(self->GetState(), kRunnable);
853 Locks::mutator_lock_->AssertNotHeld(self);
Elliott Hughes475fc232011-10-25 15:00:35 -0700854
jeffhaoa77f0f62012-12-05 17:19:31 -0800855 {
856 // Collisions with other suspends aren't really interesting. We want
857 // to ensure that we're the only one fiddling with the suspend count
858 // though.
859 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
860 self->ModifySuspendCount(self, +1, true);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700861 CHECK_GT(self->GetSuspendCount(), 0);
jeffhaoa77f0f62012-12-05 17:19:31 -0800862 }
Elliott Hughes475fc232011-10-25 15:00:35 -0700863
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800864 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700865
Sebastien Hertz21e729c2014-02-18 14:16:00 +0100866 // Tell JDWP we've completed invocation and are ready to suspend.
867 DebugInvokeReq* pReq = self->GetInvokeReq();
868 DCHECK(pReq != NULL);
869 if (pReq->invoke_needed) {
870 // Clear this before signaling.
Sebastien Hertzbb43b432014-04-14 11:59:08 +0200871 pReq->Clear();
Sebastien Hertz21e729c2014-02-18 14:16:00 +0100872
873 VLOG(jdwp) << "invoke complete, signaling";
874 MutexLock mu(self, pReq->lock);
875 pReq->cond.Signal(self);
876 }
877
Elliott Hughes475fc232011-10-25 15:00:35 -0700878 // Tell JDWP that we've completed suspension. The JDWP thread can't
879 // tell us to resume before we're fully asleep because we hold the
880 // suspend count lock.
881 Dbg::ClearWaitForEventThread();
882
jeffhaoa77f0f62012-12-05 17:19:31 -0800883 {
884 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700885 while (self->GetSuspendCount() != 0) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800886 Thread::resume_cond_->Wait(self);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700887 if (self->GetSuspendCount() != 0) {
jeffhaoa77f0f62012-12-05 17:19:31 -0800888 // The condition was signaled but we're still suspended. This
Sebastien Hertzf272af42014-09-18 10:20:42 +0200889 // can happen when we suspend then resume all threads to
890 // update instrumentation or compute monitor info. This can
891 // also happen if the debugger lets go while a SIGQUIT thread
jeffhaoa77f0f62012-12-05 17:19:31 -0800892 // dump event is pending (assuming SignalCatcher was resumed for
893 // just long enough to try to grab the thread-suspend lock).
Sebastien Hertzf272af42014-09-18 10:20:42 +0200894 VLOG(jdwp) << *self << " still suspended after undo "
895 << "(suspend count=" << self->GetSuspendCount() << ", "
896 << "debug suspend count=" << self->GetDebugSuspendCount() << ")";
jeffhaoa77f0f62012-12-05 17:19:31 -0800897 }
Elliott Hughes475fc232011-10-25 15:00:35 -0700898 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700899 CHECK_EQ(self->GetSuspendCount(), 0);
Elliott Hughes475fc232011-10-25 15:00:35 -0700900 }
jeffhaoa77f0f62012-12-05 17:19:31 -0800901
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800902 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700903}
904
Sebastien Hertz253fa552014-10-14 17:27:15 +0200905void ThreadList::ResumeAllForDebugger() {
906 Thread* self = Thread::Current();
907 Thread* debug_thread = Dbg::GetDebugThread();
Sebastien Hertz253fa552014-10-14 17:27:15 +0200908
909 VLOG(threads) << *self << " ResumeAllForDebugger starting...";
910
911 // Threads can't resume if we exclusively hold the mutator lock.
912 Locks::mutator_lock_->AssertNotExclusiveHeld(self);
913
914 {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800915 MutexLock thread_list_mu(self, *Locks::thread_list_lock_);
Sebastien Hertz253fa552014-10-14 17:27:15 +0200916 {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800917 MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_);
Sebastien Hertz253fa552014-10-14 17:27:15 +0200918 // Update global suspend all state for attaching threads.
919 DCHECK_GE(suspend_all_count_, debug_suspend_all_count_);
Sebastien Hertzf9d233d2015-01-09 14:51:41 +0100920 if (debug_suspend_all_count_ > 0) {
Sebastien Hertz253fa552014-10-14 17:27:15 +0200921 --suspend_all_count_;
922 --debug_suspend_all_count_;
Sebastien Hertz253fa552014-10-14 17:27:15 +0200923 } else {
924 // We've been asked to resume all threads without being asked to
Sebastien Hertzf9d233d2015-01-09 14:51:41 +0100925 // suspend them all before. That may happen if a debugger tries
926 // to resume some suspended threads (with suspend count == 1)
927 // at once with a VirtualMachine.Resume command. Let's print a
928 // warning.
Sebastien Hertz253fa552014-10-14 17:27:15 +0200929 LOG(WARNING) << "Debugger attempted to resume all threads without "
930 << "having suspended them all before.";
931 }
Sebastien Hertzf9d233d2015-01-09 14:51:41 +0100932 // Decrement everybody's suspend count (except our own).
933 for (const auto& thread : list_) {
934 if (thread == self || thread == debug_thread) {
935 continue;
936 }
937 if (thread->GetDebugSuspendCount() == 0) {
938 // This thread may have been individually resumed with ThreadReference.Resume.
939 continue;
940 }
941 VLOG(threads) << "requesting thread resume: " << *thread;
942 thread->ModifySuspendCount(self, -1, true);
943 }
Sebastien Hertz253fa552014-10-14 17:27:15 +0200944 }
945 }
946
Sebastien Hertzf9d233d2015-01-09 14:51:41 +0100947 {
Sebastien Hertz253fa552014-10-14 17:27:15 +0200948 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
949 Thread::resume_cond_->Broadcast(self);
950 }
951
952 VLOG(threads) << *self << " ResumeAllForDebugger complete";
953}
954
Elliott Hughes234ab152011-10-26 14:02:26 -0700955void ThreadList::UndoDebuggerSuspensions() {
956 Thread* self = Thread::Current();
957
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800958 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700959
960 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700961 MutexLock mu(self, *Locks::thread_list_lock_);
962 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700963 // Update global suspend all state for attaching threads.
964 suspend_all_count_ -= debug_suspend_all_count_;
965 debug_suspend_all_count_ = 0;
966 // Update running threads.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700967 for (const auto& thread : list_) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700968 if (thread == self || thread->GetDebugSuspendCount() == 0) {
Elliott Hughes234ab152011-10-26 14:02:26 -0700969 continue;
970 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700971 thread->ModifySuspendCount(self, -thread->GetDebugSuspendCount(), true);
Elliott Hughes234ab152011-10-26 14:02:26 -0700972 }
973 }
974
975 {
Ian Rogers81d425b2012-09-27 16:03:43 -0700976 MutexLock mu(self, *Locks::thread_suspend_count_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -0700977 Thread::resume_cond_->Broadcast(self);
Elliott Hughes234ab152011-10-26 14:02:26 -0700978 }
979
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800980 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700981}
982
Elliott Hughese52e49b2012-04-02 16:05:44 -0700983void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Ian Rogers81d425b2012-09-27 16:03:43 -0700984 Thread* self = Thread::Current();
985 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700986 bool all_threads_are_daemons;
987 do {
Ian Rogers120f1c72012-09-28 17:17:10 -0700988 {
989 // No more threads can be born after we start to shutdown.
990 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700991 CHECK(Runtime::Current()->IsShuttingDownLocked());
Ian Rogers120f1c72012-09-28 17:17:10 -0700992 CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U);
993 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700994 all_threads_are_daemons = true;
Ian Rogers120f1c72012-09-28 17:17:10 -0700995 MutexLock mu(self, *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700996 for (const auto& thread : list_) {
Anwar Ghuloum97543682013-06-14 12:58:16 -0700997 if (thread != self && !thread->IsDaemon()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700998 all_threads_are_daemons = false;
999 break;
1000 }
1001 }
1002 if (!all_threads_are_daemons) {
1003 // Wait for another thread to exit before re-checking.
Ian Rogersc604d732012-10-14 16:09:54 -07001004 thread_exit_cond_.Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001005 }
Brian Carlstromdf629502013-07-17 22:39:56 -07001006 } while (!all_threads_are_daemons);
Elliott Hughes038a8062011-09-18 14:12:41 -07001007}
1008
1009void ThreadList::SuspendAllDaemonThreads() {
Ian Rogers81d425b2012-09-27 16:03:43 -07001010 Thread* self = Thread::Current();
1011 MutexLock mu(self, *Locks::thread_list_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001012 { // Tell all the daemons it's time to suspend.
Ian Rogers81d425b2012-09-27 16:03:43 -07001013 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001014 for (const auto& thread : list_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001015 // This is only run after all non-daemon threads have exited, so the remainder should all be
1016 // daemons.
Ian Rogers7e762862012-10-22 15:45:08 -07001017 CHECK(thread->IsDaemon()) << *thread;
Ian Rogers81d425b2012-09-27 16:03:43 -07001018 if (thread != self) {
Ian Rogers01ae5802012-09-28 16:14:01 -07001019 thread->ModifySuspendCount(self, +1, false);
Elliott Hughese52e49b2012-04-02 16:05:44 -07001020 }
Elliott Hughes038a8062011-09-18 14:12:41 -07001021 }
1022 }
Elliott Hughes038a8062011-09-18 14:12:41 -07001023 // Give the threads a chance to suspend, complaining if they're slow.
1024 bool have_complained = false;
1025 for (int i = 0; i < 10; ++i) {
1026 usleep(200 * 1000);
1027 bool all_suspended = true;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001028 for (const auto& thread : list_) {
Ian Rogers81d425b2012-09-27 16:03:43 -07001029 if (thread != self && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -07001030 if (!have_complained) {
1031 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
1032 have_complained = true;
1033 }
1034 all_suspended = false;
1035 }
1036 }
1037 if (all_suspended) {
1038 return;
1039 }
1040 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001041 LOG(ERROR) << "suspend all daemons failed";
1042}
1043void ThreadList::Register(Thread* self) {
1044 DCHECK_EQ(self, Thread::Current());
1045
1046 if (VLOG_IS_ON(threads)) {
1047 std::ostringstream oss;
1048 self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump.
Ian Rogers5a9ba012014-05-19 13:28:52 -07001049 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss.str();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001050 }
1051
1052 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
1053 // SuspendAll requests.
Ian Rogers81d425b2012-09-27 16:03:43 -07001054 MutexLock mu(self, *Locks::thread_list_lock_);
1055 MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001056 CHECK_GE(suspend_all_count_, debug_suspend_all_count_);
Ian Rogers2966e132014-04-02 08:34:36 -07001057 // Modify suspend count in increments of 1 to maintain invariants in ModifySuspendCount. While
1058 // this isn't particularly efficient the suspend counts are most commonly 0 or 1.
1059 for (int delta = debug_suspend_all_count_; delta > 0; delta--) {
1060 self->ModifySuspendCount(self, +1, true);
Ian Rogersdd7624d2014-03-14 17:43:00 -07001061 }
Ian Rogers2966e132014-04-02 08:34:36 -07001062 for (int delta = suspend_all_count_ - debug_suspend_all_count_; delta > 0; delta--) {
1063 self->ModifySuspendCount(self, +1, false);
Ian Rogers01ae5802012-09-28 16:14:01 -07001064 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001065 CHECK(!Contains(self));
1066 list_.push_back(self);
1067}
1068
1069void ThreadList::Unregister(Thread* self) {
1070 DCHECK_EQ(self, Thread::Current());
Ian Rogers68d8b422014-07-17 11:09:10 -07001071 CHECK_NE(self->GetState(), kRunnable);
1072 Locks::mutator_lock_->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073
1074 VLOG(threads) << "ThreadList::Unregister() " << *self;
1075
1076 // Any time-consuming destruction, plus anything that can call back into managed code or
1077 // suspend and so on, must happen at this point, and not in ~Thread.
1078 self->Destroy();
1079
Jeff Haoe094b872014-10-14 13:12:01 -07001080 // If tracing, remember thread id and name before thread exits.
1081 Trace::StoreExitingThreadInfo(self);
1082
Ian Rogersdd7624d2014-03-14 17:43:00 -07001083 uint32_t thin_lock_id = self->GetThreadId();
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -08001084 while (self != nullptr) {
Ian Rogerscfaa4552012-11-26 21:00:08 -08001085 // Remove and delete the Thread* while holding the thread_list_lock_ and
1086 // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended.
Ian Rogers0878d652013-04-18 17:38:35 -07001087 // Note: deliberately not using MutexLock that could hold a stale self pointer.
1088 Locks::thread_list_lock_->ExclusiveLock(self);
Ian Rogersa2af5c72014-09-15 15:17:07 -07001089 bool removed = true;
1090 if (!Contains(self)) {
1091 std::ostringstream os;
1092 DumpNativeStack(os, GetTid(), " native: ", nullptr);
1093 LOG(ERROR) << "Request to unregister unattached thread\n" << os.str();
1094 } else {
1095 Locks::thread_suspend_count_lock_->ExclusiveLock(self);
1096 if (!self->IsSuspended()) {
1097 list_.remove(self);
1098 } else {
1099 // We failed to remove the thread due to a suspend request, loop and try again.
1100 removed = false;
1101 }
1102 Locks::thread_suspend_count_lock_->ExclusiveUnlock(self);
Ian Rogers68d8b422014-07-17 11:09:10 -07001103 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001104 Locks::thread_list_lock_->ExclusiveUnlock(self);
1105 if (removed) {
Ian Rogerscfaa4552012-11-26 21:00:08 -08001106 delete self;
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -08001107 self = nullptr;
Ian Rogerscfaa4552012-11-26 21:00:08 -08001108 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001109 }
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -08001110 // Release the thread ID after the thread is finished and deleted to avoid cases where we can
1111 // temporarily have multiple threads with the same thread id. When this occurs, it causes
1112 // problems in FindThreadByThreadId / SuspendThreadByThreadId.
1113 ReleaseThreadId(nullptr, thin_lock_id);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001114
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001115 // Clear the TLS data, so that the underlying native thread is recognizably detached.
1116 // (It may wish to reattach later.)
1117 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
1118
1119 // Signal that a thread just detached.
Ian Rogers81d425b2012-09-27 16:03:43 -07001120 MutexLock mu(NULL, *Locks::thread_list_lock_);
Ian Rogersc604d732012-10-14 16:09:54 -07001121 thread_exit_cond_.Signal(NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001122}
1123
1124void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Mathieu Chartier02e25112013-08-14 16:14:24 -07001125 for (const auto& thread : list_) {
1126 callback(thread, context);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001127 }
1128}
1129
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08001130void ThreadList::VisitRoots(RootCallback* callback, void* arg) const {
Ian Rogers81d425b2012-09-27 16:03:43 -07001131 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001132 for (const auto& thread : list_) {
Mathieu Chartier83c8ee02014-01-28 14:50:23 -08001133 thread->VisitRoots(callback, arg);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001134 }
Elliott Hughes038a8062011-09-18 14:12:41 -07001135}
1136
Ian Rogerscfaa4552012-11-26 21:00:08 -08001137uint32_t ThreadList::AllocThreadId(Thread* self) {
Chao-ying Fu9e369312014-05-21 11:20:52 -07001138 MutexLock mu(self, *Locks::allocated_thread_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -07001139 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
1140 if (!allocated_ids_[i]) {
1141 allocated_ids_.set(i);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001142 return i + 1; // Zero is reserved to mean "invalid".
Elliott Hughes8daa0922011-09-11 13:46:25 -07001143 }
1144 }
1145 LOG(FATAL) << "Out of internal thread ids";
1146 return 0;
1147}
1148
Ian Rogerscfaa4552012-11-26 21:00:08 -08001149void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) {
Chao-ying Fu9e369312014-05-21 11:20:52 -07001150 MutexLock mu(self, *Locks::allocated_thread_ids_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001151 --id; // Zero is reserved to mean "invalid".
Elliott Hughes8daa0922011-09-11 13:46:25 -07001152 DCHECK(allocated_ids_[id]) << id;
1153 allocated_ids_.reset(id);
1154}
1155
Elliott Hughes8daa0922011-09-11 13:46:25 -07001156} // namespace art