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