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