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