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