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