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