blob: 57a21242a28fd9de5bd6a3cf9340085173e49fae [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() {
26 // Self may be null during shutdown.
27 Thread* self = Thread::Current();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070028
Elliott Hughesbbd9d832011-11-07 14:40:00 -080029 // We insist that anyone taking the thread list lock already has the heap lock,
30 // because pretty much any time someone takes the thread list lock, they may
31 // end up needing the heap lock (even removing a thread from the thread list calls
32 // back into managed code to remove the thread from its ThreadGroup, and that allocates
33 // an iterator).
34 // TODO: this makes the distinction between the two locks pretty pointless.
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080035 heap_lock_held_ = (self != NULL);
36 if (heap_lock_held_) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080037 Heap::Lock();
38 }
39
40 // Avoid deadlock between two threads trying to SuspendAll
41 // simultaneously by going to kVmWait if the lock cannot be
42 // immediately acquired.
43 // TODO: is this needed if we took the heap lock? taking the heap lock will have done this,
44 // and the other thread will now be in kVmWait waiting for the heap lock.
45 ThreadList* thread_list = Runtime::Current()->GetThreadList();
46 if (!thread_list->thread_list_lock_.TryLock()) {
47 if (self == NULL) {
48 thread_list->thread_list_lock_.Lock();
49 } else {
50 ScopedThreadStateChange tsc(self, Thread::kVmWait);
51 thread_list->thread_list_lock_.Lock();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070052 }
53 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080054}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070055
Elliott Hughesbbd9d832011-11-07 14:40:00 -080056ScopedThreadListLock::~ScopedThreadListLock() {
57 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080058 if (heap_lock_held_) {
59 Heap::Unlock();
60 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080061}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070062
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080063ThreadList::ThreadList()
64 : thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070065 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070066 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070067 thread_suspend_count_lock_("thread suspend count lock"),
68 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080069 VLOG(threads) << "Default stack size: " << Runtime::Current()->GetDefaultStackSize() / KB << "KiB";
Elliott Hughes8daa0922011-09-11 13:46:25 -070070}
71
72ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070073 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070074 if (Contains(Thread::Current())) {
75 Runtime::Current()->DetachCurrentThread();
76 }
77
Elliott Hughes038a8062011-09-18 14:12:41 -070078 WaitForNonDaemonThreadsToExit();
79 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070080}
81
82bool ThreadList::Contains(Thread* thread) {
83 return find(list_.begin(), list_.end(), thread) != list_.end();
84}
85
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070086pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070087 return thread_list_lock_.GetOwner();
88}
89
Elliott Hughes8daa0922011-09-11 13:46:25 -070090void ThreadList::Dump(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080091 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070092 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070093 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
94 (*it)->Dump(os);
95 os << "\n";
96 }
97}
98
Elliott Hughes234ab152011-10-26 14:02:26 -070099void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
100#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800101 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
102 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -0700103 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700104#endif
Elliott Hughes47179f72011-10-27 16:44:39 -0700105 if (delta == -1 && thread->suspend_count_ <= 0) {
106 // This can happen if you attach a thread during a GC.
107 LOG(WARNING) << *thread << " suspend count already zero";
108 return;
109 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700110 thread->suspend_count_ += delta;
111 if (for_debugger) {
112 thread->debug_suspend_count_ += delta;
113 }
114}
115
Elliott Hughes8d768a92011-09-14 16:35:25 -0700116void ThreadList::FullSuspendCheck(Thread* thread) {
117 CHECK(thread != NULL);
118 CHECK_GE(thread->suspend_count_, 0);
119
120 MutexLock mu(thread_suspend_count_lock_);
121 if (thread->suspend_count_ == 0) {
122 return;
123 }
124
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800125 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700126 {
127 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
128 while (thread->suspend_count_ != 0) {
129 /*
130 * Wait for wakeup signal, releasing lock. The act of releasing
131 * and re-acquiring the lock provides the memory barriers we
132 * need for correct behavior on SMP.
133 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700134 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700135 }
136 CHECK_EQ(thread->suspend_count_, 0);
137 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800138 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700139}
140
Elliott Hughes475fc232011-10-25 15:00:35 -0700141void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700142 Thread* self = Thread::Current();
143
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800144 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700145
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700146 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800147 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700148 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700149
150 {
151 // Increment everybody's suspend count (except our own).
152 MutexLock mu(thread_suspend_count_lock_);
153 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
154 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700155 if (thread == self || (for_debugger && thread == debug_thread)) {
156 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700157 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800158 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700159 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700160 }
161 }
162
163 /*
164 * Wait for everybody in kRunnable state to stop. Other states
165 * indicate the code is either running natively or sleeping quietly.
166 * Any attempt to transition back to kRunnable will cause a check
167 * for suspension, so it should be impossible for anything to execute
168 * interpreted code or modify objects (assuming native code plays nicely).
169 *
170 * It's also okay if the thread transitions to a non-kRunnable state.
171 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700172 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700173 * so if another thread is fiddling with its suspend count (perhaps
174 * self-suspending for the debugger) it won't block while we're waiting
175 * in here.
176 */
177 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
178 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700179 if (thread == self || (for_debugger && thread == debug_thread)) {
180 continue;
181 }
182 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800183 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700184 }
185
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800186 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700187}
188
Elliott Hughes4e235312011-12-02 11:34:15 -0800189void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700190 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700191 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700192
193 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
194
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800195 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700196
Elliott Hughes01158d72011-09-19 19:47:10 -0700197 if (!Contains(thread)) {
198 return;
199 }
200
201 {
202 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800203 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700204 }
205
206 thread->WaitUntilSuspended();
207
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800208 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700209}
210
Elliott Hughes475fc232011-10-25 15:00:35 -0700211void ThreadList::SuspendSelfForDebugger() {
212 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700213
Elliott Hughes475fc232011-10-25 15:00:35 -0700214 // The debugger thread must not suspend itself due to debugger activity!
215 Thread* debug_thread = Dbg::GetDebugThread();
216 CHECK(debug_thread != NULL);
217 CHECK(self != debug_thread);
218
219 // Collisions with other suspends aren't really interesting. We want
220 // to ensure that we're the only one fiddling with the suspend count
221 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700222 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700223 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700224
225 // Suspend ourselves.
226 CHECK_GT(self->suspend_count_, 0);
227 self->SetState(Thread::kSuspended);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800228 VLOG(threads) << *self << " self-suspending (dbg)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700229
230 // Tell JDWP that we've completed suspension. The JDWP thread can't
231 // tell us to resume before we're fully asleep because we hold the
232 // suspend count lock.
233 Dbg::ClearWaitForEventThread();
234
235 while (self->suspend_count_ != 0) {
236 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
237 if (self->suspend_count_ != 0) {
238 // The condition was signaled but we're still suspended. This
239 // can happen if the debugger lets go while a SIGQUIT thread
240 // dump event is pending (assuming SignalCatcher was resumed for
241 // just long enough to try to grab the thread-suspend lock).
242 LOG(DEBUG) << *self << " still suspended after undo "
243 << "(suspend count=" << self->suspend_count_ << ")";
244 }
245 }
246 CHECK_EQ(self->suspend_count_, 0);
247 self->SetState(Thread::kRunnable);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800248 VLOG(threads) << *self << " self-reviving (dbg)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700249}
250
251void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700252 Thread* self = Thread::Current();
253
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800254 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700255
256 // Decrement the suspend counts for all threads. No need for atomic
257 // writes, since nobody should be moving until we decrement the count.
258 // We do need to hold the thread list because of JNI attaches.
259 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800260 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700261 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700262 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700263 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
264 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700265 if (thread == self || (for_debugger && thread == debug_thread)) {
266 continue;
267 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700268 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700269 }
270 }
271
272 // Broadcast a notification to all suspended threads, some or all of
273 // which may choose to wake up. No need to wait for them.
274 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800275 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700276 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700277 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700278 }
279
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800280 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700281}
282
Elliott Hughes4e235312011-12-02 11:34:15 -0800283void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700284 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800285
286 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
287 thread_list_lock_.AssertHeld();
288 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700289
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800290 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700291
292 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700293 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700294 if (!Contains(thread)) {
295 return;
296 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800297 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700298 }
299
300 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800301 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700302 MutexLock mu(thread_suspend_count_lock_);
303 thread_suspend_count_cond_.Broadcast();
304 }
305
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800306 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700307}
308
309void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
310 DCHECK(thread != NULL);
311 Thread* self = Thread::Current();
312 if (thread != self) {
313 Suspend(thread);
314 }
315 callback(arg);
316 if (thread != self) {
317 Resume(thread);
318 }
319}
320
Elliott Hughes234ab152011-10-26 14:02:26 -0700321void ThreadList::UndoDebuggerSuspensions() {
322 Thread* self = Thread::Current();
323
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800324 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700325
326 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800327 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700328 MutexLock mu(thread_suspend_count_lock_);
329 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
330 Thread* thread = *it;
331 if (thread == self || thread->debug_suspend_count_ == 0) {
332 continue;
333 }
334 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
335 }
336 }
337
338 {
339 MutexLock mu(thread_suspend_count_lock_);
340 thread_suspend_count_cond_.Broadcast();
341 }
342
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800343 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700344}
345
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700346void ThreadList::Register() {
347 Thread* self = Thread::Current();
348
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800349 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700350
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800351 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700352 CHECK(!Contains(self));
353 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700354}
355
356void ThreadList::Unregister() {
357 Thread* self = Thread::Current();
358
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800359 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700360
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700361 if (self->GetPeer() != NULL) {
362 self->SetState(Thread::kRunnable);
363
364 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
365 // down the Thread* and removing it from the thread list (or start taking any locks).
366 self->HandleUncaughtExceptions();
367
368 // Make sure we remove from ThreadGroup before taking the
369 // thread_list_lock_ since it allocates an Iterator which can cause
370 // a GC which will want to suspend.
371 self->RemoveFromThreadGroup();
372 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700373
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800374 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700375
376 // Remove this thread from the list.
377 CHECK(Contains(self));
378 list_.remove(self);
379
380 // Delete the Thread* and release the thin lock id.
381 uint32_t thin_lock_id = self->thin_lock_id_;
382 delete self;
383 ReleaseThreadId(thin_lock_id);
384
385 // Clear the TLS data, so that thread is recognizably detached.
386 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700387 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700388
389 // Signal that a thread just detached.
390 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700391}
392
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700393void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700394 thread_list_lock_.AssertHeld();
395 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700396 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700397 }
398}
399
Elliott Hughes8daa0922011-09-11 13:46:25 -0700400void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800401 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700402 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
403 (*it)->VisitRoots(visitor, arg);
404 }
405}
406
Elliott Hughes93e74e82011-09-13 11:07:03 -0700407/*
408 * Tell a new thread it's safe to start.
409 *
410 * We must hold the thread list lock before messing with another thread.
411 * In the general case we would also need to verify that the new thread was
412 * still in the thread list, but in our case the thread has not started
413 * executing user code and therefore has not had a chance to exit.
414 *
415 * We move it to kVmWait, and it then shifts itself to kRunning, which
416 * comes with a suspend-pending check. We do this after
417 */
418void ThreadList::SignalGo(Thread* child) {
419 Thread* self = Thread::Current();
420 CHECK(child != self);
421
422 {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800423 // We don't use ScopedThreadListLock here because we don't want to
424 // hold the heap lock while waiting because it can lead to deadlock.
425 thread_list_lock_.Lock();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800426 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700427
428 // We wait for the child to tell us that it's in the thread list.
429 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700430 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700431 }
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800432 thread_list_lock_.Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700433 }
434
435 // If we switch out of runnable and then back in, we know there's no pending suspend.
436 self->SetState(Thread::kVmWait);
437 self->SetState(Thread::kRunnable);
438
439 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800440 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800441 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700442 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700443 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700444}
445
446void ThreadList::WaitForGo() {
447 Thread* self = Thread::Current();
448 DCHECK(Contains(self));
449
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700450 {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800451 // We don't use ScopedThreadListLock here because we don't want to
452 // hold the heap lock while waiting because it can lead to deadlock.
453 thread_list_lock_.Lock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700454
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700455 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800456 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700457 self->SetState(Thread::kStarting);
458 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700459
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700460 // Wait until our parent tells us there's no suspend still pending
461 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800462 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700463 while (self->GetState() != Thread::kVmWait) {
464 thread_start_cond_.Wait(thread_list_lock_);
465 }
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800466 thread_list_lock_.Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700467 }
468
469 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800470 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700471 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
472 // started, we wait until it's over. Which means that if there's now another GC pending, our
473 // suspend count is non-zero, so switching to the runnable state will suspend us.
474 // TODO: find a better solution!
475 Heap::Lock();
476 Heap::Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700477 self->SetState(Thread::kRunnable);
478}
479
Elliott Hughes038a8062011-09-18 14:12:41 -0700480bool ThreadList::AllThreadsAreDaemons() {
481 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700482 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
483 // is null.
484 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700485 return false;
486 }
487 }
488 return true;
489}
490
491void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800492 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700493 while (!AllThreadsAreDaemons()) {
494 thread_exit_cond_.Wait(thread_list_lock_);
495 }
496}
497
498void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800499 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700500
501 // Tell all the daemons it's time to suspend. (At this point, we know
502 // all threads are daemons.)
503 {
504 MutexLock mu(thread_suspend_count_lock_);
505 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
506 Thread* thread = *it;
507 ++thread->suspend_count_;
508 }
509 }
510
511 // Give the threads a chance to suspend, complaining if they're slow.
512 bool have_complained = false;
513 for (int i = 0; i < 10; ++i) {
514 usleep(200 * 1000);
515 bool all_suspended = true;
516 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
517 Thread* thread = *it;
518 if (thread->GetState() == Thread::kRunnable) {
519 if (!have_complained) {
520 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
521 have_complained = true;
522 }
523 all_suspended = false;
524 }
525 }
526 if (all_suspended) {
527 return;
528 }
529 }
530}
531
Elliott Hughes8daa0922011-09-11 13:46:25 -0700532uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800533 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700534 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
535 if (!allocated_ids_[i]) {
536 allocated_ids_.set(i);
537 return i + 1; // Zero is reserved to mean "invalid".
538 }
539 }
540 LOG(FATAL) << "Out of internal thread ids";
541 return 0;
542}
543
544void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700545 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700546 --id; // Zero is reserved to mean "invalid".
547 DCHECK(allocated_ids_[id]) << id;
548 allocated_ids_.reset(id);
549}
550
551} // namespace art