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