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