blob: 3f8789a25dd43ed440fa59ff4516016a3d0e3d94 [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 {
36 ScopedThreadStateChange tsc(self, Thread::kVmWait);
37 thread_list->thread_list_lock_.Lock();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070038 }
39 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080040}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070041
Elliott Hughesbbd9d832011-11-07 14:40:00 -080042ScopedThreadListLock::~ScopedThreadListLock() {
43 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
44}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070045
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080046ThreadList::ThreadList()
Elliott Hughesffb465f2012-03-01 18:46:05 -080047 : thread_list_lock_("thread list lock", kThreadListLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070048 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070049 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughesffb465f2012-03-01 18:46:05 -080050 thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070051 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080052 VLOG(threads) << "Default stack size: " << Runtime::Current()->GetDefaultStackSize() / KB << "KiB";
Elliott Hughes8daa0922011-09-11 13:46:25 -070053}
54
55ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070056 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070057 if (Contains(Thread::Current())) {
58 Runtime::Current()->DetachCurrentThread();
59 }
60
Elliott Hughes038a8062011-09-18 14:12:41 -070061 WaitForNonDaemonThreadsToExit();
62 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070063}
64
65bool ThreadList::Contains(Thread* thread) {
66 return find(list_.begin(), list_.end(), thread) != list_.end();
67}
68
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070069pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070070 return thread_list_lock_.GetOwner();
71}
72
Elliott Hughes8daa0922011-09-11 13:46:25 -070073void ThreadList::Dump(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080074 ScopedThreadListLock thread_list_lock;
Elliott Hughesff738062012-02-03 15:00:42 -080075 DumpLocked(os);
76}
77
78void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070079 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070080 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
81 (*it)->Dump(os);
82 os << "\n";
83 }
84}
85
Elliott Hughes234ab152011-10-26 14:02:26 -070086void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
87#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080088 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
89 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -070090 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -070091#endif
Elliott Hughes47179f72011-10-27 16:44:39 -070092 if (delta == -1 && thread->suspend_count_ <= 0) {
93 // This can happen if you attach a thread during a GC.
94 LOG(WARNING) << *thread << " suspend count already zero";
95 return;
96 }
Elliott Hughes234ab152011-10-26 14:02:26 -070097 thread->suspend_count_ += delta;
98 if (for_debugger) {
99 thread->debug_suspend_count_ += delta;
100 }
101}
102
Elliott Hughes8d768a92011-09-14 16:35:25 -0700103void ThreadList::FullSuspendCheck(Thread* thread) {
104 CHECK(thread != NULL);
105 CHECK_GE(thread->suspend_count_, 0);
106
107 MutexLock mu(thread_suspend_count_lock_);
108 if (thread->suspend_count_ == 0) {
109 return;
110 }
111
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800112 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700113 {
114 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
115 while (thread->suspend_count_ != 0) {
116 /*
117 * Wait for wakeup signal, releasing lock. The act of releasing
118 * and re-acquiring the lock provides the memory barriers we
119 * need for correct behavior on SMP.
120 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700121 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700122 }
123 CHECK_EQ(thread->suspend_count_, 0);
124 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800125 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700126}
127
Elliott Hughes475fc232011-10-25 15:00:35 -0700128void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700129 Thread* self = Thread::Current();
130
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800131 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700132
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700133 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800134 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700135 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700136
137 {
138 // Increment everybody's suspend count (except our own).
139 MutexLock mu(thread_suspend_count_lock_);
140 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
141 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700142 if (thread == self || (for_debugger && thread == debug_thread)) {
143 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700144 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800145 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700146 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700147 }
148 }
149
150 /*
151 * Wait for everybody in kRunnable state to stop. Other states
152 * indicate the code is either running natively or sleeping quietly.
153 * Any attempt to transition back to kRunnable will cause a check
154 * for suspension, so it should be impossible for anything to execute
155 * interpreted code or modify objects (assuming native code plays nicely).
156 *
157 * It's also okay if the thread transitions to a non-kRunnable state.
158 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700159 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700160 * so if another thread is fiddling with its suspend count (perhaps
161 * self-suspending for the debugger) it won't block while we're waiting
162 * in here.
163 */
164 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
165 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700166 if (thread == self || (for_debugger && thread == debug_thread)) {
167 continue;
168 }
169 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800170 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700171 }
172
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800173 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700174}
175
Elliott Hughes4e235312011-12-02 11:34:15 -0800176void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700177 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700178 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700179
180 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
181
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800182 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700183
Elliott Hughes01158d72011-09-19 19:47:10 -0700184 if (!Contains(thread)) {
185 return;
186 }
187
188 {
189 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800190 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700191 }
192
193 thread->WaitUntilSuspended();
194
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800195 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700196}
197
Elliott Hughes475fc232011-10-25 15:00:35 -0700198void ThreadList::SuspendSelfForDebugger() {
199 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700200
Elliott Hughes475fc232011-10-25 15:00:35 -0700201 // The debugger thread must not suspend itself due to debugger activity!
202 Thread* debug_thread = Dbg::GetDebugThread();
203 CHECK(debug_thread != NULL);
204 CHECK(self != debug_thread);
205
206 // Collisions with other suspends aren't really interesting. We want
207 // to ensure that we're the only one fiddling with the suspend count
208 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700209 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700210 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700211
212 // Suspend ourselves.
213 CHECK_GT(self->suspend_count_, 0);
214 self->SetState(Thread::kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800215 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700216
217 // Tell JDWP that we've completed suspension. The JDWP thread can't
218 // tell us to resume before we're fully asleep because we hold the
219 // suspend count lock.
220 Dbg::ClearWaitForEventThread();
221
222 while (self->suspend_count_ != 0) {
223 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
224 if (self->suspend_count_ != 0) {
225 // The condition was signaled but we're still suspended. This
226 // can happen if the debugger lets go while a SIGQUIT thread
227 // dump event is pending (assuming SignalCatcher was resumed for
228 // just long enough to try to grab the thread-suspend lock).
229 LOG(DEBUG) << *self << " still suspended after undo "
230 << "(suspend count=" << self->suspend_count_ << ")";
231 }
232 }
233 CHECK_EQ(self->suspend_count_, 0);
234 self->SetState(Thread::kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800235 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700236}
237
238void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700239 Thread* self = Thread::Current();
240
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800241 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700242
243 // Decrement the suspend counts for all threads. No need for atomic
244 // writes, since nobody should be moving until we decrement the count.
245 // We do need to hold the thread list because of JNI attaches.
246 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800247 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700248 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700249 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700250 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
251 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700252 if (thread == self || (for_debugger && thread == debug_thread)) {
253 continue;
254 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700255 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700256 }
257 }
258
259 // Broadcast a notification to all suspended threads, some or all of
260 // which may choose to wake up. No need to wait for them.
261 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800262 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700263 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700264 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700265 }
266
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800267 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700268}
269
Elliott Hughes4e235312011-12-02 11:34:15 -0800270void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700271 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800272
273 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
274 thread_list_lock_.AssertHeld();
275 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700276
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800277 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700278
279 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700280 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700281 if (!Contains(thread)) {
282 return;
283 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800284 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700285 }
286
287 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800288 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700289 MutexLock mu(thread_suspend_count_lock_);
290 thread_suspend_count_cond_.Broadcast();
291 }
292
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800293 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700294}
295
296void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
297 DCHECK(thread != NULL);
298 Thread* self = Thread::Current();
299 if (thread != self) {
300 Suspend(thread);
301 }
302 callback(arg);
303 if (thread != self) {
304 Resume(thread);
305 }
306}
307
Elliott Hughes234ab152011-10-26 14:02:26 -0700308void ThreadList::UndoDebuggerSuspensions() {
309 Thread* self = Thread::Current();
310
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800311 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700312
313 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800314 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700315 MutexLock mu(thread_suspend_count_lock_);
316 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
317 Thread* thread = *it;
318 if (thread == self || thread->debug_suspend_count_ == 0) {
319 continue;
320 }
321 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
322 }
323 }
324
325 {
326 MutexLock mu(thread_suspend_count_lock_);
327 thread_suspend_count_cond_.Broadcast();
328 }
329
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800330 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700331}
332
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700333void ThreadList::Register() {
334 Thread* self = Thread::Current();
335
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800336 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700337
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800338 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700339 CHECK(!Contains(self));
340 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700341}
342
343void ThreadList::Unregister() {
344 Thread* self = Thread::Current();
345
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800346 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700347
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700348 if (self->GetPeer() != NULL) {
349 self->SetState(Thread::kRunnable);
350
351 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
352 // down the Thread* and removing it from the thread list (or start taking any locks).
353 self->HandleUncaughtExceptions();
354
355 // Make sure we remove from ThreadGroup before taking the
356 // thread_list_lock_ since it allocates an Iterator which can cause
357 // a GC which will want to suspend.
358 self->RemoveFromThreadGroup();
359 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700360
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800361 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700362
363 // Remove this thread from the list.
364 CHECK(Contains(self));
365 list_.remove(self);
366
367 // Delete the Thread* and release the thin lock id.
368 uint32_t thin_lock_id = self->thin_lock_id_;
369 delete self;
370 ReleaseThreadId(thin_lock_id);
371
372 // Clear the TLS data, so that thread is recognizably detached.
373 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700374 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700375
376 // Signal that a thread just detached.
377 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700378}
379
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700380void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700381 thread_list_lock_.AssertHeld();
382 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700383 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700384 }
385}
386
Elliott Hughes8daa0922011-09-11 13:46:25 -0700387void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800388 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700389 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
390 (*it)->VisitRoots(visitor, arg);
391 }
392}
393
Elliott Hughes93e74e82011-09-13 11:07:03 -0700394/*
395 * Tell a new thread it's safe to start.
396 *
397 * We must hold the thread list lock before messing with another thread.
398 * In the general case we would also need to verify that the new thread was
399 * still in the thread list, but in our case the thread has not started
400 * executing user code and therefore has not had a chance to exit.
401 *
402 * We move it to kVmWait, and it then shifts itself to kRunning, which
403 * comes with a suspend-pending check. We do this after
404 */
405void ThreadList::SignalGo(Thread* child) {
406 Thread* self = Thread::Current();
407 CHECK(child != self);
408
409 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800410 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800411 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700412
413 // We wait for the child to tell us that it's in the thread list.
414 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700415 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700416 }
417 }
418
419 // If we switch out of runnable and then back in, we know there's no pending suspend.
420 self->SetState(Thread::kVmWait);
421 self->SetState(Thread::kRunnable);
422
423 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800424 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800425 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700426 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700427 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700428}
429
430void ThreadList::WaitForGo() {
431 Thread* self = Thread::Current();
432 DCHECK(Contains(self));
433
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700434 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800435 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700436
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700437 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800438 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700439 self->SetState(Thread::kStarting);
440 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700441
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700442 // Wait until our parent tells us there's no suspend still pending
443 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800444 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700445 while (self->GetState() != Thread::kVmWait) {
446 thread_start_cond_.Wait(thread_list_lock_);
447 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700448 }
449
450 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800451 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700452 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
453 // started, we wait until it's over. Which means that if there's now another GC pending, our
454 // suspend count is non-zero, so switching to the runnable state will suspend us.
455 // TODO: find a better solution!
456 Heap::Lock();
457 Heap::Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700458 self->SetState(Thread::kRunnable);
459}
460
Elliott Hughes038a8062011-09-18 14:12:41 -0700461bool ThreadList::AllThreadsAreDaemons() {
462 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700463 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
464 // is null.
465 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700466 return false;
467 }
468 }
469 return true;
470}
471
472void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800473 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700474 while (!AllThreadsAreDaemons()) {
475 thread_exit_cond_.Wait(thread_list_lock_);
476 }
477}
478
479void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800480 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700481
482 // Tell all the daemons it's time to suspend. (At this point, we know
483 // all threads are daemons.)
484 {
485 MutexLock mu(thread_suspend_count_lock_);
486 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
487 Thread* thread = *it;
488 ++thread->suspend_count_;
489 }
490 }
491
492 // Give the threads a chance to suspend, complaining if they're slow.
493 bool have_complained = false;
494 for (int i = 0; i < 10; ++i) {
495 usleep(200 * 1000);
496 bool all_suspended = true;
497 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
498 Thread* thread = *it;
499 if (thread->GetState() == Thread::kRunnable) {
500 if (!have_complained) {
501 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
502 have_complained = true;
503 }
504 all_suspended = false;
505 }
506 }
507 if (all_suspended) {
508 return;
509 }
510 }
511}
512
Elliott Hughes8daa0922011-09-11 13:46:25 -0700513uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800514 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700515 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
516 if (!allocated_ids_[i]) {
517 allocated_ids_.set(i);
518 return i + 1; // Zero is reserved to mean "invalid".
519 }
520 }
521 LOG(FATAL) << "Out of internal thread ids";
522 return 0;
523}
524
525void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700526 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700527 --id; // Zero is reserved to mean "invalid".
528 DCHECK(allocated_ids_[id]) << id;
529 allocated_ids_.reset(id);
530}
531
532} // namespace art