blob: b2cf6760442f49006fbe6535d2570428450b232a [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 Hughes475fc232011-10-25 15:00:35 -070023
Elliott Hughes8daa0922011-09-11 13:46:25 -070024namespace art {
25
Elliott Hughesbbd9d832011-11-07 14:40:00 -080026ScopedThreadListLock::ScopedThreadListLock() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080027 // Avoid deadlock between two threads trying to SuspendAll
28 // simultaneously by going to kVmWait if the lock cannot be
29 // immediately acquired.
Elliott Hughesbbd9d832011-11-07 14:40:00 -080030 ThreadList* thread_list = Runtime::Current()->GetThreadList();
31 if (!thread_list->thread_list_lock_.TryLock()) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080032 Thread* self = Thread::Current();
Elliott Hughesbbd9d832011-11-07 14:40:00 -080033 if (self == NULL) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080034 // Self may be null during shutdown, but in that case there's no point going to kVmWait.
Elliott Hughesbbd9d832011-11-07 14:40:00 -080035 thread_list->thread_list_lock_.Lock();
36 } else {
Elliott Hughesa4060e52012-03-02 16:51:35 -080037 Thread::State old_thread_state = self->SetState(Thread::kVmWait);
Elliott Hughesbbd9d832011-11-07 14:40:00 -080038 thread_list->thread_list_lock_.Lock();
Elliott Hughesa4060e52012-03-02 16:51:35 -080039 // If we have the lock, by definition there's no GC in progress (though we
40 // might be taking the lock in order to start one). We avoid the suspend
41 // check here so we don't risk going to sleep on the thread suspend count lock
42 // while holding the thread list lock.
43 self->SetStateWithoutSuspendCheck(old_thread_state);
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070044 }
45 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080046}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070047
Elliott Hughesbbd9d832011-11-07 14:40:00 -080048ScopedThreadListLock::~ScopedThreadListLock() {
49 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
50}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070051
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080052ThreadList::ThreadList()
Elliott Hughesffb465f2012-03-01 18:46:05 -080053 : thread_list_lock_("thread list lock", kThreadListLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070054 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070055 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughesffb465f2012-03-01 18:46:05 -080056 thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070057 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080058 VLOG(threads) << "Default stack size: " << Runtime::Current()->GetDefaultStackSize() / KB << "KiB";
Elliott Hughes8daa0922011-09-11 13:46:25 -070059}
60
61ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070062 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070063 if (Contains(Thread::Current())) {
64 Runtime::Current()->DetachCurrentThread();
65 }
66
Elliott Hughes038a8062011-09-18 14:12:41 -070067 WaitForNonDaemonThreadsToExit();
68 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070069}
70
71bool ThreadList::Contains(Thread* thread) {
72 return find(list_.begin(), list_.end(), thread) != list_.end();
73}
74
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070075pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070076 return thread_list_lock_.GetOwner();
77}
78
Elliott Hughes8daa0922011-09-11 13:46:25 -070079void ThreadList::Dump(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080080 ScopedThreadListLock thread_list_lock;
Elliott Hughesff738062012-02-03 15:00:42 -080081 DumpLocked(os);
82}
83
84void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070085 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070086 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
87 (*it)->Dump(os);
88 os << "\n";
89 }
90}
91
Elliott Hughes234ab152011-10-26 14:02:26 -070092void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
93#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080094 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
95 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -070096 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -070097#endif
Elliott Hughes47179f72011-10-27 16:44:39 -070098 if (delta == -1 && thread->suspend_count_ <= 0) {
99 // This can happen if you attach a thread during a GC.
100 LOG(WARNING) << *thread << " suspend count already zero";
101 return;
102 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700103 thread->suspend_count_ += delta;
104 if (for_debugger) {
105 thread->debug_suspend_count_ += delta;
106 }
107}
108
Elliott Hughes8d768a92011-09-14 16:35:25 -0700109void ThreadList::FullSuspendCheck(Thread* thread) {
110 CHECK(thread != NULL);
111 CHECK_GE(thread->suspend_count_, 0);
112
113 MutexLock mu(thread_suspend_count_lock_);
114 if (thread->suspend_count_ == 0) {
115 return;
116 }
117
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800118 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700119 {
120 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
121 while (thread->suspend_count_ != 0) {
122 /*
123 * Wait for wakeup signal, releasing lock. The act of releasing
124 * and re-acquiring the lock provides the memory barriers we
125 * need for correct behavior on SMP.
126 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700127 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700128 }
129 CHECK_EQ(thread->suspend_count_, 0);
130 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800131 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700132}
133
Elliott Hughes475fc232011-10-25 15:00:35 -0700134void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700135 Thread* self = Thread::Current();
136
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800137 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700138
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700139 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800140 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700141 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700142
143 {
144 // Increment everybody's suspend count (except our own).
145 MutexLock mu(thread_suspend_count_lock_);
146 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
147 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700148 if (thread == self || (for_debugger && thread == debug_thread)) {
149 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700150 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800151 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700152 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700153 }
154 }
155
156 /*
157 * Wait for everybody in kRunnable state to stop. Other states
158 * indicate the code is either running natively or sleeping quietly.
159 * Any attempt to transition back to kRunnable will cause a check
160 * for suspension, so it should be impossible for anything to execute
161 * interpreted code or modify objects (assuming native code plays nicely).
162 *
163 * It's also okay if the thread transitions to a non-kRunnable state.
164 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700165 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700166 * so if another thread is fiddling with its suspend count (perhaps
167 * self-suspending for the debugger) it won't block while we're waiting
168 * in here.
169 */
170 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
171 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700172 if (thread == self || (for_debugger && thread == debug_thread)) {
173 continue;
174 }
175 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800176 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700177 }
178
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800179 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700180}
181
Elliott Hughes4e235312011-12-02 11:34:15 -0800182void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700183 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700184 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700185
186 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
187
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800188 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700189
Elliott Hughes01158d72011-09-19 19:47:10 -0700190 if (!Contains(thread)) {
191 return;
192 }
193
194 {
195 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800196 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700197 }
198
199 thread->WaitUntilSuspended();
200
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800201 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700202}
203
Elliott Hughes475fc232011-10-25 15:00:35 -0700204void ThreadList::SuspendSelfForDebugger() {
205 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700206
Elliott Hughes475fc232011-10-25 15:00:35 -0700207 // The debugger thread must not suspend itself due to debugger activity!
208 Thread* debug_thread = Dbg::GetDebugThread();
209 CHECK(debug_thread != NULL);
210 CHECK(self != debug_thread);
211
212 // Collisions with other suspends aren't really interesting. We want
213 // to ensure that we're the only one fiddling with the suspend count
214 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700215 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700216 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700217
218 // Suspend ourselves.
219 CHECK_GT(self->suspend_count_, 0);
220 self->SetState(Thread::kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800221 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700222
223 // Tell JDWP that we've completed suspension. The JDWP thread can't
224 // tell us to resume before we're fully asleep because we hold the
225 // suspend count lock.
226 Dbg::ClearWaitForEventThread();
227
228 while (self->suspend_count_ != 0) {
229 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
230 if (self->suspend_count_ != 0) {
231 // The condition was signaled but we're still suspended. This
232 // can happen if the debugger lets go while a SIGQUIT thread
233 // dump event is pending (assuming SignalCatcher was resumed for
234 // just long enough to try to grab the thread-suspend lock).
235 LOG(DEBUG) << *self << " still suspended after undo "
236 << "(suspend count=" << self->suspend_count_ << ")";
237 }
238 }
239 CHECK_EQ(self->suspend_count_, 0);
240 self->SetState(Thread::kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800241 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700242}
243
244void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700245 Thread* self = Thread::Current();
246
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800247 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700248
249 // Decrement the suspend counts for all threads. No need for atomic
250 // writes, since nobody should be moving until we decrement the count.
251 // We do need to hold the thread list because of JNI attaches.
252 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800253 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700254 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700255 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700256 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
257 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700258 if (thread == self || (for_debugger && thread == debug_thread)) {
259 continue;
260 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700261 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700262 }
263 }
264
265 // Broadcast a notification to all suspended threads, some or all of
266 // which may choose to wake up. No need to wait for them.
267 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800268 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700269 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700270 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700271 }
272
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800273 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700274}
275
Elliott Hughes4e235312011-12-02 11:34:15 -0800276void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700277 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800278
279 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
280 thread_list_lock_.AssertHeld();
281 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700282
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800283 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700284
285 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700286 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700287 if (!Contains(thread)) {
288 return;
289 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800290 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700291 }
292
293 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800294 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700295 MutexLock mu(thread_suspend_count_lock_);
296 thread_suspend_count_cond_.Broadcast();
297 }
298
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800299 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700300}
301
302void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
303 DCHECK(thread != NULL);
304 Thread* self = Thread::Current();
305 if (thread != self) {
306 Suspend(thread);
307 }
308 callback(arg);
309 if (thread != self) {
310 Resume(thread);
311 }
312}
313
Elliott Hughes234ab152011-10-26 14:02:26 -0700314void ThreadList::UndoDebuggerSuspensions() {
315 Thread* self = Thread::Current();
316
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800317 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700318
319 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800320 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700321 MutexLock mu(thread_suspend_count_lock_);
322 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
323 Thread* thread = *it;
324 if (thread == self || thread->debug_suspend_count_ == 0) {
325 continue;
326 }
327 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
328 }
329 }
330
331 {
332 MutexLock mu(thread_suspend_count_lock_);
333 thread_suspend_count_cond_.Broadcast();
334 }
335
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800336 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700337}
338
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700339void ThreadList::Register() {
340 Thread* self = Thread::Current();
341
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800342 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700343
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800344 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700345 CHECK(!Contains(self));
346 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700347}
348
349void ThreadList::Unregister() {
350 Thread* self = Thread::Current();
351
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800352 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700353
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700354 if (self->GetPeer() != NULL) {
355 self->SetState(Thread::kRunnable);
356
357 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
358 // down the Thread* and removing it from the thread list (or start taking any locks).
359 self->HandleUncaughtExceptions();
360
361 // Make sure we remove from ThreadGroup before taking the
362 // thread_list_lock_ since it allocates an Iterator which can cause
363 // a GC which will want to suspend.
364 self->RemoveFromThreadGroup();
365 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700366
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800367 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700368
369 // Remove this thread from the list.
370 CHECK(Contains(self));
371 list_.remove(self);
372
373 // Delete the Thread* and release the thin lock id.
374 uint32_t thin_lock_id = self->thin_lock_id_;
375 delete self;
376 ReleaseThreadId(thin_lock_id);
377
378 // Clear the TLS data, so that thread is recognizably detached.
379 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700380 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700381
382 // Signal that a thread just detached.
383 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700384}
385
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700386void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700387 thread_list_lock_.AssertHeld();
388 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700389 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700390 }
391}
392
Elliott Hughes8daa0922011-09-11 13:46:25 -0700393void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800394 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700395 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
396 (*it)->VisitRoots(visitor, arg);
397 }
398}
399
Elliott Hughes93e74e82011-09-13 11:07:03 -0700400/*
401 * Tell a new thread it's safe to start.
402 *
403 * We must hold the thread list lock before messing with another thread.
404 * In the general case we would also need to verify that the new thread was
405 * still in the thread list, but in our case the thread has not started
406 * executing user code and therefore has not had a chance to exit.
407 *
408 * We move it to kVmWait, and it then shifts itself to kRunning, which
409 * comes with a suspend-pending check. We do this after
410 */
411void ThreadList::SignalGo(Thread* child) {
412 Thread* self = Thread::Current();
413 CHECK(child != self);
414
415 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800416 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800417 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700418
419 // We wait for the child to tell us that it's in the thread list.
420 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700421 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700422 }
423 }
424
425 // If we switch out of runnable and then back in, we know there's no pending suspend.
426 self->SetState(Thread::kVmWait);
427 self->SetState(Thread::kRunnable);
428
429 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800430 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800431 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700432 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700433 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700434}
435
436void ThreadList::WaitForGo() {
437 Thread* self = Thread::Current();
438 DCHECK(Contains(self));
439
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700440 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800441 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700442
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700443 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800444 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700445 self->SetState(Thread::kStarting);
446 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700447
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700448 // Wait until our parent tells us there's no suspend still pending
449 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800450 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700451 while (self->GetState() != Thread::kVmWait) {
452 thread_start_cond_.Wait(thread_list_lock_);
453 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700454 }
455
456 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800457 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700458 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
459 // started, we wait until it's over. Which means that if there's now another GC pending, our
460 // suspend count is non-zero, so switching to the runnable state will suspend us.
461 // TODO: find a better solution!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800462 {
463 ScopedHeapLock heap_lock;
464 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700465 self->SetState(Thread::kRunnable);
466}
467
Elliott Hughes038a8062011-09-18 14:12:41 -0700468bool ThreadList::AllThreadsAreDaemons() {
469 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700470 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
471 // is null.
472 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700473 return false;
474 }
475 }
476 return true;
477}
478
479void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800480 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700481 while (!AllThreadsAreDaemons()) {
482 thread_exit_cond_.Wait(thread_list_lock_);
483 }
484}
485
486void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800487 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700488
489 // Tell all the daemons it's time to suspend. (At this point, we know
490 // all threads are daemons.)
491 {
492 MutexLock mu(thread_suspend_count_lock_);
493 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
494 Thread* thread = *it;
495 ++thread->suspend_count_;
496 }
497 }
498
499 // Give the threads a chance to suspend, complaining if they're slow.
500 bool have_complained = false;
501 for (int i = 0; i < 10; ++i) {
502 usleep(200 * 1000);
503 bool all_suspended = true;
504 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
505 Thread* thread = *it;
506 if (thread->GetState() == Thread::kRunnable) {
507 if (!have_complained) {
508 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
509 have_complained = true;
510 }
511 all_suspended = false;
512 }
513 }
514 if (all_suspended) {
515 return;
516 }
517 }
518}
519
Elliott Hughes8daa0922011-09-11 13:46:25 -0700520uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800521 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700522 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
523 if (!allocated_ids_[i]) {
524 allocated_ids_.set(i);
525 return i + 1; // Zero is reserved to mean "invalid".
526 }
527 }
528 LOG(FATAL) << "Out of internal thread ids";
529 return 0;
530}
531
532void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700533 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700534 --id; // Zero is reserved to mean "invalid".
535 DCHECK(allocated_ids_[id]) << id;
536 allocated_ids_.reset(id);
537}
538
539} // namespace art