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> |
| 20 | #include <sys/types.h> |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 23 | #include "debugger.h" |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 24 | #include "mutex.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 25 | #include "timing_logger.h" |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 26 | #include "utils.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 30 | ThreadList::ThreadList() |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 31 | : allocated_ids_lock_("allocated thread ids lock"), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 32 | suspend_all_count_(0), debug_suspend_all_count_(0), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 33 | thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | ThreadList::~ThreadList() { |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 37 | // 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] | 38 | // We need to detach the current thread here in case there's another thread waiting to join with |
| 39 | // us. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 40 | if (Contains(Thread::Current())) { |
| 41 | Runtime::Current()->DetachCurrentThread(); |
| 42 | } |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 43 | |
| 44 | WaitForOtherNonDaemonThreadsToExit(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 45 | // TODO: there's an unaddressed race here where a thread may attach during shutdown, see |
| 46 | // Thread::Init. |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 47 | SuspendAllDaemonThreads(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | bool ThreadList::Contains(Thread* thread) { |
| 51 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 52 | } |
| 53 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 54 | bool ThreadList::Contains(pid_t tid) { |
| 55 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 56 | if ((*it)->tid_ == tid) { |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 63 | pid_t ThreadList::GetLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 64 | return Locks::thread_list_lock_->GetExclusiveOwnerTid(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 67 | void ThreadList::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 68 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 69 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 70 | DumpLocked(os); |
| 71 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 72 | DumpUnattachedThreads(os); |
| 73 | } |
| 74 | |
| 75 | static void DumpUnattachedThread(std::ostream& os, pid_t tid) { |
| 76 | Thread::DumpState(os, NULL, tid); |
| 77 | DumpKernelStack(os, tid, " kernel: ", false); |
Brian Carlstrom | ed8b723 | 2012-06-27 17:54:47 -0700 | [diff] [blame] | 78 | // TODO: Reenable this when the native code in system_server can handle it. |
| 79 | // Currently "adb shell kill -3 `pid system_server`" will cause it to exit. |
| 80 | if (false) { |
| 81 | DumpNativeStack(os, tid, " native: ", false); |
| 82 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 83 | os << "\n"; |
| 84 | } |
| 85 | |
| 86 | void ThreadList::DumpUnattachedThreads(std::ostream& os) { |
| 87 | DIR* d = opendir("/proc/self/task"); |
| 88 | if (!d) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | dirent de; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 93 | dirent* e; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 94 | Thread* self = Thread::Current(); |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 95 | while (!readdir_r(d, &de, &e) && e != NULL) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 96 | char* end; |
| 97 | pid_t tid = strtol(de.d_name, &end, 10); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 98 | if (!*end) { |
| 99 | bool contains; |
| 100 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 101 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 102 | contains = Contains(tid); |
| 103 | } |
| 104 | if (!contains) { |
| 105 | DumpUnattachedThread(os, tid); |
| 106 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | closedir(d); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void ThreadList::DumpLocked(std::ostream& os) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 113 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 114 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 115 | (*it)->Dump(os); |
| 116 | os << "\n"; |
| 117 | } |
| 118 | } |
| 119 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 120 | void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) { |
| 121 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 122 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 123 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 124 | Thread* thread = *it; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 125 | if (thread != ignore1 || thread == ignore2) { |
| 126 | CHECK(thread->IsSuspended()) |
| 127 | << "\nUnsuspended thread: <<" << *thread << "\n" |
| 128 | << "self: <<" << *Thread::Current(); |
| 129 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 130 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 133 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 134 | // 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] | 135 | static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 136 | Runtime* runtime = Runtime::Current(); |
| 137 | std::ostringstream ss; |
| 138 | ss << "Thread suspend timeout\n"; |
| 139 | runtime->DumpLockHolders(ss); |
| 140 | ss << "\n"; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 141 | Locks::mutator_lock_->SharedTryLock(self); |
| 142 | if (!Locks::mutator_lock_->IsSharedHeld(self)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 143 | LOG(WARNING) << "Dumping thread list without holding mutator_lock_"; |
| 144 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 145 | Locks::thread_list_lock_->TryLock(self); |
| 146 | if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 147 | LOG(WARNING) << "Dumping thread list without holding thread_list_lock_"; |
| 148 | } |
| 149 | runtime->GetThreadList()->DumpLocked(ss); |
| 150 | LOG(FATAL) << ss.str(); |
| 151 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 152 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 153 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 154 | size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 155 | Thread* self = Thread::Current(); |
| 156 | if (kIsDebugBuild) { |
| 157 | Locks::mutator_lock_->AssertNotHeld(self); |
| 158 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 159 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
| 160 | CHECK_NE(self->GetState(), kRunnable); |
| 161 | } |
| 162 | |
| 163 | std::vector<Thread*> suspended_count_modified_threads; |
| 164 | size_t count = 0; |
| 165 | { |
| 166 | // Call a checkpoint function for each thread, threads which are suspend get their checkpoint |
| 167 | // manually called. |
| 168 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 169 | // TODO: C++0x auto. |
| 170 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 171 | Thread* thread = *it; |
| 172 | if (thread != self) { |
| 173 | for (;;) { |
| 174 | if (thread->RequestCheckpoint(checkpoint_function)) { |
| 175 | // This thread will run it's checkpoint some time in the near future. |
| 176 | count++; |
| 177 | break; |
| 178 | } else { |
| 179 | // We are probably suspended, try to make sure that we stay suspended. |
| 180 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 181 | // The thread switched back to runnable. |
| 182 | if (thread->GetState() == kRunnable) { |
| 183 | continue; |
| 184 | } |
| 185 | thread->ModifySuspendCount(self, +1, false); |
| 186 | suspended_count_modified_threads.push_back(thread); |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Run the checkpoint on ourself while we wait for threads to suspend. |
| 195 | checkpoint_function->Run(self); |
| 196 | |
| 197 | // Run the checkpoint on the suspended threads. |
| 198 | for (size_t i = 0; i < suspended_count_modified_threads.size(); ++i) { |
| 199 | Thread* thread = suspended_count_modified_threads[i]; |
| 200 | if (!thread->IsSuspended()) { |
| 201 | // Wait until the thread is suspended. |
| 202 | uint64_t start = NanoTime(); |
| 203 | do { |
| 204 | // Sleep for 100us. |
| 205 | usleep(100); |
| 206 | } while (!thread->IsSuspended()); |
| 207 | uint64_t end = NanoTime(); |
| 208 | // Shouldn't need to wait for longer than 1 millisecond. |
| 209 | const uint64_t threshold = 1; |
| 210 | if (NsToMs(end - start) > threshold) { |
| 211 | LOG(INFO) << "Warning: waited longer than " << threshold << " ms for thrad suspend" |
| 212 | << std::endl; |
| 213 | } |
| 214 | } |
| 215 | // We know for sure that the thread is suspended at this point. |
| 216 | thread->RunCheckpointFunction(); |
| 217 | { |
| 218 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 219 | thread->ModifySuspendCount(self, -1, false); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Add one for self. |
| 224 | return count + suspended_count_modified_threads.size() + 1; |
| 225 | } |
| 226 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 227 | void ThreadList::SuspendAll() { |
| 228 | Thread* self = Thread::Current(); |
| 229 | |
| 230 | VLOG(threads) << *self << " SuspendAll starting..."; |
| 231 | |
| 232 | if (kIsDebugBuild) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 233 | Locks::mutator_lock_->AssertNotHeld(self); |
| 234 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 235 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 236 | CHECK_NE(self->GetState(), kRunnable); |
| 237 | } |
| 238 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 239 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 240 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 241 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 242 | // Update global suspend all state for attaching threads. |
| 243 | ++suspend_all_count_; |
| 244 | // Increment everybody's suspend count (except our own). |
| 245 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 246 | Thread* thread = *it; |
| 247 | if (thread == self) { |
| 248 | continue; |
| 249 | } |
| 250 | VLOG(threads) << "requesting thread suspend: " << *thread; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 251 | thread->ModifySuspendCount(self, +1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 256 | // Block on the mutator lock until all Runnable threads release their share of access. |
| 257 | #if HAVE_TIMED_RWLOCK |
| 258 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 259 | if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0))) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 260 | UnsafeLogFatalForThreadSuspendAllTimeout(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 261 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 262 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 263 | Locks::mutator_lock_->ExclusiveLock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 264 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 265 | |
| 266 | // Debug check that all threads are suspended. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 267 | AssertThreadsAreSuspended(self, self); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 268 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 269 | VLOG(threads) << *self << " SuspendAll complete"; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 272 | void ThreadList::ResumeAll() { |
| 273 | Thread* self = Thread::Current(); |
| 274 | |
| 275 | VLOG(threads) << *self << " ResumeAll starting"; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 276 | |
| 277 | // Debug check that all threads are suspended. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 278 | AssertThreadsAreSuspended(self, self); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 279 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 280 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 281 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 282 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 283 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 284 | // Update global suspend all state for attaching threads. |
| 285 | --suspend_all_count_; |
| 286 | // Decrement the suspend counts for all threads. |
| 287 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 288 | Thread* thread = *it; |
| 289 | if (thread == self) { |
| 290 | continue; |
| 291 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 292 | thread->ModifySuspendCount(self, -1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Broadcast a notification to all suspended threads, some or all of |
| 296 | // which may choose to wake up. No need to wait for them. |
| 297 | VLOG(threads) << *self << " ResumeAll waking others"; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 298 | Thread::resume_cond_->Broadcast(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 299 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 300 | VLOG(threads) << *self << " ResumeAll complete"; |
| 301 | } |
| 302 | |
| 303 | void ThreadList::Resume(Thread* thread, bool for_debugger) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 304 | Thread* self = Thread::Current(); |
| 305 | DCHECK_NE(thread, self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 306 | VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : ""); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 307 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 308 | { |
| 309 | // To check Contains. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 310 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 311 | // To check IsSuspended. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 312 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 313 | DCHECK(thread->IsSuspended()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 314 | if (!Contains(thread)) { |
| 315 | return; |
| 316 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 317 | thread->ModifySuspendCount(self, -1, for_debugger); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 321 | VLOG(threads) << "Resume(" << *thread << ") waking others"; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 322 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 323 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 326 | VLOG(threads) << "Resume(" << *thread << ") complete"; |
| 327 | } |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 328 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 329 | void ThreadList::SuspendAllForDebugger() { |
| 330 | Thread* self = Thread::Current(); |
| 331 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 332 | |
| 333 | VLOG(threads) << *self << " SuspendAllForDebugger starting..."; |
| 334 | |
| 335 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 336 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 337 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 338 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 339 | // Update global suspend all state for attaching threads. |
| 340 | ++suspend_all_count_; |
| 341 | ++debug_suspend_all_count_; |
| 342 | // Increment everybody's suspend count (except our own). |
| 343 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 344 | Thread* thread = *it; |
| 345 | if (thread == self || thread == debug_thread) { |
| 346 | continue; |
| 347 | } |
| 348 | VLOG(threads) << "requesting thread suspend: " << *thread; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 349 | thread->ModifySuspendCount(self, +1, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 354 | // Block on the mutator lock until all Runnable threads release their share of access then |
| 355 | // immediately unlock again. |
| 356 | #if HAVE_TIMED_RWLOCK |
| 357 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 358 | if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 359 | UnsafeLogFatalForThreadSuspendAllTimeout(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 360 | } else { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 361 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 362 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 363 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 364 | Locks::mutator_lock_->ExclusiveLock(self); |
| 365 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 366 | #endif |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 367 | AssertThreadsAreSuspended(self, self, debug_thread); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 368 | |
| 369 | VLOG(threads) << *self << " SuspendAll complete"; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 372 | void ThreadList::SuspendSelfForDebugger() { |
| 373 | Thread* self = Thread::Current(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 374 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 375 | // The debugger thread must not suspend itself due to debugger activity! |
| 376 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 377 | CHECK(debug_thread != NULL); |
| 378 | CHECK(self != debug_thread); |
| 379 | |
| 380 | // Collisions with other suspends aren't really interesting. We want |
| 381 | // to ensure that we're the only one fiddling with the suspend count |
| 382 | // though. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 383 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 384 | self->ModifySuspendCount(self, +1, true); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 385 | |
| 386 | // Suspend ourselves. |
| 387 | CHECK_GT(self->suspend_count_, 0); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 388 | self->SetState(kSuspended); |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 389 | VLOG(threads) << *self << " self-suspending (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 390 | |
| 391 | // Tell JDWP that we've completed suspension. The JDWP thread can't |
| 392 | // tell us to resume before we're fully asleep because we hold the |
| 393 | // suspend count lock. |
| 394 | Dbg::ClearWaitForEventThread(); |
| 395 | |
| 396 | while (self->suspend_count_ != 0) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 397 | Thread::resume_cond_->Wait(self); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 398 | if (self->suspend_count_ != 0) { |
| 399 | // The condition was signaled but we're still suspended. This |
| 400 | // can happen if the debugger lets go while a SIGQUIT thread |
| 401 | // dump event is pending (assuming SignalCatcher was resumed for |
| 402 | // just long enough to try to grab the thread-suspend lock). |
| 403 | LOG(DEBUG) << *self << " still suspended after undo " |
| 404 | << "(suspend count=" << self->suspend_count_ << ")"; |
| 405 | } |
| 406 | } |
| 407 | CHECK_EQ(self->suspend_count_, 0); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 408 | self->SetState(kRunnable); |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 409 | VLOG(threads) << *self << " self-reviving (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 412 | void ThreadList::UndoDebuggerSuspensions() { |
| 413 | Thread* self = Thread::Current(); |
| 414 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 415 | VLOG(threads) << *self << " UndoDebuggerSuspensions starting"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 416 | |
| 417 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 418 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 419 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 420 | // Update global suspend all state for attaching threads. |
| 421 | suspend_all_count_ -= debug_suspend_all_count_; |
| 422 | debug_suspend_all_count_ = 0; |
| 423 | // Update running threads. |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 424 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 425 | Thread* thread = *it; |
| 426 | if (thread == self || thread->debug_suspend_count_ == 0) { |
| 427 | continue; |
| 428 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 429 | thread->ModifySuspendCount(self, -thread->debug_suspend_count_, true); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 434 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 435 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 438 | VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 441 | void ThreadList::WaitForOtherNonDaemonThreadsToExit() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 442 | Thread* self = Thread::Current(); |
| 443 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 444 | bool all_threads_are_daemons; |
| 445 | do { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 446 | { |
| 447 | // No more threads can be born after we start to shutdown. |
| 448 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 449 | CHECK(Runtime::Current()->IsShuttingDown()); |
| 450 | CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U); |
| 451 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 452 | all_threads_are_daemons = true; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 453 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 454 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 455 | // TODO: there's a race here with thread exit that's being worked around by checking if the |
| 456 | // thread has a peer. |
| 457 | Thread* thread = *it; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 458 | if (thread != self && thread->HasPeer() && !thread->IsDaemon()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 459 | all_threads_are_daemons = false; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | if (!all_threads_are_daemons) { |
| 464 | // Wait for another thread to exit before re-checking. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 465 | thread_exit_cond_.Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 466 | } |
| 467 | } while(!all_threads_are_daemons); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void ThreadList::SuspendAllDaemonThreads() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 471 | Thread* self = Thread::Current(); |
| 472 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 473 | { // Tell all the daemons it's time to suspend. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 474 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 475 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 476 | Thread* thread = *it; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 477 | // This is only run after all non-daemon threads have exited, so the remainder should all be |
| 478 | // daemons. |
Ian Rogers | 7e76286 | 2012-10-22 15:45:08 -0700 | [diff] [blame] | 479 | CHECK(thread->IsDaemon()) << *thread; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 480 | if (thread != self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 481 | thread->ModifySuspendCount(self, +1, false); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 482 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 485 | // Give the threads a chance to suspend, complaining if they're slow. |
| 486 | bool have_complained = false; |
| 487 | for (int i = 0; i < 10; ++i) { |
| 488 | usleep(200 * 1000); |
| 489 | bool all_suspended = true; |
| 490 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 491 | Thread* thread = *it; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 492 | if (thread != self && thread->GetState() == kRunnable) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 493 | if (!have_complained) { |
| 494 | LOG(WARNING) << "daemon thread not yet suspended: " << *thread; |
| 495 | have_complained = true; |
| 496 | } |
| 497 | all_suspended = false; |
| 498 | } |
| 499 | } |
| 500 | if (all_suspended) { |
| 501 | return; |
| 502 | } |
| 503 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 504 | LOG(ERROR) << "suspend all daemons failed"; |
| 505 | } |
| 506 | void ThreadList::Register(Thread* self) { |
| 507 | DCHECK_EQ(self, Thread::Current()); |
| 508 | |
| 509 | if (VLOG_IS_ON(threads)) { |
| 510 | std::ostringstream oss; |
| 511 | self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump. |
| 512 | LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss; |
| 513 | } |
| 514 | |
| 515 | // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing |
| 516 | // SuspendAll requests. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 517 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 518 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 519 | self->suspend_count_ = suspend_all_count_; |
| 520 | self->debug_suspend_count_ = debug_suspend_all_count_; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 521 | if (self->suspend_count_ > 0) { |
| 522 | self->AtomicSetFlag(kSuspendRequest); |
| 523 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 524 | CHECK(!Contains(self)); |
| 525 | list_.push_back(self); |
| 526 | } |
| 527 | |
| 528 | void ThreadList::Unregister(Thread* self) { |
| 529 | DCHECK_EQ(self, Thread::Current()); |
| 530 | |
| 531 | VLOG(threads) << "ThreadList::Unregister() " << *self; |
| 532 | |
| 533 | // Any time-consuming destruction, plus anything that can call back into managed code or |
| 534 | // suspend and so on, must happen at this point, and not in ~Thread. |
| 535 | self->Destroy(); |
| 536 | |
| 537 | { |
| 538 | // Remove this thread from the list. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 539 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 540 | CHECK(Contains(self)); |
| 541 | list_.remove(self); |
| 542 | } |
| 543 | |
| 544 | // Delete the Thread* and release the thin lock id. |
| 545 | uint32_t thin_lock_id = self->thin_lock_id_; |
| 546 | ReleaseThreadId(thin_lock_id); |
| 547 | delete self; |
| 548 | |
| 549 | // Clear the TLS data, so that the underlying native thread is recognizably detached. |
| 550 | // (It may wish to reattach later.) |
| 551 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self"); |
| 552 | |
| 553 | // Signal that a thread just detached. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 554 | MutexLock mu(NULL, *Locks::thread_list_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 555 | thread_exit_cond_.Signal(NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) { |
| 559 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 560 | callback(*it, context); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 565 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 566 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 567 | (*it)->VisitRoots(visitor, arg); |
| 568 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 571 | void ThreadList::VerifyRoots(Heap::VerifyRootVisitor* visitor, void* arg) const { |
| 572 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 573 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 574 | (*it)->VerifyRoots(visitor, arg); |
| 575 | } |
| 576 | } |
| 577 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 578 | uint32_t ThreadList::AllocThreadId() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 579 | MutexLock mu(Thread::Current(), allocated_ids_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 580 | for (size_t i = 0; i < allocated_ids_.size(); ++i) { |
| 581 | if (!allocated_ids_[i]) { |
| 582 | allocated_ids_.set(i); |
| 583 | return i + 1; // Zero is reserved to mean "invalid". |
| 584 | } |
| 585 | } |
| 586 | LOG(FATAL) << "Out of internal thread ids"; |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | void ThreadList::ReleaseThreadId(uint32_t id) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 591 | MutexLock mu(Thread::Current(), allocated_ids_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 592 | --id; // Zero is reserved to mean "invalid". |
| 593 | DCHECK(allocated_ids_[id]) << id; |
| 594 | allocated_ids_.reset(id); |
| 595 | } |
| 596 | |
| 597 | } // namespace art |