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