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