blob: 26faedec8ff6b9fe011587a94065ca384bd7d8a0 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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 Hughes038a8062011-09-18 14:12:41 -070019#include <unistd.h>
20
Elliott Hughes475fc232011-10-25 15:00:35 -070021#include "debugger.h"
22
Elliott Hughes8daa0922011-09-11 13:46:25 -070023namespace art {
24
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070025// TODO: merge with ThreadListLock?
26class 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 Carlstrom4514d3c2011-10-21 17:01:31 -070034 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 Carlstrom4f20aef2011-10-21 00:16:18 -070041 }
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 Hughes14357e82011-09-26 10:42:15 -070053ThreadList::ThreadList(bool verbose)
54 : verbose_(verbose),
55 thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070056 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070057 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070058 thread_suspend_count_lock_("thread suspend count lock"),
59 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes8daa0922011-09-11 13:46:25 -070060}
61
62ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070063 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070064 if (Contains(Thread::Current())) {
65 Runtime::Current()->DetachCurrentThread();
66 }
67
Elliott Hughes038a8062011-09-18 14:12:41 -070068 WaitForNonDaemonThreadsToExit();
69 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070070}
71
72bool ThreadList::Contains(Thread* thread) {
73 return find(list_.begin(), list_.end(), thread) != list_.end();
74}
75
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070076pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070077 return thread_list_lock_.GetOwner();
78}
79
Elliott Hughes8daa0922011-09-11 13:46:25 -070080void ThreadList::Dump(std::ostream& os) {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070081 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -070082 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070083 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
84 (*it)->Dump(os);
85 os << "\n";
86 }
87}
88
Elliott Hughes8d768a92011-09-14 16:35:25 -070089void 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 Hughes14357e82011-09-26 10:42:15 -070098 if (verbose_) {
99 LOG(INFO) << *thread << " self-suspending";
100 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700101 {
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 Hughes5f791332011-09-15 17:45:30 -0700109 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700110 }
111 CHECK_EQ(thread->suspend_count_, 0);
112 }
Elliott Hughes14357e82011-09-26 10:42:15 -0700113 if (verbose_) {
114 LOG(INFO) << *thread << " self-reviving";
115 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700116}
117
Elliott Hughes475fc232011-10-25 15:00:35 -0700118void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700119 Thread* self = Thread::Current();
120
121 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
122
Elliott Hughes14357e82011-09-26 10:42:15 -0700123 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700124 LOG(INFO) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700125 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700126
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700127 ThreadListLocker locker(this);
Elliott Hughes475fc232011-10-25 15:00:35 -0700128 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700129
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 Hughesf6a1e1e2011-10-25 16:28:04 -0700135 if (thread == self || (for_debugger && thread == debug_thread)) {
136 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700137 }
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700138 if (verbose_) {
139 LOG(INFO) << "requesting thread suspend: " << *thread;
140 }
141 ++thread->suspend_count_;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700142 }
143 }
144
145 /*
146 * Wait for everybody in kRunnable state to stop. Other states
147 * indicate the code is either running natively or sleeping quietly.
148 * Any attempt to transition back to kRunnable will cause a check
149 * for suspension, so it should be impossible for anything to execute
150 * interpreted code or modify objects (assuming native code plays nicely).
151 *
152 * It's also okay if the thread transitions to a non-kRunnable state.
153 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700154 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700155 * so if another thread is fiddling with its suspend count (perhaps
156 * self-suspending for the debugger) it won't block while we're waiting
157 * in here.
158 */
159 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
160 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700161 if (thread == self || (for_debugger && thread == debug_thread)) {
162 continue;
163 }
164 thread->WaitUntilSuspended();
165 if (verbose_) {
166 LOG(INFO) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700167 }
168 }
169
Elliott Hughes14357e82011-09-26 10:42:15 -0700170 if (verbose_) {
171 LOG(INFO) << *self << " SuspendAll complete";
172 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700173}
174
Elliott Hughes01158d72011-09-19 19:47:10 -0700175void ThreadList::Suspend(Thread* thread) {
176 DCHECK(thread != Thread::Current());
177
178 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
179
Elliott Hughes14357e82011-09-26 10:42:15 -0700180 if (verbose_) {
181 LOG(INFO) << "Suspend(" << *thread << ") starting...";
182 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700183
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700184 ThreadListLocker locker(this);
Elliott Hughes01158d72011-09-19 19:47:10 -0700185 if (!Contains(thread)) {
186 return;
187 }
188
189 {
190 MutexLock mu(thread_suspend_count_lock_);
191 ++thread->suspend_count_;
192 }
193
194 thread->WaitUntilSuspended();
195
Elliott Hughes14357e82011-09-26 10:42:15 -0700196 if (verbose_) {
197 LOG(INFO) << "Suspend(" << *thread << ") complete";
198 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700199}
200
Elliott Hughes475fc232011-10-25 15:00:35 -0700201void ThreadList::SuspendSelfForDebugger() {
202 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700203
Elliott Hughes475fc232011-10-25 15:00:35 -0700204 // The debugger thread must not suspend itself due to debugger activity!
205 Thread* debug_thread = Dbg::GetDebugThread();
206 CHECK(debug_thread != NULL);
207 CHECK(self != debug_thread);
208
209 // Collisions with other suspends aren't really interesting. We want
210 // to ensure that we're the only one fiddling with the suspend count
211 // though.
212 ThreadListLocker locker(this);
213 MutexLock mu(thread_suspend_count_lock_);
214 ++self->suspend_count_;
215
216 // Suspend ourselves.
217 CHECK_GT(self->suspend_count_, 0);
218 self->SetState(Thread::kSuspended);
219 if (verbose_) {
220 LOG(INFO) << *self << " self-suspending (dbg)";
221 }
222
223 // Tell JDWP that we've completed suspension. The JDWP thread can't
224 // tell us to resume before we're fully asleep because we hold the
225 // suspend count lock.
226 Dbg::ClearWaitForEventThread();
227
228 while (self->suspend_count_ != 0) {
229 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
230 if (self->suspend_count_ != 0) {
231 // The condition was signaled but we're still suspended. This
232 // can happen if the debugger lets go while a SIGQUIT thread
233 // dump event is pending (assuming SignalCatcher was resumed for
234 // just long enough to try to grab the thread-suspend lock).
235 LOG(DEBUG) << *self << " still suspended after undo "
236 << "(suspend count=" << self->suspend_count_ << ")";
237 }
238 }
239 CHECK_EQ(self->suspend_count_, 0);
240 self->SetState(Thread::kRunnable);
241 if (verbose_) {
242 LOG(INFO) << *self << " self-reviving (dbg)";
243 }
244}
245
246void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700247 Thread* self = Thread::Current();
248
Elliott Hughes14357e82011-09-26 10:42:15 -0700249 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700250 LOG(INFO) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700251 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700252
253 // Decrement the suspend counts for all threads. No need for atomic
254 // writes, since nobody should be moving until we decrement the count.
255 // We do need to hold the thread list because of JNI attaches.
256 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700257 ThreadListLocker locker(this);
Elliott Hughes475fc232011-10-25 15:00:35 -0700258 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700259 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700260 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
261 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700262 if (thread == self || (for_debugger && thread == debug_thread)) {
263 continue;
264 }
265 if (thread->suspend_count_ > 0) {
266 --thread->suspend_count_;
267 } else {
268 LOG(WARNING) << *thread << " suspend count already zero";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700269 }
270 }
271 }
272
273 // Broadcast a notification to all suspended threads, some or all of
274 // which may choose to wake up. No need to wait for them.
275 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700276 if (verbose_) {
277 LOG(INFO) << *self << " ResumeAll waking others";
278 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700279 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700280 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700281 }
282
Elliott Hughes14357e82011-09-26 10:42:15 -0700283 if (verbose_) {
284 LOG(INFO) << *self << " ResumeAll complete";
285 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700286}
287
Elliott Hughes01158d72011-09-19 19:47:10 -0700288void ThreadList::Resume(Thread* thread) {
289 DCHECK(thread != Thread::Current());
290
Elliott Hughes14357e82011-09-26 10:42:15 -0700291 if (verbose_) {
292 LOG(INFO) << "Resume(" << *thread << ") starting...";
293 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700294
295 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700296 ThreadListLocker locker(this);
297 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700298 if (!Contains(thread)) {
299 return;
300 }
301 if (thread->suspend_count_ > 0) {
302 --thread->suspend_count_;
303 } else {
304 LOG(WARNING) << *thread << " suspend count already zero";
305 }
306 }
307
308 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700309 if (verbose_) {
310 LOG(INFO) << "Resume(" << *thread << ") waking others";
311 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700312 MutexLock mu(thread_suspend_count_lock_);
313 thread_suspend_count_cond_.Broadcast();
314 }
315
Elliott Hughes14357e82011-09-26 10:42:15 -0700316 if (verbose_) {
317 LOG(INFO) << "Resume(" << *thread << ") complete";
318 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700319}
320
321void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
322 DCHECK(thread != NULL);
323 Thread* self = Thread::Current();
324 if (thread != self) {
325 Suspend(thread);
326 }
327 callback(arg);
328 if (thread != self) {
329 Resume(thread);
330 }
331}
332
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700333void ThreadList::Register() {
334 Thread* self = Thread::Current();
335
Elliott Hughes14357e82011-09-26 10:42:15 -0700336 if (verbose_) {
337 LOG(INFO) << "ThreadList::Register() " << *self;
338 self->Dump(std::cerr);
339 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700340
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700341 ThreadListLocker locker(this);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700342 CHECK(!Contains(self));
343 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700344}
345
346void ThreadList::Unregister() {
347 Thread* self = Thread::Current();
348
Elliott Hughes14357e82011-09-26 10:42:15 -0700349 if (verbose_) {
350 LOG(INFO) << "ThreadList::Unregister() " << *self;
351 }
352
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700353 if (self->GetPeer() != NULL) {
354 self->SetState(Thread::kRunnable);
355
356 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
357 // down the Thread* and removing it from the thread list (or start taking any locks).
358 self->HandleUncaughtExceptions();
359
360 // Make sure we remove from ThreadGroup before taking the
361 // thread_list_lock_ since it allocates an Iterator which can cause
362 // a GC which will want to suspend.
363 self->RemoveFromThreadGroup();
364 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700365
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700366 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700367
368 // Remove this thread from the list.
369 CHECK(Contains(self));
370 list_.remove(self);
371
372 // Delete the Thread* and release the thin lock id.
373 uint32_t thin_lock_id = self->thin_lock_id_;
374 delete self;
375 ReleaseThreadId(thin_lock_id);
376
377 // Clear the TLS data, so that thread is recognizably detached.
378 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700379 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700380
381 // Signal that a thread just detached.
382 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700383}
384
Elliott Hughes47fce012011-10-25 18:37:19 -0700385void ThreadList::ForEach(void (*callback)(Thread*)) {
386 thread_list_lock_.AssertHeld();
387 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
388 callback(*it);
389 }
390}
391
Elliott Hughes8daa0922011-09-11 13:46:25 -0700392void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700393 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700394 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
395 (*it)->VisitRoots(visitor, arg);
396 }
397}
398
Elliott Hughes93e74e82011-09-13 11:07:03 -0700399/*
400 * Tell a new thread it's safe to start.
401 *
402 * We must hold the thread list lock before messing with another thread.
403 * In the general case we would also need to verify that the new thread was
404 * still in the thread list, but in our case the thread has not started
405 * executing user code and therefore has not had a chance to exit.
406 *
407 * We move it to kVmWait, and it then shifts itself to kRunning, which
408 * comes with a suspend-pending check. We do this after
409 */
410void ThreadList::SignalGo(Thread* child) {
411 Thread* self = Thread::Current();
412 CHECK(child != self);
413
414 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700415 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700416
417 // We wait for the child to tell us that it's in the thread list.
418 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700419 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700420 }
421 }
422
423 // If we switch out of runnable and then back in, we know there's no pending suspend.
424 self->SetState(Thread::kVmWait);
425 self->SetState(Thread::kRunnable);
426
427 // Tell the child that it's safe: it will see any future suspend request.
428 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700429 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700430}
431
432void ThreadList::WaitForGo() {
433 Thread* self = Thread::Current();
434 DCHECK(Contains(self));
435
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700436 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700437 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700438
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700439 // Tell our parent that we're in the thread list.
440 self->SetState(Thread::kStarting);
441 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700442
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700443 // Wait until our parent tells us there's no suspend still pending
444 // from before we were on the thread list.
445 while (self->GetState() != Thread::kVmWait) {
446 thread_start_cond_.Wait(thread_list_lock_);
447 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700448 }
449
450 // Enter the runnable state. We know that any pending suspend will affect us now.
451 self->SetState(Thread::kRunnable);
452}
453
Elliott Hughes038a8062011-09-18 14:12:41 -0700454bool ThreadList::AllThreadsAreDaemons() {
455 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700456 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
457 // is null.
458 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700459 return false;
460 }
461 }
462 return true;
463}
464
465void ThreadList::WaitForNonDaemonThreadsToExit() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700466 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700467 while (!AllThreadsAreDaemons()) {
468 thread_exit_cond_.Wait(thread_list_lock_);
469 }
470}
471
472void ThreadList::SuspendAllDaemonThreads() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700473 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700474
475 // Tell all the daemons it's time to suspend. (At this point, we know
476 // all threads are daemons.)
477 {
478 MutexLock mu(thread_suspend_count_lock_);
479 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
480 Thread* thread = *it;
481 ++thread->suspend_count_;
482 }
483 }
484
485 // Give the threads a chance to suspend, complaining if they're slow.
486 bool have_complained = false;
487 for (int i = 0; i < 10; ++i) {
488 usleep(200 * 1000);
489 bool all_suspended = true;
490 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
491 Thread* thread = *it;
492 if (thread->GetState() == Thread::kRunnable) {
493 if (!have_complained) {
494 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
495 have_complained = true;
496 }
497 all_suspended = false;
498 }
499 }
500 if (all_suspended) {
501 return;
502 }
503 }
504}
505
Elliott Hughes8daa0922011-09-11 13:46:25 -0700506uint32_t ThreadList::AllocThreadId() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700507 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700508 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
509 if (!allocated_ids_[i]) {
510 allocated_ids_.set(i);
511 return i + 1; // Zero is reserved to mean "invalid".
512 }
513 }
514 LOG(FATAL) << "Out of internal thread ids";
515 return 0;
516}
517
518void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700519 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700520 --id; // Zero is reserved to mean "invalid".
521 DCHECK(allocated_ids_[id]) << id;
522 allocated_ids_.reset(id);
523}
524
525} // namespace art