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