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