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