blob: ae744208bd53828f52f22bbb6529e0519c368982 [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);
Brian Carlstromed8b7232012-06-27 17:54:47 -070078 // 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 Hughesabbe07d2012-06-05 17:42:23 -070083 os << "\n";
84}
85
86void ThreadList::DumpUnattachedThreads(std::ostream& os) {
87 DIR* d = opendir("/proc/self/task");
88 if (!d) {
89 return;
90 }
91
92 dirent de;
Elliott Hughes0d39c122012-06-06 16:41:17 -070093 dirent* e;
94 while (!readdir_r(d, &de, &e) && e != NULL) {
Elliott Hughesabbe07d2012-06-05 17:42:23 -070095 char* end;
96 pid_t tid = strtol(de.d_name, &end, 10);
97 if (!*end && !Contains(tid)) {
98 DumpUnattachedThread(os, tid);
99 }
100 }
101 closedir(d);
Elliott Hughesff738062012-02-03 15:00:42 -0800102}
103
104void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700105 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700106 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
107 (*it)->Dump(os);
108 os << "\n";
109 }
110}
111
Elliott Hughes234ab152011-10-26 14:02:26 -0700112void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
113#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800114 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
115 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -0700116 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700117#endif
Elliott Hughes47179f72011-10-27 16:44:39 -0700118 if (delta == -1 && thread->suspend_count_ <= 0) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700119 // This is expected if you attach a thread during a GC.
Ian Rogersd237a382012-06-01 08:53:29 -0700120 if (UNLIKELY(!thread->IsStillStarting())) {
121 std::ostringstream ss;
122 Runtime::Current()->GetThreadList()->DumpLocked(ss);
123 LOG(FATAL) << *thread << " suspend count already zero.\n" << ss.str();
Elliott Hughes34e06962012-04-09 13:55:55 -0700124 }
Elliott Hughes47179f72011-10-27 16:44:39 -0700125 return;
126 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700127 thread->suspend_count_ += delta;
128 if (for_debugger) {
129 thread->debug_suspend_count_ += delta;
130 }
131}
132
Elliott Hughes8d768a92011-09-14 16:35:25 -0700133void ThreadList::FullSuspendCheck(Thread* thread) {
134 CHECK(thread != NULL);
135 CHECK_GE(thread->suspend_count_, 0);
136
137 MutexLock mu(thread_suspend_count_lock_);
138 if (thread->suspend_count_ == 0) {
139 return;
140 }
141
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800142 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700143 {
Elliott Hughes34e06962012-04-09 13:55:55 -0700144 ScopedThreadStateChange tsc(thread, kSuspended);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700145 while (thread->suspend_count_ != 0) {
146 /*
147 * Wait for wakeup signal, releasing lock. The act of releasing
148 * and re-acquiring the lock provides the memory barriers we
149 * need for correct behavior on SMP.
150 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700151 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700152 }
153 CHECK_EQ(thread->suspend_count_, 0);
154 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800155 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700156}
157
Elliott Hughes475fc232011-10-25 15:00:35 -0700158void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700159 Thread* self = Thread::Current();
160
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800161 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700162
Elliott Hughes34e06962012-04-09 13:55:55 -0700163 CHECK_EQ(self->GetState(), kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800164 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700165 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700166 {
167 // Increment everybody's suspend count (except our own).
168 MutexLock mu(thread_suspend_count_lock_);
169 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
170 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700171 if (thread == self || (for_debugger && thread == debug_thread)) {
172 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700173 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800174 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700175 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700176 }
177 }
178
179 /*
180 * Wait for everybody in kRunnable state to stop. Other states
181 * indicate the code is either running natively or sleeping quietly.
182 * Any attempt to transition back to kRunnable will cause a check
183 * for suspension, so it should be impossible for anything to execute
184 * interpreted code or modify objects (assuming native code plays nicely).
185 *
186 * It's also okay if the thread transitions to a non-kRunnable state.
187 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700188 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700189 * so if another thread is fiddling with its suspend count (perhaps
190 * self-suspending for the debugger) it won't block while we're waiting
191 * in here.
192 */
193 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
194 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700195 if (thread == self || (for_debugger && thread == debug_thread)) {
196 continue;
197 }
198 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800199 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700200 }
201
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800202 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700203}
204
Elliott Hughes4e235312011-12-02 11:34:15 -0800205void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700206 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700207 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700208
209 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
210
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800211 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700212
Elliott Hughes01158d72011-09-19 19:47:10 -0700213 if (!Contains(thread)) {
214 return;
215 }
216
217 {
218 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800219 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700220 }
221
222 thread->WaitUntilSuspended();
223
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800224 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700225}
226
Elliott Hughes475fc232011-10-25 15:00:35 -0700227void ThreadList::SuspendSelfForDebugger() {
228 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700229
Elliott Hughes475fc232011-10-25 15:00:35 -0700230 // The debugger thread must not suspend itself due to debugger activity!
231 Thread* debug_thread = Dbg::GetDebugThread();
232 CHECK(debug_thread != NULL);
233 CHECK(self != debug_thread);
234
235 // Collisions with other suspends aren't really interesting. We want
236 // to ensure that we're the only one fiddling with the suspend count
237 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700238 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700239 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700240
241 // Suspend ourselves.
242 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700243 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800244 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700245
246 // Tell JDWP that we've completed suspension. The JDWP thread can't
247 // tell us to resume before we're fully asleep because we hold the
248 // suspend count lock.
249 Dbg::ClearWaitForEventThread();
250
251 while (self->suspend_count_ != 0) {
252 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
253 if (self->suspend_count_ != 0) {
254 // The condition was signaled but we're still suspended. This
255 // can happen if the debugger lets go while a SIGQUIT thread
256 // dump event is pending (assuming SignalCatcher was resumed for
257 // just long enough to try to grab the thread-suspend lock).
258 LOG(DEBUG) << *self << " still suspended after undo "
259 << "(suspend count=" << self->suspend_count_ << ")";
260 }
261 }
262 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700263 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800264 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700265}
266
267void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700268 Thread* self = Thread::Current();
269
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800270 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700271
272 // Decrement the suspend counts for all threads. No need for atomic
273 // writes, since nobody should be moving until we decrement the count.
274 // We do need to hold the thread list because of JNI attaches.
275 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800276 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700277 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700278 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700279 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
280 Thread* thread = *it;
Elliott Hughesc61a2672012-06-21 14:52:29 -0700281 if (thread == self || (for_debugger && thread == debug_thread)) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700282 continue;
283 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700284 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700285 }
286 }
287
288 // Broadcast a notification to all suspended threads, some or all of
289 // which may choose to wake up. No need to wait for them.
290 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800291 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700292 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700293 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700294 }
295
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800296 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700297}
298
Elliott Hughes4e235312011-12-02 11:34:15 -0800299void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700300 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800301
302 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
303 thread_list_lock_.AssertHeld();
304 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700305
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800306 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700307
308 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700309 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700310 if (!Contains(thread)) {
311 return;
312 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800313 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700314 }
315
316 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800317 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700318 MutexLock mu(thread_suspend_count_lock_);
319 thread_suspend_count_cond_.Broadcast();
320 }
321
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800322 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700323}
324
Elliott Hughes398f64b2012-03-26 18:05:48 -0700325void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) { // NOLINT
Elliott Hughes01158d72011-09-19 19:47:10 -0700326 DCHECK(thread != NULL);
327 Thread* self = Thread::Current();
328 if (thread != self) {
329 Suspend(thread);
330 }
331 callback(arg);
332 if (thread != self) {
333 Resume(thread);
334 }
335}
336
Elliott Hughes234ab152011-10-26 14:02:26 -0700337void ThreadList::UndoDebuggerSuspensions() {
338 Thread* self = Thread::Current();
339
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800340 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700341
342 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800343 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700344 MutexLock mu(thread_suspend_count_lock_);
345 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
346 Thread* thread = *it;
347 if (thread == self || thread->debug_suspend_count_ == 0) {
348 continue;
349 }
350 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
351 }
352 }
353
354 {
355 MutexLock mu(thread_suspend_count_lock_);
356 thread_suspend_count_cond_.Broadcast();
357 }
358
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800359 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700360}
361
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700362void ThreadList::Register() {
363 Thread* self = Thread::Current();
364
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800365 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700366
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800367 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700368 CHECK(!Contains(self));
369 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700370}
371
372void ThreadList::Unregister() {
373 Thread* self = Thread::Current();
374
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800375 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700376
Elliott Hughesc0f09332012-03-26 13:27:06 -0700377 // Any time-consuming destruction, plus anything that can call back into managed code or
378 // suspend and so on, must happen at this point, and not in ~Thread.
379 self->Destroy();
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700380
Elliott Hughesc0f09332012-03-26 13:27:06 -0700381 {
382 // Remove this thread from the list.
383 ScopedThreadListLock thread_list_lock;
384 CHECK(Contains(self));
385 list_.remove(self);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700386 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700387
Elliott Hughese52e49b2012-04-02 16:05:44 -0700388 // Delete the Thread* and release the thin lock id.
389 uint32_t thin_lock_id = self->thin_lock_id_;
390 delete self;
391 ReleaseThreadId(thin_lock_id);
392
Elliott Hughesc0f09332012-03-26 13:27:06 -0700393 // Clear the TLS data, so that the underlying native thread is recognizably detached.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700394 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700395 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700396
397 // Signal that a thread just detached.
398 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700399}
400
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700401void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughesf8349362012-06-18 15:00:06 -0700402 ScopedThreadListLock thread_list_lock;
Elliott Hughes47fce012011-10-25 18:37:19 -0700403 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700404 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700405 }
406}
407
Elliott Hughes8daa0922011-09-11 13:46:25 -0700408void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800409 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700410 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
411 (*it)->VisitRoots(visitor, arg);
412 }
413}
414
Elliott Hughes93e74e82011-09-13 11:07:03 -0700415/*
416 * Tell a new thread it's safe to start.
417 *
418 * We must hold the thread list lock before messing with another thread.
419 * In the general case we would also need to verify that the new thread was
420 * still in the thread list, but in our case the thread has not started
421 * executing user code and therefore has not had a chance to exit.
422 *
423 * We move it to kVmWait, and it then shifts itself to kRunning, which
424 * comes with a suspend-pending check. We do this after
425 */
426void ThreadList::SignalGo(Thread* child) {
427 Thread* self = Thread::Current();
428 CHECK(child != self);
429
430 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800431 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800432 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700433
434 // We wait for the child to tell us that it's in the thread list.
Elliott Hughes34e06962012-04-09 13:55:55 -0700435 while (child->GetState() != kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700436 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700437 }
438 }
439
440 // If we switch out of runnable and then back in, we know there's no pending suspend.
Elliott Hughes34e06962012-04-09 13:55:55 -0700441 self->SetState(kVmWait);
442 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700443
444 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800445 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800446 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700447 child->SetState(kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700448 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700449}
450
451void ThreadList::WaitForGo() {
452 Thread* self = Thread::Current();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700453
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700454 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800455 ScopedThreadListLock thread_list_lock;
Elliott Hughesf8349362012-06-18 15:00:06 -0700456 DCHECK(Contains(self));
Elliott Hughes93e74e82011-09-13 11:07:03 -0700457
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700458 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800459 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700460 self->SetState(kStarting);
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700461 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700462
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700463 // Wait until our parent tells us there's no suspend still pending
464 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800465 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700466 while (self->GetState() != kVmWait) {
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700467 thread_start_cond_.Wait(thread_list_lock_);
468 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700469 }
470
471 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800472 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700473 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
474 // started, we wait until it's over. Which means that if there's now another GC pending, our
475 // suspend count is non-zero, so switching to the runnable state will suspend us.
476 // TODO: find a better solution!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800477 {
478 ScopedHeapLock heap_lock;
479 }
Elliott Hughes34e06962012-04-09 13:55:55 -0700480 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700481}
482
Elliott Hughese52e49b2012-04-02 16:05:44 -0700483bool ThreadList::AllOtherThreadsAreDaemons() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700484 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700485 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
486 // is null.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700487 Thread* thread = *it;
488 if (thread != Thread::Current() && thread->GetPeer() != NULL && !thread->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700489 return false;
490 }
491 }
492 return true;
493}
494
Elliott Hughese52e49b2012-04-02 16:05:44 -0700495void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800496 ScopedThreadListLock thread_list_lock;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700497 while (!AllOtherThreadsAreDaemons()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700498 thread_exit_cond_.Wait(thread_list_lock_);
499 }
500}
501
502void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800503 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700504
Elliott Hughese52e49b2012-04-02 16:05:44 -0700505 // Tell all the daemons it's time to suspend.
Elliott Hughes038a8062011-09-18 14:12:41 -0700506 {
507 MutexLock mu(thread_suspend_count_lock_);
508 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
509 Thread* thread = *it;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700510 if (thread != Thread::Current()) {
511 ++thread->suspend_count_;
512 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700513 }
514 }
515
516 // Give the threads a chance to suspend, complaining if they're slow.
517 bool have_complained = false;
518 for (int i = 0; i < 10; ++i) {
519 usleep(200 * 1000);
520 bool all_suspended = true;
521 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
522 Thread* thread = *it;
Elliott Hughes34e06962012-04-09 13:55:55 -0700523 if (thread != Thread::Current() && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700524 if (!have_complained) {
525 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
526 have_complained = true;
527 }
528 all_suspended = false;
529 }
530 }
531 if (all_suspended) {
532 return;
533 }
534 }
535}
536
Elliott Hughes8daa0922011-09-11 13:46:25 -0700537uint32_t ThreadList::AllocThreadId() {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700538 MutexLock mu(allocated_ids_lock_);
539 //ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700540 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
541 if (!allocated_ids_[i]) {
542 allocated_ids_.set(i);
543 return i + 1; // Zero is reserved to mean "invalid".
544 }
545 }
546 LOG(FATAL) << "Out of internal thread ids";
547 return 0;
548}
549
550void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700551 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700552 --id; // Zero is reserved to mean "invalid".
553 DCHECK(allocated_ids_[id]) << id;
554 allocated_ids_.reset(id);
555}
556
557} // namespace art