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