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 | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 21 | ThreadList::ThreadList() |
| 22 | : thread_list_lock_("thread list lock"), |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 23 | thread_start_cond_("thread_start_cond_"), |
| 24 | thread_suspend_count_lock_("thread suspend count lock"), |
| 25 | thread_suspend_count_cond_("thread_suspend_count_cond_") { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | ThreadList::~ThreadList() { |
| 29 | if (Contains(Thread::Current())) { |
| 30 | Runtime::Current()->DetachCurrentThread(); |
| 31 | } |
| 32 | |
| 33 | // All threads should have exited and unregistered when we |
| 34 | // reach this point. This means that all daemon threads had been |
| 35 | // shutdown cleanly. |
| 36 | // TODO: dump ThreadList if non-empty. |
| 37 | CHECK_EQ(list_.size(), 0U); |
| 38 | } |
| 39 | |
| 40 | bool ThreadList::Contains(Thread* thread) { |
| 41 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 42 | } |
| 43 | |
| 44 | void ThreadList::Dump(std::ostream& os) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 45 | MutexLock mu(thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 46 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 47 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 48 | (*it)->Dump(os); |
| 49 | os << "\n"; |
| 50 | } |
| 51 | } |
| 52 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 53 | void ThreadList::FullSuspendCheck(Thread* thread) { |
| 54 | CHECK(thread != NULL); |
| 55 | CHECK_GE(thread->suspend_count_, 0); |
| 56 | |
| 57 | MutexLock mu(thread_suspend_count_lock_); |
| 58 | if (thread->suspend_count_ == 0) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | //LOG(INFO) << *thread << " self-suspending"; |
| 63 | { |
| 64 | ScopedThreadStateChange tsc(thread, Thread::kSuspended); |
| 65 | while (thread->suspend_count_ != 0) { |
| 66 | /* |
| 67 | * Wait for wakeup signal, releasing lock. The act of releasing |
| 68 | * and re-acquiring the lock provides the memory barriers we |
| 69 | * need for correct behavior on SMP. |
| 70 | */ |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 71 | thread_suspend_count_cond_.Wait(thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 72 | } |
| 73 | CHECK_EQ(thread->suspend_count_, 0); |
| 74 | } |
| 75 | //LOG(INFO) << *thread << " self-reviving"; |
| 76 | } |
| 77 | |
| 78 | void ThreadList::SuspendAll() { |
| 79 | Thread* self = Thread::Current(); |
| 80 | |
| 81 | // TODO: add another thread_suspend_lock_ to avoid GC/debugger races. |
| 82 | |
| 83 | //LOG(INFO) << *self << " SuspendAll starting..."; |
| 84 | |
| 85 | MutexLock mu(thread_list_lock_); |
| 86 | |
| 87 | { |
| 88 | // Increment everybody's suspend count (except our own). |
| 89 | MutexLock mu(thread_suspend_count_lock_); |
| 90 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 91 | Thread* thread = *it; |
| 92 | if (thread != self) { |
| 93 | //LOG(INFO) << "requesting thread suspend: " << *thread; |
| 94 | ++thread->suspend_count_; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Wait for everybody in kRunnable state to stop. Other states |
| 101 | * indicate the code is either running natively or sleeping quietly. |
| 102 | * Any attempt to transition back to kRunnable will cause a check |
| 103 | * for suspension, so it should be impossible for anything to execute |
| 104 | * interpreted code or modify objects (assuming native code plays nicely). |
| 105 | * |
| 106 | * It's also okay if the thread transitions to a non-kRunnable state. |
| 107 | * |
| 108 | * Note we released the threadSuspendCountLock before getting here, |
| 109 | * so if another thread is fiddling with its suspend count (perhaps |
| 110 | * self-suspending for the debugger) it won't block while we're waiting |
| 111 | * in here. |
| 112 | */ |
| 113 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 114 | Thread* thread = *it; |
| 115 | if (thread != self) { |
| 116 | thread->WaitUntilSuspended(); |
| 117 | //LOG(INFO) << "thread suspended: " << *thread; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | //LOG(INFO) << *self << " SuspendAll complete"; |
| 122 | } |
| 123 | |
| 124 | void ThreadList::ResumeAll() { |
| 125 | Thread* self = Thread::Current(); |
| 126 | |
| 127 | //LOG(INFO) << *self << " ResumeAll starting"; |
| 128 | |
| 129 | // Decrement the suspend counts for all threads. No need for atomic |
| 130 | // writes, since nobody should be moving until we decrement the count. |
| 131 | // We do need to hold the thread list because of JNI attaches. |
| 132 | { |
| 133 | MutexLock mu1(thread_list_lock_); |
| 134 | MutexLock mu2(thread_suspend_count_lock_); |
| 135 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 136 | Thread* thread = *it; |
| 137 | if (thread != self) { |
| 138 | if (thread->suspend_count_ > 0) { |
| 139 | --thread->suspend_count_; |
| 140 | } else { |
| 141 | LOG(WARNING) << *thread << " suspend count already zero"; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Broadcast a notification to all suspended threads, some or all of |
| 148 | // which may choose to wake up. No need to wait for them. |
| 149 | { |
| 150 | //LOG(INFO) << *self << " ResumeAll waking others"; |
| 151 | MutexLock mu(thread_suspend_count_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 152 | thread_suspend_count_cond_.Broadcast(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | //LOG(INFO) << *self << " ResumeAll complete"; |
| 156 | } |
| 157 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 158 | void ThreadList::Register(Thread* thread) { |
| 159 | //LOG(INFO) << "ThreadList::Register() " << *thread; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 160 | MutexLock mu(thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 161 | CHECK(!Contains(thread)); |
| 162 | list_.push_back(thread); |
| 163 | } |
| 164 | |
| 165 | void ThreadList::Unregister() { |
| 166 | Thread* self = Thread::Current(); |
| 167 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 168 | //LOG(INFO) << "ThreadList::Unregister() " << *self; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 169 | MutexLock mu(thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 170 | |
| 171 | // Remove this thread from the list. |
| 172 | CHECK(Contains(self)); |
| 173 | list_.remove(self); |
| 174 | |
| 175 | // Delete the Thread* and release the thin lock id. |
| 176 | uint32_t thin_lock_id = self->thin_lock_id_; |
| 177 | delete self; |
| 178 | ReleaseThreadId(thin_lock_id); |
| 179 | |
| 180 | // Clear the TLS data, so that thread is recognizably detached. |
| 181 | // (It may wish to reattach later.) |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 182 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self"); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 186 | MutexLock mu(thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 187 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 188 | (*it)->VisitRoots(visitor, arg); |
| 189 | } |
| 190 | } |
| 191 | |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Tell a new thread it's safe to start. |
| 194 | * |
| 195 | * We must hold the thread list lock before messing with another thread. |
| 196 | * In the general case we would also need to verify that the new thread was |
| 197 | * still in the thread list, but in our case the thread has not started |
| 198 | * executing user code and therefore has not had a chance to exit. |
| 199 | * |
| 200 | * We move it to kVmWait, and it then shifts itself to kRunning, which |
| 201 | * comes with a suspend-pending check. We do this after |
| 202 | */ |
| 203 | void ThreadList::SignalGo(Thread* child) { |
| 204 | Thread* self = Thread::Current(); |
| 205 | CHECK(child != self); |
| 206 | |
| 207 | { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 208 | MutexLock mu(thread_list_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 209 | |
| 210 | // We wait for the child to tell us that it's in the thread list. |
| 211 | while (child->GetState() != Thread::kStarting) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 212 | thread_start_cond_.Wait(thread_list_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | // If we switch out of runnable and then back in, we know there's no pending suspend. |
| 217 | self->SetState(Thread::kVmWait); |
| 218 | self->SetState(Thread::kRunnable); |
| 219 | |
| 220 | // Tell the child that it's safe: it will see any future suspend request. |
| 221 | child->SetState(Thread::kVmWait); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 222 | thread_start_cond_.Broadcast(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void ThreadList::WaitForGo() { |
| 226 | Thread* self = Thread::Current(); |
| 227 | DCHECK(Contains(self)); |
| 228 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 229 | MutexLock mu(thread_list_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 230 | |
| 231 | // Tell our parent that we're in the thread list. |
| 232 | self->SetState(Thread::kStarting); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 233 | thread_start_cond_.Broadcast(); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 234 | |
| 235 | // Wait until our parent tells us there's no suspend still pending |
| 236 | // from before we were on the thread list. |
| 237 | while (self->GetState() != Thread::kVmWait) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 238 | thread_start_cond_.Wait(thread_list_lock_); |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Enter the runnable state. We know that any pending suspend will affect us now. |
| 242 | self->SetState(Thread::kRunnable); |
| 243 | } |
| 244 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 245 | uint32_t ThreadList::AllocThreadId() { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 246 | MutexLock mu(thread_list_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 247 | for (size_t i = 0; i < allocated_ids_.size(); ++i) { |
| 248 | if (!allocated_ids_[i]) { |
| 249 | allocated_ids_.set(i); |
| 250 | return i + 1; // Zero is reserved to mean "invalid". |
| 251 | } |
| 252 | } |
| 253 | LOG(FATAL) << "Out of internal thread ids"; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | void ThreadList::ReleaseThreadId(uint32_t id) { |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 258 | thread_list_lock_.AssertHeld(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 259 | --id; // Zero is reserved to mean "invalid". |
| 260 | DCHECK(allocated_ids_[id]) << id; |
| 261 | allocated_ids_.reset(id); |
| 262 | } |
| 263 | |
| 264 | } // namespace art |