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