blob: 05121862f2efae105679994da972ddedee43626a [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
Elliott Hughes14357e82011-09-26 10:42:15 -0700121 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700122 LOG(INFO) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700123 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700124
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700125 CHECK_EQ(self->GetState(), Thread::kRunnable);
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700126 ThreadListLocker locker(this);
Elliott Hughes475fc232011-10-25 15:00:35 -0700127 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700128
129 {
130 // Increment everybody's suspend count (except our own).
131 MutexLock mu(thread_suspend_count_lock_);
132 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
133 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700134 if (thread == self || (for_debugger && thread == debug_thread)) {
135 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700136 }
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700137 if (verbose_) {
138 LOG(INFO) << "requesting thread suspend: " << *thread;
139 }
140 ++thread->suspend_count_;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700141 }
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 Hughes038a8062011-09-18 14:12:41 -0700153 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700154 * 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 Hughesf6a1e1e2011-10-25 16:28:04 -0700160 if (thread == self || (for_debugger && thread == debug_thread)) {
161 continue;
162 }
163 thread->WaitUntilSuspended();
164 if (verbose_) {
165 LOG(INFO) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700166 }
167 }
168
Elliott Hughes14357e82011-09-26 10:42:15 -0700169 if (verbose_) {
170 LOG(INFO) << *self << " SuspendAll complete";
171 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700172}
173
Elliott Hughes01158d72011-09-19 19:47:10 -0700174void ThreadList::Suspend(Thread* thread) {
175 DCHECK(thread != Thread::Current());
176
177 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
178
Elliott Hughes14357e82011-09-26 10:42:15 -0700179 if (verbose_) {
180 LOG(INFO) << "Suspend(" << *thread << ") starting...";
181 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700182
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700183 ThreadListLocker locker(this);
Elliott Hughes01158d72011-09-19 19:47:10 -0700184 if (!Contains(thread)) {
185 return;
186 }
187
188 {
189 MutexLock mu(thread_suspend_count_lock_);
190 ++thread->suspend_count_;
191 }
192
193 thread->WaitUntilSuspended();
194
Elliott Hughes14357e82011-09-26 10:42:15 -0700195 if (verbose_) {
196 LOG(INFO) << "Suspend(" << *thread << ") complete";
197 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700198}
199
Elliott Hughes475fc232011-10-25 15:00:35 -0700200void ThreadList::SuspendSelfForDebugger() {
201 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700202
Elliott Hughes475fc232011-10-25 15:00:35 -0700203 // The debugger thread must not suspend itself due to debugger activity!
204 Thread* debug_thread = Dbg::GetDebugThread();
205 CHECK(debug_thread != NULL);
206 CHECK(self != debug_thread);
207
208 // Collisions with other suspends aren't really interesting. We want
209 // to ensure that we're the only one fiddling with the suspend count
210 // though.
211 ThreadListLocker locker(this);
212 MutexLock mu(thread_suspend_count_lock_);
213 ++self->suspend_count_;
214
215 // Suspend ourselves.
216 CHECK_GT(self->suspend_count_, 0);
217 self->SetState(Thread::kSuspended);
218 if (verbose_) {
219 LOG(INFO) << *self << " self-suspending (dbg)";
220 }
221
222 // Tell JDWP that we've completed suspension. The JDWP thread can't
223 // tell us to resume before we're fully asleep because we hold the
224 // suspend count lock.
225 Dbg::ClearWaitForEventThread();
226
227 while (self->suspend_count_ != 0) {
228 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
229 if (self->suspend_count_ != 0) {
230 // The condition was signaled but we're still suspended. This
231 // can happen if the debugger lets go while a SIGQUIT thread
232 // dump event is pending (assuming SignalCatcher was resumed for
233 // just long enough to try to grab the thread-suspend lock).
234 LOG(DEBUG) << *self << " still suspended after undo "
235 << "(suspend count=" << self->suspend_count_ << ")";
236 }
237 }
238 CHECK_EQ(self->suspend_count_, 0);
239 self->SetState(Thread::kRunnable);
240 if (verbose_) {
241 LOG(INFO) << *self << " self-reviving (dbg)";
242 }
243}
244
245void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700246 Thread* self = Thread::Current();
247
Elliott Hughes14357e82011-09-26 10:42:15 -0700248 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700249 LOG(INFO) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700250 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700251
252 // Decrement the suspend counts for all threads. No need for atomic
253 // writes, since nobody should be moving until we decrement the count.
254 // We do need to hold the thread list because of JNI attaches.
255 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700256 ThreadListLocker locker(this);
Elliott Hughes475fc232011-10-25 15:00:35 -0700257 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700258 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700259 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
260 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700261 if (thread == self || (for_debugger && thread == debug_thread)) {
262 continue;
263 }
264 if (thread->suspend_count_ > 0) {
265 --thread->suspend_count_;
266 } else {
267 LOG(WARNING) << *thread << " suspend count already zero";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700268 }
269 }
270 }
271
272 // Broadcast a notification to all suspended threads, some or all of
273 // which may choose to wake up. No need to wait for them.
274 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700275 if (verbose_) {
276 LOG(INFO) << *self << " ResumeAll waking others";
277 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700278 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700279 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700280 }
281
Elliott Hughes14357e82011-09-26 10:42:15 -0700282 if (verbose_) {
283 LOG(INFO) << *self << " ResumeAll complete";
284 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700285}
286
Elliott Hughes01158d72011-09-19 19:47:10 -0700287void ThreadList::Resume(Thread* thread) {
288 DCHECK(thread != Thread::Current());
289
Elliott Hughes14357e82011-09-26 10:42:15 -0700290 if (verbose_) {
291 LOG(INFO) << "Resume(" << *thread << ") starting...";
292 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700293
294 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700295 ThreadListLocker locker(this);
296 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700297 if (!Contains(thread)) {
298 return;
299 }
300 if (thread->suspend_count_ > 0) {
301 --thread->suspend_count_;
302 } else {
303 LOG(WARNING) << *thread << " suspend count already zero";
304 }
305 }
306
307 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700308 if (verbose_) {
309 LOG(INFO) << "Resume(" << *thread << ") waking others";
310 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700311 MutexLock mu(thread_suspend_count_lock_);
312 thread_suspend_count_cond_.Broadcast();
313 }
314
Elliott Hughes14357e82011-09-26 10:42:15 -0700315 if (verbose_) {
316 LOG(INFO) << "Resume(" << *thread << ") complete";
317 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700318}
319
320void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
321 DCHECK(thread != NULL);
322 Thread* self = Thread::Current();
323 if (thread != self) {
324 Suspend(thread);
325 }
326 callback(arg);
327 if (thread != self) {
328 Resume(thread);
329 }
330}
331
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700332void ThreadList::Register() {
333 Thread* self = Thread::Current();
334
Elliott Hughes14357e82011-09-26 10:42:15 -0700335 if (verbose_) {
336 LOG(INFO) << "ThreadList::Register() " << *self;
337 self->Dump(std::cerr);
338 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700339
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700340 ThreadListLocker locker(this);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700341 CHECK(!Contains(self));
342 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700343}
344
345void ThreadList::Unregister() {
346 Thread* self = Thread::Current();
347
Elliott Hughes14357e82011-09-26 10:42:15 -0700348 if (verbose_) {
349 LOG(INFO) << "ThreadList::Unregister() " << *self;
350 }
351
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700352 if (self->GetPeer() != NULL) {
353 self->SetState(Thread::kRunnable);
354
355 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
356 // down the Thread* and removing it from the thread list (or start taking any locks).
357 self->HandleUncaughtExceptions();
358
359 // Make sure we remove from ThreadGroup before taking the
360 // thread_list_lock_ since it allocates an Iterator which can cause
361 // a GC which will want to suspend.
362 self->RemoveFromThreadGroup();
363 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700364
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700365 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700366
367 // Remove this thread from the list.
368 CHECK(Contains(self));
369 list_.remove(self);
370
371 // Delete the Thread* and release the thin lock id.
372 uint32_t thin_lock_id = self->thin_lock_id_;
373 delete self;
374 ReleaseThreadId(thin_lock_id);
375
376 // Clear the TLS data, so that thread is recognizably detached.
377 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700378 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700379
380 // Signal that a thread just detached.
381 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700382}
383
Elliott Hughes47fce012011-10-25 18:37:19 -0700384void ThreadList::ForEach(void (*callback)(Thread*)) {
385 thread_list_lock_.AssertHeld();
386 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
387 callback(*it);
388 }
389}
390
Elliott Hughes8daa0922011-09-11 13:46:25 -0700391void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700392 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700393 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
394 (*it)->VisitRoots(visitor, arg);
395 }
396}
397
Elliott Hughes93e74e82011-09-13 11:07:03 -0700398/*
399 * Tell a new thread it's safe to start.
400 *
401 * We must hold the thread list lock before messing with another thread.
402 * In the general case we would also need to verify that the new thread was
403 * still in the thread list, but in our case the thread has not started
404 * executing user code and therefore has not had a chance to exit.
405 *
406 * We move it to kVmWait, and it then shifts itself to kRunning, which
407 * comes with a suspend-pending check. We do this after
408 */
409void ThreadList::SignalGo(Thread* child) {
410 Thread* self = Thread::Current();
411 CHECK(child != self);
412
413 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700414 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700415
416 // We wait for the child to tell us that it's in the thread list.
417 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700418 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700419 }
420 }
421
422 // If we switch out of runnable and then back in, we know there's no pending suspend.
423 self->SetState(Thread::kVmWait);
424 self->SetState(Thread::kRunnable);
425
426 // Tell the child that it's safe: it will see any future suspend request.
427 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700428 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700429}
430
431void ThreadList::WaitForGo() {
432 Thread* self = Thread::Current();
433 DCHECK(Contains(self));
434
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700435 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700436 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700437
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700438 // Tell our parent that we're in the thread list.
439 self->SetState(Thread::kStarting);
440 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700441
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700442 // Wait until our parent tells us there's no suspend still pending
443 // from before we were on the thread list.
444 while (self->GetState() != Thread::kVmWait) {
445 thread_start_cond_.Wait(thread_list_lock_);
446 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700447 }
448
449 // Enter the runnable state. We know that any pending suspend will affect us now.
450 self->SetState(Thread::kRunnable);
451}
452
Elliott Hughes038a8062011-09-18 14:12:41 -0700453bool ThreadList::AllThreadsAreDaemons() {
454 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700455 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
456 // is null.
457 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700458 return false;
459 }
460 }
461 return true;
462}
463
464void ThreadList::WaitForNonDaemonThreadsToExit() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700465 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700466 while (!AllThreadsAreDaemons()) {
467 thread_exit_cond_.Wait(thread_list_lock_);
468 }
469}
470
471void ThreadList::SuspendAllDaemonThreads() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700472 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700473
474 // Tell all the daemons it's time to suspend. (At this point, we know
475 // all threads are daemons.)
476 {
477 MutexLock mu(thread_suspend_count_lock_);
478 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
479 Thread* thread = *it;
480 ++thread->suspend_count_;
481 }
482 }
483
484 // Give the threads a chance to suspend, complaining if they're slow.
485 bool have_complained = false;
486 for (int i = 0; i < 10; ++i) {
487 usleep(200 * 1000);
488 bool all_suspended = true;
489 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
490 Thread* thread = *it;
491 if (thread->GetState() == Thread::kRunnable) {
492 if (!have_complained) {
493 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
494 have_complained = true;
495 }
496 all_suspended = false;
497 }
498 }
499 if (all_suspended) {
500 return;
501 }
502 }
503}
504
Elliott Hughes8daa0922011-09-11 13:46:25 -0700505uint32_t ThreadList::AllocThreadId() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700506 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700507 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
508 if (!allocated_ids_[i]) {
509 allocated_ids_.set(i);
510 return i + 1; // Zero is reserved to mean "invalid".
511 }
512 }
513 LOG(FATAL) << "Out of internal thread ids";
514 return 0;
515}
516
517void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700518 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700519 --id; // Zero is reserved to mean "invalid".
520 DCHECK(allocated_ids_[id]) << id;
521 allocated_ids_.reset(id);
522}
523
524} // namespace art