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