blob: f344e674f9649b5f2256e95eea1ac7e520548fb4 [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 Hughesff738062012-02-03 15:00:42 -080092 DumpLocked(os);
93}
94
95void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070096 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070097 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
98 (*it)->Dump(os);
99 os << "\n";
100 }
101}
102
Elliott Hughes234ab152011-10-26 14:02:26 -0700103void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
104#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -0800105 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
106 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -0700107 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700108#endif
Elliott Hughes47179f72011-10-27 16:44:39 -0700109 if (delta == -1 && thread->suspend_count_ <= 0) {
110 // This can happen if you attach a thread during a GC.
111 LOG(WARNING) << *thread << " suspend count already zero";
112 return;
113 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700114 thread->suspend_count_ += delta;
115 if (for_debugger) {
116 thread->debug_suspend_count_ += delta;
117 }
118}
119
Elliott Hughes8d768a92011-09-14 16:35:25 -0700120void ThreadList::FullSuspendCheck(Thread* thread) {
121 CHECK(thread != NULL);
122 CHECK_GE(thread->suspend_count_, 0);
123
124 MutexLock mu(thread_suspend_count_lock_);
125 if (thread->suspend_count_ == 0) {
126 return;
127 }
128
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800129 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700130 {
131 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
132 while (thread->suspend_count_ != 0) {
133 /*
134 * Wait for wakeup signal, releasing lock. The act of releasing
135 * and re-acquiring the lock provides the memory barriers we
136 * need for correct behavior on SMP.
137 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700138 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700139 }
140 CHECK_EQ(thread->suspend_count_, 0);
141 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800142 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700143}
144
Elliott Hughes475fc232011-10-25 15:00:35 -0700145void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700146 Thread* self = Thread::Current();
147
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800148 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700149
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700150 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800151 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700152 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700153
154 {
155 // Increment everybody's suspend count (except our own).
156 MutexLock mu(thread_suspend_count_lock_);
157 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
158 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700159 if (thread == self || (for_debugger && thread == debug_thread)) {
160 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700161 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800162 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700163 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700164 }
165 }
166
167 /*
168 * Wait for everybody in kRunnable state to stop. Other states
169 * indicate the code is either running natively or sleeping quietly.
170 * Any attempt to transition back to kRunnable will cause a check
171 * for suspension, so it should be impossible for anything to execute
172 * interpreted code or modify objects (assuming native code plays nicely).
173 *
174 * It's also okay if the thread transitions to a non-kRunnable state.
175 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700176 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700177 * so if another thread is fiddling with its suspend count (perhaps
178 * self-suspending for the debugger) it won't block while we're waiting
179 * in here.
180 */
181 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
182 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700183 if (thread == self || (for_debugger && thread == debug_thread)) {
184 continue;
185 }
186 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800187 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700188 }
189
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800190 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700191}
192
Elliott Hughes4e235312011-12-02 11:34:15 -0800193void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700194 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700195 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700196
197 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
198
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800199 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700200
Elliott Hughes01158d72011-09-19 19:47:10 -0700201 if (!Contains(thread)) {
202 return;
203 }
204
205 {
206 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800207 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700208 }
209
210 thread->WaitUntilSuspended();
211
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800212 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700213}
214
Elliott Hughes475fc232011-10-25 15:00:35 -0700215void ThreadList::SuspendSelfForDebugger() {
216 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700217
Elliott Hughes475fc232011-10-25 15:00:35 -0700218 // The debugger thread must not suspend itself due to debugger activity!
219 Thread* debug_thread = Dbg::GetDebugThread();
220 CHECK(debug_thread != NULL);
221 CHECK(self != debug_thread);
222
223 // Collisions with other suspends aren't really interesting. We want
224 // to ensure that we're the only one fiddling with the suspend count
225 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700226 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700227 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700228
229 // Suspend ourselves.
230 CHECK_GT(self->suspend_count_, 0);
231 self->SetState(Thread::kSuspended);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800232 VLOG(threads) << *self << " self-suspending (dbg)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700233
234 // Tell JDWP that we've completed suspension. The JDWP thread can't
235 // tell us to resume before we're fully asleep because we hold the
236 // suspend count lock.
237 Dbg::ClearWaitForEventThread();
238
239 while (self->suspend_count_ != 0) {
240 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
241 if (self->suspend_count_ != 0) {
242 // The condition was signaled but we're still suspended. This
243 // can happen if the debugger lets go while a SIGQUIT thread
244 // dump event is pending (assuming SignalCatcher was resumed for
245 // just long enough to try to grab the thread-suspend lock).
246 LOG(DEBUG) << *self << " still suspended after undo "
247 << "(suspend count=" << self->suspend_count_ << ")";
248 }
249 }
250 CHECK_EQ(self->suspend_count_, 0);
251 self->SetState(Thread::kRunnable);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800252 VLOG(threads) << *self << " self-reviving (dbg)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700253}
254
255void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700256 Thread* self = Thread::Current();
257
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800258 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700259
260 // Decrement the suspend counts for all threads. No need for atomic
261 // writes, since nobody should be moving until we decrement the count.
262 // We do need to hold the thread list because of JNI attaches.
263 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800264 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700265 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700266 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700267 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
268 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700269 if (thread == self || (for_debugger && thread == debug_thread)) {
270 continue;
271 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700272 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700273 }
274 }
275
276 // Broadcast a notification to all suspended threads, some or all of
277 // which may choose to wake up. No need to wait for them.
278 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800279 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700280 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700281 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700282 }
283
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800284 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700285}
286
Elliott Hughes4e235312011-12-02 11:34:15 -0800287void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700288 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800289
290 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
291 thread_list_lock_.AssertHeld();
292 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700293
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800294 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700295
296 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700297 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700298 if (!Contains(thread)) {
299 return;
300 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800301 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700302 }
303
304 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800305 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700306 MutexLock mu(thread_suspend_count_lock_);
307 thread_suspend_count_cond_.Broadcast();
308 }
309
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800310 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700311}
312
313void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
314 DCHECK(thread != NULL);
315 Thread* self = Thread::Current();
316 if (thread != self) {
317 Suspend(thread);
318 }
319 callback(arg);
320 if (thread != self) {
321 Resume(thread);
322 }
323}
324
Elliott Hughes234ab152011-10-26 14:02:26 -0700325void ThreadList::UndoDebuggerSuspensions() {
326 Thread* self = Thread::Current();
327
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800328 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700329
330 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800331 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700332 MutexLock mu(thread_suspend_count_lock_);
333 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
334 Thread* thread = *it;
335 if (thread == self || thread->debug_suspend_count_ == 0) {
336 continue;
337 }
338 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
339 }
340 }
341
342 {
343 MutexLock mu(thread_suspend_count_lock_);
344 thread_suspend_count_cond_.Broadcast();
345 }
346
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800347 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700348}
349
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700350void ThreadList::Register() {
351 Thread* self = Thread::Current();
352
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800353 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700354
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800355 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700356 CHECK(!Contains(self));
357 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700358}
359
360void ThreadList::Unregister() {
361 Thread* self = Thread::Current();
362
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800363 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700364
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700365 if (self->GetPeer() != NULL) {
366 self->SetState(Thread::kRunnable);
367
368 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
369 // down the Thread* and removing it from the thread list (or start taking any locks).
370 self->HandleUncaughtExceptions();
371
372 // Make sure we remove from ThreadGroup before taking the
373 // thread_list_lock_ since it allocates an Iterator which can cause
374 // a GC which will want to suspend.
375 self->RemoveFromThreadGroup();
376 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700377
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800378 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700379
380 // Remove this thread from the list.
381 CHECK(Contains(self));
382 list_.remove(self);
383
384 // Delete the Thread* and release the thin lock id.
385 uint32_t thin_lock_id = self->thin_lock_id_;
386 delete self;
387 ReleaseThreadId(thin_lock_id);
388
389 // Clear the TLS data, so that thread is recognizably detached.
390 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700391 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700392
393 // Signal that a thread just detached.
394 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700395}
396
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700397void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700398 thread_list_lock_.AssertHeld();
399 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700400 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700401 }
402}
403
Elliott Hughes8daa0922011-09-11 13:46:25 -0700404void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800405 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700406 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
407 (*it)->VisitRoots(visitor, arg);
408 }
409}
410
Elliott Hughes93e74e82011-09-13 11:07:03 -0700411/*
412 * Tell a new thread it's safe to start.
413 *
414 * We must hold the thread list lock before messing with another thread.
415 * In the general case we would also need to verify that the new thread was
416 * still in the thread list, but in our case the thread has not started
417 * executing user code and therefore has not had a chance to exit.
418 *
419 * We move it to kVmWait, and it then shifts itself to kRunning, which
420 * comes with a suspend-pending check. We do this after
421 */
422void ThreadList::SignalGo(Thread* child) {
423 Thread* self = Thread::Current();
424 CHECK(child != self);
425
426 {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800427 // We don't use ScopedThreadListLock here because we don't want to
428 // hold the heap lock while waiting because it can lead to deadlock.
429 thread_list_lock_.Lock();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800430 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700431
432 // We wait for the child to tell us that it's in the thread list.
433 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700434 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700435 }
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800436 thread_list_lock_.Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700437 }
438
439 // If we switch out of runnable and then back in, we know there's no pending suspend.
440 self->SetState(Thread::kVmWait);
441 self->SetState(Thread::kRunnable);
442
443 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800444 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800445 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700446 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700447 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700448}
449
450void ThreadList::WaitForGo() {
451 Thread* self = Thread::Current();
452 DCHECK(Contains(self));
453
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700454 {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800455 // We don't use ScopedThreadListLock here because we don't want to
456 // hold the heap lock while waiting because it can lead to deadlock.
457 thread_list_lock_.Lock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700458
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700459 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800460 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700461 self->SetState(Thread::kStarting);
462 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700463
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700464 // Wait until our parent tells us there's no suspend still pending
465 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800466 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700467 while (self->GetState() != Thread::kVmWait) {
468 thread_start_cond_.Wait(thread_list_lock_);
469 }
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800470 thread_list_lock_.Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700471 }
472
473 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800474 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700475 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
476 // started, we wait until it's over. Which means that if there's now another GC pending, our
477 // suspend count is non-zero, so switching to the runnable state will suspend us.
478 // TODO: find a better solution!
479 Heap::Lock();
480 Heap::Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700481 self->SetState(Thread::kRunnable);
482}
483
Elliott Hughes038a8062011-09-18 14:12:41 -0700484bool ThreadList::AllThreadsAreDaemons() {
485 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700486 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
487 // is null.
488 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700489 return false;
490 }
491 }
492 return true;
493}
494
495void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800496 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700497 while (!AllThreadsAreDaemons()) {
498 thread_exit_cond_.Wait(thread_list_lock_);
499 }
500}
501
502void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800503 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700504
505 // Tell all the daemons it's time to suspend. (At this point, we know
506 // all threads are daemons.)
507 {
508 MutexLock mu(thread_suspend_count_lock_);
509 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
510 Thread* thread = *it;
511 ++thread->suspend_count_;
512 }
513 }
514
515 // Give the threads a chance to suspend, complaining if they're slow.
516 bool have_complained = false;
517 for (int i = 0; i < 10; ++i) {
518 usleep(200 * 1000);
519 bool all_suspended = true;
520 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
521 Thread* thread = *it;
522 if (thread->GetState() == Thread::kRunnable) {
523 if (!have_complained) {
524 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
525 have_complained = true;
526 }
527 all_suspended = false;
528 }
529 }
530 if (all_suspended) {
531 return;
532 }
533 }
534}
535
Elliott Hughes8daa0922011-09-11 13:46:25 -0700536uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800537 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700538 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
539 if (!allocated_ids_[i]) {
540 allocated_ids_.set(i);
541 return i + 1; // Zero is reserved to mean "invalid".
542 }
543 }
544 LOG(FATAL) << "Out of internal thread ids";
545 return 0;
546}
547
548void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700549 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700550 --id; // Zero is reserved to mean "invalid".
551 DCHECK(allocated_ids_[id]) << id;
552 allocated_ids_.reset(id);
553}
554
555} // namespace art