blob: f386547d304908ca2c8e18a1e48bc82fc2dd8811 [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 Hughes475fc232011-10-25 15:00:35 -0700135 if (thread != self && thread != debug_thread) {
Elliott Hughes14357e82011-09-26 10:42:15 -0700136 if (verbose_) {
137 LOG(INFO) << "requesting thread suspend: " << *thread;
138 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700139 ++thread->suspend_count_;
140 }
141 }
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 Hughes475fc232011-10-25 15:00:35 -0700160 if (thread != self && thread != debug_thread) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700161 thread->WaitUntilSuspended();
Elliott Hughes14357e82011-09-26 10:42:15 -0700162 if (verbose_) {
163 LOG(INFO) << "thread suspended: " << *thread;
164 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700165 }
166 }
167
Elliott Hughes14357e82011-09-26 10:42:15 -0700168 if (verbose_) {
169 LOG(INFO) << *self << " SuspendAll complete";
170 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700171}
172
Elliott Hughes01158d72011-09-19 19:47:10 -0700173void ThreadList::Suspend(Thread* thread) {
174 DCHECK(thread != Thread::Current());
175
176 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
177
Elliott Hughes14357e82011-09-26 10:42:15 -0700178 if (verbose_) {
179 LOG(INFO) << "Suspend(" << *thread << ") starting...";
180 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700181
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700182 ThreadListLocker locker(this);
Elliott Hughes01158d72011-09-19 19:47:10 -0700183 if (!Contains(thread)) {
184 return;
185 }
186
187 {
188 MutexLock mu(thread_suspend_count_lock_);
189 ++thread->suspend_count_;
190 }
191
192 thread->WaitUntilSuspended();
193
Elliott Hughes14357e82011-09-26 10:42:15 -0700194 if (verbose_) {
195 LOG(INFO) << "Suspend(" << *thread << ") complete";
196 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700197}
198
Elliott Hughes475fc232011-10-25 15:00:35 -0700199void ThreadList::SuspendSelfForDebugger() {
200 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700201
Elliott Hughes475fc232011-10-25 15:00:35 -0700202 // The debugger thread must not suspend itself due to debugger activity!
203 Thread* debug_thread = Dbg::GetDebugThread();
204 CHECK(debug_thread != NULL);
205 CHECK(self != debug_thread);
206
207 // Collisions with other suspends aren't really interesting. We want
208 // to ensure that we're the only one fiddling with the suspend count
209 // though.
210 ThreadListLocker locker(this);
211 MutexLock mu(thread_suspend_count_lock_);
212 ++self->suspend_count_;
213
214 // Suspend ourselves.
215 CHECK_GT(self->suspend_count_, 0);
216 self->SetState(Thread::kSuspended);
217 if (verbose_) {
218 LOG(INFO) << *self << " self-suspending (dbg)";
219 }
220
221 // Tell JDWP that we've completed suspension. The JDWP thread can't
222 // tell us to resume before we're fully asleep because we hold the
223 // suspend count lock.
224 Dbg::ClearWaitForEventThread();
225
226 while (self->suspend_count_ != 0) {
227 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
228 if (self->suspend_count_ != 0) {
229 // The condition was signaled but we're still suspended. This
230 // can happen if the debugger lets go while a SIGQUIT thread
231 // dump event is pending (assuming SignalCatcher was resumed for
232 // just long enough to try to grab the thread-suspend lock).
233 LOG(DEBUG) << *self << " still suspended after undo "
234 << "(suspend count=" << self->suspend_count_ << ")";
235 }
236 }
237 CHECK_EQ(self->suspend_count_, 0);
238 self->SetState(Thread::kRunnable);
239 if (verbose_) {
240 LOG(INFO) << *self << " self-reviving (dbg)";
241 }
242}
243
244void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700245 Thread* self = Thread::Current();
246
Elliott Hughes14357e82011-09-26 10:42:15 -0700247 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700248 LOG(INFO) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700249 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700250
251 // Decrement the suspend counts for all threads. No need for atomic
252 // writes, since nobody should be moving until we decrement the count.
253 // We do need to hold the thread list because of JNI attaches.
254 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700255 ThreadListLocker locker(this);
Elliott Hughes475fc232011-10-25 15:00:35 -0700256 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700257 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700258 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
259 Thread* thread = *it;
Elliott Hughes475fc232011-10-25 15:00:35 -0700260 if (thread != self && thread != debug_thread) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700261 if (thread->suspend_count_ > 0) {
262 --thread->suspend_count_;
263 } else {
264 LOG(WARNING) << *thread << " suspend count already zero";
265 }
266 }
267 }
268 }
269
270 // Broadcast a notification to all suspended threads, some or all of
271 // which may choose to wake up. No need to wait for them.
272 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700273 if (verbose_) {
274 LOG(INFO) << *self << " ResumeAll waking others";
275 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700276 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700277 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700278 }
279
Elliott Hughes14357e82011-09-26 10:42:15 -0700280 if (verbose_) {
281 LOG(INFO) << *self << " ResumeAll complete";
282 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700283}
284
Elliott Hughes01158d72011-09-19 19:47:10 -0700285void ThreadList::Resume(Thread* thread) {
286 DCHECK(thread != Thread::Current());
287
Elliott Hughes14357e82011-09-26 10:42:15 -0700288 if (verbose_) {
289 LOG(INFO) << "Resume(" << *thread << ") starting...";
290 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700291
292 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700293 ThreadListLocker locker(this);
294 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700295 if (!Contains(thread)) {
296 return;
297 }
298 if (thread->suspend_count_ > 0) {
299 --thread->suspend_count_;
300 } else {
301 LOG(WARNING) << *thread << " suspend count already zero";
302 }
303 }
304
305 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700306 if (verbose_) {
307 LOG(INFO) << "Resume(" << *thread << ") waking others";
308 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700309 MutexLock mu(thread_suspend_count_lock_);
310 thread_suspend_count_cond_.Broadcast();
311 }
312
Elliott Hughes14357e82011-09-26 10:42:15 -0700313 if (verbose_) {
314 LOG(INFO) << "Resume(" << *thread << ") complete";
315 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700316}
317
318void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
319 DCHECK(thread != NULL);
320 Thread* self = Thread::Current();
321 if (thread != self) {
322 Suspend(thread);
323 }
324 callback(arg);
325 if (thread != self) {
326 Resume(thread);
327 }
328}
329
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700330void ThreadList::Register() {
331 Thread* self = Thread::Current();
332
Elliott Hughes14357e82011-09-26 10:42:15 -0700333 if (verbose_) {
334 LOG(INFO) << "ThreadList::Register() " << *self;
335 self->Dump(std::cerr);
336 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700337
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700338 ThreadListLocker locker(this);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700339 CHECK(!Contains(self));
340 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700341}
342
343void ThreadList::Unregister() {
344 Thread* self = Thread::Current();
345
Elliott Hughes14357e82011-09-26 10:42:15 -0700346 if (verbose_) {
347 LOG(INFO) << "ThreadList::Unregister() " << *self;
348 }
349
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700350 if (self->GetPeer() != NULL) {
351 self->SetState(Thread::kRunnable);
352
353 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
354 // down the Thread* and removing it from the thread list (or start taking any locks).
355 self->HandleUncaughtExceptions();
356
357 // Make sure we remove from ThreadGroup before taking the
358 // thread_list_lock_ since it allocates an Iterator which can cause
359 // a GC which will want to suspend.
360 self->RemoveFromThreadGroup();
361 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700362
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700363 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700364
365 // Remove this thread from the list.
366 CHECK(Contains(self));
367 list_.remove(self);
368
369 // Delete the Thread* and release the thin lock id.
370 uint32_t thin_lock_id = self->thin_lock_id_;
371 delete self;
372 ReleaseThreadId(thin_lock_id);
373
374 // Clear the TLS data, so that thread is recognizably detached.
375 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700376 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700377
378 // Signal that a thread just detached.
379 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700380}
381
382void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700383 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700384 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
385 (*it)->VisitRoots(visitor, arg);
386 }
387}
388
Elliott Hughes93e74e82011-09-13 11:07:03 -0700389/*
390 * Tell a new thread it's safe to start.
391 *
392 * We must hold the thread list lock before messing with another thread.
393 * In the general case we would also need to verify that the new thread was
394 * still in the thread list, but in our case the thread has not started
395 * executing user code and therefore has not had a chance to exit.
396 *
397 * We move it to kVmWait, and it then shifts itself to kRunning, which
398 * comes with a suspend-pending check. We do this after
399 */
400void ThreadList::SignalGo(Thread* child) {
401 Thread* self = Thread::Current();
402 CHECK(child != self);
403
404 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700405 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700406
407 // We wait for the child to tell us that it's in the thread list.
408 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700409 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700410 }
411 }
412
413 // If we switch out of runnable and then back in, we know there's no pending suspend.
414 self->SetState(Thread::kVmWait);
415 self->SetState(Thread::kRunnable);
416
417 // Tell the child that it's safe: it will see any future suspend request.
418 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700419 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700420}
421
422void ThreadList::WaitForGo() {
423 Thread* self = Thread::Current();
424 DCHECK(Contains(self));
425
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700426 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700427 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700428
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700429 // Tell our parent that we're in the thread list.
430 self->SetState(Thread::kStarting);
431 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700432
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700433 // Wait until our parent tells us there's no suspend still pending
434 // from before we were on the thread list.
435 while (self->GetState() != Thread::kVmWait) {
436 thread_start_cond_.Wait(thread_list_lock_);
437 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700438 }
439
440 // Enter the runnable state. We know that any pending suspend will affect us now.
441 self->SetState(Thread::kRunnable);
442}
443
Elliott Hughes038a8062011-09-18 14:12:41 -0700444bool ThreadList::AllThreadsAreDaemons() {
445 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700446 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
447 // is null.
448 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700449 return false;
450 }
451 }
452 return true;
453}
454
455void ThreadList::WaitForNonDaemonThreadsToExit() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700456 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700457 while (!AllThreadsAreDaemons()) {
458 thread_exit_cond_.Wait(thread_list_lock_);
459 }
460}
461
462void ThreadList::SuspendAllDaemonThreads() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700463 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700464
465 // Tell all the daemons it's time to suspend. (At this point, we know
466 // all threads are daemons.)
467 {
468 MutexLock mu(thread_suspend_count_lock_);
469 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
470 Thread* thread = *it;
471 ++thread->suspend_count_;
472 }
473 }
474
475 // Give the threads a chance to suspend, complaining if they're slow.
476 bool have_complained = false;
477 for (int i = 0; i < 10; ++i) {
478 usleep(200 * 1000);
479 bool all_suspended = true;
480 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
481 Thread* thread = *it;
482 if (thread->GetState() == Thread::kRunnable) {
483 if (!have_complained) {
484 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
485 have_complained = true;
486 }
487 all_suspended = false;
488 }
489 }
490 if (all_suspended) {
491 return;
492 }
493 }
494}
495
Elliott Hughes8daa0922011-09-11 13:46:25 -0700496uint32_t ThreadList::AllocThreadId() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700497 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700498 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
499 if (!allocated_ids_[i]) {
500 allocated_ids_.set(i);
501 return i + 1; // Zero is reserved to mean "invalid".
502 }
503 }
504 LOG(FATAL) << "Out of internal thread ids";
505 return 0;
506}
507
508void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700509 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700510 --id; // Zero is reserved to mean "invalid".
511 DCHECK(allocated_ids_[id]) << id;
512 allocated_ids_.reset(id);
513}
514
515} // namespace art