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