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 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 20 | #include <ScopedLocalRef.h> |
| 21 | #include <ScopedUtfChars.h> |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 25 | #include "base/mutex.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 26 | #include "base/mutex-inl.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 27 | #include "base/timing_logger.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 28 | #include "debugger.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 29 | #include "jni_internal.h" |
| 30 | #include "lock_word.h" |
| 31 | #include "monitor.h" |
| 32 | #include "scoped_thread_state_change.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "thread.h" |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 34 | #include "utils.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 35 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 39 | ThreadList::ThreadList() |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 40 | : allocated_ids_lock_("allocated thread ids lock"), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 41 | suspend_all_count_(0), debug_suspend_all_count_(0), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 42 | thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 43 | CHECK(Monitor::IsValidLockWord(LockWord::FromThinLockId(kMaxThreadId, 1))); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | ThreadList::~ThreadList() { |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 47 | // 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] | 48 | // We need to detach the current thread here in case there's another thread waiting to join with |
| 49 | // us. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 50 | if (Contains(Thread::Current())) { |
| 51 | Runtime::Current()->DetachCurrentThread(); |
| 52 | } |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 53 | |
| 54 | WaitForOtherNonDaemonThreadsToExit(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 55 | // TODO: there's an unaddressed race here where a thread may attach during shutdown, see |
| 56 | // Thread::Init. |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 57 | SuspendAllDaemonThreads(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool ThreadList::Contains(Thread* thread) { |
| 61 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 62 | } |
| 63 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 64 | bool ThreadList::Contains(pid_t tid) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 65 | for (const auto& thread : list_) { |
| 66 | if (thread->tid_ == tid) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 73 | pid_t ThreadList::GetLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 74 | return Locks::thread_list_lock_->GetExclusiveOwnerTid(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 77 | void ThreadList::DumpNativeStacks(std::ostream& os) { |
| 78 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 79 | for (const auto& thread : list_) { |
| 80 | os << "DUMPING THREAD " << thread->tid_ << "\n"; |
| 81 | DumpNativeStack(os, thread->tid_, "\t", true); |
| 82 | os << "\n"; |
| 83 | } |
| 84 | } |
| 85 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 86 | void ThreadList::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 87 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 88 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 89 | DumpLocked(os); |
| 90 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 91 | DumpUnattachedThreads(os); |
| 92 | } |
| 93 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 94 | static void DumpUnattachedThread(std::ostream& os, pid_t tid) NO_THREAD_SAFETY_ANALYSIS { |
| 95 | // TODO: No thread safety analysis as DumpState with a NULL thread won't access fields, should |
| 96 | // refactor DumpState to avoid skipping analysis. |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 97 | Thread::DumpState(os, NULL, tid); |
| 98 | DumpKernelStack(os, tid, " kernel: ", false); |
Brian Carlstrom | ed8b723 | 2012-06-27 17:54:47 -0700 | [diff] [blame] | 99 | // TODO: Reenable this when the native code in system_server can handle it. |
| 100 | // Currently "adb shell kill -3 `pid system_server`" will cause it to exit. |
| 101 | if (false) { |
| 102 | DumpNativeStack(os, tid, " native: ", false); |
| 103 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 104 | os << "\n"; |
| 105 | } |
| 106 | |
| 107 | void ThreadList::DumpUnattachedThreads(std::ostream& os) { |
| 108 | DIR* d = opendir("/proc/self/task"); |
| 109 | if (!d) { |
| 110 | return; |
| 111 | } |
| 112 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 113 | Thread* self = Thread::Current(); |
Elliott Hughes | 4696b5b | 2012-10-30 10:35:10 -0700 | [diff] [blame] | 114 | dirent* e; |
| 115 | while ((e = readdir(d)) != NULL) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 116 | char* end; |
Elliott Hughes | 4696b5b | 2012-10-30 10:35:10 -0700 | [diff] [blame] | 117 | pid_t tid = strtol(e->d_name, &end, 10); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 118 | if (!*end) { |
| 119 | bool contains; |
| 120 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 121 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 122 | contains = Contains(tid); |
| 123 | } |
| 124 | if (!contains) { |
| 125 | DumpUnattachedThread(os, tid); |
| 126 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | closedir(d); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void ThreadList::DumpLocked(std::ostream& os) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 133 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 134 | for (const auto& thread : list_) { |
| 135 | thread->Dump(os); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 136 | os << "\n"; |
| 137 | } |
| 138 | } |
| 139 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 140 | void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) { |
| 141 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 142 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 143 | for (const auto& thread : list_) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 144 | if (thread != ignore1 && thread != ignore2) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 145 | CHECK(thread->IsSuspended()) |
| 146 | << "\nUnsuspended thread: <<" << *thread << "\n" |
| 147 | << "self: <<" << *Thread::Current(); |
| 148 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 149 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 152 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 153 | // Attempt to rectify locks so that we dump thread list with required locks before exiting. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 154 | static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 155 | Runtime* runtime = Runtime::Current(); |
| 156 | std::ostringstream ss; |
| 157 | ss << "Thread suspend timeout\n"; |
| 158 | runtime->DumpLockHolders(ss); |
| 159 | ss << "\n"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 160 | runtime->GetThreadList()->DumpLocked(ss); |
| 161 | LOG(FATAL) << ss.str(); |
| 162 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 163 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 164 | |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 165 | // Unlike suspending all threads where we can wait to acquire the mutator_lock_, suspending an |
| 166 | // individual thread requires polling. delay_us is the requested sleep and total_delay_us |
| 167 | // accumulates the total time spent sleeping for timeouts. The first sleep is just a yield, |
| 168 | // subsequently sleeps increase delay_us from 1ms to 500ms by doubling. |
| 169 | static void ThreadSuspendSleep(Thread* self, useconds_t* delay_us, useconds_t* total_delay_us, |
| 170 | bool holding_locks) { |
| 171 | if (!holding_locks) { |
| 172 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
| 173 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
| 174 | if (held_mutex != NULL) { |
| 175 | LOG(FATAL) << "Holding " << held_mutex->GetName() << " while sleeping for thread suspension"; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | useconds_t new_delay_us = (*delay_us) * 2; |
| 180 | CHECK_GE(new_delay_us, *delay_us); |
| 181 | if (new_delay_us < 500000) { // Don't allow sleeping to be more than 0.5s. |
| 182 | *delay_us = new_delay_us; |
| 183 | } |
| 184 | if (*delay_us == 0) { |
| 185 | sched_yield(); |
| 186 | // Default to 1 milliseconds (note that this gets multiplied by 2 before the first sleep). |
| 187 | *delay_us = 500; |
| 188 | } else { |
| 189 | usleep(*delay_us); |
| 190 | *total_delay_us += *delay_us; |
| 191 | } |
| 192 | } |
| 193 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 194 | size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 195 | Thread* self = Thread::Current(); |
| 196 | if (kIsDebugBuild) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 197 | Locks::mutator_lock_->AssertNotExclusiveHeld(self); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 198 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 199 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
| 200 | CHECK_NE(self->GetState(), kRunnable); |
| 201 | } |
| 202 | |
| 203 | std::vector<Thread*> suspended_count_modified_threads; |
| 204 | size_t count = 0; |
| 205 | { |
| 206 | // Call a checkpoint function for each thread, threads which are suspend get their checkpoint |
| 207 | // manually called. |
| 208 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 209 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 210 | for (const auto& thread : list_) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 211 | if (thread != self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 212 | while (true) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 213 | if (thread->RequestCheckpoint(checkpoint_function)) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 214 | // This thread will run its checkpoint some time in the near future. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 215 | count++; |
| 216 | break; |
| 217 | } else { |
| 218 | // We are probably suspended, try to make sure that we stay suspended. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 219 | // The thread switched back to runnable. |
| 220 | if (thread->GetState() == kRunnable) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 221 | // Spurious fail, try again. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 222 | continue; |
| 223 | } |
| 224 | thread->ModifySuspendCount(self, +1, false); |
| 225 | suspended_count_modified_threads.push_back(thread); |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Run the checkpoint on ourself while we wait for threads to suspend. |
| 234 | checkpoint_function->Run(self); |
| 235 | |
| 236 | // Run the checkpoint on the suspended threads. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 237 | for (const auto& thread : suspended_count_modified_threads) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 238 | if (!thread->IsSuspended()) { |
| 239 | // Wait until the thread is suspended. |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 240 | useconds_t total_delay_us = 0; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 241 | do { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 242 | useconds_t delay_us = 100; |
| 243 | ThreadSuspendSleep(self, &delay_us, &total_delay_us, true); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 244 | } while (!thread->IsSuspended()); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 245 | // Shouldn't need to wait for longer than 1000 microseconds. |
| 246 | constexpr useconds_t kLongWaitThresholdUS = 1000; |
| 247 | if (UNLIKELY(total_delay_us > kLongWaitThresholdUS)) { |
| 248 | LOG(WARNING) << "Waited " << total_delay_us << " us for thread suspend!"; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | // We know for sure that the thread is suspended at this point. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 252 | checkpoint_function->Run(thread); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 253 | { |
| 254 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 255 | thread->ModifySuspendCount(self, -1, false); |
| 256 | } |
| 257 | } |
| 258 | |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 259 | { |
| 260 | // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their |
| 261 | // suspend count. Now the suspend_count_ is lowered so we must do the broadcast. |
| 262 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 263 | Thread::resume_cond_->Broadcast(self); |
| 264 | } |
| 265 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 266 | // Add one for self. |
| 267 | return count + suspended_count_modified_threads.size() + 1; |
| 268 | } |
| 269 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 270 | void ThreadList::SuspendAll() { |
| 271 | Thread* self = Thread::Current(); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 272 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 273 | |
| 274 | VLOG(threads) << *self << " SuspendAll starting..."; |
| 275 | |
| 276 | if (kIsDebugBuild) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 277 | Locks::mutator_lock_->AssertNotHeld(self); |
| 278 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 279 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 280 | CHECK_NE(self->GetState(), kRunnable); |
| 281 | } |
| 282 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 283 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 284 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 285 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 286 | // Update global suspend all state for attaching threads. |
| 287 | ++suspend_all_count_; |
| 288 | // Increment everybody's suspend count (except our own). |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 289 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 290 | if (thread == self) { |
| 291 | continue; |
| 292 | } |
| 293 | VLOG(threads) << "requesting thread suspend: " << *thread; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 294 | thread->ModifySuspendCount(self, +1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 299 | // Block on the mutator lock until all Runnable threads release their share of access. |
| 300 | #if HAVE_TIMED_RWLOCK |
| 301 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 302 | if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0))) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 303 | UnsafeLogFatalForThreadSuspendAllTimeout(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 304 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 305 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 306 | Locks::mutator_lock_->ExclusiveLock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 307 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 308 | |
| 309 | // Debug check that all threads are suspended. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 310 | AssertThreadsAreSuspended(self, self); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 311 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 312 | VLOG(threads) << *self << " SuspendAll complete"; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 315 | void ThreadList::ResumeAll() { |
| 316 | Thread* self = Thread::Current(); |
| 317 | |
| 318 | VLOG(threads) << *self << " ResumeAll starting"; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 319 | |
| 320 | // Debug check that all threads are suspended. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 321 | AssertThreadsAreSuspended(self, self); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 322 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 323 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 324 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 325 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 326 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 327 | // Update global suspend all state for attaching threads. |
| 328 | --suspend_all_count_; |
| 329 | // Decrement the suspend counts for all threads. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 330 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 331 | if (thread == self) { |
| 332 | continue; |
| 333 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 334 | thread->ModifySuspendCount(self, -1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // Broadcast a notification to all suspended threads, some or all of |
| 338 | // which may choose to wake up. No need to wait for them. |
| 339 | VLOG(threads) << *self << " ResumeAll waking others"; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 340 | Thread::resume_cond_->Broadcast(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 341 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 342 | VLOG(threads) << *self << " ResumeAll complete"; |
| 343 | } |
| 344 | |
| 345 | void ThreadList::Resume(Thread* thread, bool for_debugger) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 346 | Thread* self = Thread::Current(); |
| 347 | DCHECK_NE(thread, self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 348 | VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : ""); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 349 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 350 | { |
| 351 | // To check Contains. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 352 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 353 | // To check IsSuspended. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 354 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 355 | DCHECK(thread->IsSuspended()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 356 | if (!Contains(thread)) { |
| 357 | return; |
| 358 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 359 | thread->ModifySuspendCount(self, -1, for_debugger); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 363 | VLOG(threads) << "Resume(" << *thread << ") waking others"; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 364 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 365 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 368 | VLOG(threads) << "Resume(" << *thread << ") complete"; |
| 369 | } |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 370 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 371 | static void ThreadSuspendByPeerWarning(Thread* self, int level, const char* message, jobject peer) { |
| 372 | JNIEnvExt* env = self->GetJniEnv(); |
| 373 | ScopedLocalRef<jstring> |
| 374 | scoped_name_string(env, (jstring)env->GetObjectField(peer, |
| 375 | WellKnownClasses::java_lang_Thread_name)); |
| 376 | ScopedUtfChars scoped_name_chars(env, scoped_name_string.get()); |
| 377 | if (scoped_name_chars.c_str() == NULL) { |
| 378 | LOG(level) << message << ": " << peer; |
| 379 | env->ExceptionClear(); |
| 380 | } else { |
| 381 | LOG(level) << message << ": " << peer << ":" << scoped_name_chars.c_str(); |
| 382 | } |
| 383 | } |
| 384 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 385 | Thread* ThreadList::SuspendThreadByPeer(jobject peer, bool request_suspension, |
| 386 | bool debug_suspension, bool* timed_out) { |
| 387 | static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. |
| 388 | useconds_t total_delay_us = 0; |
| 389 | useconds_t delay_us = 0; |
| 390 | bool did_suspend_request = false; |
| 391 | *timed_out = false; |
| 392 | Thread* self = Thread::Current(); |
| 393 | while (true) { |
| 394 | Thread* thread; |
| 395 | { |
| 396 | ScopedObjectAccess soa(self); |
| 397 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 398 | thread = Thread::FromManagedThread(soa, peer); |
| 399 | if (thread == NULL) { |
| 400 | ThreadSuspendByPeerWarning(self, WARNING, "No such thread for suspend", peer); |
| 401 | return NULL; |
| 402 | } |
| 403 | { |
| 404 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
| 405 | if (request_suspension) { |
| 406 | thread->ModifySuspendCount(self, +1, debug_suspension); |
| 407 | request_suspension = false; |
| 408 | did_suspend_request = true; |
| 409 | } else { |
| 410 | // If the caller isn't requesting suspension, a suspension should have already occurred. |
| 411 | CHECK_GT(thread->GetSuspendCount(), 0); |
| 412 | } |
| 413 | // IsSuspended on the current thread will fail as the current thread is changed into |
| 414 | // Runnable above. As the suspend count is now raised if this is the current thread |
| 415 | // it will self suspend on transition to Runnable, making it hard to work with. It's simpler |
| 416 | // to just explicitly handle the current thread in the callers to this code. |
| 417 | CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger"; |
| 418 | // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend |
| 419 | // count, or else we've waited and it has self suspended) or is the current thread, we're |
| 420 | // done. |
| 421 | if (thread->IsSuspended()) { |
| 422 | return thread; |
| 423 | } |
| 424 | if (total_delay_us >= kTimeoutUs) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 425 | ThreadSuspendByPeerWarning(self, FATAL, "Thread suspension timed out", peer); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 426 | if (did_suspend_request) { |
| 427 | thread->ModifySuspendCount(soa.Self(), -1, debug_suspension); |
| 428 | } |
| 429 | *timed_out = true; |
| 430 | return NULL; |
| 431 | } |
| 432 | } |
| 433 | // Release locks and come out of runnable state. |
| 434 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 435 | ThreadSuspendSleep(self, &delay_us, &total_delay_us, false); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | static void ThreadSuspendByThreadIdWarning(int level, const char* message, uint32_t thread_id) { |
| 440 | LOG(level) << StringPrintf("%s: %d", message, thread_id); |
| 441 | } |
| 442 | |
| 443 | Thread* ThreadList::SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension, |
| 444 | bool* timed_out) { |
| 445 | static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. |
| 446 | useconds_t total_delay_us = 0; |
| 447 | useconds_t delay_us = 0; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 448 | *timed_out = false; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 449 | Thread* suspended_thread = nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 450 | Thread* self = Thread::Current(); |
| 451 | CHECK_NE(thread_id, kInvalidThreadId); |
| 452 | while (true) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 453 | { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 454 | Thread* thread = NULL; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 455 | ScopedObjectAccess soa(self); |
| 456 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 457 | for (const auto& it : list_) { |
| 458 | if (it->GetThreadId() == thread_id) { |
| 459 | thread = it; |
| 460 | break; |
| 461 | } |
| 462 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 463 | if (thread == nullptr) { |
| 464 | CHECK(suspended_thread == nullptr) << "Suspended thread " << suspended_thread |
| 465 | << " no longer in thread list"; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 466 | // There's a race in inflating a lock and the owner giving up ownership and then dying. |
| 467 | ThreadSuspendByThreadIdWarning(WARNING, "No such thread id for suspend", thread_id); |
| 468 | return NULL; |
| 469 | } |
| 470 | { |
| 471 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 472 | if (suspended_thread == nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 473 | thread->ModifySuspendCount(self, +1, debug_suspension); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 474 | suspended_thread = thread; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 475 | } else { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 476 | CHECK_EQ(suspended_thread, thread); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 477 | // If the caller isn't requesting suspension, a suspension should have already occurred. |
| 478 | CHECK_GT(thread->GetSuspendCount(), 0); |
| 479 | } |
| 480 | // IsSuspended on the current thread will fail as the current thread is changed into |
| 481 | // Runnable above. As the suspend count is now raised if this is the current thread |
| 482 | // it will self suspend on transition to Runnable, making it hard to work with. It's simpler |
| 483 | // to just explicitly handle the current thread in the callers to this code. |
| 484 | CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger"; |
| 485 | // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend |
| 486 | // count, or else we've waited and it has self suspended) or is the current thread, we're |
| 487 | // done. |
| 488 | if (thread->IsSuspended()) { |
| 489 | return thread; |
| 490 | } |
| 491 | if (total_delay_us >= kTimeoutUs) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 492 | ThreadSuspendByThreadIdWarning(WARNING, "Thread suspension timed out", thread_id); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 493 | if (suspended_thread != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 494 | thread->ModifySuspendCount(soa.Self(), -1, debug_suspension); |
| 495 | } |
| 496 | *timed_out = true; |
| 497 | return NULL; |
| 498 | } |
| 499 | } |
| 500 | // Release locks and come out of runnable state. |
| 501 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 502 | ThreadSuspendSleep(self, &delay_us, &total_delay_us, false); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | Thread* ThreadList::FindThreadByThreadId(uint32_t thin_lock_id) { |
| 507 | Thread* self = Thread::Current(); |
| 508 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 509 | for (const auto& thread : list_) { |
| 510 | if (thread->GetThreadId() == thin_lock_id) { |
| 511 | CHECK(thread == self || thread->IsSuspended()); |
| 512 | return thread; |
| 513 | } |
| 514 | } |
| 515 | return NULL; |
| 516 | } |
| 517 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 518 | void ThreadList::SuspendAllForDebugger() { |
| 519 | Thread* self = Thread::Current(); |
| 520 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 521 | |
| 522 | VLOG(threads) << *self << " SuspendAllForDebugger starting..."; |
| 523 | |
| 524 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 525 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 526 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 527 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 528 | // Update global suspend all state for attaching threads. |
| 529 | ++suspend_all_count_; |
| 530 | ++debug_suspend_all_count_; |
| 531 | // Increment everybody's suspend count (except our own). |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 532 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 533 | if (thread == self || thread == debug_thread) { |
| 534 | continue; |
| 535 | } |
| 536 | VLOG(threads) << "requesting thread suspend: " << *thread; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 537 | thread->ModifySuspendCount(self, +1, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 542 | // Block on the mutator lock until all Runnable threads release their share of access then |
| 543 | // immediately unlock again. |
| 544 | #if HAVE_TIMED_RWLOCK |
| 545 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 546 | if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 547 | UnsafeLogFatalForThreadSuspendAllTimeout(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 548 | } else { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 549 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 550 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 551 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 552 | Locks::mutator_lock_->ExclusiveLock(self); |
| 553 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 554 | #endif |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 555 | AssertThreadsAreSuspended(self, self, debug_thread); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 556 | |
| 557 | VLOG(threads) << *self << " SuspendAll complete"; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 560 | void ThreadList::SuspendSelfForDebugger() { |
| 561 | Thread* self = Thread::Current(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 562 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 563 | // The debugger thread must not suspend itself due to debugger activity! |
| 564 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 565 | CHECK(debug_thread != NULL); |
| 566 | CHECK(self != debug_thread); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 567 | CHECK_NE(self->GetState(), kRunnable); |
| 568 | Locks::mutator_lock_->AssertNotHeld(self); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 569 | |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 570 | { |
| 571 | // Collisions with other suspends aren't really interesting. We want |
| 572 | // to ensure that we're the only one fiddling with the suspend count |
| 573 | // though. |
| 574 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
| 575 | self->ModifySuspendCount(self, +1, true); |
| 576 | CHECK_GT(self->suspend_count_, 0); |
| 577 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 578 | |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 579 | VLOG(threads) << *self << " self-suspending (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 580 | |
Sebastien Hertz | 21e729c | 2014-02-18 14:16:00 +0100 | [diff] [blame] | 581 | // Tell JDWP we've completed invocation and are ready to suspend. |
| 582 | DebugInvokeReq* pReq = self->GetInvokeReq(); |
| 583 | DCHECK(pReq != NULL); |
| 584 | if (pReq->invoke_needed) { |
| 585 | // Clear this before signaling. |
| 586 | pReq->invoke_needed = false; |
| 587 | |
| 588 | VLOG(jdwp) << "invoke complete, signaling"; |
| 589 | MutexLock mu(self, pReq->lock); |
| 590 | pReq->cond.Signal(self); |
| 591 | } |
| 592 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 593 | // Tell JDWP that we've completed suspension. The JDWP thread can't |
| 594 | // tell us to resume before we're fully asleep because we hold the |
| 595 | // suspend count lock. |
| 596 | Dbg::ClearWaitForEventThread(); |
| 597 | |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 598 | { |
| 599 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
| 600 | while (self->suspend_count_ != 0) { |
| 601 | Thread::resume_cond_->Wait(self); |
| 602 | if (self->suspend_count_ != 0) { |
| 603 | // The condition was signaled but we're still suspended. This |
| 604 | // can happen if the debugger lets go while a SIGQUIT thread |
| 605 | // dump event is pending (assuming SignalCatcher was resumed for |
| 606 | // just long enough to try to grab the thread-suspend lock). |
| 607 | LOG(DEBUG) << *self << " still suspended after undo " |
| 608 | << "(suspend count=" << self->suspend_count_ << ")"; |
| 609 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 610 | } |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 611 | CHECK_EQ(self->suspend_count_, 0); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 612 | } |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 613 | |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 614 | VLOG(threads) << *self << " self-reviving (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 617 | void ThreadList::UndoDebuggerSuspensions() { |
| 618 | Thread* self = Thread::Current(); |
| 619 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 620 | VLOG(threads) << *self << " UndoDebuggerSuspensions starting"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 621 | |
| 622 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 623 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 624 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 625 | // Update global suspend all state for attaching threads. |
| 626 | suspend_all_count_ -= debug_suspend_all_count_; |
| 627 | debug_suspend_all_count_ = 0; |
| 628 | // Update running threads. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 629 | for (const auto& thread : list_) { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 630 | if (thread == self || thread->debug_suspend_count_ == 0) { |
| 631 | continue; |
| 632 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 633 | thread->ModifySuspendCount(self, -thread->debug_suspend_count_, true); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 638 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 639 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 642 | VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 645 | void ThreadList::WaitForOtherNonDaemonThreadsToExit() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 646 | Thread* self = Thread::Current(); |
| 647 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 648 | bool all_threads_are_daemons; |
| 649 | do { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 650 | { |
| 651 | // No more threads can be born after we start to shutdown. |
| 652 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 653 | CHECK(Runtime::Current()->IsShuttingDownLocked()); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 654 | CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U); |
| 655 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 656 | all_threads_are_daemons = true; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 657 | MutexLock mu(self, *Locks::thread_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 658 | for (const auto& thread : list_) { |
Anwar Ghuloum | 9754368 | 2013-06-14 12:58:16 -0700 | [diff] [blame] | 659 | if (thread != self && !thread->IsDaemon()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 660 | all_threads_are_daemons = false; |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | if (!all_threads_are_daemons) { |
| 665 | // Wait for another thread to exit before re-checking. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 666 | thread_exit_cond_.Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 667 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 668 | } while (!all_threads_are_daemons); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | void ThreadList::SuspendAllDaemonThreads() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 672 | Thread* self = Thread::Current(); |
| 673 | MutexLock mu(self, *Locks::thread_list_lock_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 674 | { // Tell all the daemons it's time to suspend. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 675 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 676 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 677 | // This is only run after all non-daemon threads have exited, so the remainder should all be |
| 678 | // daemons. |
Ian Rogers | 7e76286 | 2012-10-22 15:45:08 -0700 | [diff] [blame] | 679 | CHECK(thread->IsDaemon()) << *thread; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 680 | if (thread != self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 681 | thread->ModifySuspendCount(self, +1, false); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 682 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 685 | // Give the threads a chance to suspend, complaining if they're slow. |
| 686 | bool have_complained = false; |
| 687 | for (int i = 0; i < 10; ++i) { |
| 688 | usleep(200 * 1000); |
| 689 | bool all_suspended = true; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 690 | for (const auto& thread : list_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 691 | if (thread != self && thread->GetState() == kRunnable) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 692 | if (!have_complained) { |
| 693 | LOG(WARNING) << "daemon thread not yet suspended: " << *thread; |
| 694 | have_complained = true; |
| 695 | } |
| 696 | all_suspended = false; |
| 697 | } |
| 698 | } |
| 699 | if (all_suspended) { |
| 700 | return; |
| 701 | } |
| 702 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 703 | LOG(ERROR) << "suspend all daemons failed"; |
| 704 | } |
| 705 | void ThreadList::Register(Thread* self) { |
| 706 | DCHECK_EQ(self, Thread::Current()); |
| 707 | |
| 708 | if (VLOG_IS_ON(threads)) { |
| 709 | std::ostringstream oss; |
| 710 | self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump. |
| 711 | LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss; |
| 712 | } |
| 713 | |
| 714 | // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing |
| 715 | // SuspendAll requests. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 716 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 717 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 718 | self->suspend_count_ = suspend_all_count_; |
| 719 | self->debug_suspend_count_ = debug_suspend_all_count_; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 720 | if (self->suspend_count_ > 0) { |
| 721 | self->AtomicSetFlag(kSuspendRequest); |
| 722 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 723 | CHECK(!Contains(self)); |
| 724 | list_.push_back(self); |
| 725 | } |
| 726 | |
| 727 | void ThreadList::Unregister(Thread* self) { |
| 728 | DCHECK_EQ(self, Thread::Current()); |
| 729 | |
| 730 | VLOG(threads) << "ThreadList::Unregister() " << *self; |
| 731 | |
| 732 | // Any time-consuming destruction, plus anything that can call back into managed code or |
| 733 | // suspend and so on, must happen at this point, and not in ~Thread. |
| 734 | self->Destroy(); |
| 735 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 736 | uint32_t thin_lock_id = self->thin_lock_thread_id_; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 737 | while (self != nullptr) { |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 738 | // Remove and delete the Thread* while holding the thread_list_lock_ and |
| 739 | // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended. |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 740 | // Note: deliberately not using MutexLock that could hold a stale self pointer. |
| 741 | Locks::thread_list_lock_->ExclusiveLock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 742 | CHECK(Contains(self)); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 743 | // Note: we don't take the thread_suspend_count_lock_ here as to be suspending a thread other |
| 744 | // than yourself you need to hold the thread_list_lock_ (see Thread::ModifySuspendCount). |
| 745 | if (!self->IsSuspended()) { |
| 746 | list_.remove(self); |
| 747 | delete self; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 748 | self = nullptr; |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 749 | } |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 750 | Locks::thread_list_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 751 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 752 | // Release the thread ID after the thread is finished and deleted to avoid cases where we can |
| 753 | // temporarily have multiple threads with the same thread id. When this occurs, it causes |
| 754 | // problems in FindThreadByThreadId / SuspendThreadByThreadId. |
| 755 | ReleaseThreadId(nullptr, thin_lock_id); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 756 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 757 | // Clear the TLS data, so that the underlying native thread is recognizably detached. |
| 758 | // (It may wish to reattach later.) |
| 759 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self"); |
| 760 | |
| 761 | // Signal that a thread just detached. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 762 | MutexLock mu(NULL, *Locks::thread_list_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 763 | thread_exit_cond_.Signal(NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 767 | for (const auto& thread : list_) { |
| 768 | callback(thread, context); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 772 | void ThreadList::VisitRoots(RootCallback* callback, void* arg) const { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 773 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 774 | for (const auto& thread : list_) { |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 775 | thread->VisitRoots(callback, arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 776 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 779 | class VerifyRootWrapperArg { |
| 780 | public: |
| 781 | VerifyRootWrapperArg(VerifyRootCallback* callback, void* arg) : callback_(callback), arg_(arg) { |
| 782 | } |
| 783 | VerifyRootCallback* const callback_; |
| 784 | void* const arg_; |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 785 | }; |
| 786 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 787 | static void VerifyRootWrapperCallback(mirror::Object** root, void* arg, uint32_t /*thread_id*/, |
| 788 | RootType /*root_type*/) { |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 789 | VerifyRootWrapperArg* wrapperArg = reinterpret_cast<VerifyRootWrapperArg*>(arg); |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 790 | wrapperArg->callback_(*root, wrapperArg->arg_, 0, NULL); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 793 | void ThreadList::VerifyRoots(VerifyRootCallback* callback, void* arg) const { |
| 794 | VerifyRootWrapperArg wrapper(callback, arg); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 795 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 796 | for (const auto& thread : list_) { |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 797 | thread->VisitRoots(VerifyRootWrapperCallback, &wrapper); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 801 | uint32_t ThreadList::AllocThreadId(Thread* self) { |
| 802 | MutexLock mu(self, allocated_ids_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 803 | for (size_t i = 0; i < allocated_ids_.size(); ++i) { |
| 804 | if (!allocated_ids_[i]) { |
| 805 | allocated_ids_.set(i); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 806 | return i + 1; // Zero is reserved to mean "invalid". |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | LOG(FATAL) << "Out of internal thread ids"; |
| 810 | return 0; |
| 811 | } |
| 812 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 813 | void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) { |
| 814 | MutexLock mu(self, allocated_ids_lock_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 815 | --id; // Zero is reserved to mean "invalid". |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 816 | DCHECK(allocated_ids_[id]) << id; |
| 817 | allocated_ids_.reset(id); |
| 818 | } |
| 819 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 820 | } // namespace art |