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