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