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