blob: 186ddbc93e023e8c420d9074c4aaa982fcdbb2d9 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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 Hughesabbe07d2012-06-05 17:42:23 -070019#include <dirent.h>
20#include <sys/types.h>
Elliott Hughes038a8062011-09-18 14:12:41 -070021#include <unistd.h>
22
Elliott Hughes475fc232011-10-25 15:00:35 -070023#include "debugger.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080024#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070025#include "scoped_thread_list_lock.h"
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070026#include "timing_logger.h"
Elliott Hughesabbe07d2012-06-05 17:42:23 -070027#include "utils.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070028
Elliott Hughes8daa0922011-09-11 13:46:25 -070029namespace art {
30
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080031ThreadList::ThreadList()
Elliott Hughese52e49b2012-04-02 16:05:44 -070032 : allocated_ids_lock_("allocated thread ids lock"),
33 thread_list_lock_("thread list lock", kThreadListLock),
Elliott Hughese62934d2012-04-09 11:24:29 -070034 thread_start_cond_("thread start condition variable"),
35 thread_exit_cond_("thread exit condition variable"),
Elliott Hughesffb465f2012-03-01 18:46:05 -080036 thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock),
Elliott Hughese62934d2012-04-09 11:24:29 -070037 thread_suspend_count_cond_("thread suspend count condition variable") {
Elliott Hughes8daa0922011-09-11 13:46:25 -070038}
39
40ThreadList::~ThreadList() {
Elliott Hughese52e49b2012-04-02 16:05:44 -070041 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
Elliott Hughes6a144332012-04-03 13:07:11 -070042 // We need to detach the current thread here in case there's another thread waiting to join with
43 // us.
Elliott Hughes8daa0922011-09-11 13:46:25 -070044 if (Contains(Thread::Current())) {
45 Runtime::Current()->DetachCurrentThread();
46 }
Elliott Hughes6a144332012-04-03 13:07:11 -070047
48 WaitForOtherNonDaemonThreadsToExit();
49 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070050}
51
52bool ThreadList::Contains(Thread* thread) {
53 return find(list_.begin(), list_.end(), thread) != list_.end();
54}
55
Elliott Hughesabbe07d2012-06-05 17:42:23 -070056bool ThreadList::Contains(pid_t tid) {
57 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
58 if ((*it)->tid_ == tid) {
59 return true;
60 }
61 }
62 return false;
63}
64
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070065pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070066 return thread_list_lock_.GetOwner();
67}
68
Elliott Hughesc967f782012-04-16 10:23:15 -070069void ThreadList::DumpForSigQuit(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080070 ScopedThreadListLock thread_list_lock;
Elliott Hughesff738062012-02-03 15:00:42 -080071 DumpLocked(os);
Elliott Hughesabbe07d2012-06-05 17:42:23 -070072 DumpUnattachedThreads(os);
73}
74
75static void DumpUnattachedThread(std::ostream& os, pid_t tid) {
76 Thread::DumpState(os, NULL, tid);
77 DumpKernelStack(os, tid, " kernel: ", false);
78 DumpNativeStack(os, tid, " native: ", false);
79 os << "\n";
80}
81
82void ThreadList::DumpUnattachedThreads(std::ostream& os) {
83 DIR* d = opendir("/proc/self/task");
84 if (!d) {
85 return;
86 }
87
88 dirent de;
Elliott Hughes0d39c122012-06-06 16:41:17 -070089 dirent* e;
90 while (!readdir_r(d, &de, &e) && e != NULL) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070091 char* end;
92 pid_t tid = strtol(de.d_name, &end, 10);
93 if (!*end && !Contains(tid)) {
94 DumpUnattachedThread(os, tid);
95 }
96 }
97 closedir(d);
Elliott Hughesff738062012-02-03 15:00:42 -080098}
99
100void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700101 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700102 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
103 (*it)->Dump(os);
104 os << "\n";
105 }
106}
107
Elliott Hughes234ab152011-10-26 14:02:26 -0700108void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
109#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800110 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
111 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -0700112 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700113#endif
Elliott Hughes47179f72011-10-27 16:44:39 -0700114 if (delta == -1 && thread->suspend_count_ <= 0) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700115 // This is expected if you attach a thread during a GC.
Ian Rogersd237a382012-06-01 08:53:29 -0700116 if (UNLIKELY(!thread->IsStillStarting())) {
117 std::ostringstream ss;
118 Runtime::Current()->GetThreadList()->DumpLocked(ss);
119 LOG(FATAL) << *thread << " suspend count already zero.\n" << ss.str();
Elliott Hughes34e06962012-04-09 13:55:55 -0700120 }
Elliott Hughes47179f72011-10-27 16:44:39 -0700121 return;
122 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700123 thread->suspend_count_ += delta;
124 if (for_debugger) {
125 thread->debug_suspend_count_ += delta;
126 }
127}
128
Elliott Hughes8d768a92011-09-14 16:35:25 -0700129void ThreadList::FullSuspendCheck(Thread* thread) {
130 CHECK(thread != NULL);
131 CHECK_GE(thread->suspend_count_, 0);
132
133 MutexLock mu(thread_suspend_count_lock_);
134 if (thread->suspend_count_ == 0) {
135 return;
136 }
137
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800138 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700139 {
Elliott Hughes34e06962012-04-09 13:55:55 -0700140 ScopedThreadStateChange tsc(thread, kSuspended);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700141 while (thread->suspend_count_ != 0) {
142 /*
143 * Wait for wakeup signal, releasing lock. The act of releasing
144 * and re-acquiring the lock provides the memory barriers we
145 * need for correct behavior on SMP.
146 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700147 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700148 }
149 CHECK_EQ(thread->suspend_count_, 0);
150 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800151 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700152}
153
Elliott Hughes475fc232011-10-25 15:00:35 -0700154void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700155 Thread* self = Thread::Current();
156
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800157 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700158
Elliott Hughes34e06962012-04-09 13:55:55 -0700159 CHECK_EQ(self->GetState(), kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800160 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700161 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700162 {
163 // Increment everybody's suspend count (except our own).
164 MutexLock mu(thread_suspend_count_lock_);
165 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
166 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700167 if (thread == self || (for_debugger && thread == debug_thread)) {
168 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700169 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800170 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700171 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700172 }
173 }
174
175 /*
176 * Wait for everybody in kRunnable state to stop. Other states
177 * indicate the code is either running natively or sleeping quietly.
178 * Any attempt to transition back to kRunnable will cause a check
179 * for suspension, so it should be impossible for anything to execute
180 * interpreted code or modify objects (assuming native code plays nicely).
181 *
182 * It's also okay if the thread transitions to a non-kRunnable state.
183 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700184 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700185 * so if another thread is fiddling with its suspend count (perhaps
186 * self-suspending for the debugger) it won't block while we're waiting
187 * in here.
188 */
189 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
190 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700191 if (thread == self || (for_debugger && thread == debug_thread)) {
192 continue;
193 }
194 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800195 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700196 }
197
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800198 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700199}
200
Elliott Hughes4e235312011-12-02 11:34:15 -0800201void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700202 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700203 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700204
205 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
206
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800207 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700208
Elliott Hughes01158d72011-09-19 19:47:10 -0700209 if (!Contains(thread)) {
210 return;
211 }
212
213 {
214 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800215 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700216 }
217
218 thread->WaitUntilSuspended();
219
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800220 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700221}
222
Elliott Hughes475fc232011-10-25 15:00:35 -0700223void ThreadList::SuspendSelfForDebugger() {
224 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700225
Elliott Hughes475fc232011-10-25 15:00:35 -0700226 // The debugger thread must not suspend itself due to debugger activity!
227 Thread* debug_thread = Dbg::GetDebugThread();
228 CHECK(debug_thread != NULL);
229 CHECK(self != debug_thread);
230
231 // Collisions with other suspends aren't really interesting. We want
232 // to ensure that we're the only one fiddling with the suspend count
233 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700234 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700235 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700236
237 // Suspend ourselves.
238 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700239 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800240 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700241
242 // Tell JDWP that we've completed suspension. The JDWP thread can't
243 // tell us to resume before we're fully asleep because we hold the
244 // suspend count lock.
245 Dbg::ClearWaitForEventThread();
246
247 while (self->suspend_count_ != 0) {
248 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
249 if (self->suspend_count_ != 0) {
250 // The condition was signaled but we're still suspended. This
251 // can happen if the debugger lets go while a SIGQUIT thread
252 // dump event is pending (assuming SignalCatcher was resumed for
253 // just long enough to try to grab the thread-suspend lock).
254 LOG(DEBUG) << *self << " still suspended after undo "
255 << "(suspend count=" << self->suspend_count_ << ")";
256 }
257 }
258 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700259 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800260 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700261}
262
263void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700264 Thread* self = Thread::Current();
265
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800266 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700267
268 // Decrement the suspend counts for all threads. No need for atomic
269 // writes, since nobody should be moving until we decrement the count.
270 // We do need to hold the thread list because of JNI attaches.
271 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800272 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700273 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700274 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700275 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
276 Thread* thread = *it;
Elliott Hughesc61a2672012-06-21 14:52:29 -0700277 if (thread == self || (for_debugger && thread == debug_thread)) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700278 continue;
279 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700280 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700281 }
282 }
283
284 // Broadcast a notification to all suspended threads, some or all of
285 // which may choose to wake up. No need to wait for them.
286 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800287 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700288 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700289 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700290 }
291
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800292 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700293}
294
Elliott Hughes4e235312011-12-02 11:34:15 -0800295void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700296 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800297
298 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
299 thread_list_lock_.AssertHeld();
300 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700301
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800302 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700303
304 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700305 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700306 if (!Contains(thread)) {
307 return;
308 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800309 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700310 }
311
312 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800313 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700314 MutexLock mu(thread_suspend_count_lock_);
315 thread_suspend_count_cond_.Broadcast();
316 }
317
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800318 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700319}
320
Elliott Hughes398f64b2012-03-26 18:05:48 -0700321void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) { // NOLINT
Elliott Hughes01158d72011-09-19 19:47:10 -0700322 DCHECK(thread != NULL);
323 Thread* self = Thread::Current();
324 if (thread != self) {
325 Suspend(thread);
326 }
327 callback(arg);
328 if (thread != self) {
329 Resume(thread);
330 }
331}
332
Elliott Hughes234ab152011-10-26 14:02:26 -0700333void ThreadList::UndoDebuggerSuspensions() {
334 Thread* self = Thread::Current();
335
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800336 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700337
338 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800339 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700340 MutexLock mu(thread_suspend_count_lock_);
341 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
342 Thread* thread = *it;
343 if (thread == self || thread->debug_suspend_count_ == 0) {
344 continue;
345 }
346 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
347 }
348 }
349
350 {
351 MutexLock mu(thread_suspend_count_lock_);
352 thread_suspend_count_cond_.Broadcast();
353 }
354
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800355 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700356}
357
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700358void ThreadList::Register() {
359 Thread* self = Thread::Current();
360
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800361 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700362
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800363 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700364 CHECK(!Contains(self));
365 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700366}
367
368void ThreadList::Unregister() {
369 Thread* self = Thread::Current();
370
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800371 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700372
Elliott Hughesc0f09332012-03-26 13:27:06 -0700373 // Any time-consuming destruction, plus anything that can call back into managed code or
374 // suspend and so on, must happen at this point, and not in ~Thread.
375 self->Destroy();
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700376
Elliott Hughesc0f09332012-03-26 13:27:06 -0700377 {
378 // Remove this thread from the list.
379 ScopedThreadListLock thread_list_lock;
380 CHECK(Contains(self));
381 list_.remove(self);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700382 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700383
Elliott Hughese52e49b2012-04-02 16:05:44 -0700384 // Delete the Thread* and release the thin lock id.
385 uint32_t thin_lock_id = self->thin_lock_id_;
386 delete self;
387 ReleaseThreadId(thin_lock_id);
388
Elliott Hughesc0f09332012-03-26 13:27:06 -0700389 // Clear the TLS data, so that the underlying native thread is recognizably detached.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700390 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700391 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700392
393 // Signal that a thread just detached.
394 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700395}
396
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700397void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700398 ScopedThreadListLock thread_list_lock;
Elliott Hughes47fce012011-10-25 18:37:19 -0700399 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700400 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700401 }
402}
403
Elliott Hughes8daa0922011-09-11 13:46:25 -0700404void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800405 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700406 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
407 (*it)->VisitRoots(visitor, arg);
408 }
409}
410
Elliott Hughes93e74e82011-09-13 11:07:03 -0700411/*
412 * Tell a new thread it's safe to start.
413 *
414 * We must hold the thread list lock before messing with another thread.
415 * In the general case we would also need to verify that the new thread was
416 * still in the thread list, but in our case the thread has not started
417 * executing user code and therefore has not had a chance to exit.
418 *
419 * We move it to kVmWait, and it then shifts itself to kRunning, which
420 * comes with a suspend-pending check. We do this after
421 */
422void ThreadList::SignalGo(Thread* child) {
423 Thread* self = Thread::Current();
424 CHECK(child != self);
425
426 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800427 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800428 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700429
430 // We wait for the child to tell us that it's in the thread list.
Elliott Hughes34e06962012-04-09 13:55:55 -0700431 while (child->GetState() != kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700432 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700433 }
434 }
435
436 // If we switch out of runnable and then back in, we know there's no pending suspend.
Elliott Hughes34e06962012-04-09 13:55:55 -0700437 self->SetState(kVmWait);
438 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700439
440 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800441 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800442 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700443 child->SetState(kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700444 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700445}
446
447void ThreadList::WaitForGo() {
448 Thread* self = Thread::Current();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700449
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700450 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800451 ScopedThreadListLock thread_list_lock;
Elliott Hughesf8349362012-06-18 15:00:06 -0700452 DCHECK(Contains(self));
Elliott Hughes93e74e82011-09-13 11:07:03 -0700453
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700454 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800455 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700456 self->SetState(kStarting);
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700457 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700458
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700459 // Wait until our parent tells us there's no suspend still pending
460 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800461 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700462 while (self->GetState() != kVmWait) {
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700463 thread_start_cond_.Wait(thread_list_lock_);
464 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700465 }
466
467 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800468 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700469 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
470 // started, we wait until it's over. Which means that if there's now another GC pending, our
471 // suspend count is non-zero, so switching to the runnable state will suspend us.
472 // TODO: find a better solution!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800473 {
474 ScopedHeapLock heap_lock;
475 }
Elliott Hughes34e06962012-04-09 13:55:55 -0700476 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700477}
478
Elliott Hughese52e49b2012-04-02 16:05:44 -0700479bool ThreadList::AllOtherThreadsAreDaemons() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700480 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700481 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
482 // is null.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700483 Thread* thread = *it;
484 if (thread != Thread::Current() && thread->GetPeer() != NULL && !thread->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700485 return false;
486 }
487 }
488 return true;
489}
490
Elliott Hughese52e49b2012-04-02 16:05:44 -0700491void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800492 ScopedThreadListLock thread_list_lock;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700493 while (!AllOtherThreadsAreDaemons()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700494 thread_exit_cond_.Wait(thread_list_lock_);
495 }
496}
497
498void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800499 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700500
Elliott Hughese52e49b2012-04-02 16:05:44 -0700501 // Tell all the daemons it's time to suspend.
Elliott Hughes038a8062011-09-18 14:12:41 -0700502 {
503 MutexLock mu(thread_suspend_count_lock_);
504 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
505 Thread* thread = *it;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700506 if (thread != Thread::Current()) {
507 ++thread->suspend_count_;
508 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700509 }
510 }
511
512 // Give the threads a chance to suspend, complaining if they're slow.
513 bool have_complained = false;
514 for (int i = 0; i < 10; ++i) {
515 usleep(200 * 1000);
516 bool all_suspended = true;
517 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
518 Thread* thread = *it;
Elliott Hughes34e06962012-04-09 13:55:55 -0700519 if (thread != Thread::Current() && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700520 if (!have_complained) {
521 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
522 have_complained = true;
523 }
524 all_suspended = false;
525 }
526 }
527 if (all_suspended) {
528 return;
529 }
530 }
531}
532
Elliott Hughes8daa0922011-09-11 13:46:25 -0700533uint32_t ThreadList::AllocThreadId() {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700534 MutexLock mu(allocated_ids_lock_);
535 //ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700536 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
537 if (!allocated_ids_[i]) {
538 allocated_ids_.set(i);
539 return i + 1; // Zero is reserved to mean "invalid".
540 }
541 }
542 LOG(FATAL) << "Out of internal thread ids";
543 return 0;
544}
545
546void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700547 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700548 --id; // Zero is reserved to mean "invalid".
549 DCHECK(allocated_ids_[id]) << id;
550 allocated_ids_.reset(id);
551}
552
553} // namespace art