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