blob: 0bd587ca7bbb88b8d667d11ef2e84a918a68cd4a [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 Hughesabbe07d2012-06-05 17:42:23 -070019#include <dirent.h>
20#include <sys/types.h>
Elliott Hughes038a8062011-09-18 14:12:41 -070021#include <unistd.h>
22
Elliott Hughes475fc232011-10-25 15:00:35 -070023#include "debugger.h"
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070024#include "timing_logger.h"
Elliott Hughesabbe07d2012-06-05 17:42:23 -070025#include "utils.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070026
Elliott Hughes8daa0922011-09-11 13:46:25 -070027namespace art {
28
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080029ThreadList::ThreadList()
Elliott Hughese52e49b2012-04-02 16:05:44 -070030 : allocated_ids_lock_("allocated thread ids lock"),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031 suspend_all_count_(0), debug_suspend_all_count_(0),
32 thread_exit_cond_("thread exit condition variable") {
Elliott Hughes8daa0922011-09-11 13:46:25 -070033}
34
35ThreadList::~ThreadList() {
Elliott Hughese52e49b2012-04-02 16:05:44 -070036 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
Elliott Hughes6a144332012-04-03 13:07:11 -070037 // We need to detach the current thread here in case there's another thread waiting to join with
38 // us.
Elliott Hughes8daa0922011-09-11 13:46:25 -070039 if (Contains(Thread::Current())) {
40 Runtime::Current()->DetachCurrentThread();
41 }
Elliott Hughes6a144332012-04-03 13:07:11 -070042
43 WaitForOtherNonDaemonThreadsToExit();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044 // TODO: there's an unaddressed race here where a thread may attach during shutdown, see
45 // Thread::Init.
Elliott Hughes6a144332012-04-03 13:07:11 -070046 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070047}
48
49bool ThreadList::Contains(Thread* thread) {
50 return find(list_.begin(), list_.end(), thread) != list_.end();
51}
52
Elliott Hughesabbe07d2012-06-05 17:42:23 -070053bool ThreadList::Contains(pid_t tid) {
54 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
55 if ((*it)->tid_ == tid) {
56 return true;
57 }
58 }
59 return false;
60}
61
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070062pid_t ThreadList::GetLockOwner() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 return GlobalSynchronization::thread_list_lock_->GetExclusiveOwnerTid();
Elliott Hughesaccd83d2011-10-17 14:25:58 -070064}
65
Elliott Hughesc967f782012-04-16 10:23:15 -070066void ThreadList::DumpForSigQuit(std::ostream& os) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 {
68 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
69 DumpLocked(os);
70 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -070071 DumpUnattachedThreads(os);
72}
73
74static void DumpUnattachedThread(std::ostream& os, pid_t tid) {
75 Thread::DumpState(os, NULL, tid);
76 DumpKernelStack(os, tid, " kernel: ", false);
Brian Carlstromed8b7232012-06-27 17:54:47 -070077 // TODO: Reenable this when the native code in system_server can handle it.
78 // Currently "adb shell kill -3 `pid system_server`" will cause it to exit.
79 if (false) {
80 DumpNativeStack(os, tid, " native: ", false);
81 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -070082 os << "\n";
83}
84
85void ThreadList::DumpUnattachedThreads(std::ostream& os) {
86 DIR* d = opendir("/proc/self/task");
87 if (!d) {
88 return;
89 }
90
91 dirent de;
Elliott Hughes0d39c122012-06-06 16:41:17 -070092 dirent* e;
93 while (!readdir_r(d, &de, &e) && e != NULL) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070094 char* end;
95 pid_t tid = strtol(de.d_name, &end, 10);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 if (!*end) {
97 bool contains;
98 {
99 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
100 contains = Contains(tid);
101 }
102 if (!contains) {
103 DumpUnattachedThread(os, tid);
104 }
Elliott Hughesabbe07d2012-06-05 17:42:23 -0700105 }
106 }
107 closedir(d);
Elliott Hughesff738062012-02-03 15:00:42 -0800108}
109
110void ThreadList::DumpLocked(std::ostream& os) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 GlobalSynchronization::thread_list_lock_->AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700112 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700113 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
114 (*it)->Dump(os);
115 os << "\n";
116 }
117}
118
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119void ThreadList::AssertThreadsAreSuspended() {
120 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
121 MutexLock mu2(*GlobalSynchronization::thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700122 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
123 Thread* thread = *it;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 CHECK_NE(thread->GetState(), kRunnable);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700125 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126}
127
128// Attempt to rectify locks so that we dump thread list with required locks before exiting.
129static void UnsafeLogFatalForThreadSuspendAllTimeout() NO_THREAD_SAFETY_ANALYSIS {
130 Runtime* runtime = Runtime::Current();
131 std::ostringstream ss;
132 ss << "Thread suspend timeout\n";
133 runtime->DumpLockHolders(ss);
134 ss << "\n";
135 GlobalSynchronization::mutator_lock_->SharedTryLock();
136 if (!GlobalSynchronization::mutator_lock_->IsSharedHeld()) {
137 LOG(WARNING) << "Dumping thread list without holding mutator_lock_";
138 }
139 GlobalSynchronization::thread_list_lock_->TryLock();
140 if (!GlobalSynchronization::thread_list_lock_->IsExclusiveHeld()) {
141 LOG(WARNING) << "Dumping thread list without holding thread_list_lock_";
142 }
143 runtime->GetThreadList()->DumpLocked(ss);
144 LOG(FATAL) << ss.str();
145}
146
147void ThreadList::SuspendAll() {
148 Thread* self = Thread::Current();
149
150 VLOG(threads) << *self << " SuspendAll starting...";
151
152 if (kIsDebugBuild) {
153 GlobalSynchronization::mutator_lock_->AssertNotHeld();
154 GlobalSynchronization::thread_list_lock_->AssertNotHeld();
155 GlobalSynchronization::thread_suspend_count_lock_->AssertNotHeld();
156 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
157 CHECK_NE(self->GetState(), kRunnable);
158 }
159 {
160 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
161 {
162 MutexLock mu2(*GlobalSynchronization::thread_suspend_count_lock_);
163 // Update global suspend all state for attaching threads.
164 ++suspend_all_count_;
165 // Increment everybody's suspend count (except our own).
166 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
167 Thread* thread = *it;
168 if (thread == self) {
169 continue;
170 }
171 VLOG(threads) << "requesting thread suspend: " << *thread;
172 thread->ModifySuspendCount(+1, false);
173 }
174 }
175 }
176
177 // Block on the mutator lock until all Runnable threads release their share of access. Timeout
178 // if we wait more than 30 seconds.
179 timespec timeout;
180 clock_gettime(CLOCK_REALTIME, &timeout);
181 timeout.tv_sec += 30;
182 if (UNLIKELY(!GlobalSynchronization::mutator_lock_->ExclusiveLockWithTimeout(timeout))) {
183 UnsafeLogFatalForThreadSuspendAllTimeout();
184 }
185
186 // Debug check that all threads are suspended.
187 AssertThreadsAreSuspended();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700188
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800189 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700190}
191
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700192void ThreadList::ResumeAll() {
193 Thread* self = Thread::Current();
194
195 VLOG(threads) << *self << " ResumeAll starting";
196 {
197 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
198 MutexLock mu2(*GlobalSynchronization::thread_suspend_count_lock_);
199 // Update global suspend all state for attaching threads.
200 --suspend_all_count_;
201 // Decrement the suspend counts for all threads.
202 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
203 Thread* thread = *it;
204 if (thread == self) {
205 continue;
206 }
207 thread->ModifySuspendCount(-1, false);
208 }
209
210 // Broadcast a notification to all suspended threads, some or all of
211 // which may choose to wake up. No need to wait for them.
212 VLOG(threads) << *self << " ResumeAll waking others";
213 Thread::resume_cond_->Broadcast();
214 }
215 GlobalSynchronization::mutator_lock_->ExclusiveUnlock();
216 VLOG(threads) << *self << " ResumeAll complete";
217}
218
219void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700220 DCHECK(thread != Thread::Current());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700222
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700223 {
224 // To check Contains.
225 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
226 // To check IsSuspended.
227 MutexLock mu2(*GlobalSynchronization::thread_suspend_count_lock_);
228 CHECK(thread->IsSuspended());
229 if (!Contains(thread)) {
230 return;
231 }
232 thread->ModifySuspendCount(-1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700233 }
234
235 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700236 VLOG(threads) << "Resume(" << *thread << ") waking others";
237 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
238 Thread::resume_cond_->Broadcast();
Elliott Hughes01158d72011-09-19 19:47:10 -0700239 }
240
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700241 VLOG(threads) << "Resume(" << *thread << ") complete";
242}
Elliott Hughes01158d72011-09-19 19:47:10 -0700243
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700244void ThreadList::SuspendAllForDebugger() {
245 Thread* self = Thread::Current();
246 Thread* debug_thread = Dbg::GetDebugThread();
247
248 VLOG(threads) << *self << " SuspendAllForDebugger starting...";
249
250 {
251 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
252 {
253 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
254 // Update global suspend all state for attaching threads.
255 ++suspend_all_count_;
256 ++debug_suspend_all_count_;
257 // Increment everybody's suspend count (except our own).
258 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
259 Thread* thread = *it;
260 if (thread == self || thread == debug_thread) {
261 continue;
262 }
263 VLOG(threads) << "requesting thread suspend: " << *thread;
264 thread->ModifySuspendCount(+1, true);
265 }
266 }
267 }
268
269 // Block on the mutator lock until all Runnable threads release their share of access. Timeout
270 // if we wait more than 30 seconds.
271 timespec timeout;
272 clock_gettime(CLOCK_REALTIME, &timeout);
273 timeout.tv_sec += 30;
274 if (!GlobalSynchronization::mutator_lock_->ExclusiveLockWithTimeout(timeout)) {
275 UnsafeLogFatalForThreadSuspendAllTimeout();
276 } else {
277 // Debugger suspends all threads but doesn't hold onto the mutator_lock_.
278 GlobalSynchronization::mutator_lock_->ExclusiveUnlock();
279 }
280
281 AssertThreadsAreSuspended();
282
283 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700284}
285
Elliott Hughes475fc232011-10-25 15:00:35 -0700286void ThreadList::SuspendSelfForDebugger() {
287 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700288
Elliott Hughes475fc232011-10-25 15:00:35 -0700289 // The debugger thread must not suspend itself due to debugger activity!
290 Thread* debug_thread = Dbg::GetDebugThread();
291 CHECK(debug_thread != NULL);
292 CHECK(self != debug_thread);
293
294 // Collisions with other suspends aren't really interesting. We want
295 // to ensure that we're the only one fiddling with the suspend count
296 // though.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700297 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
298 self->ModifySuspendCount(+1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700299
300 // Suspend ourselves.
301 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700302 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800303 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700304
305 // Tell JDWP that we've completed suspension. The JDWP thread can't
306 // tell us to resume before we're fully asleep because we hold the
307 // suspend count lock.
308 Dbg::ClearWaitForEventThread();
309
310 while (self->suspend_count_ != 0) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 Thread::resume_cond_->Wait(*GlobalSynchronization::thread_suspend_count_lock_);
Elliott Hughes475fc232011-10-25 15:00:35 -0700312 if (self->suspend_count_ != 0) {
313 // The condition was signaled but we're still suspended. This
314 // can happen if the debugger lets go while a SIGQUIT thread
315 // dump event is pending (assuming SignalCatcher was resumed for
316 // just long enough to try to grab the thread-suspend lock).
317 LOG(DEBUG) << *self << " still suspended after undo "
318 << "(suspend count=" << self->suspend_count_ << ")";
319 }
320 }
321 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700322 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800323 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700324}
325
Elliott Hughes234ab152011-10-26 14:02:26 -0700326void ThreadList::UndoDebuggerSuspensions() {
327 Thread* self = Thread::Current();
328
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800329 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700330
331 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700332 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
333 MutexLock mu2(*GlobalSynchronization::thread_suspend_count_lock_);
334 // Update global suspend all state for attaching threads.
335 suspend_all_count_ -= debug_suspend_all_count_;
336 debug_suspend_all_count_ = 0;
337 // Update running threads.
Elliott Hughes234ab152011-10-26 14:02:26 -0700338 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
339 Thread* thread = *it;
340 if (thread == self || thread->debug_suspend_count_ == 0) {
341 continue;
342 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700343 thread->ModifySuspendCount(-thread->debug_suspend_count_, true);
Elliott Hughes234ab152011-10-26 14:02:26 -0700344 }
345 }
346
347 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
349 Thread::resume_cond_->Broadcast();
Elliott Hughes234ab152011-10-26 14:02:26 -0700350 }
351
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800352 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700353}
354
Elliott Hughese52e49b2012-04-02 16:05:44 -0700355void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 GlobalSynchronization::mutator_lock_->AssertNotHeld();
357 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
358 bool all_threads_are_daemons;
359 do {
360 all_threads_are_daemons = true;
361 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
362 // TODO: there's a race here with thread exit that's being worked around by checking if the
363 // thread has a peer.
364 Thread* thread = *it;
365 if (thread != Thread::Current() && thread->HasPeer() && !thread->IsDaemon()) {
366 all_threads_are_daemons = false;
367 break;
368 }
369 }
370 if (!all_threads_are_daemons) {
371 // Wait for another thread to exit before re-checking.
372 thread_exit_cond_.Wait(*GlobalSynchronization::thread_list_lock_);
373 }
374 } while(!all_threads_are_daemons);
Elliott Hughes038a8062011-09-18 14:12:41 -0700375}
376
377void ThreadList::SuspendAllDaemonThreads() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700378 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
379 { // Tell all the daemons it's time to suspend.
380 MutexLock mu2(*GlobalSynchronization::thread_suspend_count_lock_);
Elliott Hughes038a8062011-09-18 14:12:41 -0700381 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
382 Thread* thread = *it;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700383 // This is only run after all non-daemon threads have exited, so the remainder should all be
384 // daemons.
385 CHECK(thread->IsDaemon());
Elliott Hughese52e49b2012-04-02 16:05:44 -0700386 if (thread != Thread::Current()) {
387 ++thread->suspend_count_;
388 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700389 }
390 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700391 // Give the threads a chance to suspend, complaining if they're slow.
392 bool have_complained = false;
393 for (int i = 0; i < 10; ++i) {
394 usleep(200 * 1000);
395 bool all_suspended = true;
396 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
397 Thread* thread = *it;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700398 MutexLock mu2(*GlobalSynchronization::thread_suspend_count_lock_);
Elliott Hughes34e06962012-04-09 13:55:55 -0700399 if (thread != Thread::Current() && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700400 if (!have_complained) {
401 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
402 have_complained = true;
403 }
404 all_suspended = false;
405 }
406 }
407 if (all_suspended) {
408 return;
409 }
410 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700411 LOG(ERROR) << "suspend all daemons failed";
412}
413void ThreadList::Register(Thread* self) {
414 DCHECK_EQ(self, Thread::Current());
415
416 if (VLOG_IS_ON(threads)) {
417 std::ostringstream oss;
418 self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump.
419 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss;
420 }
421
422 // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
423 // SuspendAll requests.
424 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
425 MutexLock mu2(*GlobalSynchronization::thread_suspend_count_lock_);
426 self->suspend_count_ = suspend_all_count_;
427 self->debug_suspend_count_ = debug_suspend_all_count_;
428 CHECK(!Contains(self));
429 list_.push_back(self);
430}
431
432void ThreadList::Unregister(Thread* self) {
433 DCHECK_EQ(self, Thread::Current());
434
435 VLOG(threads) << "ThreadList::Unregister() " << *self;
436
437 // Any time-consuming destruction, plus anything that can call back into managed code or
438 // suspend and so on, must happen at this point, and not in ~Thread.
439 self->Destroy();
440
441 {
442 // Remove this thread from the list.
443 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
444 CHECK(Contains(self));
445 list_.remove(self);
446 }
447
448 // Delete the Thread* and release the thin lock id.
449 uint32_t thin_lock_id = self->thin_lock_id_;
450 ReleaseThreadId(thin_lock_id);
451 delete self;
452
453 // Clear the TLS data, so that the underlying native thread is recognizably detached.
454 // (It may wish to reattach later.)
455 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
456
457 // Signal that a thread just detached.
458 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
459 thread_exit_cond_.Signal();
460}
461
462void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
463 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
464 callback(*it, context);
465 }
466}
467
468void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
469 MutexLock mu(*GlobalSynchronization::thread_list_lock_);
470 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
471 (*it)->VisitRoots(visitor, arg);
472 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700473}
474
Elliott Hughes8daa0922011-09-11 13:46:25 -0700475uint32_t ThreadList::AllocThreadId() {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700476 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700477 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
478 if (!allocated_ids_[i]) {
479 allocated_ids_.set(i);
480 return i + 1; // Zero is reserved to mean "invalid".
481 }
482 }
483 LOG(FATAL) << "Out of internal thread ids";
484 return 0;
485}
486
487void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700488 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700489 --id; // Zero is reserved to mean "invalid".
490 DCHECK(allocated_ids_[id]) << id;
491 allocated_ids_.reset(id);
492}
493
494} // namespace art