blob: e780e2cd52fe7f1d72a60c18854aab1f39166f1e [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"
22
Elliott Hughes8daa0922011-09-11 13:46:25 -070023namespace art {
24
Elliott Hughesbbd9d832011-11-07 14:40:00 -080025ScopedThreadListLock::ScopedThreadListLock() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080026 // Avoid deadlock between two threads trying to SuspendAll
27 // simultaneously by going to kVmWait if the lock cannot be
28 // immediately acquired.
Elliott Hughesbbd9d832011-11-07 14:40:00 -080029 ThreadList* thread_list = Runtime::Current()->GetThreadList();
30 if (!thread_list->thread_list_lock_.TryLock()) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080031 Thread* self = Thread::Current();
Elliott Hughesbbd9d832011-11-07 14:40:00 -080032 if (self == NULL) {
Elliott Hughesffb465f2012-03-01 18:46:05 -080033 // Self may be null during shutdown, but in that case there's no point going to kVmWait.
Elliott Hughesbbd9d832011-11-07 14:40:00 -080034 thread_list->thread_list_lock_.Lock();
35 } else {
Elliott Hughesa4060e52012-03-02 16:51:35 -080036 Thread::State old_thread_state = self->SetState(Thread::kVmWait);
Elliott Hughesbbd9d832011-11-07 14:40:00 -080037 thread_list->thread_list_lock_.Lock();
Elliott Hughesa4060e52012-03-02 16:51:35 -080038 // If we have the lock, by definition there's no GC in progress (though we
39 // might be taking the lock in order to start one). We avoid the suspend
40 // check here so we don't risk going to sleep on the thread suspend count lock
41 // while holding the thread list lock.
42 self->SetStateWithoutSuspendCheck(old_thread_state);
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070043 }
44 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080045}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070046
Elliott Hughesbbd9d832011-11-07 14:40:00 -080047ScopedThreadListLock::~ScopedThreadListLock() {
48 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
49}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070050
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080051ThreadList::ThreadList()
Elliott Hughesffb465f2012-03-01 18:46:05 -080052 : thread_list_lock_("thread list lock", kThreadListLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070053 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070054 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughesffb465f2012-03-01 18:46:05 -080055 thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070056 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080057 VLOG(threads) << "Default stack size: " << Runtime::Current()->GetDefaultStackSize() / KB << "KiB";
Elliott Hughes8daa0922011-09-11 13:46:25 -070058}
59
60ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070061 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070062 if (Contains(Thread::Current())) {
63 Runtime::Current()->DetachCurrentThread();
64 }
65
Elliott Hughes038a8062011-09-18 14:12:41 -070066 WaitForNonDaemonThreadsToExit();
67 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070068}
69
70bool ThreadList::Contains(Thread* thread) {
71 return find(list_.begin(), list_.end(), thread) != list_.end();
72}
73
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070074pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070075 return thread_list_lock_.GetOwner();
76}
77
Elliott Hughes8daa0922011-09-11 13:46:25 -070078void ThreadList::Dump(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080079 ScopedThreadListLock thread_list_lock;
Elliott Hughesff738062012-02-03 15:00:42 -080080 DumpLocked(os);
81}
82
83void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070084 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070085 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
86 (*it)->Dump(os);
87 os << "\n";
88 }
89}
90
Elliott Hughes234ab152011-10-26 14:02:26 -070091void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
92#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080093 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
94 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -070095 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -070096#endif
Elliott Hughes47179f72011-10-27 16:44:39 -070097 if (delta == -1 && thread->suspend_count_ <= 0) {
98 // This can happen if you attach a thread during a GC.
99 LOG(WARNING) << *thread << " suspend count already zero";
100 return;
101 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700102 thread->suspend_count_ += delta;
103 if (for_debugger) {
104 thread->debug_suspend_count_ += delta;
105 }
106}
107
Elliott Hughes8d768a92011-09-14 16:35:25 -0700108void ThreadList::FullSuspendCheck(Thread* thread) {
109 CHECK(thread != NULL);
110 CHECK_GE(thread->suspend_count_, 0);
111
112 MutexLock mu(thread_suspend_count_lock_);
113 if (thread->suspend_count_ == 0) {
114 return;
115 }
116
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800117 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700118 {
119 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
120 while (thread->suspend_count_ != 0) {
121 /*
122 * Wait for wakeup signal, releasing lock. The act of releasing
123 * and re-acquiring the lock provides the memory barriers we
124 * need for correct behavior on SMP.
125 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700126 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700127 }
128 CHECK_EQ(thread->suspend_count_, 0);
129 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800130 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700131}
132
Elliott Hughes475fc232011-10-25 15:00:35 -0700133void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700134 Thread* self = Thread::Current();
135
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800136 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700137
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700138 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800139 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700140 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700141
142 {
143 // Increment everybody's suspend count (except our own).
144 MutexLock mu(thread_suspend_count_lock_);
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;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700149 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800150 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700151 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700152 }
153 }
154
155 /*
156 * Wait for everybody in kRunnable state to stop. Other states
157 * indicate the code is either running natively or sleeping quietly.
158 * Any attempt to transition back to kRunnable will cause a check
159 * for suspension, so it should be impossible for anything to execute
160 * interpreted code or modify objects (assuming native code plays nicely).
161 *
162 * It's also okay if the thread transitions to a non-kRunnable state.
163 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700164 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700165 * so if another thread is fiddling with its suspend count (perhaps
166 * self-suspending for the debugger) it won't block while we're waiting
167 * in here.
168 */
169 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
170 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700171 if (thread == self || (for_debugger && thread == debug_thread)) {
172 continue;
173 }
174 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800175 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700176 }
177
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800178 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700179}
180
Elliott Hughes4e235312011-12-02 11:34:15 -0800181void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700182 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700183 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700184
185 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
186
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800187 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700188
Elliott Hughes01158d72011-09-19 19:47:10 -0700189 if (!Contains(thread)) {
190 return;
191 }
192
193 {
194 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800195 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700196 }
197
198 thread->WaitUntilSuspended();
199
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800200 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700201}
202
Elliott Hughes475fc232011-10-25 15:00:35 -0700203void ThreadList::SuspendSelfForDebugger() {
204 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700205
Elliott Hughes475fc232011-10-25 15:00:35 -0700206 // The debugger thread must not suspend itself due to debugger activity!
207 Thread* debug_thread = Dbg::GetDebugThread();
208 CHECK(debug_thread != NULL);
209 CHECK(self != debug_thread);
210
211 // Collisions with other suspends aren't really interesting. We want
212 // to ensure that we're the only one fiddling with the suspend count
213 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700214 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700215 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700216
217 // Suspend ourselves.
218 CHECK_GT(self->suspend_count_, 0);
219 self->SetState(Thread::kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800220 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700221
222 // Tell JDWP that we've completed suspension. The JDWP thread can't
223 // tell us to resume before we're fully asleep because we hold the
224 // suspend count lock.
225 Dbg::ClearWaitForEventThread();
226
227 while (self->suspend_count_ != 0) {
228 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
229 if (self->suspend_count_ != 0) {
230 // The condition was signaled but we're still suspended. This
231 // can happen if the debugger lets go while a SIGQUIT thread
232 // dump event is pending (assuming SignalCatcher was resumed for
233 // just long enough to try to grab the thread-suspend lock).
234 LOG(DEBUG) << *self << " still suspended after undo "
235 << "(suspend count=" << self->suspend_count_ << ")";
236 }
237 }
238 CHECK_EQ(self->suspend_count_, 0);
239 self->SetState(Thread::kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800240 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700241}
242
243void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700244 Thread* self = Thread::Current();
245
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800246 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700247
248 // Decrement the suspend counts for all threads. No need for atomic
249 // writes, since nobody should be moving until we decrement the count.
250 // We do need to hold the thread list because of JNI attaches.
251 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800252 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700253 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700254 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700255 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
256 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700257 if (thread == self || (for_debugger && thread == debug_thread)) {
258 continue;
259 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700260 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700261 }
262 }
263
264 // Broadcast a notification to all suspended threads, some or all of
265 // which may choose to wake up. No need to wait for them.
266 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800267 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700268 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700269 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700270 }
271
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800272 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700273}
274
Elliott Hughes4e235312011-12-02 11:34:15 -0800275void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700276 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800277
278 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
279 thread_list_lock_.AssertHeld();
280 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700281
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800282 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700283
284 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700285 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700286 if (!Contains(thread)) {
287 return;
288 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800289 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700290 }
291
292 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800293 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700294 MutexLock mu(thread_suspend_count_lock_);
295 thread_suspend_count_cond_.Broadcast();
296 }
297
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800298 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700299}
300
301void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
302 DCHECK(thread != NULL);
303 Thread* self = Thread::Current();
304 if (thread != self) {
305 Suspend(thread);
306 }
307 callback(arg);
308 if (thread != self) {
309 Resume(thread);
310 }
311}
312
Elliott Hughes234ab152011-10-26 14:02:26 -0700313void ThreadList::UndoDebuggerSuspensions() {
314 Thread* self = Thread::Current();
315
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800316 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700317
318 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800319 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700320 MutexLock mu(thread_suspend_count_lock_);
321 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
322 Thread* thread = *it;
323 if (thread == self || thread->debug_suspend_count_ == 0) {
324 continue;
325 }
326 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
327 }
328 }
329
330 {
331 MutexLock mu(thread_suspend_count_lock_);
332 thread_suspend_count_cond_.Broadcast();
333 }
334
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800335 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700336}
337
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700338void ThreadList::Register() {
339 Thread* self = Thread::Current();
340
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800341 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700342
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800343 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700344 CHECK(!Contains(self));
345 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700346}
347
348void ThreadList::Unregister() {
349 Thread* self = Thread::Current();
350
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800351 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700352
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700353 if (self->GetPeer() != NULL) {
354 self->SetState(Thread::kRunnable);
355
356 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
357 // down the Thread* and removing it from the thread list (or start taking any locks).
358 self->HandleUncaughtExceptions();
359
360 // Make sure we remove from ThreadGroup before taking the
361 // thread_list_lock_ since it allocates an Iterator which can cause
362 // a GC which will want to suspend.
363 self->RemoveFromThreadGroup();
364 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700365
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800366 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700367
368 // Remove this thread from the list.
369 CHECK(Contains(self));
370 list_.remove(self);
371
372 // Delete the Thread* and release the thin lock id.
373 uint32_t thin_lock_id = self->thin_lock_id_;
374 delete self;
375 ReleaseThreadId(thin_lock_id);
376
377 // Clear the TLS data, so that thread is recognizably detached.
378 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700379 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700380
381 // Signal that a thread just detached.
382 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700383}
384
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700385void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700386 thread_list_lock_.AssertHeld();
387 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700388 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700389 }
390}
391
Elliott Hughes8daa0922011-09-11 13:46:25 -0700392void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800393 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700394 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
395 (*it)->VisitRoots(visitor, arg);
396 }
397}
398
Elliott Hughes93e74e82011-09-13 11:07:03 -0700399/*
400 * Tell a new thread it's safe to start.
401 *
402 * We must hold the thread list lock before messing with another thread.
403 * In the general case we would also need to verify that the new thread was
404 * still in the thread list, but in our case the thread has not started
405 * executing user code and therefore has not had a chance to exit.
406 *
407 * We move it to kVmWait, and it then shifts itself to kRunning, which
408 * comes with a suspend-pending check. We do this after
409 */
410void ThreadList::SignalGo(Thread* child) {
411 Thread* self = Thread::Current();
412 CHECK(child != self);
413
414 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800415 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800416 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700417
418 // We wait for the child to tell us that it's in the thread list.
419 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700420 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700421 }
422 }
423
424 // If we switch out of runnable and then back in, we know there's no pending suspend.
425 self->SetState(Thread::kVmWait);
426 self->SetState(Thread::kRunnable);
427
428 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800429 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800430 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700431 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700432 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700433}
434
435void ThreadList::WaitForGo() {
436 Thread* self = Thread::Current();
437 DCHECK(Contains(self));
438
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700439 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800440 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700441
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700442 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800443 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700444 self->SetState(Thread::kStarting);
445 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700446
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700447 // Wait until our parent tells us there's no suspend still pending
448 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800449 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700450 while (self->GetState() != Thread::kVmWait) {
451 thread_start_cond_.Wait(thread_list_lock_);
452 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700453 }
454
455 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800456 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700457 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
458 // started, we wait until it's over. Which means that if there's now another GC pending, our
459 // suspend count is non-zero, so switching to the runnable state will suspend us.
460 // TODO: find a better solution!
461 Heap::Lock();
462 Heap::Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700463 self->SetState(Thread::kRunnable);
464}
465
Elliott Hughes038a8062011-09-18 14:12:41 -0700466bool ThreadList::AllThreadsAreDaemons() {
467 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700468 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
469 // is null.
470 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700471 return false;
472 }
473 }
474 return true;
475}
476
477void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800478 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700479 while (!AllThreadsAreDaemons()) {
480 thread_exit_cond_.Wait(thread_list_lock_);
481 }
482}
483
484void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800485 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700486
487 // Tell all the daemons it's time to suspend. (At this point, we know
488 // all threads are daemons.)
489 {
490 MutexLock mu(thread_suspend_count_lock_);
491 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
492 Thread* thread = *it;
493 ++thread->suspend_count_;
494 }
495 }
496
497 // Give the threads a chance to suspend, complaining if they're slow.
498 bool have_complained = false;
499 for (int i = 0; i < 10; ++i) {
500 usleep(200 * 1000);
501 bool all_suspended = true;
502 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
503 Thread* thread = *it;
504 if (thread->GetState() == Thread::kRunnable) {
505 if (!have_complained) {
506 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
507 have_complained = true;
508 }
509 all_suspended = false;
510 }
511 }
512 if (all_suspended) {
513 return;
514 }
515 }
516}
517
Elliott Hughes8daa0922011-09-11 13:46:25 -0700518uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800519 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700520 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
521 if (!allocated_ids_[i]) {
522 allocated_ids_.set(i);
523 return i + 1; // Zero is reserved to mean "invalid".
524 }
525 }
526 LOG(FATAL) << "Out of internal thread ids";
527 return 0;
528}
529
530void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700531 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700532 --id; // Zero is reserved to mean "invalid".
533 DCHECK(allocated_ids_[id]) << id;
534 allocated_ids_.reset(id);
535}
536
537} // namespace art