blob: e8b412c29149ccc04266a1ac1dd587f5b4fb4e73 [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 Hughes038a8062011-09-18 14:12:41 -070019#include <unistd.h>
20
Elliott Hughes475fc232011-10-25 15:00:35 -070021#include "debugger.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080022#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070023#include "scoped_thread_list_lock.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070024
Elliott Hughes8daa0922011-09-11 13:46:25 -070025namespace art {
26
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080027ThreadList::ThreadList()
Elliott Hughese52e49b2012-04-02 16:05:44 -070028 : allocated_ids_lock_("allocated thread ids lock"),
29 thread_list_lock_("thread list lock", kThreadListLock),
Elliott Hughese62934d2012-04-09 11:24:29 -070030 thread_start_cond_("thread start condition variable"),
31 thread_exit_cond_("thread exit condition variable"),
Elliott Hughesffb465f2012-03-01 18:46:05 -080032 thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock),
Elliott Hughese62934d2012-04-09 11:24:29 -070033 thread_suspend_count_cond_("thread suspend count condition variable") {
Elliott Hughes409d2732012-04-03 13:34:44 -070034 VLOG(threads) << "Default stack size: " << PrettySize(Runtime::Current()->GetDefaultStackSize());
Elliott Hughes8daa0922011-09-11 13:46:25 -070035}
36
37ThreadList::~ThreadList() {
Elliott Hughese52e49b2012-04-02 16:05:44 -070038 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
Elliott Hughes6a144332012-04-03 13:07:11 -070039 // We need to detach the current thread here in case there's another thread waiting to join with
40 // us.
Elliott Hughes8daa0922011-09-11 13:46:25 -070041 if (Contains(Thread::Current())) {
42 Runtime::Current()->DetachCurrentThread();
43 }
Elliott Hughes6a144332012-04-03 13:07:11 -070044
45 WaitForOtherNonDaemonThreadsToExit();
46 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070047}
48
49bool ThreadList::Contains(Thread* thread) {
50 return find(list_.begin(), list_.end(), thread) != list_.end();
51}
52
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070053pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070054 return thread_list_lock_.GetOwner();
55}
56
Elliott Hughesc967f782012-04-16 10:23:15 -070057void ThreadList::DumpForSigQuit(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080058 ScopedThreadListLock thread_list_lock;
Elliott Hughesff738062012-02-03 15:00:42 -080059 DumpLocked(os);
60}
61
62void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070063 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070064 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
65 (*it)->Dump(os);
66 os << "\n";
67 }
68}
69
Elliott Hughes234ab152011-10-26 14:02:26 -070070void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
71#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080072 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
73 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -070074 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -070075#endif
Elliott Hughes47179f72011-10-27 16:44:39 -070076 if (delta == -1 && thread->suspend_count_ <= 0) {
Elliott Hughes34e06962012-04-09 13:55:55 -070077 // This is expected if you attach a thread during a GC.
Ian Rogersd237a382012-06-01 08:53:29 -070078 if (UNLIKELY(!thread->IsStillStarting())) {
79 std::ostringstream ss;
80 Runtime::Current()->GetThreadList()->DumpLocked(ss);
81 LOG(FATAL) << *thread << " suspend count already zero.\n" << ss.str();
Elliott Hughes34e06962012-04-09 13:55:55 -070082 }
Elliott Hughes47179f72011-10-27 16:44:39 -070083 return;
84 }
Elliott Hughes234ab152011-10-26 14:02:26 -070085 thread->suspend_count_ += delta;
86 if (for_debugger) {
87 thread->debug_suspend_count_ += delta;
88 }
89}
90
Elliott Hughes8d768a92011-09-14 16:35:25 -070091void ThreadList::FullSuspendCheck(Thread* thread) {
92 CHECK(thread != NULL);
93 CHECK_GE(thread->suspend_count_, 0);
94
95 MutexLock mu(thread_suspend_count_lock_);
96 if (thread->suspend_count_ == 0) {
97 return;
98 }
99
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800100 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700101 {
Elliott Hughes34e06962012-04-09 13:55:55 -0700102 ScopedThreadStateChange tsc(thread, kSuspended);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700103 while (thread->suspend_count_ != 0) {
104 /*
105 * Wait for wakeup signal, releasing lock. The act of releasing
106 * and re-acquiring the lock provides the memory barriers we
107 * need for correct behavior on SMP.
108 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700109 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700110 }
111 CHECK_EQ(thread->suspend_count_, 0);
112 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800113 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700114}
115
Elliott Hughes475fc232011-10-25 15:00:35 -0700116void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700117 Thread* self = Thread::Current();
118
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800119 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700120
Elliott Hughes34e06962012-04-09 13:55:55 -0700121 CHECK_EQ(self->GetState(), kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800122 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700123 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700124
125 {
126 // Increment everybody's suspend count (except our own).
127 MutexLock mu(thread_suspend_count_lock_);
128 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
129 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700130 if (thread == self || (for_debugger && thread == debug_thread)) {
131 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700132 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800133 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700134 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700135 }
136 }
137
138 /*
139 * Wait for everybody in kRunnable state to stop. Other states
140 * indicate the code is either running natively or sleeping quietly.
141 * Any attempt to transition back to kRunnable will cause a check
142 * for suspension, so it should be impossible for anything to execute
143 * interpreted code or modify objects (assuming native code plays nicely).
144 *
145 * It's also okay if the thread transitions to a non-kRunnable state.
146 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700147 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700148 * so if another thread is fiddling with its suspend count (perhaps
149 * self-suspending for the debugger) it won't block while we're waiting
150 * in here.
151 */
152 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
153 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700154 if (thread == self || (for_debugger && thread == debug_thread)) {
155 continue;
156 }
157 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800158 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700159 }
160
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800161 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700162}
163
Elliott Hughes4e235312011-12-02 11:34:15 -0800164void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700165 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700166 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700167
168 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
169
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800170 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700171
Elliott Hughes01158d72011-09-19 19:47:10 -0700172 if (!Contains(thread)) {
173 return;
174 }
175
176 {
177 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800178 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700179 }
180
181 thread->WaitUntilSuspended();
182
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800183 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700184}
185
Elliott Hughes475fc232011-10-25 15:00:35 -0700186void ThreadList::SuspendSelfForDebugger() {
187 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700188
Elliott Hughes475fc232011-10-25 15:00:35 -0700189 // The debugger thread must not suspend itself due to debugger activity!
190 Thread* debug_thread = Dbg::GetDebugThread();
191 CHECK(debug_thread != NULL);
192 CHECK(self != debug_thread);
193
194 // Collisions with other suspends aren't really interesting. We want
195 // to ensure that we're the only one fiddling with the suspend count
196 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700197 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700198 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700199
200 // Suspend ourselves.
201 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700202 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800203 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700204
205 // Tell JDWP that we've completed suspension. The JDWP thread can't
206 // tell us to resume before we're fully asleep because we hold the
207 // suspend count lock.
208 Dbg::ClearWaitForEventThread();
209
210 while (self->suspend_count_ != 0) {
211 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
212 if (self->suspend_count_ != 0) {
213 // The condition was signaled but we're still suspended. This
214 // can happen if the debugger lets go while a SIGQUIT thread
215 // dump event is pending (assuming SignalCatcher was resumed for
216 // just long enough to try to grab the thread-suspend lock).
217 LOG(DEBUG) << *self << " still suspended after undo "
218 << "(suspend count=" << self->suspend_count_ << ")";
219 }
220 }
221 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700222 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800223 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700224}
225
226void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700227 Thread* self = Thread::Current();
228
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800229 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700230
231 // Decrement the suspend counts for all threads. No need for atomic
232 // writes, since nobody should be moving until we decrement the count.
233 // We do need to hold the thread list because of JNI attaches.
234 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800235 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700236 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700237 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700238 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
239 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700240 if (thread == self || (for_debugger && thread == debug_thread)) {
241 continue;
242 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700243 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700244 }
245 }
246
247 // Broadcast a notification to all suspended threads, some or all of
248 // which may choose to wake up. No need to wait for them.
249 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800250 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700251 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700252 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700253 }
254
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800255 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700256}
257
Elliott Hughes4e235312011-12-02 11:34:15 -0800258void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700259 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800260
261 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
262 thread_list_lock_.AssertHeld();
263 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700264
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800265 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700266
267 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700268 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700269 if (!Contains(thread)) {
270 return;
271 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800272 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700273 }
274
275 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800276 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700277 MutexLock mu(thread_suspend_count_lock_);
278 thread_suspend_count_cond_.Broadcast();
279 }
280
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800281 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700282}
283
Elliott Hughes398f64b2012-03-26 18:05:48 -0700284void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) { // NOLINT
Elliott Hughes01158d72011-09-19 19:47:10 -0700285 DCHECK(thread != NULL);
286 Thread* self = Thread::Current();
287 if (thread != self) {
288 Suspend(thread);
289 }
290 callback(arg);
291 if (thread != self) {
292 Resume(thread);
293 }
294}
295
Elliott Hughes234ab152011-10-26 14:02:26 -0700296void ThreadList::UndoDebuggerSuspensions() {
297 Thread* self = Thread::Current();
298
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800299 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700300
301 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800302 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700303 MutexLock mu(thread_suspend_count_lock_);
304 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
305 Thread* thread = *it;
306 if (thread == self || thread->debug_suspend_count_ == 0) {
307 continue;
308 }
309 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
310 }
311 }
312
313 {
314 MutexLock mu(thread_suspend_count_lock_);
315 thread_suspend_count_cond_.Broadcast();
316 }
317
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800318 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700319}
320
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700321void ThreadList::Register() {
322 Thread* self = Thread::Current();
323
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800324 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700325
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800326 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700327 CHECK(!Contains(self));
328 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700329}
330
331void ThreadList::Unregister() {
332 Thread* self = Thread::Current();
333
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800334 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700335
Elliott Hughesc0f09332012-03-26 13:27:06 -0700336 // Any time-consuming destruction, plus anything that can call back into managed code or
337 // suspend and so on, must happen at this point, and not in ~Thread.
338 self->Destroy();
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700339
Elliott Hughesc0f09332012-03-26 13:27:06 -0700340 {
341 // Remove this thread from the list.
342 ScopedThreadListLock thread_list_lock;
343 CHECK(Contains(self));
344 list_.remove(self);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700345 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700346
Elliott Hughese52e49b2012-04-02 16:05:44 -0700347 // Delete the Thread* and release the thin lock id.
348 uint32_t thin_lock_id = self->thin_lock_id_;
349 delete self;
350 ReleaseThreadId(thin_lock_id);
351
Elliott Hughesc0f09332012-03-26 13:27:06 -0700352 // Clear the TLS data, so that the underlying native thread is recognizably detached.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700353 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700354 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700355
356 // Signal that a thread just detached.
357 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700358}
359
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700360void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700361 thread_list_lock_.AssertHeld();
362 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700363 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700364 }
365}
366
Elliott Hughes8daa0922011-09-11 13:46:25 -0700367void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800368 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700369 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
370 (*it)->VisitRoots(visitor, arg);
371 }
372}
373
Elliott Hughes93e74e82011-09-13 11:07:03 -0700374/*
375 * Tell a new thread it's safe to start.
376 *
377 * We must hold the thread list lock before messing with another thread.
378 * In the general case we would also need to verify that the new thread was
379 * still in the thread list, but in our case the thread has not started
380 * executing user code and therefore has not had a chance to exit.
381 *
382 * We move it to kVmWait, and it then shifts itself to kRunning, which
383 * comes with a suspend-pending check. We do this after
384 */
385void ThreadList::SignalGo(Thread* child) {
386 Thread* self = Thread::Current();
387 CHECK(child != self);
388
389 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800390 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800391 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700392
393 // We wait for the child to tell us that it's in the thread list.
Elliott Hughes34e06962012-04-09 13:55:55 -0700394 while (child->GetState() != kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700395 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700396 }
397 }
398
399 // If we switch out of runnable and then back in, we know there's no pending suspend.
Elliott Hughes34e06962012-04-09 13:55:55 -0700400 self->SetState(kVmWait);
401 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700402
403 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800404 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800405 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700406 child->SetState(kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700407 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700408}
409
410void ThreadList::WaitForGo() {
411 Thread* self = Thread::Current();
412 DCHECK(Contains(self));
413
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700414 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800415 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700416
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700417 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800418 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700419 self->SetState(kStarting);
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700420 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700421
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700422 // Wait until our parent tells us there's no suspend still pending
423 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800424 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700425 while (self->GetState() != kVmWait) {
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700426 thread_start_cond_.Wait(thread_list_lock_);
427 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700428 }
429
430 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800431 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700432 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
433 // started, we wait until it's over. Which means that if there's now another GC pending, our
434 // suspend count is non-zero, so switching to the runnable state will suspend us.
435 // TODO: find a better solution!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800436 {
437 ScopedHeapLock heap_lock;
438 }
Elliott Hughes34e06962012-04-09 13:55:55 -0700439 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700440}
441
Elliott Hughese52e49b2012-04-02 16:05:44 -0700442bool ThreadList::AllOtherThreadsAreDaemons() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700443 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700444 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
445 // is null.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700446 Thread* thread = *it;
447 if (thread != Thread::Current() && thread->GetPeer() != NULL && !thread->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700448 return false;
449 }
450 }
451 return true;
452}
453
Elliott Hughese52e49b2012-04-02 16:05:44 -0700454void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800455 ScopedThreadListLock thread_list_lock;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700456 while (!AllOtherThreadsAreDaemons()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700457 thread_exit_cond_.Wait(thread_list_lock_);
458 }
459}
460
461void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800462 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700463
Elliott Hughese52e49b2012-04-02 16:05:44 -0700464 // Tell all the daemons it's time to suspend.
Elliott Hughes038a8062011-09-18 14:12:41 -0700465 {
466 MutexLock mu(thread_suspend_count_lock_);
467 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
468 Thread* thread = *it;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700469 if (thread != Thread::Current()) {
470 ++thread->suspend_count_;
471 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700472 }
473 }
474
475 // Give the threads a chance to suspend, complaining if they're slow.
476 bool have_complained = false;
477 for (int i = 0; i < 10; ++i) {
478 usleep(200 * 1000);
479 bool all_suspended = true;
480 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
481 Thread* thread = *it;
Elliott Hughes34e06962012-04-09 13:55:55 -0700482 if (thread != Thread::Current() && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700483 if (!have_complained) {
484 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
485 have_complained = true;
486 }
487 all_suspended = false;
488 }
489 }
490 if (all_suspended) {
491 return;
492 }
493 }
494}
495
Elliott Hughes8daa0922011-09-11 13:46:25 -0700496uint32_t ThreadList::AllocThreadId() {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700497 MutexLock mu(allocated_ids_lock_);
498 //ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700499 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
500 if (!allocated_ids_[i]) {
501 allocated_ids_.set(i);
502 return i + 1; // Zero is reserved to mean "invalid".
503 }
504 }
505 LOG(FATAL) << "Out of internal thread ids";
506 return 0;
507}
508
509void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700510 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700511 --id; // Zero is reserved to mean "invalid".
512 DCHECK(allocated_ids_[id]) << id;
513 allocated_ids_.reset(id);
514}
515
516} // namespace art