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 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame^] | 21 | pthread_cond_t ThreadList::thread_start_cond_ = PTHREAD_COND_INITIALIZER; |
| 22 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 23 | ThreadList::ThreadList() : lock_("ThreadList lock") { |
| 24 | } |
| 25 | |
| 26 | ThreadList::~ThreadList() { |
| 27 | if (Contains(Thread::Current())) { |
| 28 | Runtime::Current()->DetachCurrentThread(); |
| 29 | } |
| 30 | |
| 31 | // All threads should have exited and unregistered when we |
| 32 | // reach this point. This means that all daemon threads had been |
| 33 | // shutdown cleanly. |
| 34 | // TODO: dump ThreadList if non-empty. |
| 35 | CHECK_EQ(list_.size(), 0U); |
| 36 | } |
| 37 | |
| 38 | bool ThreadList::Contains(Thread* thread) { |
| 39 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 40 | } |
| 41 | |
| 42 | void ThreadList::Dump(std::ostream& os) { |
| 43 | MutexLock mu(lock_); |
| 44 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
| 45 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 46 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 47 | (*it)->Dump(os); |
| 48 | os << "\n"; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void ThreadList::Register(Thread* thread) { |
| 53 | //LOG(INFO) << "ThreadList::Register() " << *thread; |
| 54 | MutexLock mu(lock_); |
| 55 | CHECK(!Contains(thread)); |
| 56 | list_.push_back(thread); |
| 57 | } |
| 58 | |
| 59 | void ThreadList::Unregister() { |
| 60 | Thread* self = Thread::Current(); |
| 61 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame^] | 62 | //LOG(INFO) << "ThreadList::Unregister() " << *self; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 63 | MutexLock mu(lock_); |
| 64 | |
| 65 | // Remove this thread from the list. |
| 66 | CHECK(Contains(self)); |
| 67 | list_.remove(self); |
| 68 | |
| 69 | // Delete the Thread* and release the thin lock id. |
| 70 | uint32_t thin_lock_id = self->thin_lock_id_; |
| 71 | delete self; |
| 72 | ReleaseThreadId(thin_lock_id); |
| 73 | |
| 74 | // Clear the TLS data, so that thread is recognizably detached. |
| 75 | // (It may wish to reattach later.) |
| 76 | errno = pthread_setspecific(Thread::pthread_key_self_, NULL); |
| 77 | if (errno != 0) { |
| 78 | PLOG(FATAL) << "pthread_setspecific failed"; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 83 | MutexLock mu(lock_); |
| 84 | typedef std::list<Thread*>::const_iterator It; // TODO: C++0x auto |
| 85 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 86 | (*it)->VisitRoots(visitor, arg); |
| 87 | } |
| 88 | } |
| 89 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame^] | 90 | /* |
| 91 | * Tell a new thread it's safe to start. |
| 92 | * |
| 93 | * We must hold the thread list lock before messing with another thread. |
| 94 | * In the general case we would also need to verify that the new thread was |
| 95 | * still in the thread list, but in our case the thread has not started |
| 96 | * executing user code and therefore has not had a chance to exit. |
| 97 | * |
| 98 | * We move it to kVmWait, and it then shifts itself to kRunning, which |
| 99 | * comes with a suspend-pending check. We do this after |
| 100 | */ |
| 101 | void ThreadList::SignalGo(Thread* child) { |
| 102 | Thread* self = Thread::Current(); |
| 103 | CHECK(child != self); |
| 104 | |
| 105 | { |
| 106 | MutexLock mu(lock_); |
| 107 | |
| 108 | // We wait for the child to tell us that it's in the thread list. |
| 109 | while (child->GetState() != Thread::kStarting) { |
| 110 | pthread_cond_wait(&thread_start_cond_, lock_.GetImpl()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // If we switch out of runnable and then back in, we know there's no pending suspend. |
| 115 | self->SetState(Thread::kVmWait); |
| 116 | self->SetState(Thread::kRunnable); |
| 117 | |
| 118 | // Tell the child that it's safe: it will see any future suspend request. |
| 119 | child->SetState(Thread::kVmWait); |
| 120 | pthread_cond_broadcast(&thread_start_cond_); |
| 121 | } |
| 122 | |
| 123 | void ThreadList::WaitForGo() { |
| 124 | Thread* self = Thread::Current(); |
| 125 | DCHECK(Contains(self)); |
| 126 | |
| 127 | MutexLock mu(lock_); |
| 128 | |
| 129 | // Tell our parent that we're in the thread list. |
| 130 | self->SetState(Thread::kStarting); |
| 131 | pthread_cond_broadcast(&thread_start_cond_); |
| 132 | |
| 133 | // Wait until our parent tells us there's no suspend still pending |
| 134 | // from before we were on the thread list. |
| 135 | while (self->GetState() != Thread::kVmWait) { |
| 136 | pthread_cond_wait(&thread_start_cond_, lock_.GetImpl()); |
| 137 | } |
| 138 | |
| 139 | // Enter the runnable state. We know that any pending suspend will affect us now. |
| 140 | self->SetState(Thread::kRunnable); |
| 141 | } |
| 142 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 143 | uint32_t ThreadList::AllocThreadId() { |
| 144 | MutexLock mu(lock_); |
| 145 | for (size_t i = 0; i < allocated_ids_.size(); ++i) { |
| 146 | if (!allocated_ids_[i]) { |
| 147 | allocated_ids_.set(i); |
| 148 | return i + 1; // Zero is reserved to mean "invalid". |
| 149 | } |
| 150 | } |
| 151 | LOG(FATAL) << "Out of internal thread ids"; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | void ThreadList::ReleaseThreadId(uint32_t id) { |
| 156 | lock_.AssertHeld(); |
| 157 | --id; // Zero is reserved to mean "invalid". |
| 158 | DCHECK(allocated_ids_[id]) << id; |
| 159 | allocated_ids_.reset(id); |
| 160 | } |
| 161 | |
| 162 | } // namespace art |