Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 20 | |
| 21 | #include <cutils/trace.h> |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 22 | #include <dirent.h> |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 23 | #include <ScopedLocalRef.h> |
| 24 | #include <ScopedUtfChars.h> |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 25 | #include <sys/types.h> |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 28 | #include <sstream> |
| 29 | |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 30 | #include "base/histogram-inl.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 31 | #include "base/mutex.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 32 | #include "base/mutex-inl.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 33 | #include "base/timing_logger.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 34 | #include "debugger.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 35 | #include "jni_internal.h" |
| 36 | #include "lock_word.h" |
| 37 | #include "monitor.h" |
| 38 | #include "scoped_thread_state_change.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | #include "thread.h" |
Jeff Hao | e094b87 | 2014-10-14 13:12:01 -0700 | [diff] [blame] | 40 | #include "trace.h" |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 41 | #include "utils.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 42 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 44 | namespace art { |
| 45 | |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 46 | static constexpr uint64_t kLongThreadSuspendThreshold = MsToNs(5); |
| 47 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 48 | ThreadList::ThreadList() |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 49 | : suspend_all_count_(0), debug_suspend_all_count_(0), |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 50 | thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_), |
| 51 | suspend_all_historam_("suspend all histogram", 16, 64) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 52 | CHECK(Monitor::IsValidLockWord(LockWord::FromThinLockId(kMaxThreadId, 1))); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ThreadList::~ThreadList() { |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 56 | // Detach the current thread if necessary. If we failed to start, there might not be any threads. |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 57 | // We need to detach the current thread here in case there's another thread waiting to join with |
| 58 | // us. |
Mathieu Chartier | fec72f4 | 2014-10-09 12:57:58 -0700 | [diff] [blame] | 59 | 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 Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 66 | Runtime::Current()->DetachCurrentThread(); |
| 67 | } |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 68 | |
| 69 | WaitForOtherNonDaemonThreadsToExit(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 70 | // TODO: there's an unaddressed race here where a thread may attach during shutdown, see |
| 71 | // Thread::Init. |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 72 | SuspendAllDaemonThreads(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool ThreadList::Contains(Thread* thread) { |
| 76 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 77 | } |
| 78 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 79 | bool ThreadList::Contains(pid_t tid) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 80 | for (const auto& thread : list_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 81 | if (thread->GetTid() == tid) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 88 | pid_t ThreadList::GetLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 89 | return Locks::thread_list_lock_->GetExclusiveOwnerTid(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 92 | void ThreadList::DumpNativeStacks(std::ostream& os) { |
| 93 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 94 | for (const auto& thread : list_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 95 | os << "DUMPING THREAD " << thread->GetTid() << "\n"; |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 96 | DumpNativeStack(os, thread->GetTid(), "\t"); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 97 | os << "\n"; |
| 98 | } |
| 99 | } |
| 100 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 101 | void ThreadList::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 102 | { |
| 103 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 23f6e69 | 2014-12-18 18:24:39 -0800 | [diff] [blame] | 104 | // 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 Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 110 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 111 | Dump(os); |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 112 | DumpUnattachedThreads(os); |
| 113 | } |
| 114 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 115 | static 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 Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 118 | Thread::DumpState(os, NULL, tid); |
| 119 | DumpKernelStack(os, tid, " kernel: ", false); |
Brian Carlstrom | ed8b723 | 2012-06-27 17:54:47 -0700 | [diff] [blame] | 120 | // 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 Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 123 | DumpNativeStack(os, tid, " native: "); |
Brian Carlstrom | ed8b723 | 2012-06-27 17:54:47 -0700 | [diff] [blame] | 124 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 125 | os << "\n"; |
| 126 | } |
| 127 | |
| 128 | void ThreadList::DumpUnattachedThreads(std::ostream& os) { |
| 129 | DIR* d = opendir("/proc/self/task"); |
| 130 | if (!d) { |
| 131 | return; |
| 132 | } |
| 133 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 134 | Thread* self = Thread::Current(); |
Elliott Hughes | 4696b5b | 2012-10-30 10:35:10 -0700 | [diff] [blame] | 135 | dirent* e; |
| 136 | while ((e = readdir(d)) != NULL) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 137 | char* end; |
Elliott Hughes | 4696b5b | 2012-10-30 10:35:10 -0700 | [diff] [blame] | 138 | pid_t tid = strtol(e->d_name, &end, 10); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 139 | if (!*end) { |
| 140 | bool contains; |
| 141 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 142 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 143 | contains = Contains(tid); |
| 144 | } |
| 145 | if (!contains) { |
| 146 | DumpUnattachedThread(os, tid); |
| 147 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | closedir(d); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 153 | // A closure used by Thread::Dump. |
| 154 | class DumpCheckpoint FINAL : public Closure { |
| 155 | public: |
| 156 | explicit DumpCheckpoint(std::ostream* os) : os_(os), barrier_(0) {} |
| 157 | |
| 158 | void Run(Thread* thread) OVERRIDE { |
| 159 | // Note thread and self may not be equal if thread was already suspended at the point of the |
| 160 | // request. |
| 161 | Thread* self = Thread::Current(); |
| 162 | std::ostringstream local_os; |
| 163 | { |
| 164 | ScopedObjectAccess soa(self); |
| 165 | thread->Dump(local_os); |
| 166 | } |
| 167 | local_os << "\n"; |
| 168 | { |
| 169 | // Use the logging lock to ensure serialization when writing to the common ostream. |
| 170 | MutexLock mu(self, *Locks::logging_lock_); |
| 171 | *os_ << local_os.str(); |
| 172 | } |
| 173 | barrier_.Pass(self); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 174 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 175 | |
| 176 | void WaitForThreadsToRunThroughCheckpoint(size_t threads_running_checkpoint) { |
| 177 | Thread* self = Thread::Current(); |
| 178 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
Ian Rogers | 2156ff1 | 2014-09-13 19:20:54 -0700 | [diff] [blame] | 179 | const uint32_t kWaitTimeoutMs = 10000; |
| 180 | bool timed_out = barrier_.Increment(self, threads_running_checkpoint, kWaitTimeoutMs); |
| 181 | if (timed_out) { |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 182 | // Avoid a recursive abort. |
| 183 | LOG((kIsDebugBuild && (gAborting == 0)) ? FATAL : ERROR) |
| 184 | << "Unexpected time out during dump checkpoint."; |
Ian Rogers | 2156ff1 | 2014-09-13 19:20:54 -0700 | [diff] [blame] | 185 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | private: |
| 189 | // The common stream that will accumulate all the dumps. |
| 190 | std::ostream* const os_; |
| 191 | // The barrier to be passed through and for the requestor to wait upon. |
| 192 | Barrier barrier_; |
| 193 | }; |
| 194 | |
| 195 | void ThreadList::Dump(std::ostream& os) { |
| 196 | { |
| 197 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 198 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
| 199 | } |
| 200 | DumpCheckpoint checkpoint(&os); |
| 201 | size_t threads_running_checkpoint = RunCheckpoint(&checkpoint); |
| 202 | checkpoint.WaitForThreadsToRunThroughCheckpoint(threads_running_checkpoint); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 205 | void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) { |
| 206 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 207 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 208 | for (const auto& thread : list_) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 209 | if (thread != ignore1 && thread != ignore2) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 210 | CHECK(thread->IsSuspended()) |
| 211 | << "\nUnsuspended thread: <<" << *thread << "\n" |
| 212 | << "self: <<" << *Thread::Current(); |
| 213 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 214 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 217 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 218 | // Attempt to rectify locks so that we dump thread list with required locks before exiting. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 219 | static void UnsafeLogFatalForThreadSuspendAllTimeout() __attribute__((noreturn)); |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 220 | static void UnsafeLogFatalForThreadSuspendAllTimeout() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 221 | Runtime* runtime = Runtime::Current(); |
| 222 | std::ostringstream ss; |
| 223 | ss << "Thread suspend timeout\n"; |
Mathieu Chartier | 5869a2c | 2014-10-08 14:26:23 -0700 | [diff] [blame] | 224 | Locks::mutator_lock_->Dump(ss); |
| 225 | ss << "\n"; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 226 | runtime->GetThreadList()->Dump(ss); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 227 | LOG(FATAL) << ss.str(); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 228 | exit(0); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 229 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 230 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 231 | |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 232 | // Unlike suspending all threads where we can wait to acquire the mutator_lock_, suspending an |
| 233 | // individual thread requires polling. delay_us is the requested sleep and total_delay_us |
| 234 | // accumulates the total time spent sleeping for timeouts. The first sleep is just a yield, |
| 235 | // subsequently sleeps increase delay_us from 1ms to 500ms by doubling. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 236 | static void ThreadSuspendSleep(useconds_t* delay_us, useconds_t* total_delay_us) { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 237 | useconds_t new_delay_us = (*delay_us) * 2; |
| 238 | CHECK_GE(new_delay_us, *delay_us); |
| 239 | if (new_delay_us < 500000) { // Don't allow sleeping to be more than 0.5s. |
| 240 | *delay_us = new_delay_us; |
| 241 | } |
| 242 | if (*delay_us == 0) { |
| 243 | sched_yield(); |
| 244 | // Default to 1 milliseconds (note that this gets multiplied by 2 before the first sleep). |
| 245 | *delay_us = 500; |
| 246 | } else { |
| 247 | usleep(*delay_us); |
| 248 | *total_delay_us += *delay_us; |
| 249 | } |
| 250 | } |
| 251 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 252 | size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 253 | Thread* self = Thread::Current(); |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 254 | Locks::mutator_lock_->AssertNotExclusiveHeld(self); |
| 255 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 256 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 257 | if (kDebugLocking && gAborting == 0) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 258 | CHECK_NE(self->GetState(), kRunnable); |
| 259 | } |
| 260 | |
| 261 | std::vector<Thread*> suspended_count_modified_threads; |
| 262 | size_t count = 0; |
| 263 | { |
| 264 | // Call a checkpoint function for each thread, threads which are suspend get their checkpoint |
| 265 | // manually called. |
| 266 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 267 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 268 | for (const auto& thread : list_) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 269 | if (thread != self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 270 | while (true) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 271 | if (thread->RequestCheckpoint(checkpoint_function)) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 272 | // This thread will run its checkpoint some time in the near future. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 273 | count++; |
| 274 | break; |
| 275 | } else { |
| 276 | // We are probably suspended, try to make sure that we stay suspended. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 277 | // The thread switched back to runnable. |
| 278 | if (thread->GetState() == kRunnable) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 279 | // Spurious fail, try again. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 280 | continue; |
| 281 | } |
| 282 | thread->ModifySuspendCount(self, +1, false); |
| 283 | suspended_count_modified_threads.push_back(thread); |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Run the checkpoint on ourself while we wait for threads to suspend. |
| 292 | checkpoint_function->Run(self); |
| 293 | |
| 294 | // Run the checkpoint on the suspended threads. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 295 | for (const auto& thread : suspended_count_modified_threads) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 296 | if (!thread->IsSuspended()) { |
| 297 | // Wait until the thread is suspended. |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 298 | useconds_t total_delay_us = 0; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 299 | do { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 300 | useconds_t delay_us = 100; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 301 | ThreadSuspendSleep(&delay_us, &total_delay_us); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 302 | } while (!thread->IsSuspended()); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 303 | // Shouldn't need to wait for longer than 1000 microseconds. |
| 304 | constexpr useconds_t kLongWaitThresholdUS = 1000; |
| 305 | if (UNLIKELY(total_delay_us > kLongWaitThresholdUS)) { |
| 306 | LOG(WARNING) << "Waited " << total_delay_us << " us for thread suspend!"; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | // We know for sure that the thread is suspended at this point. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 310 | checkpoint_function->Run(thread); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 311 | { |
| 312 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 313 | thread->ModifySuspendCount(self, -1, false); |
| 314 | } |
| 315 | } |
| 316 | |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 317 | { |
| 318 | // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their |
| 319 | // suspend count. Now the suspend_count_ is lowered so we must do the broadcast. |
| 320 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 321 | Thread::resume_cond_->Broadcast(self); |
| 322 | } |
| 323 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 324 | // Add one for self. |
| 325 | return count + suspended_count_modified_threads.size() + 1; |
| 326 | } |
| 327 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 328 | // Request that a checkpoint function be run on all active (non-suspended) |
| 329 | // threads. Returns the number of successful requests. |
| 330 | size_t ThreadList::RunCheckpointOnRunnableThreads(Closure* checkpoint_function) { |
| 331 | Thread* self = Thread::Current(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 332 | Locks::mutator_lock_->AssertNotExclusiveHeld(self); |
| 333 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 334 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
| 335 | CHECK_NE(self->GetState(), kRunnable); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 336 | |
| 337 | size_t count = 0; |
| 338 | { |
| 339 | // Call a checkpoint function for each non-suspended thread. |
| 340 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 341 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 342 | for (const auto& thread : list_) { |
| 343 | if (thread != self) { |
| 344 | if (thread->RequestCheckpoint(checkpoint_function)) { |
| 345 | // This thread will run its checkpoint some time in the near future. |
| 346 | count++; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // Return the number of threads that will run the checkpoint function. |
| 353 | return count; |
| 354 | } |
| 355 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 356 | void ThreadList::SuspendAll() { |
| 357 | Thread* self = Thread::Current(); |
| 358 | |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 359 | if (self != nullptr) { |
| 360 | VLOG(threads) << *self << " SuspendAll starting..."; |
| 361 | } else { |
| 362 | VLOG(threads) << "Thread[null] SuspendAll starting..."; |
| 363 | } |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 364 | ATRACE_BEGIN("Suspending mutator threads"); |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 365 | const uint64_t start_time = NanoTime(); |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 366 | |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 367 | Locks::mutator_lock_->AssertNotHeld(self); |
| 368 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 369 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 370 | if (kDebugLocking && self != nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 371 | CHECK_NE(self->GetState(), kRunnable); |
| 372 | } |
| 373 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 374 | MutexLock mu(self, *Locks::thread_list_lock_); |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 375 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 376 | // Update global suspend all state for attaching threads. |
| 377 | ++suspend_all_count_; |
| 378 | // Increment everybody's suspend count (except our own). |
| 379 | for (const auto& thread : list_) { |
| 380 | if (thread == self) { |
| 381 | continue; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 382 | } |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 383 | VLOG(threads) << "requesting thread suspend: " << *thread; |
| 384 | thread->ModifySuspendCount(self, +1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 388 | // Block on the mutator lock until all Runnable threads release their share of access. |
| 389 | #if HAVE_TIMED_RWLOCK |
| 390 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 391 | if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) { |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 392 | UnsafeLogFatalForThreadSuspendAllTimeout(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 393 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 394 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 395 | Locks::mutator_lock_->ExclusiveLock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 396 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 397 | |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 398 | const uint64_t end_time = NanoTime(); |
| 399 | const uint64_t suspend_time = end_time - start_time; |
| 400 | suspend_all_historam_.AdjustAndAddValue(suspend_time); |
| 401 | if (suspend_time > kLongThreadSuspendThreshold) { |
| 402 | LOG(WARNING) << "Suspending all threads took: " << PrettyDuration(suspend_time); |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 405 | if (kDebugLocking) { |
| 406 | // Debug check that all threads are suspended. |
| 407 | AssertThreadsAreSuspended(self, self); |
| 408 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 409 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 410 | ATRACE_END(); |
| 411 | ATRACE_BEGIN("Mutator threads suspended"); |
| 412 | |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 413 | if (self != nullptr) { |
| 414 | VLOG(threads) << *self << " SuspendAll complete"; |
| 415 | } else { |
| 416 | VLOG(threads) << "Thread[null] SuspendAll complete"; |
| 417 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 420 | void ThreadList::ResumeAll() { |
| 421 | Thread* self = Thread::Current(); |
| 422 | |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 423 | if (self != nullptr) { |
| 424 | VLOG(threads) << *self << " ResumeAll starting"; |
| 425 | } else { |
| 426 | VLOG(threads) << "Thread[null] ResumeAll starting"; |
| 427 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 428 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 429 | ATRACE_END(); |
| 430 | ATRACE_BEGIN("Resuming mutator threads"); |
| 431 | |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 432 | if (kDebugLocking) { |
| 433 | // Debug check that all threads are suspended. |
| 434 | AssertThreadsAreSuspended(self, self); |
| 435 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 436 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 437 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 438 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 439 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 440 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 441 | // Update global suspend all state for attaching threads. |
| 442 | --suspend_all_count_; |
| 443 | // Decrement the suspend counts for all threads. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 444 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 445 | if (thread == self) { |
| 446 | continue; |
| 447 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 448 | thread->ModifySuspendCount(self, -1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Broadcast a notification to all suspended threads, some or all of |
| 452 | // which may choose to wake up. No need to wait for them. |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 453 | if (self != nullptr) { |
| 454 | VLOG(threads) << *self << " ResumeAll waking others"; |
| 455 | } else { |
| 456 | VLOG(threads) << "Thread[null] ResumeAll waking others"; |
| 457 | } |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 458 | Thread::resume_cond_->Broadcast(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 459 | } |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 460 | ATRACE_END(); |
Jeff Hao | c5d824a | 2014-07-28 18:35:38 -0700 | [diff] [blame] | 461 | |
| 462 | if (self != nullptr) { |
| 463 | VLOG(threads) << *self << " ResumeAll complete"; |
| 464 | } else { |
| 465 | VLOG(threads) << "Thread[null] ResumeAll complete"; |
| 466 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void ThreadList::Resume(Thread* thread, bool for_debugger) { |
Mathieu Chartier | f0dc8b5 | 2014-12-17 10:13:30 -0800 | [diff] [blame] | 470 | // This assumes there was an ATRACE_BEGIN when we suspended the thread. |
| 471 | ATRACE_END(); |
| 472 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 473 | Thread* self = Thread::Current(); |
| 474 | DCHECK_NE(thread, self); |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 475 | VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") starting..." |
| 476 | << (for_debugger ? " (debugger)" : ""); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 477 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 478 | { |
| 479 | // To check Contains. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 480 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 481 | // To check IsSuspended. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 482 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 483 | DCHECK(thread->IsSuspended()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 484 | if (!Contains(thread)) { |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 485 | // We only expect threads within the thread-list to have been suspended otherwise we can't |
| 486 | // stop such threads from delete-ing themselves. |
| 487 | LOG(ERROR) << "Resume(" << reinterpret_cast<void*>(thread) |
| 488 | << ") thread not within thread list"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 489 | return; |
| 490 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 491 | thread->ModifySuspendCount(self, -1, for_debugger); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | { |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 495 | VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") waking others"; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 496 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 497 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 500 | VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") complete"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 501 | } |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 502 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 503 | static void ThreadSuspendByPeerWarning(Thread* self, LogSeverity severity, const char* message, |
| 504 | jobject peer) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 505 | JNIEnvExt* env = self->GetJniEnv(); |
| 506 | ScopedLocalRef<jstring> |
| 507 | scoped_name_string(env, (jstring)env->GetObjectField(peer, |
| 508 | WellKnownClasses::java_lang_Thread_name)); |
| 509 | ScopedUtfChars scoped_name_chars(env, scoped_name_string.get()); |
| 510 | if (scoped_name_chars.c_str() == NULL) { |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 511 | LOG(severity) << message << ": " << peer; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 512 | env->ExceptionClear(); |
| 513 | } else { |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 514 | LOG(severity) << message << ": " << peer << ":" << scoped_name_chars.c_str(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 518 | Thread* ThreadList::SuspendThreadByPeer(jobject peer, bool request_suspension, |
| 519 | bool debug_suspension, bool* timed_out) { |
| 520 | static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. |
| 521 | useconds_t total_delay_us = 0; |
| 522 | useconds_t delay_us = 0; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 523 | *timed_out = false; |
| 524 | Thread* self = Thread::Current(); |
Mathieu Chartier | 82a800d | 2014-12-15 15:59:49 -0800 | [diff] [blame] | 525 | Thread* suspended_thread = nullptr; |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 526 | VLOG(threads) << "SuspendThreadByPeer starting"; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 527 | while (true) { |
| 528 | Thread* thread; |
| 529 | { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 530 | // Note: this will transition to runnable and potentially suspend. We ensure only one thread |
| 531 | // is requesting another suspend, to avoid deadlock, by requiring this function be called |
| 532 | // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather |
| 533 | // than request thread suspension, to avoid potential cycles in threads requesting each other |
| 534 | // suspend. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 535 | ScopedObjectAccess soa(self); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 536 | MutexLock thread_list_mu(self, *Locks::thread_list_lock_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 537 | thread = Thread::FromManagedThread(soa, peer); |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 538 | if (thread == nullptr) { |
Mathieu Chartier | 82a800d | 2014-12-15 15:59:49 -0800 | [diff] [blame] | 539 | if (suspended_thread != nullptr) { |
| 540 | MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_); |
| 541 | // If we incremented the suspend count but the thread reset its peer, we need to |
| 542 | // re-decrement it since it is shutting down and may deadlock the runtime in |
| 543 | // ThreadList::WaitForOtherNonDaemonThreadsToExit. |
| 544 | suspended_thread->ModifySuspendCount(soa.Self(), -1, debug_suspension); |
| 545 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 546 | ThreadSuspendByPeerWarning(self, WARNING, "No such thread for suspend", peer); |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 547 | return nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 548 | } |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 549 | if (!Contains(thread)) { |
Mathieu Chartier | 82a800d | 2014-12-15 15:59:49 -0800 | [diff] [blame] | 550 | CHECK(suspended_thread == nullptr); |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 551 | VLOG(threads) << "SuspendThreadByPeer failed for unattached thread: " |
| 552 | << reinterpret_cast<void*>(thread); |
| 553 | return nullptr; |
| 554 | } |
| 555 | VLOG(threads) << "SuspendThreadByPeer found thread: " << *thread; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 556 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 557 | MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 558 | if (request_suspension) { |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 559 | if (self->GetSuspendCount() > 0) { |
| 560 | // We hold the suspend count lock but another thread is trying to suspend us. Its not |
| 561 | // safe to try to suspend another thread in case we get a cycle. Start the loop again |
| 562 | // which will allow this thread to be suspended. |
| 563 | continue; |
| 564 | } |
Mathieu Chartier | 82a800d | 2014-12-15 15:59:49 -0800 | [diff] [blame] | 565 | CHECK(suspended_thread == nullptr); |
| 566 | suspended_thread = thread; |
| 567 | suspended_thread->ModifySuspendCount(self, +1, debug_suspension); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 568 | request_suspension = false; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 569 | } else { |
| 570 | // If the caller isn't requesting suspension, a suspension should have already occurred. |
| 571 | CHECK_GT(thread->GetSuspendCount(), 0); |
| 572 | } |
| 573 | // IsSuspended on the current thread will fail as the current thread is changed into |
| 574 | // Runnable above. As the suspend count is now raised if this is the current thread |
| 575 | // it will self suspend on transition to Runnable, making it hard to work with. It's simpler |
| 576 | // to just explicitly handle the current thread in the callers to this code. |
| 577 | CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger"; |
| 578 | // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend |
| 579 | // count, or else we've waited and it has self suspended) or is the current thread, we're |
| 580 | // done. |
| 581 | if (thread->IsSuspended()) { |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 582 | VLOG(threads) << "SuspendThreadByPeer thread suspended: " << *thread; |
Mathieu Chartier | f0dc8b5 | 2014-12-17 10:13:30 -0800 | [diff] [blame] | 583 | if (ATRACE_ENABLED()) { |
| 584 | std::string name; |
| 585 | thread->GetThreadName(name); |
| 586 | ATRACE_BEGIN(StringPrintf("SuspendThreadByPeer suspended %s for peer=%p", name.c_str(), |
| 587 | peer).c_str()); |
| 588 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 589 | return thread; |
| 590 | } |
| 591 | if (total_delay_us >= kTimeoutUs) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 592 | ThreadSuspendByPeerWarning(self, FATAL, "Thread suspension timed out", peer); |
Mathieu Chartier | 82a800d | 2014-12-15 15:59:49 -0800 | [diff] [blame] | 593 | if (suspended_thread != nullptr) { |
| 594 | CHECK_EQ(suspended_thread, thread); |
| 595 | suspended_thread->ModifySuspendCount(soa.Self(), -1, debug_suspension); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 596 | } |
| 597 | *timed_out = true; |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 598 | return nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | // Release locks and come out of runnable state. |
| 602 | } |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 603 | VLOG(threads) << "SuspendThreadByPeer sleeping to allow thread chance to suspend"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 604 | ThreadSuspendSleep(&delay_us, &total_delay_us); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 608 | static void ThreadSuspendByThreadIdWarning(LogSeverity severity, const char* message, |
| 609 | uint32_t thread_id) { |
| 610 | LOG(severity) << StringPrintf("%s: %d", message, thread_id); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | Thread* ThreadList::SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension, |
| 614 | bool* timed_out) { |
| 615 | static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. |
| 616 | useconds_t total_delay_us = 0; |
| 617 | useconds_t delay_us = 0; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 618 | *timed_out = false; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 619 | Thread* suspended_thread = nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 620 | Thread* self = Thread::Current(); |
| 621 | CHECK_NE(thread_id, kInvalidThreadId); |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 622 | VLOG(threads) << "SuspendThreadByThreadId starting"; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 623 | while (true) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 624 | { |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 625 | // Note: this will transition to runnable and potentially suspend. We ensure only one thread |
| 626 | // is requesting another suspend, to avoid deadlock, by requiring this function be called |
| 627 | // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather |
| 628 | // than request thread suspension, to avoid potential cycles in threads requesting each other |
| 629 | // suspend. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 630 | ScopedObjectAccess soa(self); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 631 | MutexLock thread_list_mu(self, *Locks::thread_list_lock_); |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 632 | Thread* thread = nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 633 | for (const auto& it : list_) { |
| 634 | if (it->GetThreadId() == thread_id) { |
| 635 | thread = it; |
| 636 | break; |
| 637 | } |
| 638 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 639 | if (thread == nullptr) { |
| 640 | CHECK(suspended_thread == nullptr) << "Suspended thread " << suspended_thread |
| 641 | << " no longer in thread list"; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 642 | // There's a race in inflating a lock and the owner giving up ownership and then dying. |
| 643 | ThreadSuspendByThreadIdWarning(WARNING, "No such thread id for suspend", thread_id); |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 644 | return nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 645 | } |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 646 | VLOG(threads) << "SuspendThreadByThreadId found thread: " << *thread; |
| 647 | DCHECK(Contains(thread)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 648 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 649 | MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 650 | if (suspended_thread == nullptr) { |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 651 | 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 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 657 | thread->ModifySuspendCount(self, +1, debug_suspension); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 658 | suspended_thread = thread; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 659 | } else { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 660 | CHECK_EQ(suspended_thread, thread); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 661 | // If the caller isn't requesting suspension, a suspension should have already occurred. |
| 662 | CHECK_GT(thread->GetSuspendCount(), 0); |
| 663 | } |
| 664 | // IsSuspended on the current thread will fail as the current thread is changed into |
| 665 | // Runnable above. As the suspend count is now raised if this is the current thread |
| 666 | // it will self suspend on transition to Runnable, making it hard to work with. It's simpler |
| 667 | // to just explicitly handle the current thread in the callers to this code. |
| 668 | CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger"; |
| 669 | // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend |
| 670 | // count, or else we've waited and it has self suspended) or is the current thread, we're |
| 671 | // done. |
| 672 | if (thread->IsSuspended()) { |
Mathieu Chartier | f0dc8b5 | 2014-12-17 10:13:30 -0800 | [diff] [blame] | 673 | if (ATRACE_ENABLED()) { |
| 674 | std::string name; |
| 675 | thread->GetThreadName(name); |
| 676 | ATRACE_BEGIN(StringPrintf("SuspendThreadByThreadId suspended %s id=%d", |
| 677 | name.c_str(), thread_id).c_str()); |
| 678 | } |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 679 | VLOG(threads) << "SuspendThreadByThreadId thread suspended: " << *thread; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 680 | return thread; |
| 681 | } |
| 682 | if (total_delay_us >= kTimeoutUs) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 683 | ThreadSuspendByThreadIdWarning(WARNING, "Thread suspension timed out", thread_id); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 684 | if (suspended_thread != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 685 | thread->ModifySuspendCount(soa.Self(), -1, debug_suspension); |
| 686 | } |
| 687 | *timed_out = true; |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 688 | return nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | // Release locks and come out of runnable state. |
| 692 | } |
Brian Carlstrom | ba32de4 | 2014-08-27 23:43:46 -0700 | [diff] [blame] | 693 | VLOG(threads) << "SuspendThreadByThreadId sleeping to allow thread chance to suspend"; |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 694 | ThreadSuspendSleep(&delay_us, &total_delay_us); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | Thread* ThreadList::FindThreadByThreadId(uint32_t thin_lock_id) { |
| 699 | Thread* self = Thread::Current(); |
| 700 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 701 | for (const auto& thread : list_) { |
| 702 | if (thread->GetThreadId() == thin_lock_id) { |
| 703 | CHECK(thread == self || thread->IsSuspended()); |
| 704 | return thread; |
| 705 | } |
| 706 | } |
| 707 | return NULL; |
| 708 | } |
| 709 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 710 | void ThreadList::SuspendAllForDebugger() { |
| 711 | Thread* self = Thread::Current(); |
| 712 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 713 | |
| 714 | VLOG(threads) << *self << " SuspendAllForDebugger starting..."; |
| 715 | |
| 716 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 717 | MutexLock thread_list_mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 718 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 719 | MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 720 | // Update global suspend all state for attaching threads. |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 721 | DCHECK_GE(suspend_all_count_, debug_suspend_all_count_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 722 | ++suspend_all_count_; |
| 723 | ++debug_suspend_all_count_; |
| 724 | // Increment everybody's suspend count (except our own). |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 725 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 726 | if (thread == self || thread == debug_thread) { |
| 727 | continue; |
| 728 | } |
| 729 | VLOG(threads) << "requesting thread suspend: " << *thread; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 730 | thread->ModifySuspendCount(self, +1, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 735 | // Block on the mutator lock until all Runnable threads release their share of access then |
| 736 | // immediately unlock again. |
| 737 | #if HAVE_TIMED_RWLOCK |
| 738 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 739 | if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) { |
Sebastien Hertz | bae182c | 2013-12-17 10:42:03 +0100 | [diff] [blame] | 740 | UnsafeLogFatalForThreadSuspendAllTimeout(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 741 | } else { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 742 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 743 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 744 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 745 | Locks::mutator_lock_->ExclusiveLock(self); |
| 746 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 747 | #endif |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 748 | AssertThreadsAreSuspended(self, self, debug_thread); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 749 | |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 750 | VLOG(threads) << *self << " SuspendAllForDebugger complete"; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 753 | void ThreadList::SuspendSelfForDebugger() { |
| 754 | Thread* self = Thread::Current(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 755 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 756 | // The debugger thread must not suspend itself due to debugger activity! |
| 757 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 758 | CHECK(debug_thread != NULL); |
| 759 | CHECK(self != debug_thread); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 760 | CHECK_NE(self->GetState(), kRunnable); |
| 761 | Locks::mutator_lock_->AssertNotHeld(self); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 762 | |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 763 | { |
| 764 | // Collisions with other suspends aren't really interesting. We want |
| 765 | // to ensure that we're the only one fiddling with the suspend count |
| 766 | // though. |
| 767 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
| 768 | self->ModifySuspendCount(self, +1, true); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 769 | CHECK_GT(self->GetSuspendCount(), 0); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 770 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 771 | |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 772 | VLOG(threads) << *self << " self-suspending (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 773 | |
Sebastien Hertz | 21e729c | 2014-02-18 14:16:00 +0100 | [diff] [blame] | 774 | // Tell JDWP we've completed invocation and are ready to suspend. |
| 775 | DebugInvokeReq* pReq = self->GetInvokeReq(); |
| 776 | DCHECK(pReq != NULL); |
| 777 | if (pReq->invoke_needed) { |
| 778 | // Clear this before signaling. |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 779 | pReq->Clear(); |
Sebastien Hertz | 21e729c | 2014-02-18 14:16:00 +0100 | [diff] [blame] | 780 | |
| 781 | VLOG(jdwp) << "invoke complete, signaling"; |
| 782 | MutexLock mu(self, pReq->lock); |
| 783 | pReq->cond.Signal(self); |
| 784 | } |
| 785 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 786 | // Tell JDWP that we've completed suspension. The JDWP thread can't |
| 787 | // tell us to resume before we're fully asleep because we hold the |
| 788 | // suspend count lock. |
| 789 | Dbg::ClearWaitForEventThread(); |
| 790 | |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 791 | { |
| 792 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 793 | while (self->GetSuspendCount() != 0) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 794 | Thread::resume_cond_->Wait(self); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 795 | if (self->GetSuspendCount() != 0) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 796 | // The condition was signaled but we're still suspended. This |
Sebastien Hertz | f272af4 | 2014-09-18 10:20:42 +0200 | [diff] [blame] | 797 | // can happen when we suspend then resume all threads to |
| 798 | // update instrumentation or compute monitor info. This can |
| 799 | // also happen if the debugger lets go while a SIGQUIT thread |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 800 | // dump event is pending (assuming SignalCatcher was resumed for |
| 801 | // just long enough to try to grab the thread-suspend lock). |
Sebastien Hertz | f272af4 | 2014-09-18 10:20:42 +0200 | [diff] [blame] | 802 | VLOG(jdwp) << *self << " still suspended after undo " |
| 803 | << "(suspend count=" << self->GetSuspendCount() << ", " |
| 804 | << "debug suspend count=" << self->GetDebugSuspendCount() << ")"; |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 805 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 806 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 807 | CHECK_EQ(self->GetSuspendCount(), 0); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 808 | } |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 809 | |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 810 | VLOG(threads) << *self << " self-reviving (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 813 | void ThreadList::ResumeAllForDebugger() { |
| 814 | Thread* self = Thread::Current(); |
| 815 | Thread* debug_thread = Dbg::GetDebugThread(); |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 816 | |
| 817 | VLOG(threads) << *self << " ResumeAllForDebugger starting..."; |
| 818 | |
| 819 | // Threads can't resume if we exclusively hold the mutator lock. |
| 820 | Locks::mutator_lock_->AssertNotExclusiveHeld(self); |
| 821 | |
| 822 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 823 | MutexLock thread_list_mu(self, *Locks::thread_list_lock_); |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 824 | { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 825 | MutexLock suspend_count_mu(self, *Locks::thread_suspend_count_lock_); |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 826 | // Update global suspend all state for attaching threads. |
| 827 | DCHECK_GE(suspend_all_count_, debug_suspend_all_count_); |
Sebastien Hertz | f9d233d | 2015-01-09 14:51:41 +0100 | [diff] [blame^] | 828 | if (debug_suspend_all_count_ > 0) { |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 829 | --suspend_all_count_; |
| 830 | --debug_suspend_all_count_; |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 831 | } else { |
| 832 | // We've been asked to resume all threads without being asked to |
Sebastien Hertz | f9d233d | 2015-01-09 14:51:41 +0100 | [diff] [blame^] | 833 | // suspend them all before. That may happen if a debugger tries |
| 834 | // to resume some suspended threads (with suspend count == 1) |
| 835 | // at once with a VirtualMachine.Resume command. Let's print a |
| 836 | // warning. |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 837 | LOG(WARNING) << "Debugger attempted to resume all threads without " |
| 838 | << "having suspended them all before."; |
| 839 | } |
Sebastien Hertz | f9d233d | 2015-01-09 14:51:41 +0100 | [diff] [blame^] | 840 | // Decrement everybody's suspend count (except our own). |
| 841 | for (const auto& thread : list_) { |
| 842 | if (thread == self || thread == debug_thread) { |
| 843 | continue; |
| 844 | } |
| 845 | if (thread->GetDebugSuspendCount() == 0) { |
| 846 | // This thread may have been individually resumed with ThreadReference.Resume. |
| 847 | continue; |
| 848 | } |
| 849 | VLOG(threads) << "requesting thread resume: " << *thread; |
| 850 | thread->ModifySuspendCount(self, -1, true); |
| 851 | } |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Sebastien Hertz | f9d233d | 2015-01-09 14:51:41 +0100 | [diff] [blame^] | 855 | { |
Sebastien Hertz | 253fa55 | 2014-10-14 17:27:15 +0200 | [diff] [blame] | 856 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
| 857 | Thread::resume_cond_->Broadcast(self); |
| 858 | } |
| 859 | |
| 860 | VLOG(threads) << *self << " ResumeAllForDebugger complete"; |
| 861 | } |
| 862 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 863 | void ThreadList::UndoDebuggerSuspensions() { |
| 864 | Thread* self = Thread::Current(); |
| 865 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 866 | VLOG(threads) << *self << " UndoDebuggerSuspensions starting"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 867 | |
| 868 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 869 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 870 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 871 | // Update global suspend all state for attaching threads. |
| 872 | suspend_all_count_ -= debug_suspend_all_count_; |
| 873 | debug_suspend_all_count_ = 0; |
| 874 | // Update running threads. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 875 | for (const auto& thread : list_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 876 | if (thread == self || thread->GetDebugSuspendCount() == 0) { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 877 | continue; |
| 878 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 879 | thread->ModifySuspendCount(self, -thread->GetDebugSuspendCount(), true); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
| 883 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 884 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 885 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 888 | VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 891 | void ThreadList::WaitForOtherNonDaemonThreadsToExit() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 892 | Thread* self = Thread::Current(); |
| 893 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 894 | bool all_threads_are_daemons; |
| 895 | do { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 896 | { |
| 897 | // No more threads can be born after we start to shutdown. |
| 898 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 899 | CHECK(Runtime::Current()->IsShuttingDownLocked()); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 900 | CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U); |
| 901 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 902 | all_threads_are_daemons = true; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 903 | MutexLock mu(self, *Locks::thread_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 904 | for (const auto& thread : list_) { |
Anwar Ghuloum | 9754368 | 2013-06-14 12:58:16 -0700 | [diff] [blame] | 905 | if (thread != self && !thread->IsDaemon()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 906 | all_threads_are_daemons = false; |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | if (!all_threads_are_daemons) { |
| 911 | // Wait for another thread to exit before re-checking. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 912 | thread_exit_cond_.Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 913 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 914 | } while (!all_threads_are_daemons); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | void ThreadList::SuspendAllDaemonThreads() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 918 | Thread* self = Thread::Current(); |
| 919 | MutexLock mu(self, *Locks::thread_list_lock_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 920 | { // Tell all the daemons it's time to suspend. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 921 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 922 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 923 | // This is only run after all non-daemon threads have exited, so the remainder should all be |
| 924 | // daemons. |
Ian Rogers | 7e76286 | 2012-10-22 15:45:08 -0700 | [diff] [blame] | 925 | CHECK(thread->IsDaemon()) << *thread; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 926 | if (thread != self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 927 | thread->ModifySuspendCount(self, +1, false); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 928 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 929 | } |
| 930 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 931 | // Give the threads a chance to suspend, complaining if they're slow. |
| 932 | bool have_complained = false; |
| 933 | for (int i = 0; i < 10; ++i) { |
| 934 | usleep(200 * 1000); |
| 935 | bool all_suspended = true; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 936 | for (const auto& thread : list_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 937 | if (thread != self && thread->GetState() == kRunnable) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 938 | if (!have_complained) { |
| 939 | LOG(WARNING) << "daemon thread not yet suspended: " << *thread; |
| 940 | have_complained = true; |
| 941 | } |
| 942 | all_suspended = false; |
| 943 | } |
| 944 | } |
| 945 | if (all_suspended) { |
| 946 | return; |
| 947 | } |
| 948 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 949 | LOG(ERROR) << "suspend all daemons failed"; |
| 950 | } |
| 951 | void ThreadList::Register(Thread* self) { |
| 952 | DCHECK_EQ(self, Thread::Current()); |
| 953 | |
| 954 | if (VLOG_IS_ON(threads)) { |
| 955 | std::ostringstream oss; |
| 956 | self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump. |
Ian Rogers | 5a9ba01 | 2014-05-19 13:28:52 -0700 | [diff] [blame] | 957 | LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss.str(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing |
| 961 | // SuspendAll requests. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 962 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 963 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 964 | CHECK_GE(suspend_all_count_, debug_suspend_all_count_); |
Ian Rogers | 2966e13 | 2014-04-02 08:34:36 -0700 | [diff] [blame] | 965 | // Modify suspend count in increments of 1 to maintain invariants in ModifySuspendCount. While |
| 966 | // this isn't particularly efficient the suspend counts are most commonly 0 or 1. |
| 967 | for (int delta = debug_suspend_all_count_; delta > 0; delta--) { |
| 968 | self->ModifySuspendCount(self, +1, true); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 969 | } |
Ian Rogers | 2966e13 | 2014-04-02 08:34:36 -0700 | [diff] [blame] | 970 | for (int delta = suspend_all_count_ - debug_suspend_all_count_; delta > 0; delta--) { |
| 971 | self->ModifySuspendCount(self, +1, false); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 972 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 973 | CHECK(!Contains(self)); |
| 974 | list_.push_back(self); |
| 975 | } |
| 976 | |
| 977 | void ThreadList::Unregister(Thread* self) { |
| 978 | DCHECK_EQ(self, Thread::Current()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 979 | CHECK_NE(self->GetState(), kRunnable); |
| 980 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 981 | |
| 982 | VLOG(threads) << "ThreadList::Unregister() " << *self; |
| 983 | |
| 984 | // Any time-consuming destruction, plus anything that can call back into managed code or |
| 985 | // suspend and so on, must happen at this point, and not in ~Thread. |
| 986 | self->Destroy(); |
| 987 | |
Jeff Hao | e094b87 | 2014-10-14 13:12:01 -0700 | [diff] [blame] | 988 | // If tracing, remember thread id and name before thread exits. |
| 989 | Trace::StoreExitingThreadInfo(self); |
| 990 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 991 | uint32_t thin_lock_id = self->GetThreadId(); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 992 | while (self != nullptr) { |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 993 | // Remove and delete the Thread* while holding the thread_list_lock_ and |
| 994 | // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended. |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 995 | // Note: deliberately not using MutexLock that could hold a stale self pointer. |
| 996 | Locks::thread_list_lock_->ExclusiveLock(self); |
Ian Rogers | a2af5c7 | 2014-09-15 15:17:07 -0700 | [diff] [blame] | 997 | bool removed = true; |
| 998 | if (!Contains(self)) { |
| 999 | std::ostringstream os; |
| 1000 | DumpNativeStack(os, GetTid(), " native: ", nullptr); |
| 1001 | LOG(ERROR) << "Request to unregister unattached thread\n" << os.str(); |
| 1002 | } else { |
| 1003 | Locks::thread_suspend_count_lock_->ExclusiveLock(self); |
| 1004 | if (!self->IsSuspended()) { |
| 1005 | list_.remove(self); |
| 1006 | } else { |
| 1007 | // We failed to remove the thread due to a suspend request, loop and try again. |
| 1008 | removed = false; |
| 1009 | } |
| 1010 | Locks::thread_suspend_count_lock_->ExclusiveUnlock(self); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1011 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 1012 | Locks::thread_list_lock_->ExclusiveUnlock(self); |
| 1013 | if (removed) { |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1014 | delete self; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 1015 | self = nullptr; |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1016 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1017 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 1018 | // Release the thread ID after the thread is finished and deleted to avoid cases where we can |
| 1019 | // temporarily have multiple threads with the same thread id. When this occurs, it causes |
| 1020 | // problems in FindThreadByThreadId / SuspendThreadByThreadId. |
| 1021 | ReleaseThreadId(nullptr, thin_lock_id); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1022 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1023 | // Clear the TLS data, so that the underlying native thread is recognizably detached. |
| 1024 | // (It may wish to reattach later.) |
| 1025 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self"); |
| 1026 | |
| 1027 | // Signal that a thread just detached. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1028 | MutexLock mu(NULL, *Locks::thread_list_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1029 | thread_exit_cond_.Signal(NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1033 | for (const auto& thread : list_) { |
| 1034 | callback(thread, context); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1038 | void ThreadList::VisitRoots(RootCallback* callback, void* arg) const { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1039 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1040 | for (const auto& thread : list_) { |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1041 | thread->VisitRoots(callback, arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1042 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1045 | class VerifyRootWrapperArg { |
| 1046 | public: |
| 1047 | VerifyRootWrapperArg(VerifyRootCallback* callback, void* arg) : callback_(callback), arg_(arg) { |
| 1048 | } |
| 1049 | VerifyRootCallback* const callback_; |
| 1050 | void* const arg_; |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1051 | }; |
| 1052 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 1053 | static void VerifyRootWrapperCallback(mirror::Object** root, void* arg, uint32_t /*thread_id*/, |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 1054 | RootType root_type) { |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1055 | VerifyRootWrapperArg* wrapperArg = reinterpret_cast<VerifyRootWrapperArg*>(arg); |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 1056 | wrapperArg->callback_(*root, wrapperArg->arg_, 0, NULL, root_type); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1059 | void ThreadList::VerifyRoots(VerifyRootCallback* callback, void* arg) const { |
| 1060 | VerifyRootWrapperArg wrapper(callback, arg); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 1061 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1062 | for (const auto& thread : list_) { |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1063 | thread->VisitRoots(VerifyRootWrapperCallback, &wrapper); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1067 | uint32_t ThreadList::AllocThreadId(Thread* self) { |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 1068 | MutexLock mu(self, *Locks::allocated_thread_ids_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1069 | for (size_t i = 0; i < allocated_ids_.size(); ++i) { |
| 1070 | if (!allocated_ids_[i]) { |
| 1071 | allocated_ids_.set(i); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1072 | return i + 1; // Zero is reserved to mean "invalid". |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | LOG(FATAL) << "Out of internal thread ids"; |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 1079 | void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) { |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 1080 | MutexLock mu(self, *Locks::allocated_thread_ids_lock_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1081 | --id; // Zero is reserved to mean "invalid". |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1082 | DCHECK(allocated_ids_[id]) << id; |
| 1083 | allocated_ids_.reset(id); |
| 1084 | } |
| 1085 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1086 | } // namespace art |