blob: 8927820a4a23cf7cf9566dc85c31aae67b84b4a9 [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 Hughesffb465f2012-03-01 18:46:05 -080028 : thread_list_lock_("thread list lock", kThreadListLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070029 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070030 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughesffb465f2012-03-01 18:46:05 -080031 thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070032 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080033 VLOG(threads) << "Default stack size: " << Runtime::Current()->GetDefaultStackSize() / KB << "KiB";
Elliott Hughes8daa0922011-09-11 13:46:25 -070034}
35
36ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070037 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070038 if (Contains(Thread::Current())) {
39 Runtime::Current()->DetachCurrentThread();
40 }
41
Elliott Hughes038a8062011-09-18 14:12:41 -070042 WaitForNonDaemonThreadsToExit();
43 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070044}
45
46bool ThreadList::Contains(Thread* thread) {
47 return find(list_.begin(), list_.end(), thread) != list_.end();
48}
49
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070050pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070051 return thread_list_lock_.GetOwner();
52}
53
Elliott Hughes8daa0922011-09-11 13:46:25 -070054void ThreadList::Dump(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080055 ScopedThreadListLock thread_list_lock;
Elliott Hughesff738062012-02-03 15:00:42 -080056 DumpLocked(os);
57}
58
59void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070060 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070061 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
62 (*it)->Dump(os);
63 os << "\n";
64 }
65}
66
Elliott Hughes234ab152011-10-26 14:02:26 -070067void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
68#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080069 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
70 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -070071 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -070072#endif
Elliott Hughes47179f72011-10-27 16:44:39 -070073 if (delta == -1 && thread->suspend_count_ <= 0) {
74 // This can happen if you attach a thread during a GC.
75 LOG(WARNING) << *thread << " suspend count already zero";
76 return;
77 }
Elliott Hughes234ab152011-10-26 14:02:26 -070078 thread->suspend_count_ += delta;
79 if (for_debugger) {
80 thread->debug_suspend_count_ += delta;
81 }
82}
83
Elliott Hughes8d768a92011-09-14 16:35:25 -070084void ThreadList::FullSuspendCheck(Thread* thread) {
85 CHECK(thread != NULL);
86 CHECK_GE(thread->suspend_count_, 0);
87
88 MutexLock mu(thread_suspend_count_lock_);
89 if (thread->suspend_count_ == 0) {
90 return;
91 }
92
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080093 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -070094 {
95 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
96 while (thread->suspend_count_ != 0) {
97 /*
98 * Wait for wakeup signal, releasing lock. The act of releasing
99 * and re-acquiring the lock provides the memory barriers we
100 * need for correct behavior on SMP.
101 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700102 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700103 }
104 CHECK_EQ(thread->suspend_count_, 0);
105 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800106 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700107}
108
Elliott Hughes475fc232011-10-25 15:00:35 -0700109void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700110 Thread* self = Thread::Current();
111
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800112 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700113
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700114 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800115 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700116 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700117
118 {
119 // Increment everybody's suspend count (except our own).
120 MutexLock mu(thread_suspend_count_lock_);
121 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
122 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700123 if (thread == self || (for_debugger && thread == debug_thread)) {
124 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700125 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800126 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700127 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700128 }
129 }
130
131 /*
132 * Wait for everybody in kRunnable state to stop. Other states
133 * indicate the code is either running natively or sleeping quietly.
134 * Any attempt to transition back to kRunnable will cause a check
135 * for suspension, so it should be impossible for anything to execute
136 * interpreted code or modify objects (assuming native code plays nicely).
137 *
138 * It's also okay if the thread transitions to a non-kRunnable state.
139 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700140 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700141 * so if another thread is fiddling with its suspend count (perhaps
142 * self-suspending for the debugger) it won't block while we're waiting
143 * in here.
144 */
145 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
146 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700147 if (thread == self || (for_debugger && thread == debug_thread)) {
148 continue;
149 }
150 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800151 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700152 }
153
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800154 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700155}
156
Elliott Hughes4e235312011-12-02 11:34:15 -0800157void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700158 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700159 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700160
161 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
162
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800163 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700164
Elliott Hughes01158d72011-09-19 19:47:10 -0700165 if (!Contains(thread)) {
166 return;
167 }
168
169 {
170 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800171 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700172 }
173
174 thread->WaitUntilSuspended();
175
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800176 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700177}
178
Elliott Hughes475fc232011-10-25 15:00:35 -0700179void ThreadList::SuspendSelfForDebugger() {
180 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700181
Elliott Hughes475fc232011-10-25 15:00:35 -0700182 // The debugger thread must not suspend itself due to debugger activity!
183 Thread* debug_thread = Dbg::GetDebugThread();
184 CHECK(debug_thread != NULL);
185 CHECK(self != debug_thread);
186
187 // Collisions with other suspends aren't really interesting. We want
188 // to ensure that we're the only one fiddling with the suspend count
189 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700190 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700191 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700192
193 // Suspend ourselves.
194 CHECK_GT(self->suspend_count_, 0);
195 self->SetState(Thread::kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800196 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700197
198 // Tell JDWP that we've completed suspension. The JDWP thread can't
199 // tell us to resume before we're fully asleep because we hold the
200 // suspend count lock.
201 Dbg::ClearWaitForEventThread();
202
203 while (self->suspend_count_ != 0) {
204 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
205 if (self->suspend_count_ != 0) {
206 // The condition was signaled but we're still suspended. This
207 // can happen if the debugger lets go while a SIGQUIT thread
208 // dump event is pending (assuming SignalCatcher was resumed for
209 // just long enough to try to grab the thread-suspend lock).
210 LOG(DEBUG) << *self << " still suspended after undo "
211 << "(suspend count=" << self->suspend_count_ << ")";
212 }
213 }
214 CHECK_EQ(self->suspend_count_, 0);
215 self->SetState(Thread::kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800216 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700217}
218
219void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700220 Thread* self = Thread::Current();
221
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800222 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700223
224 // Decrement the suspend counts for all threads. No need for atomic
225 // writes, since nobody should be moving until we decrement the count.
226 // We do need to hold the thread list because of JNI attaches.
227 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800228 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700229 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700230 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700231 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
232 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700233 if (thread == self || (for_debugger && thread == debug_thread)) {
234 continue;
235 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700236 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700237 }
238 }
239
240 // Broadcast a notification to all suspended threads, some or all of
241 // which may choose to wake up. No need to wait for them.
242 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800243 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700244 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700245 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700246 }
247
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800248 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700249}
250
Elliott Hughes4e235312011-12-02 11:34:15 -0800251void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700252 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800253
254 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
255 thread_list_lock_.AssertHeld();
256 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700257
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800258 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700259
260 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700261 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700262 if (!Contains(thread)) {
263 return;
264 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800265 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700266 }
267
268 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800269 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700270 MutexLock mu(thread_suspend_count_lock_);
271 thread_suspend_count_cond_.Broadcast();
272 }
273
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800274 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700275}
276
277void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
278 DCHECK(thread != NULL);
279 Thread* self = Thread::Current();
280 if (thread != self) {
281 Suspend(thread);
282 }
283 callback(arg);
284 if (thread != self) {
285 Resume(thread);
286 }
287}
288
Elliott Hughes234ab152011-10-26 14:02:26 -0700289void ThreadList::UndoDebuggerSuspensions() {
290 Thread* self = Thread::Current();
291
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800292 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700293
294 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800295 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700296 MutexLock mu(thread_suspend_count_lock_);
297 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
298 Thread* thread = *it;
299 if (thread == self || thread->debug_suspend_count_ == 0) {
300 continue;
301 }
302 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
303 }
304 }
305
306 {
307 MutexLock mu(thread_suspend_count_lock_);
308 thread_suspend_count_cond_.Broadcast();
309 }
310
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800311 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700312}
313
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700314void ThreadList::Register() {
315 Thread* self = Thread::Current();
316
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800317 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700318
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800319 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700320 CHECK(!Contains(self));
321 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700322}
323
324void ThreadList::Unregister() {
325 Thread* self = Thread::Current();
326
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800327 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700328
Elliott Hughesc0f09332012-03-26 13:27:06 -0700329 // Any time-consuming destruction, plus anything that can call back into managed code or
330 // suspend and so on, must happen at this point, and not in ~Thread.
331 self->Destroy();
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700332
Elliott Hughesc0f09332012-03-26 13:27:06 -0700333 {
334 // Remove this thread from the list.
335 ScopedThreadListLock thread_list_lock;
336 CHECK(Contains(self));
337 list_.remove(self);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700338
Elliott Hughesc0f09332012-03-26 13:27:06 -0700339 // Delete the Thread* and release the thin lock id.
340 uint32_t thin_lock_id = self->thin_lock_id_;
341 delete self;
342 ReleaseThreadId(thin_lock_id);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700343 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700344
Elliott Hughesc0f09332012-03-26 13:27:06 -0700345 // Clear the TLS data, so that the underlying native thread is recognizably detached.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700346 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700347 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700348
349 // Signal that a thread just detached.
350 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700351}
352
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700353void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700354 thread_list_lock_.AssertHeld();
355 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700356 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700357 }
358}
359
Elliott Hughes8daa0922011-09-11 13:46:25 -0700360void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800361 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700362 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
363 (*it)->VisitRoots(visitor, arg);
364 }
365}
366
Elliott Hughes93e74e82011-09-13 11:07:03 -0700367/*
368 * Tell a new thread it's safe to start.
369 *
370 * We must hold the thread list lock before messing with another thread.
371 * In the general case we would also need to verify that the new thread was
372 * still in the thread list, but in our case the thread has not started
373 * executing user code and therefore has not had a chance to exit.
374 *
375 * We move it to kVmWait, and it then shifts itself to kRunning, which
376 * comes with a suspend-pending check. We do this after
377 */
378void ThreadList::SignalGo(Thread* child) {
379 Thread* self = Thread::Current();
380 CHECK(child != self);
381
382 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800383 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800384 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700385
386 // We wait for the child to tell us that it's in the thread list.
387 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700388 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700389 }
390 }
391
392 // If we switch out of runnable and then back in, we know there's no pending suspend.
393 self->SetState(Thread::kVmWait);
394 self->SetState(Thread::kRunnable);
395
396 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800397 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800398 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700399 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700400 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700401}
402
403void ThreadList::WaitForGo() {
404 Thread* self = Thread::Current();
405 DCHECK(Contains(self));
406
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700407 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800408 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700409
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700410 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800411 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700412 self->SetState(Thread::kStarting);
413 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700414
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700415 // Wait until our parent tells us there's no suspend still pending
416 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800417 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700418 while (self->GetState() != Thread::kVmWait) {
419 thread_start_cond_.Wait(thread_list_lock_);
420 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700421 }
422
423 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800424 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700425 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
426 // started, we wait until it's over. Which means that if there's now another GC pending, our
427 // suspend count is non-zero, so switching to the runnable state will suspend us.
428 // TODO: find a better solution!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800429 {
430 ScopedHeapLock heap_lock;
431 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700432 self->SetState(Thread::kRunnable);
433}
434
Elliott Hughes038a8062011-09-18 14:12:41 -0700435bool ThreadList::AllThreadsAreDaemons() {
436 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700437 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
438 // is null.
439 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700440 return false;
441 }
442 }
443 return true;
444}
445
446void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800447 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700448 while (!AllThreadsAreDaemons()) {
449 thread_exit_cond_.Wait(thread_list_lock_);
450 }
451}
452
453void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800454 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700455
456 // Tell all the daemons it's time to suspend. (At this point, we know
457 // all threads are daemons.)
458 {
459 MutexLock mu(thread_suspend_count_lock_);
460 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
461 Thread* thread = *it;
462 ++thread->suspend_count_;
463 }
464 }
465
466 // Give the threads a chance to suspend, complaining if they're slow.
467 bool have_complained = false;
468 for (int i = 0; i < 10; ++i) {
469 usleep(200 * 1000);
470 bool all_suspended = true;
471 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
472 Thread* thread = *it;
473 if (thread->GetState() == Thread::kRunnable) {
474 if (!have_complained) {
475 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
476 have_complained = true;
477 }
478 all_suspended = false;
479 }
480 }
481 if (all_suspended) {
482 return;
483 }
484 }
485}
486
Elliott Hughes8daa0922011-09-11 13:46:25 -0700487uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800488 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700489 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
490 if (!allocated_ids_[i]) {
491 allocated_ids_.set(i);
492 return i + 1; // Zero is reserved to mean "invalid".
493 }
494 }
495 LOG(FATAL) << "Out of internal thread ids";
496 return 0;
497}
498
499void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700500 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700501 --id; // Zero is reserved to mean "invalid".
502 DCHECK(allocated_ids_[id]) << id;
503 allocated_ids_.reset(id);
504}
505
506} // namespace art