blob: 0e01c0d8f35bf77640f2b9a28c18adc8da6af4e6 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "thread_list.h"
18
Elliott Hughes038a8062011-09-18 14:12:41 -070019#include <unistd.h>
20
Elliott Hughes475fc232011-10-25 15:00:35 -070021#include "debugger.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080022#include "scoped_heap_lock.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070023#include "scoped_thread_list_lock.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070024
Elliott Hughes8daa0922011-09-11 13:46:25 -070025namespace art {
26
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080027ThreadList::ThreadList()
Elliott Hughese52e49b2012-04-02 16:05:44 -070028 : allocated_ids_lock_("allocated thread ids lock"),
29 thread_list_lock_("thread list lock", kThreadListLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070030 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070031 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughesffb465f2012-03-01 18:46:05 -080032 thread_suspend_count_lock_("thread suspend count lock", kThreadSuspendCountLock),
Elliott Hughes5f791332011-09-15 17:45:30 -070033 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080034 VLOG(threads) << "Default stack size: " << Runtime::Current()->GetDefaultStackSize() / KB << "KiB";
Elliott Hughes8daa0922011-09-11 13:46:25 -070035}
36
37ThreadList::~ThreadList() {
Elliott Hughese52e49b2012-04-02 16:05:44 -070038 // Detach the current thread if necessary. If we failed to start, there might not be any threads.
Elliott Hughes6a144332012-04-03 13:07:11 -070039 // We need to detach the current thread here in case there's another thread waiting to join with
40 // us.
Elliott Hughes8daa0922011-09-11 13:46:25 -070041 if (Contains(Thread::Current())) {
42 Runtime::Current()->DetachCurrentThread();
43 }
Elliott Hughes6a144332012-04-03 13:07:11 -070044
45 WaitForOtherNonDaemonThreadsToExit();
46 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070047}
48
49bool ThreadList::Contains(Thread* thread) {
50 return find(list_.begin(), list_.end(), thread) != list_.end();
51}
52
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070053pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070054 return thread_list_lock_.GetOwner();
55}
56
Elliott Hughes8daa0922011-09-11 13:46:25 -070057void ThreadList::Dump(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080058 ScopedThreadListLock thread_list_lock;
Elliott Hughesff738062012-02-03 15:00:42 -080059 DumpLocked(os);
60}
61
62void ThreadList::DumpLocked(std::ostream& os) {
Elliott Hughes8daa0922011-09-11 13:46:25 -070063 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070064 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
65 (*it)->Dump(os);
66 os << "\n";
67 }
68}
69
Elliott Hughes234ab152011-10-26 14:02:26 -070070void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
71#ifndef NDEBUG
Elliott Hughes3d30d9b2011-12-07 17:35:48 -080072 DCHECK(delta == -1 || delta == +1 || delta == -thread->debug_suspend_count_)
73 << delta << " " << thread->debug_suspend_count_ << " " << *thread;
Elliott Hughes47179f72011-10-27 16:44:39 -070074 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -070075#endif
Elliott Hughes47179f72011-10-27 16:44:39 -070076 if (delta == -1 && thread->suspend_count_ <= 0) {
77 // This can happen if you attach a thread during a GC.
78 LOG(WARNING) << *thread << " suspend count already zero";
79 return;
80 }
Elliott Hughes234ab152011-10-26 14:02:26 -070081 thread->suspend_count_ += delta;
82 if (for_debugger) {
83 thread->debug_suspend_count_ += delta;
84 }
85}
86
Elliott Hughes8d768a92011-09-14 16:35:25 -070087void ThreadList::FullSuspendCheck(Thread* thread) {
88 CHECK(thread != NULL);
89 CHECK_GE(thread->suspend_count_, 0);
90
91 MutexLock mu(thread_suspend_count_lock_);
92 if (thread->suspend_count_ == 0) {
93 return;
94 }
95
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080096 VLOG(threads) << *thread << " self-suspending";
Elliott Hughes8d768a92011-09-14 16:35:25 -070097 {
98 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
99 while (thread->suspend_count_ != 0) {
100 /*
101 * Wait for wakeup signal, releasing lock. The act of releasing
102 * and re-acquiring the lock provides the memory barriers we
103 * need for correct behavior on SMP.
104 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700105 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700106 }
107 CHECK_EQ(thread->suspend_count_, 0);
108 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800109 VLOG(threads) << *thread << " self-reviving";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700110}
111
Elliott Hughes475fc232011-10-25 15:00:35 -0700112void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700113 Thread* self = Thread::Current();
114
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800115 VLOG(threads) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700116
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700117 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800118 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700119 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700120
121 {
122 // Increment everybody's suspend count (except our own).
123 MutexLock mu(thread_suspend_count_lock_);
124 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
125 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700126 if (thread == self || (for_debugger && thread == debug_thread)) {
127 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700128 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800129 VLOG(threads) << "requesting thread suspend: " << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700130 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700131 }
132 }
133
134 /*
135 * Wait for everybody in kRunnable state to stop. Other states
136 * indicate the code is either running natively or sleeping quietly.
137 * Any attempt to transition back to kRunnable will cause a check
138 * for suspension, so it should be impossible for anything to execute
139 * interpreted code or modify objects (assuming native code plays nicely).
140 *
141 * It's also okay if the thread transitions to a non-kRunnable state.
142 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700143 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700144 * so if another thread is fiddling with its suspend count (perhaps
145 * self-suspending for the debugger) it won't block while we're waiting
146 * in here.
147 */
148 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
149 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700150 if (thread == self || (for_debugger && thread == debug_thread)) {
151 continue;
152 }
153 thread->WaitUntilSuspended();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800154 VLOG(threads) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700155 }
156
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800157 VLOG(threads) << *self << " SuspendAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700158}
159
Elliott Hughes4e235312011-12-02 11:34:15 -0800160void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700161 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700162 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700163
164 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
165
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800166 VLOG(threads) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700167
Elliott Hughes01158d72011-09-19 19:47:10 -0700168 if (!Contains(thread)) {
169 return;
170 }
171
172 {
173 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800174 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700175 }
176
177 thread->WaitUntilSuspended();
178
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800179 VLOG(threads) << "Suspend(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700180}
181
Elliott Hughes475fc232011-10-25 15:00:35 -0700182void ThreadList::SuspendSelfForDebugger() {
183 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700184
Elliott Hughes475fc232011-10-25 15:00:35 -0700185 // The debugger thread must not suspend itself due to debugger activity!
186 Thread* debug_thread = Dbg::GetDebugThread();
187 CHECK(debug_thread != NULL);
188 CHECK(self != debug_thread);
189
190 // Collisions with other suspends aren't really interesting. We want
191 // to ensure that we're the only one fiddling with the suspend count
192 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700193 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700194 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700195
196 // Suspend ourselves.
197 CHECK_GT(self->suspend_count_, 0);
198 self->SetState(Thread::kSuspended);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800199 VLOG(threads) << *self << " self-suspending (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700200
201 // Tell JDWP that we've completed suspension. The JDWP thread can't
202 // tell us to resume before we're fully asleep because we hold the
203 // suspend count lock.
204 Dbg::ClearWaitForEventThread();
205
206 while (self->suspend_count_ != 0) {
207 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
208 if (self->suspend_count_ != 0) {
209 // The condition was signaled but we're still suspended. This
210 // can happen if the debugger lets go while a SIGQUIT thread
211 // dump event is pending (assuming SignalCatcher was resumed for
212 // just long enough to try to grab the thread-suspend lock).
213 LOG(DEBUG) << *self << " still suspended after undo "
214 << "(suspend count=" << self->suspend_count_ << ")";
215 }
216 }
217 CHECK_EQ(self->suspend_count_, 0);
218 self->SetState(Thread::kRunnable);
Elliott Hughes1f729aa2012-03-02 13:55:41 -0800219 VLOG(threads) << *self << " self-reviving (debugger)";
Elliott Hughes475fc232011-10-25 15:00:35 -0700220}
221
222void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700223 Thread* self = Thread::Current();
224
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800225 VLOG(threads) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes8d768a92011-09-14 16:35:25 -0700226
227 // Decrement the suspend counts for all threads. No need for atomic
228 // writes, since nobody should be moving until we decrement the count.
229 // We do need to hold the thread list because of JNI attaches.
230 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800231 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700232 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700233 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700234 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
235 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700236 if (thread == self || (for_debugger && thread == debug_thread)) {
237 continue;
238 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700239 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700240 }
241 }
242
243 // Broadcast a notification to all suspended threads, some or all of
244 // which may choose to wake up. No need to wait for them.
245 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800246 VLOG(threads) << *self << " ResumeAll waking others";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700247 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700248 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700249 }
250
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800251 VLOG(threads) << *self << " ResumeAll complete";
Elliott Hughes8d768a92011-09-14 16:35:25 -0700252}
253
Elliott Hughes4e235312011-12-02 11:34:15 -0800254void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700255 DCHECK(thread != Thread::Current());
Elliott Hughesd07986f2011-12-06 18:27:45 -0800256
257 if (!for_debugger) { // The debugger is very naughty. See Dbg::InvokeMethod.
258 thread_list_lock_.AssertHeld();
259 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700260
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800261 VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes01158d72011-09-19 19:47:10 -0700262
263 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700264 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700265 if (!Contains(thread)) {
266 return;
267 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800268 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700269 }
270
271 {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800272 VLOG(threads) << "Resume(" << *thread << ") waking others";
Elliott Hughes01158d72011-09-19 19:47:10 -0700273 MutexLock mu(thread_suspend_count_lock_);
274 thread_suspend_count_cond_.Broadcast();
275 }
276
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800277 VLOG(threads) << "Resume(" << *thread << ") complete";
Elliott Hughes01158d72011-09-19 19:47:10 -0700278}
279
Elliott Hughes398f64b2012-03-26 18:05:48 -0700280void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) { // NOLINT
Elliott Hughes01158d72011-09-19 19:47:10 -0700281 DCHECK(thread != NULL);
282 Thread* self = Thread::Current();
283 if (thread != self) {
284 Suspend(thread);
285 }
286 callback(arg);
287 if (thread != self) {
288 Resume(thread);
289 }
290}
291
Elliott Hughes234ab152011-10-26 14:02:26 -0700292void ThreadList::UndoDebuggerSuspensions() {
293 Thread* self = Thread::Current();
294
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800295 VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
Elliott Hughes234ab152011-10-26 14:02:26 -0700296
297 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800298 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700299 MutexLock mu(thread_suspend_count_lock_);
300 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
301 Thread* thread = *it;
302 if (thread == self || thread->debug_suspend_count_ == 0) {
303 continue;
304 }
305 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
306 }
307 }
308
309 {
310 MutexLock mu(thread_suspend_count_lock_);
311 thread_suspend_count_cond_.Broadcast();
312 }
313
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800314 VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
Elliott Hughes234ab152011-10-26 14:02:26 -0700315}
316
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700317void ThreadList::Register() {
318 Thread* self = Thread::Current();
319
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800320 VLOG(threads) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700321
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800322 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700323 CHECK(!Contains(self));
324 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700325}
326
327void ThreadList::Unregister() {
328 Thread* self = Thread::Current();
329
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800330 VLOG(threads) << "ThreadList::Unregister() " << *self;
Elliott Hughes14357e82011-09-26 10:42:15 -0700331
Elliott Hughesc0f09332012-03-26 13:27:06 -0700332 // Any time-consuming destruction, plus anything that can call back into managed code or
333 // suspend and so on, must happen at this point, and not in ~Thread.
334 self->Destroy();
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700335
Elliott Hughesc0f09332012-03-26 13:27:06 -0700336 {
337 // Remove this thread from the list.
338 ScopedThreadListLock thread_list_lock;
339 CHECK(Contains(self));
340 list_.remove(self);
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700341 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700342
Elliott Hughese52e49b2012-04-02 16:05:44 -0700343 // Delete the Thread* and release the thin lock id.
344 uint32_t thin_lock_id = self->thin_lock_id_;
345 delete self;
346 ReleaseThreadId(thin_lock_id);
347
Elliott Hughesc0f09332012-03-26 13:27:06 -0700348 // Clear the TLS data, so that the underlying native thread is recognizably detached.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700349 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700350 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700351
352 // Signal that a thread just detached.
353 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700354}
355
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700356void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700357 thread_list_lock_.AssertHeld();
358 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700359 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700360 }
361}
362
Elliott Hughes8daa0922011-09-11 13:46:25 -0700363void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800364 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700365 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
366 (*it)->VisitRoots(visitor, arg);
367 }
368}
369
Elliott Hughes93e74e82011-09-13 11:07:03 -0700370/*
371 * Tell a new thread it's safe to start.
372 *
373 * We must hold the thread list lock before messing with another thread.
374 * In the general case we would also need to verify that the new thread was
375 * still in the thread list, but in our case the thread has not started
376 * executing user code and therefore has not had a chance to exit.
377 *
378 * We move it to kVmWait, and it then shifts itself to kRunning, which
379 * comes with a suspend-pending check. We do this after
380 */
381void ThreadList::SignalGo(Thread* child) {
382 Thread* self = Thread::Current();
383 CHECK(child != self);
384
385 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800386 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800387 VLOG(threads) << *self << " waiting for child " << *child << " to be in thread list...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700388
389 // We wait for the child to tell us that it's in the thread list.
390 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700391 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700392 }
393 }
394
395 // If we switch out of runnable and then back in, we know there's no pending suspend.
396 self->SetState(Thread::kVmWait);
397 self->SetState(Thread::kRunnable);
398
399 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800400 ScopedThreadListLock thread_list_lock;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800401 VLOG(threads) << *self << " telling child " << *child << " it's safe to proceed...";
Elliott Hughes93e74e82011-09-13 11:07:03 -0700402 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700403 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700404}
405
406void ThreadList::WaitForGo() {
407 Thread* self = Thread::Current();
408 DCHECK(Contains(self));
409
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700410 {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800411 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700412
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700413 // Tell our parent that we're in the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800414 VLOG(threads) << *self << " telling parent that we're now in thread list...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700415 self->SetState(Thread::kStarting);
416 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700417
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700418 // Wait until our parent tells us there's no suspend still pending
419 // from before we were on the thread list.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800420 VLOG(threads) << *self << " waiting for parent's go-ahead...";
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700421 while (self->GetState() != Thread::kVmWait) {
422 thread_start_cond_.Wait(thread_list_lock_);
423 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700424 }
425
426 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800427 VLOG(threads) << *self << " entering runnable state...";
Elliott Hughes47179f72011-10-27 16:44:39 -0700428 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
429 // started, we wait until it's over. Which means that if there's now another GC pending, our
430 // suspend count is non-zero, so switching to the runnable state will suspend us.
431 // TODO: find a better solution!
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800432 {
433 ScopedHeapLock heap_lock;
434 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700435 self->SetState(Thread::kRunnable);
436}
437
Elliott Hughese52e49b2012-04-02 16:05:44 -0700438bool ThreadList::AllOtherThreadsAreDaemons() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700439 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700440 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
441 // is null.
Elliott Hughese52e49b2012-04-02 16:05:44 -0700442 Thread* thread = *it;
443 if (thread != Thread::Current() && thread->GetPeer() != NULL && !thread->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700444 return false;
445 }
446 }
447 return true;
448}
449
Elliott Hughese52e49b2012-04-02 16:05:44 -0700450void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800451 ScopedThreadListLock thread_list_lock;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700452 while (!AllOtherThreadsAreDaemons()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700453 thread_exit_cond_.Wait(thread_list_lock_);
454 }
455}
456
457void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800458 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700459
Elliott Hughese52e49b2012-04-02 16:05:44 -0700460 // Tell all the daemons it's time to suspend.
Elliott Hughes038a8062011-09-18 14:12:41 -0700461 {
462 MutexLock mu(thread_suspend_count_lock_);
463 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
464 Thread* thread = *it;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700465 if (thread != Thread::Current()) {
466 ++thread->suspend_count_;
467 }
Elliott Hughes038a8062011-09-18 14:12:41 -0700468 }
469 }
470
471 // Give the threads a chance to suspend, complaining if they're slow.
472 bool have_complained = false;
473 for (int i = 0; i < 10; ++i) {
474 usleep(200 * 1000);
475 bool all_suspended = true;
476 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
477 Thread* thread = *it;
Elliott Hughese52e49b2012-04-02 16:05:44 -0700478 if (thread != Thread::Current() && thread->GetState() == Thread::kRunnable) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700479 if (!have_complained) {
480 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
481 have_complained = true;
482 }
483 all_suspended = false;
484 }
485 }
486 if (all_suspended) {
487 return;
488 }
489 }
490}
491
Elliott Hughes8daa0922011-09-11 13:46:25 -0700492uint32_t ThreadList::AllocThreadId() {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700493 MutexLock mu(allocated_ids_lock_);
494 //ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700495 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
496 if (!allocated_ids_[i]) {
497 allocated_ids_.set(i);
498 return i + 1; // Zero is reserved to mean "invalid".
499 }
500 }
501 LOG(FATAL) << "Out of internal thread ids";
502 return 0;
503}
504
505void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughese52e49b2012-04-02 16:05:44 -0700506 MutexLock mu(allocated_ids_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700507 --id; // Zero is reserved to mean "invalid".
508 DCHECK(allocated_ids_[id]) << id;
509 allocated_ids_.reset(id);
510}
511
512} // namespace art