blob: 1a342da58547d8a8bf02e5d2e90af4e56e449013 [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 Hughes8daa0922011-09-11 13:46:25 -070057void ThreadList::Dump(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.
78 if (thread->GetState() != kStarting) {
79 LOG(FATAL) << *thread << " suspend count already zero";
80 }
Elliott Hughes47179f72011-10-27 16:44:39 -070081 return;
82 }
Elliott Hughes234ab152011-10-26 14:02:26 -070083 thread->suspend_count_ += delta;
84 if (for_debugger) {
85 thread->debug_suspend_count_ += delta;
86 }
87}
88
Elliott Hughes8d768a92011-09-14 16:35:25 -070089void ThreadList::FullSuspendCheck(Thread* thread) {
90 CHECK(thread != NULL);
91 CHECK_GE(thread->suspend_count_, 0);
92
93 MutexLock mu(thread_suspend_count_lock_);
94 if (thread->suspend_count_ == 0) {
95 return;
96 }
97
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080098 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -070099 {
Elliott Hughes34e06962012-04-09 13:55:55 -0700100 ScopedThreadStateChange tsc(thread, kSuspended);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700101 while (thread->suspend_count_ != 0) {
102 /*
103 * Wait for wakeup signal, releasing lock. The act of releasing
104 * and re-acquiring the lock provides the memory barriers we
105 * need for correct behavior on SMP.
106 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700107 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700108 }
109 CHECK_EQ(thread->suspend_count_, 0);
110 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800111 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700112}
113
Elliott Hughes475fc232011-10-25 15:00:35 -0700114void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700115 Thread* self = Thread::Current();
116
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800117 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700118
Elliott Hughes34e06962012-04-09 13:55:55 -0700119 CHECK_EQ(self->GetState(), kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800120 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700121 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700122
123 {
124 // Increment everybody's suspend count (except our own).
125 MutexLock mu(thread_suspend_count_lock_);
126 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
127 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700128 if (thread == self || (for_debugger && thread == debug_thread)) {
129 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700130 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800131 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700132 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700133 }
134 }
135
136 /*
137 * Wait for everybody in kRunnable state to stop. Other states
138 * indicate the code is either running natively or sleeping quietly.
139 * Any attempt to transition back to kRunnable will cause a check
140 * for suspension, so it should be impossible for anything to execute
141 * interpreted code or modify objects (assuming native code plays nicely).
142 *
143 * It's also okay if the thread transitions to a non-kRunnable state.
144 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700145 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700146 * so if another thread is fiddling with its suspend count (perhaps
147 * self-suspending for the debugger) it won't block while we're waiting
148 * in here.
149 */
150 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
151 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700152 if (thread == self || (for_debugger && thread == debug_thread)) {
153 continue;
154 }
155 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800156 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700157 }
158
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800159 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700160}
161
Elliott Hughes4e235312011-12-02 11:34:15 -0800162void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700163 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700164 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700165
166 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
167
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800168 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700169
Elliott Hughes01158d72011-09-19 19:47:10 -0700170 if (!Contains(thread)) {
171 return;
172 }
173
174 {
175 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800176 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700177 }
178
179 thread->WaitUntilSuspended();
180
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800181 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700182}
183
Elliott Hughes475fc232011-10-25 15:00:35 -0700184void ThreadList::SuspendSelfForDebugger() {
185 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700186
Elliott Hughes475fc232011-10-25 15:00:35 -0700187 // The debugger thread must not suspend itself due to debugger activity!
188 Thread* debug_thread = Dbg::GetDebugThread();
189 CHECK(debug_thread != NULL);
190 CHECK(self != debug_thread);
191
192 // Collisions with other suspends aren't really interesting. We want
193 // to ensure that we're the only one fiddling with the suspend count
194 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700195 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700196 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700197
198 // Suspend ourselves.
199 CHECK_GT(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700200 self->SetState(kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800201 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700202
203 // Tell JDWP that we've completed suspension. The JDWP thread can't
204 // tell us to resume before we're fully asleep because we hold the
205 // suspend count lock.
206 Dbg::ClearWaitForEventThread();
207
208 while (self->suspend_count_ != 0) {
209 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
210 if (self->suspend_count_ != 0) {
211 // The condition was signaled but we're still suspended. This
212 // can happen if the debugger lets go while a SIGQUIT thread
213 // dump event is pending (assuming SignalCatcher was resumed for
214 // just long enough to try to grab the thread-suspend lock).
215 LOG(DEBUG) << *self << " still suspended after undo "
216 << "(suspend count=" << self->suspend_count_ << ")";
217 }
218 }
219 CHECK_EQ(self->suspend_count_, 0);
Elliott Hughes34e06962012-04-09 13:55:55 -0700220 self->SetState(kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800221 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700222}
223
224void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700225 Thread* self = Thread::Current();
226
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800227 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700228
229 // Decrement the suspend counts for all threads. No need for atomic
230 // writes, since nobody should be moving until we decrement the count.
231 // We do need to hold the thread list because of JNI attaches.
232 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800233 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700234 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700235 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700236 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
237 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700238 if (thread == self || (for_debugger && thread == debug_thread)) {
239 continue;
240 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700241 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700242 }
243 }
244
245 // Broadcast a notification to all suspended threads, some or all of
246 // which may choose to wake up. No need to wait for them.
247 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800248 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700249 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700250 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700251 }
252
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800253 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700254}
255
Elliott Hughes4e235312011-12-02 11:34:15 -0800256void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700257 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800258
259 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
260 thread_list_lock_.AssertHeld();
261 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700262
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800263 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700264
265 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700266 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700267 if (!Contains(thread)) {
268 return;
269 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800270 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700271 }
272
273 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800274 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700275 MutexLock mu(thread_suspend_count_lock_);
276 thread_suspend_count_cond_.Broadcast();
277 }
278
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800279 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700280}
281
Elliott Hughes398f64b2012-03-26 18:05:48 -0700282void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) { // NOLINT
Elliott Hughes01158d72011-09-19 19:47:10 -0700283 DCHECK(thread != NULL);
284 Thread* self = Thread::Current();
285 if (thread != self) {
286 Suspend(thread);
287 }
288 callback(arg);
289 if (thread != self) {
290 Resume(thread);
291 }
292}
293
Elliott Hughes234ab152011-10-26 14:02:26 -0700294void ThreadList::UndoDebuggerSuspensions() {
295 Thread* self = Thread::Current();
296
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800297 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700298
299 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800300 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700301 MutexLock mu(thread_suspend_count_lock_);
302 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
303 Thread* thread = *it;
304 if (thread == self || thread->debug_suspend_count_ == 0) {
305 continue;
306 }
307 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
308 }
309 }
310
311 {
312 MutexLock mu(thread_suspend_count_lock_);
313 thread_suspend_count_cond_.Broadcast();
314 }
315
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800316 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700317}
318
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700319void ThreadList::Register() {
320 Thread* self = Thread::Current();
321
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800322 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700323
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800324 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700325 CHECK(!Contains(self));
326 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700327}
328
329void ThreadList::Unregister() {
330 Thread* self = Thread::Current();
331
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800332 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700333
Elliott Hughesc0f09332012-03-26 13:27:06 -0700334 // Any time-consuming destruction, plus anything that can call back into managed code or
335 // suspend and so on, must happen at this point, and not in ~Thread.
336 self->Destroy();
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700337
Elliott Hughesc0f09332012-03-26 13:27:06 -0700338 {
339 // Remove this thread from the list.
340 ScopedThreadListLock thread_list_lock;
341 CHECK(Contains(self));
342 list_.remove(self);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700343 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700344
Elliott Hughese52e49b2012-04-02 16:05:44 -0700345 // Delete the Thread* and release the thin lock id.
346 uint32_t thin_lock_id = self->thin_lock_id_;
347 delete self;
348 ReleaseThreadId(thin_lock_id);
349
Elliott Hughesc0f09332012-03-26 13:27:06 -0700350 // Clear the TLS data, so that the underlying native thread is recognizably detached.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700351 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700352 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700353
354 // Signal that a thread just detached.
355 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700356}
357
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700358void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700359 thread_list_lock_.AssertHeld();
360 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700361 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700362 }
363}
364
Elliott Hughes8daa0922011-09-11 13:46:25 -0700365void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800366 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700367 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
368 (*it)->VisitRoots(visitor, arg);
369 }
370}
371
Elliott Hughes93e74e82011-09-13 11:07:03 -0700372/*
373 * Tell a new thread it's safe to start.
374 *
375 * We must hold the thread list lock before messing with another thread.
376 * In the general case we would also need to verify that the new thread was
377 * still in the thread list, but in our case the thread has not started
378 * executing user code and therefore has not had a chance to exit.
379 *
380 * We move it to kVmWait, and it then shifts itself to kRunning, which
381 * comes with a suspend-pending check. We do this after
382 */
383void ThreadList::SignalGo(Thread* child) {
384 Thread* self = Thread::Current();
385 CHECK(child != self);
386
387 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800388 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800389 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700390
391 // We wait for the child to tell us that it's in the thread list.
Elliott Hughes34e06962012-04-09 13:55:55 -0700392 while (child->GetState() != kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700393 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700394 }
395 }
396
397 // If we switch out of runnable and then back in, we know there's no pending suspend.
Elliott Hughes34e06962012-04-09 13:55:55 -0700398 self->SetState(kVmWait);
399 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700400
401 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800402 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800403 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700404 child->SetState(kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700405 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700406}
407
408void ThreadList::WaitForGo() {
409 Thread* self = Thread::Current();
410 DCHECK(Contains(self));
411
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700412 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800413 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700414
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700415 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800416 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700417 self->SetState(kStarting);
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700418 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700419
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700420 // Wait until our parent tells us there's no suspend still pending
421 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800422 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Elliott Hughes34e06962012-04-09 13:55:55 -0700423 while (self->GetState() != kVmWait) {
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700424 thread_start_cond_.Wait(thread_list_lock_);
425 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700426 }
427
428 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800429 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700430 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
431 // started, we wait until it's over. Which means that if there's now another GC pending, our
432 // suspend count is non-zero, so switching to the runnable state will suspend us.
433 // TODO: find a better solution!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800434 {
435 ScopedHeapLock heap_lock;
436 }
Elliott Hughes34e06962012-04-09 13:55:55 -0700437 self->SetState(kRunnable);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700438}
439
Elliott Hughese52e49b2012-04-02 16:05:44 -0700440bool ThreadList::AllOtherThreadsAreDaemons() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700441 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700442 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
443 // is null.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700444 Thread* thread = *it;
445 if (thread != Thread::Current() && thread->GetPeer() != NULL && !thread->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700446 return false;
447 }
448 }
449 return true;
450}
451
Elliott Hughese52e49b2012-04-02 16:05:44 -0700452void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800453 ScopedThreadListLock thread_list_lock;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700454 while (!AllOtherThreadsAreDaemons()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700455 thread_exit_cond_.Wait(thread_list_lock_);
456 }
457}
458
459void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800460 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700461
Elliott Hughese52e49b2012-04-02 16:05:44 -0700462 // Tell all the daemons it's time to suspend.
Elliott Hughes038a8062011-09-18 14:12:41 -0700463 {
464 MutexLock mu(thread_suspend_count_lock_);
465 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
466 Thread* thread = *it;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700467 if (thread != Thread::Current()) {
468 ++thread->suspend_count_;
469 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700470 }
471 }
472
473 // Give the threads a chance to suspend, complaining if they're slow.
474 bool have_complained = false;
475 for (int i = 0; i < 10; ++i) {
476 usleep(200 * 1000);
477 bool all_suspended = true;
478 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
479 Thread* thread = *it;
Elliott Hughes34e06962012-04-09 13:55:55 -0700480 if (thread != Thread::Current() && thread->GetState() == kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700481 if (!have_complained) {
482 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
483 have_complained = true;
484 }
485 all_suspended = false;
486 }
487 }
488 if (all_suspended) {
489 return;
490 }
491 }
492}
493
Elliott Hughes8daa0922011-09-11 13:46:25 -0700494uint32_t ThreadList::AllocThreadId() {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700495 MutexLock mu(allocated_ids_lock_);
496 //ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700497 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
498 if (!allocated_ids_[i]) {
499 allocated_ids_.set(i);
500 return i + 1; // Zero is reserved to mean "invalid".
501 }
502 }
503 LOG(FATAL) << "Out of internal thread ids";
504 return 0;
505}
506
507void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700508 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700509 --id; // Zero is reserved to mean "invalid".
510 DCHECK(allocated_ids_[id]) << id;
511 allocated_ids_.reset(id);
512}
513
514} // namespace art