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