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 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 20 | |
| 21 | #include <cutils/trace.h> |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 22 | #include <dirent.h> |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 23 | #include <ScopedLocalRef.h> |
| 24 | #include <ScopedUtfChars.h> |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 25 | #include <sys/types.h> |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 28 | #include "base/mutex.h" |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 29 | #include "base/mutex-inl.h" |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 30 | #include "base/timing_logger.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 31 | #include "debugger.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 32 | #include "jni_internal.h" |
| 33 | #include "lock_word.h" |
| 34 | #include "monitor.h" |
| 35 | #include "scoped_thread_state_change.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "thread.h" |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 37 | #include "utils.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 38 | #include "well_known_classes.h" |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 40 | namespace art { |
| 41 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 42 | ThreadList::ThreadList() |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 43 | : suspend_all_count_(0), debug_suspend_all_count_(0), |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 44 | thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 45 | CHECK(Monitor::IsValidLockWord(LockWord::FromThinLockId(kMaxThreadId, 1))); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ThreadList::~ThreadList() { |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 49 | // 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] | 50 | // We need to detach the current thread here in case there's another thread waiting to join with |
| 51 | // us. |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 52 | if (Contains(Thread::Current())) { |
| 53 | Runtime::Current()->DetachCurrentThread(); |
| 54 | } |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 55 | |
| 56 | WaitForOtherNonDaemonThreadsToExit(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 57 | // TODO: there's an unaddressed race here where a thread may attach during shutdown, see |
| 58 | // Thread::Init. |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 59 | SuspendAllDaemonThreads(); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool ThreadList::Contains(Thread* thread) { |
| 63 | return find(list_.begin(), list_.end(), thread) != list_.end(); |
| 64 | } |
| 65 | |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 66 | bool ThreadList::Contains(pid_t tid) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 67 | for (const auto& thread : list_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 68 | if (thread->GetTid() == tid) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 75 | pid_t ThreadList::GetLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 76 | return Locks::thread_list_lock_->GetExclusiveOwnerTid(); |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 79 | void ThreadList::DumpNativeStacks(std::ostream& os) { |
| 80 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 81 | for (const auto& thread : list_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 82 | os << "DUMPING THREAD " << thread->GetTid() << "\n"; |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 83 | DumpNativeStack(os, thread->GetTid(), "\t"); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 84 | os << "\n"; |
| 85 | } |
| 86 | } |
| 87 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 88 | void ThreadList::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 89 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 90 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 91 | DumpLocked(os); |
| 92 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 93 | DumpUnattachedThreads(os); |
| 94 | } |
| 95 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 96 | static void DumpUnattachedThread(std::ostream& os, pid_t tid) NO_THREAD_SAFETY_ANALYSIS { |
| 97 | // TODO: No thread safety analysis as DumpState with a NULL thread won't access fields, should |
| 98 | // refactor DumpState to avoid skipping analysis. |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 99 | Thread::DumpState(os, NULL, tid); |
| 100 | DumpKernelStack(os, tid, " kernel: ", false); |
Brian Carlstrom | ed8b723 | 2012-06-27 17:54:47 -0700 | [diff] [blame] | 101 | // TODO: Reenable this when the native code in system_server can handle it. |
| 102 | // Currently "adb shell kill -3 `pid system_server`" will cause it to exit. |
| 103 | if (false) { |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 104 | DumpNativeStack(os, tid, " native: "); |
Brian Carlstrom | ed8b723 | 2012-06-27 17:54:47 -0700 | [diff] [blame] | 105 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 106 | os << "\n"; |
| 107 | } |
| 108 | |
| 109 | void ThreadList::DumpUnattachedThreads(std::ostream& os) { |
| 110 | DIR* d = opendir("/proc/self/task"); |
| 111 | if (!d) { |
| 112 | return; |
| 113 | } |
| 114 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 115 | Thread* self = Thread::Current(); |
Elliott Hughes | 4696b5b | 2012-10-30 10:35:10 -0700 | [diff] [blame] | 116 | dirent* e; |
| 117 | while ((e = readdir(d)) != NULL) { |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 118 | char* end; |
Elliott Hughes | 4696b5b | 2012-10-30 10:35:10 -0700 | [diff] [blame] | 119 | pid_t tid = strtol(e->d_name, &end, 10); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 120 | if (!*end) { |
| 121 | bool contains; |
| 122 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 123 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 124 | contains = Contains(tid); |
| 125 | } |
| 126 | if (!contains) { |
| 127 | DumpUnattachedThread(os, tid); |
| 128 | } |
Elliott Hughes | abbe07d | 2012-06-05 17:42:23 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | closedir(d); |
Elliott Hughes | ff73806 | 2012-02-03 15:00:42 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void ThreadList::DumpLocked(std::ostream& os) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 135 | os << "DALVIK THREADS (" << list_.size() << "):\n"; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 136 | for (const auto& thread : list_) { |
| 137 | thread->Dump(os); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 138 | os << "\n"; |
| 139 | } |
| 140 | } |
| 141 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 142 | void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) { |
| 143 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 144 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 145 | for (const auto& thread : list_) { |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 146 | if (thread != ignore1 && thread != ignore2) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 147 | CHECK(thread->IsSuspended()) |
| 148 | << "\nUnsuspended thread: <<" << *thread << "\n" |
| 149 | << "self: <<" << *Thread::Current(); |
| 150 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 151 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 154 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 155 | // Attempt to rectify locks so that we dump thread list with required locks before exiting. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 156 | static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) NO_THREAD_SAFETY_ANALYSIS __attribute__((noreturn)); |
| 157 | static void UnsafeLogFatalForThreadSuspendAllTimeout(Thread* self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 158 | Runtime* runtime = Runtime::Current(); |
| 159 | std::ostringstream ss; |
| 160 | ss << "Thread suspend timeout\n"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 161 | runtime->GetThreadList()->DumpLocked(ss); |
| 162 | LOG(FATAL) << ss.str(); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 163 | exit(0); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 164 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 165 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 166 | |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 167 | // Unlike suspending all threads where we can wait to acquire the mutator_lock_, suspending an |
| 168 | // individual thread requires polling. delay_us is the requested sleep and total_delay_us |
| 169 | // accumulates the total time spent sleeping for timeouts. The first sleep is just a yield, |
| 170 | // subsequently sleeps increase delay_us from 1ms to 500ms by doubling. |
| 171 | static void ThreadSuspendSleep(Thread* self, useconds_t* delay_us, useconds_t* total_delay_us, |
| 172 | bool holding_locks) { |
| 173 | if (!holding_locks) { |
| 174 | for (int i = kLockLevelCount - 1; i >= 0; --i) { |
| 175 | BaseMutex* held_mutex = self->GetHeldMutex(static_cast<LockLevel>(i)); |
| 176 | if (held_mutex != NULL) { |
| 177 | LOG(FATAL) << "Holding " << held_mutex->GetName() << " while sleeping for thread suspension"; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | useconds_t new_delay_us = (*delay_us) * 2; |
| 182 | CHECK_GE(new_delay_us, *delay_us); |
| 183 | if (new_delay_us < 500000) { // Don't allow sleeping to be more than 0.5s. |
| 184 | *delay_us = new_delay_us; |
| 185 | } |
| 186 | if (*delay_us == 0) { |
| 187 | sched_yield(); |
| 188 | // Default to 1 milliseconds (note that this gets multiplied by 2 before the first sleep). |
| 189 | *delay_us = 500; |
| 190 | } else { |
| 191 | usleep(*delay_us); |
| 192 | *total_delay_us += *delay_us; |
| 193 | } |
| 194 | } |
| 195 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 196 | size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 197 | Thread* self = Thread::Current(); |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 198 | Locks::mutator_lock_->AssertNotExclusiveHeld(self); |
| 199 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 200 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
| 201 | if (kDebugLocking) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 202 | CHECK_NE(self->GetState(), kRunnable); |
| 203 | } |
| 204 | |
| 205 | std::vector<Thread*> suspended_count_modified_threads; |
| 206 | size_t count = 0; |
| 207 | { |
| 208 | // Call a checkpoint function for each thread, threads which are suspend get their checkpoint |
| 209 | // manually called. |
| 210 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 211 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 212 | for (const auto& thread : list_) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 213 | if (thread != self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 214 | while (true) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 215 | if (thread->RequestCheckpoint(checkpoint_function)) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 216 | // This thread will run its checkpoint some time in the near future. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 217 | count++; |
| 218 | break; |
| 219 | } else { |
| 220 | // We are probably suspended, try to make sure that we stay suspended. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 221 | // The thread switched back to runnable. |
| 222 | if (thread->GetState() == kRunnable) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 223 | // Spurious fail, try again. |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 224 | continue; |
| 225 | } |
| 226 | thread->ModifySuspendCount(self, +1, false); |
| 227 | suspended_count_modified_threads.push_back(thread); |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | // Run the checkpoint on ourself while we wait for threads to suspend. |
| 236 | checkpoint_function->Run(self); |
| 237 | |
| 238 | // Run the checkpoint on the suspended threads. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 239 | for (const auto& thread : suspended_count_modified_threads) { |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 240 | if (!thread->IsSuspended()) { |
| 241 | // Wait until the thread is suspended. |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 242 | useconds_t total_delay_us = 0; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 243 | do { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 244 | useconds_t delay_us = 100; |
| 245 | ThreadSuspendSleep(self, &delay_us, &total_delay_us, true); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 246 | } while (!thread->IsSuspended()); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 247 | // Shouldn't need to wait for longer than 1000 microseconds. |
| 248 | constexpr useconds_t kLongWaitThresholdUS = 1000; |
| 249 | if (UNLIKELY(total_delay_us > kLongWaitThresholdUS)) { |
| 250 | LOG(WARNING) << "Waited " << total_delay_us << " us for thread suspend!"; |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | // We know for sure that the thread is suspended at this point. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 254 | checkpoint_function->Run(thread); |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 255 | { |
| 256 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 257 | thread->ModifySuspendCount(self, -1, false); |
| 258 | } |
| 259 | } |
| 260 | |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame] | 261 | { |
| 262 | // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their |
| 263 | // suspend count. Now the suspend_count_ is lowered so we must do the broadcast. |
| 264 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 265 | Thread::resume_cond_->Broadcast(self); |
| 266 | } |
| 267 | |
Mathieu Chartier | 858f1c5 | 2012-10-17 17:45:55 -0700 | [diff] [blame] | 268 | // Add one for self. |
| 269 | return count + suspended_count_modified_threads.size() + 1; |
| 270 | } |
| 271 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 272 | // Request that a checkpoint function be run on all active (non-suspended) |
| 273 | // threads. Returns the number of successful requests. |
| 274 | size_t ThreadList::RunCheckpointOnRunnableThreads(Closure* checkpoint_function) { |
| 275 | Thread* self = Thread::Current(); |
| 276 | if (kIsDebugBuild) { |
| 277 | Locks::mutator_lock_->AssertNotExclusiveHeld(self); |
| 278 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 279 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
| 280 | CHECK_NE(self->GetState(), kRunnable); |
| 281 | } |
| 282 | |
| 283 | size_t count = 0; |
| 284 | { |
| 285 | // Call a checkpoint function for each non-suspended thread. |
| 286 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 287 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 288 | for (const auto& thread : list_) { |
| 289 | if (thread != self) { |
| 290 | if (thread->RequestCheckpoint(checkpoint_function)) { |
| 291 | // This thread will run its checkpoint some time in the near future. |
| 292 | count++; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Return the number of threads that will run the checkpoint function. |
| 299 | return count; |
| 300 | } |
| 301 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 302 | void ThreadList::SuspendAll() { |
| 303 | Thread* self = Thread::Current(); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 304 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 305 | |
| 306 | VLOG(threads) << *self << " SuspendAll starting..."; |
| 307 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 308 | ATRACE_BEGIN("Suspending mutator threads"); |
| 309 | |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 310 | Locks::mutator_lock_->AssertNotHeld(self); |
| 311 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 312 | Locks::thread_suspend_count_lock_->AssertNotHeld(self); |
| 313 | if (kDebugLocking) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 314 | CHECK_NE(self->GetState(), kRunnable); |
| 315 | } |
| 316 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 317 | MutexLock mu(self, *Locks::thread_list_lock_); |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 318 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 319 | // Update global suspend all state for attaching threads. |
| 320 | ++suspend_all_count_; |
| 321 | // Increment everybody's suspend count (except our own). |
| 322 | for (const auto& thread : list_) { |
| 323 | if (thread == self) { |
| 324 | continue; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 325 | } |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 326 | VLOG(threads) << "requesting thread suspend: " << *thread; |
| 327 | thread->ModifySuspendCount(self, +1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 331 | // Block on the mutator lock until all Runnable threads release their share of access. |
| 332 | #if HAVE_TIMED_RWLOCK |
| 333 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 334 | if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 335 | UnsafeLogFatalForThreadSuspendAllTimeout(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 336 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 337 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 338 | Locks::mutator_lock_->ExclusiveLock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 339 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 340 | |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 341 | if (kDebugLocking) { |
| 342 | // Debug check that all threads are suspended. |
| 343 | AssertThreadsAreSuspended(self, self); |
| 344 | } |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 345 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 346 | ATRACE_END(); |
| 347 | ATRACE_BEGIN("Mutator threads suspended"); |
| 348 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 349 | VLOG(threads) << *self << " SuspendAll complete"; |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 352 | void ThreadList::ResumeAll() { |
| 353 | Thread* self = Thread::Current(); |
| 354 | |
| 355 | VLOG(threads) << *self << " ResumeAll starting"; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 356 | |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 357 | ATRACE_END(); |
| 358 | ATRACE_BEGIN("Resuming mutator threads"); |
| 359 | |
Mathieu Chartier | 6dda898 | 2014-03-06 11:11:48 -0800 | [diff] [blame] | 360 | if (kDebugLocking) { |
| 361 | // Debug check that all threads are suspended. |
| 362 | AssertThreadsAreSuspended(self, self); |
| 363 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 364 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 365 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 366 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 367 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 368 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 369 | // Update global suspend all state for attaching threads. |
| 370 | --suspend_all_count_; |
| 371 | // Decrement the suspend counts for all threads. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 372 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 373 | if (thread == self) { |
| 374 | continue; |
| 375 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 376 | thread->ModifySuspendCount(self, -1, false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // Broadcast a notification to all suspended threads, some or all of |
| 380 | // which may choose to wake up. No need to wait for them. |
| 381 | VLOG(threads) << *self << " ResumeAll waking others"; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 382 | Thread::resume_cond_->Broadcast(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 383 | } |
Mathieu Chartier | 6f365cc | 2014-04-23 12:42:27 -0700 | [diff] [blame] | 384 | ATRACE_END(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 385 | VLOG(threads) << *self << " ResumeAll complete"; |
| 386 | } |
| 387 | |
| 388 | void ThreadList::Resume(Thread* thread, bool for_debugger) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 389 | Thread* self = Thread::Current(); |
| 390 | DCHECK_NE(thread, self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 391 | VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : ""); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 392 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 393 | { |
| 394 | // To check Contains. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 395 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 396 | // To check IsSuspended. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 397 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
| 398 | DCHECK(thread->IsSuspended()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 399 | if (!Contains(thread)) { |
| 400 | return; |
| 401 | } |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 402 | thread->ModifySuspendCount(self, -1, for_debugger); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 406 | VLOG(threads) << "Resume(" << *thread << ") waking others"; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 407 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 408 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 411 | VLOG(threads) << "Resume(" << *thread << ") complete"; |
| 412 | } |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 413 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 414 | static void ThreadSuspendByPeerWarning(Thread* self, int level, const char* message, jobject peer) { |
| 415 | JNIEnvExt* env = self->GetJniEnv(); |
| 416 | ScopedLocalRef<jstring> |
| 417 | scoped_name_string(env, (jstring)env->GetObjectField(peer, |
| 418 | WellKnownClasses::java_lang_Thread_name)); |
| 419 | ScopedUtfChars scoped_name_chars(env, scoped_name_string.get()); |
| 420 | if (scoped_name_chars.c_str() == NULL) { |
| 421 | LOG(level) << message << ": " << peer; |
| 422 | env->ExceptionClear(); |
| 423 | } else { |
| 424 | LOG(level) << message << ": " << peer << ":" << scoped_name_chars.c_str(); |
| 425 | } |
| 426 | } |
| 427 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 428 | Thread* ThreadList::SuspendThreadByPeer(jobject peer, bool request_suspension, |
| 429 | bool debug_suspension, bool* timed_out) { |
| 430 | static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. |
| 431 | useconds_t total_delay_us = 0; |
| 432 | useconds_t delay_us = 0; |
| 433 | bool did_suspend_request = false; |
| 434 | *timed_out = false; |
| 435 | Thread* self = Thread::Current(); |
| 436 | while (true) { |
| 437 | Thread* thread; |
| 438 | { |
| 439 | ScopedObjectAccess soa(self); |
| 440 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 441 | thread = Thread::FromManagedThread(soa, peer); |
| 442 | if (thread == NULL) { |
| 443 | ThreadSuspendByPeerWarning(self, WARNING, "No such thread for suspend", peer); |
| 444 | return NULL; |
| 445 | } |
| 446 | { |
| 447 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
| 448 | if (request_suspension) { |
| 449 | thread->ModifySuspendCount(self, +1, debug_suspension); |
| 450 | request_suspension = false; |
| 451 | did_suspend_request = true; |
| 452 | } else { |
| 453 | // If the caller isn't requesting suspension, a suspension should have already occurred. |
| 454 | CHECK_GT(thread->GetSuspendCount(), 0); |
| 455 | } |
| 456 | // IsSuspended on the current thread will fail as the current thread is changed into |
| 457 | // Runnable above. As the suspend count is now raised if this is the current thread |
| 458 | // it will self suspend on transition to Runnable, making it hard to work with. It's simpler |
| 459 | // to just explicitly handle the current thread in the callers to this code. |
| 460 | CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger"; |
| 461 | // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend |
| 462 | // count, or else we've waited and it has self suspended) or is the current thread, we're |
| 463 | // done. |
| 464 | if (thread->IsSuspended()) { |
| 465 | return thread; |
| 466 | } |
| 467 | if (total_delay_us >= kTimeoutUs) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 468 | ThreadSuspendByPeerWarning(self, FATAL, "Thread suspension timed out", peer); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 469 | if (did_suspend_request) { |
| 470 | thread->ModifySuspendCount(soa.Self(), -1, debug_suspension); |
| 471 | } |
| 472 | *timed_out = true; |
| 473 | return NULL; |
| 474 | } |
| 475 | } |
| 476 | // Release locks and come out of runnable state. |
| 477 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 478 | ThreadSuspendSleep(self, &delay_us, &total_delay_us, false); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | static void ThreadSuspendByThreadIdWarning(int level, const char* message, uint32_t thread_id) { |
| 483 | LOG(level) << StringPrintf("%s: %d", message, thread_id); |
| 484 | } |
| 485 | |
| 486 | Thread* ThreadList::SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension, |
| 487 | bool* timed_out) { |
| 488 | static const useconds_t kTimeoutUs = 30 * 1000000; // 30s. |
| 489 | useconds_t total_delay_us = 0; |
| 490 | useconds_t delay_us = 0; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 491 | *timed_out = false; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 492 | Thread* suspended_thread = nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 493 | Thread* self = Thread::Current(); |
| 494 | CHECK_NE(thread_id, kInvalidThreadId); |
| 495 | while (true) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 496 | { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 497 | Thread* thread = NULL; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 498 | ScopedObjectAccess soa(self); |
| 499 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 500 | for (const auto& it : list_) { |
| 501 | if (it->GetThreadId() == thread_id) { |
| 502 | thread = it; |
| 503 | break; |
| 504 | } |
| 505 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 506 | if (thread == nullptr) { |
| 507 | CHECK(suspended_thread == nullptr) << "Suspended thread " << suspended_thread |
| 508 | << " no longer in thread list"; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 509 | // There's a race in inflating a lock and the owner giving up ownership and then dying. |
| 510 | ThreadSuspendByThreadIdWarning(WARNING, "No such thread id for suspend", thread_id); |
| 511 | return NULL; |
| 512 | } |
| 513 | { |
| 514 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 515 | if (suspended_thread == nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 516 | thread->ModifySuspendCount(self, +1, debug_suspension); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 517 | suspended_thread = thread; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 518 | } else { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 519 | CHECK_EQ(suspended_thread, thread); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 520 | // If the caller isn't requesting suspension, a suspension should have already occurred. |
| 521 | CHECK_GT(thread->GetSuspendCount(), 0); |
| 522 | } |
| 523 | // IsSuspended on the current thread will fail as the current thread is changed into |
| 524 | // Runnable above. As the suspend count is now raised if this is the current thread |
| 525 | // it will self suspend on transition to Runnable, making it hard to work with. It's simpler |
| 526 | // to just explicitly handle the current thread in the callers to this code. |
| 527 | CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger"; |
| 528 | // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend |
| 529 | // count, or else we've waited and it has self suspended) or is the current thread, we're |
| 530 | // done. |
| 531 | if (thread->IsSuspended()) { |
| 532 | return thread; |
| 533 | } |
| 534 | if (total_delay_us >= kTimeoutUs) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 535 | ThreadSuspendByThreadIdWarning(WARNING, "Thread suspension timed out", thread_id); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 536 | if (suspended_thread != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 537 | thread->ModifySuspendCount(soa.Self(), -1, debug_suspension); |
| 538 | } |
| 539 | *timed_out = true; |
| 540 | return NULL; |
| 541 | } |
| 542 | } |
| 543 | // Release locks and come out of runnable state. |
| 544 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 545 | ThreadSuspendSleep(self, &delay_us, &total_delay_us, false); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | Thread* ThreadList::FindThreadByThreadId(uint32_t thin_lock_id) { |
| 550 | Thread* self = Thread::Current(); |
| 551 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 552 | for (const auto& thread : list_) { |
| 553 | if (thread->GetThreadId() == thin_lock_id) { |
| 554 | CHECK(thread == self || thread->IsSuspended()); |
| 555 | return thread; |
| 556 | } |
| 557 | } |
| 558 | return NULL; |
| 559 | } |
| 560 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 561 | void ThreadList::SuspendAllForDebugger() { |
| 562 | Thread* self = Thread::Current(); |
| 563 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 564 | |
| 565 | VLOG(threads) << *self << " SuspendAllForDebugger starting..."; |
| 566 | |
| 567 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 568 | MutexLock mu(self, *Locks::thread_list_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 569 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 570 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 571 | // Update global suspend all state for attaching threads. |
| 572 | ++suspend_all_count_; |
| 573 | ++debug_suspend_all_count_; |
| 574 | // Increment everybody's suspend count (except our own). |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 575 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 576 | if (thread == self || thread == debug_thread) { |
| 577 | continue; |
| 578 | } |
| 579 | VLOG(threads) << "requesting thread suspend: " << *thread; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 580 | thread->ModifySuspendCount(self, +1, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 585 | // Block on the mutator lock until all Runnable threads release their share of access then |
| 586 | // immediately unlock again. |
| 587 | #if HAVE_TIMED_RWLOCK |
| 588 | // Timeout if we wait more than 30 seconds. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 589 | if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 590 | UnsafeLogFatalForThreadSuspendAllTimeout(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 591 | } else { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 592 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 593 | } |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 594 | #else |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 595 | Locks::mutator_lock_->ExclusiveLock(self); |
| 596 | Locks::mutator_lock_->ExclusiveUnlock(self); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 597 | #endif |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 598 | AssertThreadsAreSuspended(self, self, debug_thread); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 599 | |
| 600 | VLOG(threads) << *self << " SuspendAll complete"; |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 603 | void ThreadList::SuspendSelfForDebugger() { |
| 604 | Thread* self = Thread::Current(); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 605 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 606 | // The debugger thread must not suspend itself due to debugger activity! |
| 607 | Thread* debug_thread = Dbg::GetDebugThread(); |
| 608 | CHECK(debug_thread != NULL); |
| 609 | CHECK(self != debug_thread); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 610 | CHECK_NE(self->GetState(), kRunnable); |
| 611 | Locks::mutator_lock_->AssertNotHeld(self); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 612 | |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 613 | { |
| 614 | // Collisions with other suspends aren't really interesting. We want |
| 615 | // to ensure that we're the only one fiddling with the suspend count |
| 616 | // though. |
| 617 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
| 618 | self->ModifySuspendCount(self, +1, true); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 619 | CHECK_GT(self->GetSuspendCount(), 0); |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 620 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 621 | |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 622 | VLOG(threads) << *self << " self-suspending (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 623 | |
Sebastien Hertz | 21e729c | 2014-02-18 14:16:00 +0100 | [diff] [blame] | 624 | // Tell JDWP we've completed invocation and are ready to suspend. |
| 625 | DebugInvokeReq* pReq = self->GetInvokeReq(); |
| 626 | DCHECK(pReq != NULL); |
| 627 | if (pReq->invoke_needed) { |
| 628 | // Clear this before signaling. |
Sebastien Hertz | bb43b43 | 2014-04-14 11:59:08 +0200 | [diff] [blame] | 629 | pReq->Clear(); |
Sebastien Hertz | 21e729c | 2014-02-18 14:16:00 +0100 | [diff] [blame] | 630 | |
| 631 | VLOG(jdwp) << "invoke complete, signaling"; |
| 632 | MutexLock mu(self, pReq->lock); |
| 633 | pReq->cond.Signal(self); |
| 634 | } |
| 635 | |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 636 | // Tell JDWP that we've completed suspension. The JDWP thread can't |
| 637 | // tell us to resume before we're fully asleep because we hold the |
| 638 | // suspend count lock. |
| 639 | Dbg::ClearWaitForEventThread(); |
| 640 | |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 641 | { |
| 642 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 643 | while (self->GetSuspendCount() != 0) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 644 | Thread::resume_cond_->Wait(self); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 645 | if (self->GetSuspendCount() != 0) { |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 646 | // The condition was signaled but we're still suspended. This |
| 647 | // can happen if the debugger lets go while a SIGQUIT thread |
| 648 | // dump event is pending (assuming SignalCatcher was resumed for |
| 649 | // just long enough to try to grab the thread-suspend lock). |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 650 | LOG(WARNING) << *self << " still suspended after undo " |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 651 | << "(suspend count=" << self->GetSuspendCount() << ")"; |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 652 | } |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 653 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 654 | CHECK_EQ(self->GetSuspendCount(), 0); |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 655 | } |
jeffhao | a77f0f6 | 2012-12-05 17:19:31 -0800 | [diff] [blame] | 656 | |
Elliott Hughes | 1f729aa | 2012-03-02 13:55:41 -0800 | [diff] [blame] | 657 | VLOG(threads) << *self << " self-reviving (debugger)"; |
Elliott Hughes | 475fc23 | 2011-10-25 15:00:35 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 660 | void ThreadList::UndoDebuggerSuspensions() { |
| 661 | Thread* self = Thread::Current(); |
| 662 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 663 | VLOG(threads) << *self << " UndoDebuggerSuspensions starting"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 664 | |
| 665 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 666 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 667 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 668 | // Update global suspend all state for attaching threads. |
| 669 | suspend_all_count_ -= debug_suspend_all_count_; |
| 670 | debug_suspend_all_count_ = 0; |
| 671 | // Update running threads. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 672 | for (const auto& thread : list_) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 673 | if (thread == self || thread->GetDebugSuspendCount() == 0) { |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 674 | continue; |
| 675 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 676 | thread->ModifySuspendCount(self, -thread->GetDebugSuspendCount(), true); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
| 680 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 681 | MutexLock mu(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 682 | Thread::resume_cond_->Broadcast(self); |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 685 | VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete"; |
Elliott Hughes | 234ab15 | 2011-10-26 14:02:26 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 688 | void ThreadList::WaitForOtherNonDaemonThreadsToExit() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 689 | Thread* self = Thread::Current(); |
| 690 | Locks::mutator_lock_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 691 | bool all_threads_are_daemons; |
| 692 | do { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 693 | { |
| 694 | // No more threads can be born after we start to shutdown. |
| 695 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 696 | CHECK(Runtime::Current()->IsShuttingDownLocked()); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 697 | CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U); |
| 698 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 699 | all_threads_are_daemons = true; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 700 | MutexLock mu(self, *Locks::thread_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 701 | for (const auto& thread : list_) { |
Anwar Ghuloum | 9754368 | 2013-06-14 12:58:16 -0700 | [diff] [blame] | 702 | if (thread != self && !thread->IsDaemon()) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 703 | all_threads_are_daemons = false; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | if (!all_threads_are_daemons) { |
| 708 | // Wait for another thread to exit before re-checking. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 709 | thread_exit_cond_.Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 710 | } |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 711 | } while (!all_threads_are_daemons); |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | void ThreadList::SuspendAllDaemonThreads() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 715 | Thread* self = Thread::Current(); |
| 716 | MutexLock mu(self, *Locks::thread_list_lock_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 717 | { // Tell all the daemons it's time to suspend. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 718 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 719 | for (const auto& thread : list_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 720 | // This is only run after all non-daemon threads have exited, so the remainder should all be |
| 721 | // daemons. |
Ian Rogers | 7e76286 | 2012-10-22 15:45:08 -0700 | [diff] [blame] | 722 | CHECK(thread->IsDaemon()) << *thread; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 723 | if (thread != self) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 724 | thread->ModifySuspendCount(self, +1, false); |
Elliott Hughes | e52e49b | 2012-04-02 16:05:44 -0700 | [diff] [blame] | 725 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 728 | // Give the threads a chance to suspend, complaining if they're slow. |
| 729 | bool have_complained = false; |
| 730 | for (int i = 0; i < 10; ++i) { |
| 731 | usleep(200 * 1000); |
| 732 | bool all_suspended = true; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 733 | for (const auto& thread : list_) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 734 | if (thread != self && thread->GetState() == kRunnable) { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 735 | if (!have_complained) { |
| 736 | LOG(WARNING) << "daemon thread not yet suspended: " << *thread; |
| 737 | have_complained = true; |
| 738 | } |
| 739 | all_suspended = false; |
| 740 | } |
| 741 | } |
| 742 | if (all_suspended) { |
| 743 | return; |
| 744 | } |
| 745 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 746 | LOG(ERROR) << "suspend all daemons failed"; |
| 747 | } |
| 748 | void ThreadList::Register(Thread* self) { |
| 749 | DCHECK_EQ(self, Thread::Current()); |
| 750 | |
| 751 | if (VLOG_IS_ON(threads)) { |
| 752 | std::ostringstream oss; |
| 753 | self->ShortDump(oss); // We don't hold the mutator_lock_ yet and so cannot call Dump. |
Ian Rogers | 5a9ba01 | 2014-05-19 13:28:52 -0700 | [diff] [blame] | 754 | LOG(INFO) << "ThreadList::Register() " << *self << "\n" << oss.str(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing |
| 758 | // SuspendAll requests. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 759 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 760 | MutexLock mu2(self, *Locks::thread_suspend_count_lock_); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 761 | CHECK_GE(suspend_all_count_, debug_suspend_all_count_); |
Ian Rogers | 2966e13 | 2014-04-02 08:34:36 -0700 | [diff] [blame] | 762 | // Modify suspend count in increments of 1 to maintain invariants in ModifySuspendCount. While |
| 763 | // this isn't particularly efficient the suspend counts are most commonly 0 or 1. |
| 764 | for (int delta = debug_suspend_all_count_; delta > 0; delta--) { |
| 765 | self->ModifySuspendCount(self, +1, true); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 766 | } |
Ian Rogers | 2966e13 | 2014-04-02 08:34:36 -0700 | [diff] [blame] | 767 | for (int delta = suspend_all_count_ - debug_suspend_all_count_; delta > 0; delta--) { |
| 768 | self->ModifySuspendCount(self, +1, false); |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 769 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 770 | CHECK(!Contains(self)); |
| 771 | list_.push_back(self); |
| 772 | } |
| 773 | |
| 774 | void ThreadList::Unregister(Thread* self) { |
| 775 | DCHECK_EQ(self, Thread::Current()); |
| 776 | |
| 777 | VLOG(threads) << "ThreadList::Unregister() " << *self; |
| 778 | |
| 779 | // Any time-consuming destruction, plus anything that can call back into managed code or |
| 780 | // suspend and so on, must happen at this point, and not in ~Thread. |
| 781 | self->Destroy(); |
| 782 | |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 783 | uint32_t thin_lock_id = self->GetThreadId(); |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 784 | while (self != nullptr) { |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 785 | // Remove and delete the Thread* while holding the thread_list_lock_ and |
| 786 | // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended. |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 787 | // Note: deliberately not using MutexLock that could hold a stale self pointer. |
| 788 | Locks::thread_list_lock_->ExclusiveLock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 789 | CHECK(Contains(self)); |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 790 | // Note: we don't take the thread_suspend_count_lock_ here as to be suspending a thread other |
| 791 | // than yourself you need to hold the thread_list_lock_ (see Thread::ModifySuspendCount). |
| 792 | if (!self->IsSuspended()) { |
| 793 | list_.remove(self); |
| 794 | delete self; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 795 | self = nullptr; |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 796 | } |
Ian Rogers | 0878d65 | 2013-04-18 17:38:35 -0700 | [diff] [blame] | 797 | Locks::thread_list_lock_->ExclusiveUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 798 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 799 | // Release the thread ID after the thread is finished and deleted to avoid cases where we can |
| 800 | // temporarily have multiple threads with the same thread id. When this occurs, it causes |
| 801 | // problems in FindThreadByThreadId / SuspendThreadByThreadId. |
| 802 | ReleaseThreadId(nullptr, thin_lock_id); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 803 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 804 | // Clear the TLS data, so that the underlying native thread is recognizably detached. |
| 805 | // (It may wish to reattach later.) |
| 806 | CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self"); |
| 807 | |
| 808 | // Signal that a thread just detached. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 809 | MutexLock mu(NULL, *Locks::thread_list_lock_); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 810 | thread_exit_cond_.Signal(NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 814 | for (const auto& thread : list_) { |
| 815 | callback(thread, context); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 819 | void ThreadList::VisitRoots(RootCallback* callback, void* arg) const { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 820 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 821 | for (const auto& thread : list_) { |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 822 | thread->VisitRoots(callback, arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 823 | } |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 826 | class VerifyRootWrapperArg { |
| 827 | public: |
| 828 | VerifyRootWrapperArg(VerifyRootCallback* callback, void* arg) : callback_(callback), arg_(arg) { |
| 829 | } |
| 830 | VerifyRootCallback* const callback_; |
| 831 | void* const arg_; |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 832 | }; |
| 833 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 834 | static void VerifyRootWrapperCallback(mirror::Object** root, void* arg, uint32_t /*thread_id*/, |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 835 | RootType root_type) { |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 836 | VerifyRootWrapperArg* wrapperArg = reinterpret_cast<VerifyRootWrapperArg*>(arg); |
Mathieu Chartier | 7bf9f19 | 2014-04-04 11:09:41 -0700 | [diff] [blame] | 837 | wrapperArg->callback_(*root, wrapperArg->arg_, 0, NULL, root_type); |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 840 | void ThreadList::VerifyRoots(VerifyRootCallback* callback, void* arg) const { |
| 841 | VerifyRootWrapperArg wrapper(callback, arg); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 842 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 843 | for (const auto& thread : list_) { |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 844 | thread->VisitRoots(VerifyRootWrapperCallback, &wrapper); |
Mathieu Chartier | 6f1c949 | 2012-10-15 12:08:41 -0700 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 848 | uint32_t ThreadList::AllocThreadId(Thread* self) { |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 849 | MutexLock mu(self, *Locks::allocated_thread_ids_lock_); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 850 | for (size_t i = 0; i < allocated_ids_.size(); ++i) { |
| 851 | if (!allocated_ids_[i]) { |
| 852 | allocated_ids_.set(i); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 853 | return i + 1; // Zero is reserved to mean "invalid". |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | LOG(FATAL) << "Out of internal thread ids"; |
| 857 | return 0; |
| 858 | } |
| 859 | |
Ian Rogers | cfaa455 | 2012-11-26 21:00:08 -0800 | [diff] [blame] | 860 | void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) { |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 861 | MutexLock mu(self, *Locks::allocated_thread_ids_lock_); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 862 | --id; // Zero is reserved to mean "invalid". |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 863 | DCHECK(allocated_ids_[id]) << id; |
| 864 | allocated_ids_.reset(id); |
| 865 | } |
| 866 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 867 | } // namespace art |