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 | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <sys/types.h> |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 23 | #include "base/mutex.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 24 | #include "debugger.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 25 | #include "timing_logger.h" |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 26 | #include "utils.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 30 | ThreadList::ThreadList() |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 31 | : allocated_ids_lock_("allocated thread ids lock"), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 32 | suspend_all_count_(0), debug_suspend_all_count_(0), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 33 | thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | ThreadList::~ThreadList() { |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 37 | // Detach the current thread if necessary. If we failed to start, there might not be any threads. |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 38 | // We need to detach the current thread here in case there's another thread waiting to join with |
| 39 | // us. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 40 | if (Contains(Thread::Current())) { |
| 41 | Runtime::Current()->DetachCurrentThread(); |
| 42 | } |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 43 | |
| 44 | WaitForOtherNonDaemonThreadsToExit(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 45 | // TODO: there's an unaddressed race here where a thread may attach during shutdown, see |
| 46 | // Thread::Init. |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 47 | SuspendAllDaemonThreads(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | bool ThreadList::Contains(Thread* thread) { |
| 51 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 52 | } |
| 53 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 54 | bool ThreadList::Contains(pid_t tid) { |
| 55 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 56 | if ((*it)->tid_ == tid) { |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 63 | pid_t ThreadList::GetLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 64 | return Locks::thread_list_lock_->GetExclusiveOwnerTid(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 67 | void ThreadList::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 68 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 69 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 70 | DumpLocked(os); |
| 71 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 72 | DumpUnattachedThreads(os); |
| 73 | } |
| 74 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 75 | static void DumpUnattachedThread(std::ostream& os, pid_t tid) NO_THREAD_SAFETY_ANALYSIS { |
| 76 | // TODO: No thread safety analysis as DumpState with a NULL thread won't access fields, should |
| 77 | // refactor DumpState to avoid skipping analysis. |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 78 | Thread::DumpState(os, NULL, tid); |
| 79 | DumpKernelStack(os, tid, " kernel: ", false); |
Brian Carlstrom | ed8b723 | 2012-06-27 17:54:47 -0700 | [diff] [blame] | 80 | // TODO: Reenable this when the native code in system_server can handle it. |
| 81 | // Currently "adb shell kill -3 `pid system_server`" will cause it to exit. |
| 82 | if (false) { |
| 83 | DumpNativeStack(os, tid, " native: ", false); |
| 84 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 85 | os << "\n"; |
| 86 | } |
| 87 | |
| 88 | void ThreadList::DumpUnattachedThreads(std::ostream& os) { |
| 89 | DIR* d = opendir("/proc/self/task"); |
| 90 | if (!d) { |
| 91 | return; |
| 92 | } |
| 93 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 94 | Thread* self = Thread::Current(); |
Elliott Hughes | 4696b5b | 2012-10-30 10:35:10 -0700 | [diff] [blame] | 95 | dirent* e; |
| 96 | while ((e = readdir(d)) != NULL) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 97 | char* end; |
Elliott Hughes | 4696b5b | 2012-10-30 10:35:10 -0700 | [diff] [blame] | 98 | pid_t tid = strtol(e->d_name, &end, 10); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 99 | if (!*end) { |
| 100 | bool contains; |
| 101 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 102 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 103 | contains = Contains(tid); |
| 104 | } |
| 105 | if (!contains) { |
| 106 | DumpUnattachedThread(os, tid); |
| 107 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | closedir(d); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void ThreadList::DumpLocked(std::ostream& os) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 114 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 115 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 116 | (*it)->Dump(os); |
| 117 | os << "\n"; |
| 118 | } |
| 119 | } |
| 120 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 121 | void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) { |
| 122 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 123 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 124 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 125 | Thread* thread = *it; |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 126 | if (thread != ignore1 && thread != ignore2) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 127 | CHECK(thread->IsSuspended()) |
| 128 | << "\nUnsuspended thread: <<" << *thread << "\n" |
| 129 | << "self: <<" << *Thread::Current(); |
| 130 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 131 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 134 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 135 | // Attempt to rectify locks so that we dump thread list with required locks before exiting. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 136 | static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 137 | Runtime* runtime = Runtime::Current(); |
| 138 | std::ostringstream ss; |
| 139 | ss << "Thread suspend timeout\n"; |
| 140 | runtime->DumpLockHolders(ss); |
| 141 | ss << "\n"; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 142 | Locks::mutator_lock_->SharedTryLock(self); |
| 143 | if (!Locks::mutator_lock_->IsSharedHeld(self)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 144 | LOG(WARNING) << "Dumping thread list without holding mutator_lock_"; |
| 145 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 146 | Locks::thread_list_lock_->TryLock(self); |
| 147 | if (!Locks::thread_list_lock_->IsExclusiveHeld(self)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 148 | LOG(WARNING) << "Dumping thread list without holding thread_list_lock_"; |
| 149 | } |
| 150 | runtime->GetThreadList()->DumpLocked(ss); |
| 151 | LOG(FATAL) << ss.str(); |
| 152 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 153 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 154 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 155 | size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 156 | Thread* self = Thread::Current(); |
| 157 | if (kIsDebugBuild) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 158 | Locks::mutator_lock_->AssertNotExclusiveHeld(self); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 159 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 160 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
| 161 | CHECK_NE(self->GetState(), kRunnable); |
| 162 | } |
| 163 | |
| 164 | std::vector<Thread*> suspended_count_modified_threads; |
| 165 | size_t count = 0; |
| 166 | { |
| 167 | // Call a checkpoint function for each thread, threads which are suspend get their checkpoint |
| 168 | // manually called. |
| 169 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 170 | // TODO: C++0x auto. |
| 171 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 172 | Thread* thread = *it; |
| 173 | if (thread != self) { |
| 174 | for (;;) { |
| 175 | if (thread->RequestCheckpoint(checkpoint_function)) { |
| 176 | // This thread will run it's checkpoint some time in the near future. |
| 177 | count++; |
| 178 | break; |
| 179 | } else { |
| 180 | // We are probably suspended, try to make sure that we stay suspended. |
| 181 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 182 | // The thread switched back to runnable. |
| 183 | if (thread->GetState() == kRunnable) { |
| 184 | continue; |
| 185 | } |
| 186 | thread->ModifySuspendCount(self, +1, false); |
| 187 | suspended_count_modified_threads.push_back(thread); |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Run the checkpoint on ourself while we wait for threads to suspend. |
| 196 | checkpoint_function->Run(self); |
| 197 | |
| 198 | // Run the checkpoint on the suspended threads. |
| 199 | for (size_t i = 0; i < suspended_count_modified_threads.size(); ++i) { |
| 200 | Thread* thread = suspended_count_modified_threads[i]; |
| 201 | if (!thread->IsSuspended()) { |
| 202 | // Wait until the thread is suspended. |
| 203 | uint64_t start = NanoTime(); |
| 204 | do { |
| 205 | // Sleep for 100us. |
| 206 | usleep(100); |
| 207 | } while (!thread->IsSuspended()); |
| 208 | uint64_t end = NanoTime(); |
| 209 | // Shouldn't need to wait for longer than 1 millisecond. |
| 210 | const uint64_t threshold = 1; |
| 211 | if (NsToMs(end - start) > threshold) { |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 212 | LOG(INFO) << "Warning: waited longer than " << threshold << " ms for thread suspend" |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 213 | << std::endl; |
| 214 | } |
| 215 | } |
| 216 | // We know for sure that the thread is suspended at this point. |
| 217 | thread->RunCheckpointFunction(); |
| 218 | { |
| 219 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 220 | thread->ModifySuspendCount(self, -1, false); |
| 221 | } |
| 222 | } |
| 223 | |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 224 | { |
| 225 | // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their |
| 226 | // suspend count. Now the suspend_count_ is lowered so we must do the broadcast. |
| 227 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 228 | Thread::resume_cond_->Broadcast(self); |
| 229 | } |
| 230 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 231 | // Add one for self. |
| 232 | return count + suspended_count_modified_threads.size() + 1; |
| 233 | } |
| 234 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 235 | void ThreadList::SuspendAll() { |
| 236 | Thread* self = Thread::Current(); |
| 237 | |
| 238 | VLOG(threads) << *self << " SuspendAll starting..."; |
| 239 | |
| 240 | if (kIsDebugBuild) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 241 | Locks::mutator_lock_->AssertNotHeld(self); |
| 242 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 243 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 244 | CHECK_NE(self->GetState(), kRunnable); |
| 245 | } |
| 246 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 247 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 248 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 249 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 250 | // Update global suspend all state for attaching threads. |
| 251 | ++suspend_all_count_; |
| 252 | // Increment everybody's suspend count (except our own). |
| 253 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 254 | Thread* thread = *it; |
| 255 | if (thread == self) { |
| 256 | continue; |
| 257 | } |
| 258 | VLOG(threads) << "requesting thread suspend: " << *thread; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 259 | thread->ModifySuspendCount(self, +1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 264 | // Block on the mutator lock until all Runnable threads release their share of access. |
| 265 | #if HAVE_TIMED_RWLOCK |
| 266 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 267 | if (UNLIKELY(!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0))) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 268 | UnsafeLogFatalForThreadSuspendAllTimeout(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 269 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 270 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 271 | Locks::mutator_lock_->ExclusiveLock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 272 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 273 | |
| 274 | // Debug check that all threads are suspended. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 275 | AssertThreadsAreSuspended(self, self); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 276 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 277 | VLOG(threads) << *self << " SuspendAll complete"; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 280 | void ThreadList::ResumeAll() { |
| 281 | Thread* self = Thread::Current(); |
| 282 | |
| 283 | VLOG(threads) << *self << " ResumeAll starting"; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 284 | |
| 285 | // Debug check that all threads are suspended. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 286 | AssertThreadsAreSuspended(self, self); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 287 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 288 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 289 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 290 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 291 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 292 | // Update global suspend all state for attaching threads. |
| 293 | --suspend_all_count_; |
| 294 | // Decrement the suspend counts for all threads. |
| 295 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 296 | Thread* thread = *it; |
| 297 | if (thread == self) { |
| 298 | continue; |
| 299 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 300 | thread->ModifySuspendCount(self, -1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | // Broadcast a notification to all suspended threads, some or all of |
| 304 | // which may choose to wake up. No need to wait for them. |
| 305 | VLOG(threads) << *self << " ResumeAll waking others"; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 306 | Thread::resume_cond_->Broadcast(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 307 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 308 | VLOG(threads) << *self << " ResumeAll complete"; |
| 309 | } |
| 310 | |
| 311 | void ThreadList::Resume(Thread* thread, bool for_debugger) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 312 | Thread* self = Thread::Current(); |
| 313 | DCHECK_NE(thread, self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 314 | VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : ""); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 315 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 316 | { |
| 317 | // To check Contains. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 318 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 319 | // To check IsSuspended. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 320 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 321 | DCHECK(thread->IsSuspended()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 322 | if (!Contains(thread)) { |
| 323 | return; |
| 324 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 325 | thread->ModifySuspendCount(self, -1, for_debugger); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 329 | VLOG(threads) << "Resume(" << *thread << ") waking others"; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 330 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 331 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 334 | VLOG(threads) << "Resume(" << *thread << ") complete"; |
| 335 | } |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 336 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 337 | void ThreadList::SuspendAllForDebugger() { |
| 338 | Thread* self = Thread::Current(); |
| 339 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 340 | |
| 341 | VLOG(threads) << *self << " SuspendAllForDebugger starting..."; |
| 342 | |
| 343 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 344 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 345 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 346 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 347 | // Update global suspend all state for attaching threads. |
| 348 | ++suspend_all_count_; |
| 349 | ++debug_suspend_all_count_; |
| 350 | // Increment everybody's suspend count (except our own). |
| 351 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 352 | Thread* thread = *it; |
| 353 | if (thread == self || thread == debug_thread) { |
| 354 | continue; |
| 355 | } |
| 356 | VLOG(threads) << "requesting thread suspend: " << *thread; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 357 | thread->ModifySuspendCount(self, +1, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 362 | // Block on the mutator lock until all Runnable threads release their share of access then |
| 363 | // immediately unlock again. |
| 364 | #if HAVE_TIMED_RWLOCK |
| 365 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 366 | if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 367 | UnsafeLogFatalForThreadSuspendAllTimeout(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 368 | } else { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 369 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 370 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 371 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 372 | Locks::mutator_lock_->ExclusiveLock(self); |
| 373 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 374 | #endif |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 375 | AssertThreadsAreSuspended(self, self, debug_thread); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 376 | |
| 377 | VLOG(threads) << *self << " SuspendAll complete"; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 380 | void ThreadList::SuspendSelfForDebugger() { |
| 381 | Thread* self = Thread::Current(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 382 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 383 | // The debugger thread must not suspend itself due to debugger activity! |
| 384 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 385 | CHECK(debug_thread != NULL); |
| 386 | CHECK(self != debug_thread); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 387 | CHECK_NE(self->GetState(), kRunnable); |
| 388 | Locks::mutator_lock_->AssertNotHeld(self); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 389 | |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 390 | { |
| 391 | // Collisions with other suspends aren't really interesting. We want |
| 392 | // to ensure that we're the only one fiddling with the suspend count |
| 393 | // though. |
| 394 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
| 395 | self->ModifySuspendCount(self, +1, true); |
| 396 | CHECK_GT(self->suspend_count_, 0); |
| 397 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 398 | |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 399 | VLOG(threads) << *self << " self-suspending (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 400 | |
| 401 | // Tell JDWP that we've completed suspension. The JDWP thread can't |
| 402 | // tell us to resume before we're fully asleep because we hold the |
| 403 | // suspend count lock. |
| 404 | Dbg::ClearWaitForEventThread(); |
| 405 | |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 406 | { |
| 407 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
| 408 | while (self->suspend_count_ != 0) { |
| 409 | Thread::resume_cond_->Wait(self); |
| 410 | if (self->suspend_count_ != 0) { |
| 411 | // The condition was signaled but we're still suspended. This |
| 412 | // can happen if the debugger lets go while a SIGQUIT thread |
| 413 | // dump event is pending (assuming SignalCatcher was resumed for |
| 414 | // just long enough to try to grab the thread-suspend lock). |
| 415 | LOG(DEBUG) << *self << " still suspended after undo " |
| 416 | << "(suspend count=" << self->suspend_count_ << ")"; |
| 417 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 418 | } |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 419 | CHECK_EQ(self->suspend_count_, 0); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 420 | } |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 421 | |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 422 | VLOG(threads) << *self << " self-reviving (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 425 | void ThreadList::UndoDebuggerSuspensions() { |
| 426 | Thread* self = Thread::Current(); |
| 427 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 428 | VLOG(threads) << *self << " UndoDebuggerSuspensions starting"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 429 | |
| 430 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 431 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 432 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 433 | // Update global suspend all state for attaching threads. |
| 434 | suspend_all_count_ -= debug_suspend_all_count_; |
| 435 | debug_suspend_all_count_ = 0; |
| 436 | // Update running threads. |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 437 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 438 | Thread* thread = *it; |
| 439 | if (thread == self || thread->debug_suspend_count_ == 0) { |
| 440 | continue; |
| 441 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 442 | thread->ModifySuspendCount(self, -thread->debug_suspend_count_, true); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 447 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 448 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 451 | VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 454 | void ThreadList::WaitForOtherNonDaemonThreadsToExit() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 455 | Thread* self = Thread::Current(); |
| 456 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 457 | bool all_threads_are_daemons; |
| 458 | do { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 459 | { |
| 460 | // No more threads can be born after we start to shutdown. |
| 461 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 462 | CHECK(Runtime::Current()->IsShuttingDown()); |
| 463 | CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U); |
| 464 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 465 | all_threads_are_daemons = true; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 466 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 467 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 468 | // TODO: there's a race here with thread exit that's being worked around by checking if the |
| 469 | // thread has a peer. |
| 470 | Thread* thread = *it; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 471 | if (thread != self && thread->HasPeer() && !thread->IsDaemon()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 472 | all_threads_are_daemons = false; |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | if (!all_threads_are_daemons) { |
| 477 | // Wait for another thread to exit before re-checking. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 478 | thread_exit_cond_.Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 479 | } |
| 480 | } while(!all_threads_are_daemons); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void ThreadList::SuspendAllDaemonThreads() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 484 | Thread* self = Thread::Current(); |
| 485 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 486 | { // Tell all the daemons it's time to suspend. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 487 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 488 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 489 | Thread* thread = *it; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 490 | // This is only run after all non-daemon threads have exited, so the remainder should all be |
| 491 | // daemons. |
Ian Rogers | 7e76286 | 2012-10-22 15:45:08 -0700 | [diff] [blame] | 492 | CHECK(thread->IsDaemon()) << *thread; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 493 | if (thread != self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 494 | thread->ModifySuspendCount(self, +1, false); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 495 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 498 | // Give the threads a chance to suspend, complaining if they're slow. |
| 499 | bool have_complained = false; |
| 500 | for (int i = 0; i < 10; ++i) { |
| 501 | usleep(200 * 1000); |
| 502 | bool all_suspended = true; |
| 503 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 504 | Thread* thread = *it; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 505 | if (thread != self && thread->GetState() == kRunnable) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 506 | if (!have_complained) { |
| 507 | LOG(WARNING) << "daemon thread not yet suspended: " << *thread; |
| 508 | have_complained = true; |
| 509 | } |
| 510 | all_suspended = false; |
| 511 | } |
| 512 | } |
| 513 | if (all_suspended) { |
| 514 | return; |
| 515 | } |
| 516 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 517 | LOG(ERROR) << "suspend all daemons failed"; |
| 518 | } |
| 519 | void ThreadList::Register(Thread* self) { |
| 520 | DCHECK_EQ(self, Thread::Current()); |
| 521 | |
| 522 | if (VLOG_IS_ON(threads)) { |
| 523 | std::ostringstream oss; |
| 524 | self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump. |
| 525 | LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss; |
| 526 | } |
| 527 | |
| 528 | // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing |
| 529 | // SuspendAll requests. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 530 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 531 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 532 | self->suspend_count_ = suspend_all_count_; |
| 533 | self->debug_suspend_count_ = debug_suspend_all_count_; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 534 | if (self->suspend_count_ > 0) { |
| 535 | self->AtomicSetFlag(kSuspendRequest); |
| 536 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 537 | CHECK(!Contains(self)); |
| 538 | list_.push_back(self); |
| 539 | } |
| 540 | |
| 541 | void ThreadList::Unregister(Thread* self) { |
| 542 | DCHECK_EQ(self, Thread::Current()); |
| 543 | |
| 544 | VLOG(threads) << "ThreadList::Unregister() " << *self; |
| 545 | |
| 546 | // Any time-consuming destruction, plus anything that can call back into managed code or |
| 547 | // suspend and so on, must happen at this point, and not in ~Thread. |
| 548 | self->Destroy(); |
| 549 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 550 | uint32_t thin_lock_id = self->thin_lock_id_; |
| 551 | self->thin_lock_id_ = 0; |
| 552 | ReleaseThreadId(self, thin_lock_id); |
| 553 | while (self != NULL) { |
| 554 | // Remove and delete the Thread* while holding the thread_list_lock_ and |
| 555 | // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 556 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 557 | CHECK(Contains(self)); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 558 | // Note: we don't take the thread_suspend_count_lock_ here as to be suspending a thread other |
| 559 | // than yourself you need to hold the thread_list_lock_ (see Thread::ModifySuspendCount). |
| 560 | if (!self->IsSuspended()) { |
| 561 | list_.remove(self); |
| 562 | delete self; |
| 563 | self = NULL; |
| 564 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 567 | // Clear the TLS data, so that the underlying native thread is recognizably detached. |
| 568 | // (It may wish to reattach later.) |
| 569 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self"); |
| 570 | |
| 571 | // Signal that a thread just detached. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 572 | MutexLock mu(NULL, *Locks::thread_list_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 573 | thread_exit_cond_.Signal(NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) { |
| 577 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 578 | callback(*it, context); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 583 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 584 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 585 | (*it)->VisitRoots(visitor, arg); |
| 586 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 589 | void ThreadList::VerifyRoots(Heap::VerifyRootVisitor* visitor, void* arg) const { |
| 590 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 591 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 592 | (*it)->VerifyRoots(visitor, arg); |
| 593 | } |
| 594 | } |
| 595 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 596 | uint32_t ThreadList::AllocThreadId(Thread* self) { |
| 597 | MutexLock mu(self, allocated_ids_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 598 | for (size_t i = 0; i < allocated_ids_.size(); ++i) { |
| 599 | if (!allocated_ids_[i]) { |
| 600 | allocated_ids_.set(i); |
| 601 | return i + 1; // Zero is reserved to mean "invalid". |
| 602 | } |
| 603 | } |
| 604 | LOG(FATAL) << "Out of internal thread ids"; |
| 605 | return 0; |
| 606 | } |
| 607 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 608 | void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) { |
| 609 | MutexLock mu(self, allocated_ids_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 610 | --id; // Zero is reserved to mean "invalid". |
| 611 | DCHECK(allocated_ids_[id]) << id; |
| 612 | allocated_ids_.reset(id); |
| 613 | } |
| 614 | |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame^] | 615 | Thread* ThreadList::FindThreadByThinLockId(uint32_t thin_lock_id) { |
| 616 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 617 | for (It it = list_.begin(), end = list_.end(); it != end; ++it) { |
| 618 | if ((*it)->GetThinLockId() == thin_lock_id) { |
| 619 | return *it; |
| 620 | } |
| 621 | } |
| 622 | return NULL; |
| 623 | } |
| 624 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 625 | } // namespace art |