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 | |
| 19 | namespace art { |
| 20 | |
| 21 | ThreadList::ThreadList() : lock_("ThreadList lock") { |
| 22 | } |
| 23 | |
| 24 | ThreadList::~ThreadList() { |
| 25 | if (Contains(Thread::Current())) { |
| 26 | Runtime::Current()->DetachCurrentThread(); |
| 27 | } |
| 28 | |
| 29 | // All threads should have exited and unregistered when we |
| 30 | // reach this point. This means that all daemon threads had been |
| 31 | // shutdown cleanly. |
| 32 | // TODO: dump ThreadList if non-empty. |
| 33 | CHECK_EQ(list_.size(), 0U); |
| 34 | } |
| 35 | |
| 36 | bool ThreadList::Contains(Thread* thread) { |
| 37 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 38 | } |
| 39 | |
| 40 | void ThreadList::Dump(std::ostream& os) { |
| 41 | MutexLock mu(lock_); |
| 42 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
| 43 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 44 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 45 | (*it)->Dump(os); |
| 46 | os << "\n"; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void ThreadList::Register(Thread* thread) { |
| 51 | //LOG(INFO) << "ThreadList::Register() " << *thread; |
| 52 | MutexLock mu(lock_); |
| 53 | CHECK(!Contains(thread)); |
| 54 | list_.push_back(thread); |
| 55 | } |
| 56 | |
| 57 | void ThreadList::Unregister() { |
| 58 | Thread* self = Thread::Current(); |
| 59 | |
| 60 | //LOG(INFO) << "ThreadList::Unregister() " << self; |
| 61 | MutexLock mu(lock_); |
| 62 | |
| 63 | // Remove this thread from the list. |
| 64 | CHECK(Contains(self)); |
| 65 | list_.remove(self); |
| 66 | |
| 67 | // Delete the Thread* and release the thin lock id. |
| 68 | uint32_t thin_lock_id = self->thin_lock_id_; |
| 69 | delete self; |
| 70 | ReleaseThreadId(thin_lock_id); |
| 71 | |
| 72 | // Clear the TLS data, so that thread is recognizably detached. |
| 73 | // (It may wish to reattach later.) |
| 74 | errno = pthread_setspecific(Thread::pthread_key_self_, NULL); |
| 75 | if (errno != 0) { |
| 76 | PLOG(FATAL) << "pthread_setspecific failed"; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 81 | MutexLock mu(lock_); |
| 82 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 83 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 84 | (*it)->VisitRoots(visitor, arg); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | uint32_t ThreadList::AllocThreadId() { |
| 89 | MutexLock mu(lock_); |
| 90 | for (size_t i = 0; i < allocated_ids_.size(); ++i) { |
| 91 | if (!allocated_ids_[i]) { |
| 92 | allocated_ids_.set(i); |
| 93 | return i + 1; // Zero is reserved to mean "invalid". |
| 94 | } |
| 95 | } |
| 96 | LOG(FATAL) << "Out of internal thread ids"; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void ThreadList::ReleaseThreadId(uint32_t id) { |
| 101 | lock_.AssertHeld(); |
| 102 | --id; // Zero is reserved to mean "invalid". |
| 103 | DCHECK(allocated_ids_[id]) << id; |
| 104 | allocated_ids_.reset(id); |
| 105 | } |
| 106 | |
| 107 | } // namespace art |