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" |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 24 | #include "scoped_heap_lock.h" |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 25 | #include "scoped_thread_list_lock.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 26 | #include "timing_logger.h" |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 27 | #include "utils.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 31 | ThreadList::ThreadList() |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 32 | : allocated_ids_lock_("allocated thread ids lock"), |
| 33 | thread_list_lock_("thread list lock", kThreadListLock), |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 34 | thread_start_cond_("thread start condition variable"), |
| 35 | thread_exit_cond_("thread exit condition variable"), |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 36 | thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock), |
Elliott Hughes | e62934d | 2012-04-09 11:24:29 -0700 | [diff] [blame] | 37 | thread_suspend_count_cond_("thread suspend count condition variable") { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | ThreadList::~ThreadList() { |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 41 | // 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] | 42 | // We need to detach the current thread here in case there's another thread waiting to join with |
| 43 | // us. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 44 | if (Contains(Thread::Current())) { |
| 45 | Runtime::Current()->DetachCurrentThread(); |
| 46 | } |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 47 | |
| 48 | WaitForOtherNonDaemonThreadsToExit(); |
| 49 | SuspendAllDaemonThreads(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool ThreadList::Contains(Thread* thread) { |
| 53 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 54 | } |
| 55 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 56 | bool ThreadList::Contains(pid_t tid) { |
| 57 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 58 | if ((*it)->tid_ == tid) { |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 65 | pid_t ThreadList::GetLockOwner() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 66 | return thread_list_lock_.GetOwner(); |
| 67 | } |
| 68 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 69 | void ThreadList::DumpForSigQuit(std::ostream& os) { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 70 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 71 | DumpLocked(os); |
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); |
| 97 | if (!*end && !Contains(tid)) { |
| 98 | DumpUnattachedThread(os, tid); |
| 99 | } |
| 100 | } |
| 101 | closedir(d); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void ThreadList::DumpLocked(std::ostream& os) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 105 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 106 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 107 | (*it)->Dump(os); |
| 108 | os << "\n"; |
| 109 | } |
| 110 | } |
| 111 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 112 | void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) { |
| 113 | #ifndef NDEBUG |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 114 | DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_) |
| 115 | << delta << " " << thread->debug_suspend_count_ << " " << *thread; |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 116 | DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 117 | #endif |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 118 | if (delta == -1 && thread->suspend_count_ <= 0) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 119 | // This is expected if you attach a thread during a GC. |
Ian Rogers | d237a38 | 2012-06-01 08:53:29 -0700 | [diff] [blame] | 120 | if (UNLIKELY(!thread->IsStillStarting())) { |
| 121 | std::ostringstream ss; |
| 122 | Runtime::Current()->GetThreadList()->DumpLocked(ss); |
| 123 | LOG(FATAL) << *thread << " suspend count already zero.\n" << ss.str(); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 124 | } |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 125 | return; |
| 126 | } |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 127 | thread->suspend_count_ += delta; |
| 128 | if (for_debugger) { |
| 129 | thread->debug_suspend_count_ += delta; |
| 130 | } |
| 131 | } |
| 132 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 133 | void ThreadList::FullSuspendCheck(Thread* thread) { |
| 134 | CHECK(thread != NULL); |
| 135 | CHECK_GE(thread->suspend_count_, 0); |
| 136 | |
| 137 | MutexLock mu(thread_suspend_count_lock_); |
| 138 | if (thread->suspend_count_ == 0) { |
| 139 | return; |
| 140 | } |
| 141 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 142 | VLOG(threads) << *thread << " self-suspending"; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 143 | { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 144 | ScopedThreadStateChange tsc(thread, kSuspended); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 145 | while (thread->suspend_count_ != 0) { |
| 146 | /* |
| 147 | * Wait for wakeup signal, releasing lock. The act of releasing |
| 148 | * and re-acquiring the lock provides the memory barriers we |
| 149 | * need for correct behavior on SMP. |
| 150 | */ |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 151 | thread_suspend_count_cond_.Wait(thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 152 | } |
| 153 | CHECK_EQ(thread->suspend_count_, 0); |
| 154 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 155 | VLOG(threads) << *thread << " self-reviving"; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 158 | void ThreadList::SuspendAll(bool for_debugger) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 159 | Thread* self = Thread::Current(); |
| 160 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 161 | VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : ""); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 162 | |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 163 | CHECK_EQ(self->GetState(), kRunnable); |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 164 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 165 | Thread* debug_thread = Dbg::GetDebugThread(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 166 | { |
| 167 | // Increment everybody's suspend count (except our own). |
| 168 | MutexLock mu(thread_suspend_count_lock_); |
| 169 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 170 | Thread* thread = *it; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 171 | if (thread == self || (for_debugger && thread == debug_thread)) { |
| 172 | continue; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 173 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 174 | VLOG(threads) << "requesting thread suspend: " << *thread; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 175 | ModifySuspendCount(thread, +1, for_debugger); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Wait for everybody in kRunnable state to stop. Other states |
| 181 | * indicate the code is either running natively or sleeping quietly. |
| 182 | * Any attempt to transition back to kRunnable will cause a check |
| 183 | * for suspension, so it should be impossible for anything to execute |
| 184 | * interpreted code or modify objects (assuming native code plays nicely). |
| 185 | * |
| 186 | * It's also okay if the thread transitions to a non-kRunnable state. |
| 187 | * |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 188 | * Note we released the thread_suspend_count_lock_ before getting here, |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 189 | * so if another thread is fiddling with its suspend count (perhaps |
| 190 | * self-suspending for the debugger) it won't block while we're waiting |
| 191 | * in here. |
| 192 | */ |
| 193 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 194 | Thread* thread = *it; |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 195 | if (thread == self || (for_debugger && thread == debug_thread)) { |
| 196 | continue; |
| 197 | } |
| 198 | thread->WaitUntilSuspended(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 199 | VLOG(threads) << "thread suspended: " << *thread; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 202 | VLOG(threads) << *self << " SuspendAll complete"; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 205 | void ThreadList::Suspend(Thread* thread, bool for_debugger) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 206 | DCHECK(thread != Thread::Current()); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 207 | thread_list_lock_.AssertHeld(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 208 | |
| 209 | // TODO: add another thread_suspend_lock_ to avoid GC/debugger races. |
| 210 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 211 | VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : ""); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 212 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 213 | if (!Contains(thread)) { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | { |
| 218 | MutexLock mu(thread_suspend_count_lock_); |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 219 | ModifySuspendCount(thread, +1, for_debugger); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | thread->WaitUntilSuspended(); |
| 223 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 224 | VLOG(threads) << "Suspend(" << *thread << ") complete"; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 227 | void ThreadList::SuspendSelfForDebugger() { |
| 228 | Thread* self = Thread::Current(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 229 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 230 | // The debugger thread must not suspend itself due to debugger activity! |
| 231 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 232 | CHECK(debug_thread != NULL); |
| 233 | CHECK(self != debug_thread); |
| 234 | |
| 235 | // Collisions with other suspends aren't really interesting. We want |
| 236 | // to ensure that we're the only one fiddling with the suspend count |
| 237 | // though. |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 238 | MutexLock mu(thread_suspend_count_lock_); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 239 | ModifySuspendCount(self, +1, true); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 240 | |
| 241 | // Suspend ourselves. |
| 242 | CHECK_GT(self->suspend_count_, 0); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 243 | self->SetState(kSuspended); |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 244 | VLOG(threads) << *self << " self-suspending (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 245 | |
| 246 | // Tell JDWP that we've completed suspension. The JDWP thread can't |
| 247 | // tell us to resume before we're fully asleep because we hold the |
| 248 | // suspend count lock. |
| 249 | Dbg::ClearWaitForEventThread(); |
| 250 | |
| 251 | while (self->suspend_count_ != 0) { |
| 252 | thread_suspend_count_cond_.Wait(thread_suspend_count_lock_); |
| 253 | if (self->suspend_count_ != 0) { |
| 254 | // The condition was signaled but we're still suspended. This |
| 255 | // can happen if the debugger lets go while a SIGQUIT thread |
| 256 | // dump event is pending (assuming SignalCatcher was resumed for |
| 257 | // just long enough to try to grab the thread-suspend lock). |
| 258 | LOG(DEBUG) << *self << " still suspended after undo " |
| 259 | << "(suspend count=" << self->suspend_count_ << ")"; |
| 260 | } |
| 261 | } |
| 262 | CHECK_EQ(self->suspend_count_, 0); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 263 | self->SetState(kRunnable); |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 264 | VLOG(threads) << *self << " self-reviving (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void ThreadList::ResumeAll(bool for_debugger) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 268 | Thread* self = Thread::Current(); |
| 269 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 270 | VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : ""); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 271 | |
| 272 | // Decrement the suspend counts for all threads. No need for atomic |
| 273 | // writes, since nobody should be moving until we decrement the count. |
| 274 | // We do need to hold the thread list because of JNI attaches. |
| 275 | { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 276 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 277 | Thread* debug_thread = Dbg::GetDebugThread(); |
Brian Carlstrom | 4f20aef | 2011-10-21 00:16:18 -0700 | [diff] [blame] | 278 | MutexLock mu(thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 279 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 280 | Thread* thread = *it; |
Elliott Hughes | c61a267 | 2012-06-21 14:52:29 -0700 | [diff] [blame] | 281 | if (thread == self || (for_debugger && thread == debug_thread)) { |
Elliott Hughes | f6a1e1e | 2011-10-25 16:28:04 -0700 | [diff] [blame] | 282 | continue; |
| 283 | } |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 284 | ModifySuspendCount(thread, -1, for_debugger); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | // Broadcast a notification to all suspended threads, some or all of |
| 289 | // which may choose to wake up. No need to wait for them. |
| 290 | { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 291 | VLOG(threads) << *self << " ResumeAll waking others"; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 292 | MutexLock mu(thread_suspend_count_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 293 | thread_suspend_count_cond_.Broadcast(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 296 | VLOG(threads) << *self << " ResumeAll complete"; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 299 | void ThreadList::Resume(Thread* thread, bool for_debugger) { |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 300 | DCHECK(thread != Thread::Current()); |
Elliott Hughes | d07986f | 2011-12-06 18:27:45 -0800 | [diff] [blame] | 301 | |
| 302 | if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod. |
| 303 | thread_list_lock_.AssertHeld(); |
| 304 | } |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 305 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 306 | VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : ""); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 307 | |
| 308 | { |
Brian Carlstrom | 4f20aef | 2011-10-21 00:16:18 -0700 | [diff] [blame] | 309 | MutexLock mu(thread_suspend_count_lock_); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 310 | if (!Contains(thread)) { |
| 311 | return; |
| 312 | } |
Elliott Hughes | 4e23531 | 2011-12-02 11:34:15 -0800 | [diff] [blame] | 313 | ModifySuspendCount(thread, -1, for_debugger); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 317 | VLOG(threads) << "Resume(" << *thread << ") waking others"; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 318 | MutexLock mu(thread_suspend_count_lock_); |
| 319 | thread_suspend_count_cond_.Broadcast(); |
| 320 | } |
| 321 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 322 | VLOG(threads) << "Resume(" << *thread << ") complete"; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 325 | void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) { // NOLINT |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 326 | DCHECK(thread != NULL); |
| 327 | Thread* self = Thread::Current(); |
| 328 | if (thread != self) { |
| 329 | Suspend(thread); |
| 330 | } |
| 331 | callback(arg); |
| 332 | if (thread != self) { |
| 333 | Resume(thread); |
| 334 | } |
| 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 | { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 343 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 344 | MutexLock mu(thread_suspend_count_lock_); |
| 345 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 346 | Thread* thread = *it; |
| 347 | if (thread == self || thread->debug_suspend_count_ == 0) { |
| 348 | continue; |
| 349 | } |
| 350 | ModifySuspendCount(thread, -thread->debug_suspend_count_, true); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | { |
| 355 | MutexLock mu(thread_suspend_count_lock_); |
| 356 | thread_suspend_count_cond_.Broadcast(); |
| 357 | } |
| 358 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 359 | VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 362 | void ThreadList::Register() { |
| 363 | Thread* self = Thread::Current(); |
| 364 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 365 | VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self); |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 366 | |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 367 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 7a3aeb4 | 2011-09-25 17:39:47 -0700 | [diff] [blame] | 368 | CHECK(!Contains(self)); |
| 369 | list_.push_back(self); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void ThreadList::Unregister() { |
| 373 | Thread* self = Thread::Current(); |
| 374 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 375 | VLOG(threads) << "ThreadList::Unregister() " << *self; |
Elliott Hughes | 14357e8 | 2011-09-26 10:42:15 -0700 | [diff] [blame] | 376 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 377 | // Any time-consuming destruction, plus anything that can call back into managed code or |
| 378 | // suspend and so on, must happen at this point, and not in ~Thread. |
| 379 | self->Destroy(); |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 380 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 381 | { |
| 382 | // Remove this thread from the list. |
| 383 | ScopedThreadListLock thread_list_lock; |
| 384 | CHECK(Contains(self)); |
| 385 | list_.remove(self); |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 386 | } |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 387 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 388 | // Delete the Thread* and release the thin lock id. |
| 389 | uint32_t thin_lock_id = self->thin_lock_id_; |
| 390 | delete self; |
| 391 | ReleaseThreadId(thin_lock_id); |
| 392 | |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 393 | // Clear the TLS data, so that the underlying native thread is recognizably detached. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 394 | // (It may wish to reattach later.) |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 395 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self"); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 396 | |
| 397 | // Signal that a thread just detached. |
| 398 | thread_exit_cond_.Signal(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 401 | void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) { |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 402 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 403 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 404 | callback(*it, context); |
Elliott Hughes | 47fce01 | 2011-10-25 18:37:19 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 408 | void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 409 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 410 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 411 | (*it)->VisitRoots(visitor, arg); |
| 412 | } |
| 413 | } |
| 414 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 415 | /* |
| 416 | * Tell a new thread it's safe to start. |
| 417 | * |
| 418 | * We must hold the thread list lock before messing with another thread. |
| 419 | * In the general case we would also need to verify that the new thread was |
| 420 | * still in the thread list, but in our case the thread has not started |
| 421 | * executing user code and therefore has not had a chance to exit. |
| 422 | * |
| 423 | * We move it to kVmWait, and it then shifts itself to kRunning, which |
| 424 | * comes with a suspend-pending check. We do this after |
| 425 | */ |
| 426 | void ThreadList::SignalGo(Thread* child) { |
| 427 | Thread* self = Thread::Current(); |
| 428 | CHECK(child != self); |
| 429 | |
| 430 | { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 431 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 432 | VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list..."; |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 433 | |
| 434 | // We wait for the child to tell us that it's in the thread list. |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 435 | while (child->GetState() != kStarting) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 436 | thread_start_cond_.Wait(thread_list_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | // If we switch out of runnable and then back in, we know there's no pending suspend. |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 441 | self->SetState(kVmWait); |
| 442 | self->SetState(kRunnable); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 443 | |
| 444 | // Tell the child that it's safe: it will see any future suspend request. |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 445 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 446 | VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed..."; |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 447 | child->SetState(kVmWait); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 448 | thread_start_cond_.Broadcast(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void ThreadList::WaitForGo() { |
| 452 | Thread* self = Thread::Current(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 453 | |
Brian Carlstrom | 6fbb516 | 2011-10-20 20:55:38 -0700 | [diff] [blame] | 454 | { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 455 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 456 | DCHECK(Contains(self)); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 457 | |
Brian Carlstrom | 6fbb516 | 2011-10-20 20:55:38 -0700 | [diff] [blame] | 458 | // Tell our parent that we're in the thread list. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 459 | VLOG(threads) << *self << " telling parent that we're now in thread list..."; |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 460 | self->SetState(kStarting); |
Brian Carlstrom | 6fbb516 | 2011-10-20 20:55:38 -0700 | [diff] [blame] | 461 | thread_start_cond_.Broadcast(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 462 | |
Brian Carlstrom | 6fbb516 | 2011-10-20 20:55:38 -0700 | [diff] [blame] | 463 | // Wait until our parent tells us there's no suspend still pending |
| 464 | // from before we were on the thread list. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 465 | VLOG(threads) << *self << " waiting for parent's go-ahead..."; |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 466 | while (self->GetState() != kVmWait) { |
Brian Carlstrom | 6fbb516 | 2011-10-20 20:55:38 -0700 | [diff] [blame] | 467 | thread_start_cond_.Wait(thread_list_lock_); |
| 468 | } |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // Enter the runnable state. We know that any pending suspend will affect us now. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 472 | VLOG(threads) << *self << " entering runnable state..."; |
Elliott Hughes | 47179f7 | 2011-10-27 16:44:39 -0700 | [diff] [blame] | 473 | // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we |
| 474 | // started, we wait until it's over. Which means that if there's now another GC pending, our |
| 475 | // suspend count is non-zero, so switching to the runnable state will suspend us. |
| 476 | // TODO: find a better solution! |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 477 | { |
| 478 | ScopedHeapLock heap_lock; |
| 479 | } |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 480 | self->SetState(kRunnable); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 483 | bool ThreadList::AllOtherThreadsAreDaemons() { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 484 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
Ian Rogers | cbba6ac | 2011-09-22 16:28:37 -0700 | [diff] [blame] | 485 | // TODO: there's a race here with thread exit that's being worked around by checking if the peer |
| 486 | // is null. |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 487 | Thread* thread = *it; |
| 488 | if (thread != Thread::Current() && thread->GetPeer() != NULL && !thread->IsDaemon()) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 489 | return false; |
| 490 | } |
| 491 | } |
| 492 | return true; |
| 493 | } |
| 494 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 495 | void ThreadList::WaitForOtherNonDaemonThreadsToExit() { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 496 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 497 | while (!AllOtherThreadsAreDaemons()) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 498 | thread_exit_cond_.Wait(thread_list_lock_); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void ThreadList::SuspendAllDaemonThreads() { |
Elliott Hughes | bbd9d83 | 2011-11-07 14:40:00 -0800 | [diff] [blame] | 503 | ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 504 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 505 | // Tell all the daemons it's time to suspend. |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 506 | { |
| 507 | MutexLock mu(thread_suspend_count_lock_); |
| 508 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 509 | Thread* thread = *it; |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 510 | if (thread != Thread::Current()) { |
| 511 | ++thread->suspend_count_; |
| 512 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | // Give the threads a chance to suspend, complaining if they're slow. |
| 517 | bool have_complained = false; |
| 518 | for (int i = 0; i < 10; ++i) { |
| 519 | usleep(200 * 1000); |
| 520 | bool all_suspended = true; |
| 521 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 522 | Thread* thread = *it; |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 523 | if (thread != Thread::Current() && thread->GetState() == kRunnable) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 524 | if (!have_complained) { |
| 525 | LOG(WARNING) << "daemon thread not yet suspended: " << *thread; |
| 526 | have_complained = true; |
| 527 | } |
| 528 | all_suspended = false; |
| 529 | } |
| 530 | } |
| 531 | if (all_suspended) { |
| 532 | return; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 537 | uint32_t ThreadList::AllocThreadId() { |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 538 | MutexLock mu(allocated_ids_lock_); |
| 539 | //ScopedThreadListLock thread_list_lock; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 540 | for (size_t i = 0; i < allocated_ids_.size(); ++i) { |
| 541 | if (!allocated_ids_[i]) { |
| 542 | allocated_ids_.set(i); |
| 543 | return i + 1; // Zero is reserved to mean "invalid". |
| 544 | } |
| 545 | } |
| 546 | LOG(FATAL) << "Out of internal thread ids"; |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | void ThreadList::ReleaseThreadId(uint32_t id) { |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 551 | MutexLock mu(allocated_ids_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 552 | --id; // Zero is reserved to mean "invalid". |
| 553 | DCHECK(allocated_ids_[id]) << id; |
| 554 | allocated_ids_.reset(id); |
| 555 | } |
| 556 | |
| 557 | } // namespace art |