blob: 21796978998cd64c619e9f94f18c94f37a72da1f [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.
35 if (self != NULL) {
36 Heap::Lock();
37 }
38
39 // Avoid deadlock between two threads trying to SuspendAll
40 // simultaneously by going to kVmWait if the lock cannot be
41 // immediately acquired.
42 // TODO: is this needed if we took the heap lock? taking the heap lock will have done this,
43 // and the other thread will now be in kVmWait waiting for the heap lock.
44 ThreadList* thread_list = Runtime::Current()->GetThreadList();
45 if (!thread_list->thread_list_lock_.TryLock()) {
46 if (self == NULL) {
47 thread_list->thread_list_lock_.Lock();
48 } else {
49 ScopedThreadStateChange tsc(self, Thread::kVmWait);
50 thread_list->thread_list_lock_.Lock();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070051 }
52 }
53
Elliott Hughesbbd9d832011-11-07 14:40:00 -080054 if (self != NULL) {
55 Heap::Unlock();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070056 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080057}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070058
Elliott Hughesbbd9d832011-11-07 14:40:00 -080059ScopedThreadListLock::~ScopedThreadListLock() {
60 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
61}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070062
Elliott Hughes14357e82011-09-26 10:42:15 -070063ThreadList::ThreadList(bool verbose)
64 : verbose_(verbose),
65 thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070066 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070067 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070068 thread_suspend_count_lock_("thread suspend count lock"),
69 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Brian Carlstrom72db0d72011-11-10 17:58:56 -080070 if (verbose_) {
71 LOG(INFO) << "default stack size " << Runtime::Current()->GetDefaultStackSize() / KB << "kb";
72 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070073}
74
75ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070076 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070077 if (Contains(Thread::Current())) {
78 Runtime::Current()->DetachCurrentThread();
79 }
80
Elliott Hughes038a8062011-09-18 14:12:41 -070081 WaitForNonDaemonThreadsToExit();
82 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070083}
84
85bool ThreadList::Contains(Thread* thread) {
86 return find(list_.begin(), list_.end(), thread) != list_.end();
87}
88
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070089pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070090 return thread_list_lock_.GetOwner();
91}
92
Elliott Hughes8daa0922011-09-11 13:46:25 -070093void ThreadList::Dump(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080094 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070095 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070096 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
97 (*it)->Dump(os);
98 os << "\n";
99 }
100}
101
Elliott Hughes234ab152011-10-26 14:02:26 -0700102void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
103#ifndef NDEBUG
Elliott Hughes47179f72011-10-27 16:44:39 -0700104 DCHECK(delta == -1 || delta == +1 || delta == thread->debug_suspend_count_) << delta << " "
105 << *thread;
106 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700107#endif
Elliott Hughes47179f72011-10-27 16:44:39 -0700108 if (delta == -1 && thread->suspend_count_ <= 0) {
109 // This can happen if you attach a thread during a GC.
110 LOG(WARNING) << *thread << " suspend count already zero";
111 return;
112 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700113 thread->suspend_count_ += delta;
114 if (for_debugger) {
115 thread->debug_suspend_count_ += delta;
116 }
117}
118
Elliott Hughes8d768a92011-09-14 16:35:25 -0700119void ThreadList::FullSuspendCheck(Thread* thread) {
120 CHECK(thread != NULL);
121 CHECK_GE(thread->suspend_count_, 0);
122
123 MutexLock mu(thread_suspend_count_lock_);
124 if (thread->suspend_count_ == 0) {
125 return;
126 }
127
Elliott Hughes14357e82011-09-26 10:42:15 -0700128 if (verbose_) {
129 LOG(INFO) << *thread << " self-suspending";
130 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700131 {
132 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
133 while (thread->suspend_count_ != 0) {
134 /*
135 * Wait for wakeup signal, releasing lock. The act of releasing
136 * and re-acquiring the lock provides the memory barriers we
137 * need for correct behavior on SMP.
138 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700139 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700140 }
141 CHECK_EQ(thread->suspend_count_, 0);
142 }
Elliott Hughes14357e82011-09-26 10:42:15 -0700143 if (verbose_) {
144 LOG(INFO) << *thread << " self-reviving";
145 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700146}
147
Elliott Hughes475fc232011-10-25 15:00:35 -0700148void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700149 Thread* self = Thread::Current();
150
Elliott Hughes14357e82011-09-26 10:42:15 -0700151 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700152 LOG(INFO) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700153 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700154
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700155 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800156 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700157 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700158
159 {
160 // Increment everybody's suspend count (except our own).
161 MutexLock mu(thread_suspend_count_lock_);
162 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
163 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700164 if (thread == self || (for_debugger && thread == debug_thread)) {
165 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700166 }
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700167 if (verbose_) {
168 LOG(INFO) << "requesting thread suspend: " << *thread;
169 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700170 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700171 }
172 }
173
174 /*
175 * Wait for everybody in kRunnable state to stop. Other states
176 * indicate the code is either running natively or sleeping quietly.
177 * Any attempt to transition back to kRunnable will cause a check
178 * for suspension, so it should be impossible for anything to execute
179 * interpreted code or modify objects (assuming native code plays nicely).
180 *
181 * It's also okay if the thread transitions to a non-kRunnable state.
182 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700183 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700184 * so if another thread is fiddling with its suspend count (perhaps
185 * self-suspending for the debugger) it won't block while we're waiting
186 * in here.
187 */
188 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
189 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700190 if (thread == self || (for_debugger && thread == debug_thread)) {
191 continue;
192 }
193 thread->WaitUntilSuspended();
194 if (verbose_) {
195 LOG(INFO) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700196 }
197 }
198
Elliott Hughes14357e82011-09-26 10:42:15 -0700199 if (verbose_) {
200 LOG(INFO) << *self << " SuspendAll complete";
201 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700202}
203
Elliott Hughes4e235312011-12-02 11:34:15 -0800204void ThreadList::Suspend(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700205 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700206 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700207
208 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
209
Elliott Hughes14357e82011-09-26 10:42:15 -0700210 if (verbose_) {
Elliott Hughes4e235312011-12-02 11:34:15 -0800211 LOG(INFO) << "Suspend(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700212 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700213
Elliott Hughes01158d72011-09-19 19:47:10 -0700214 if (!Contains(thread)) {
215 return;
216 }
217
218 {
219 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes4e235312011-12-02 11:34:15 -0800220 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700221 }
222
223 thread->WaitUntilSuspended();
224
Elliott Hughes14357e82011-09-26 10:42:15 -0700225 if (verbose_) {
226 LOG(INFO) << "Suspend(" << *thread << ") complete";
227 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700228}
229
Elliott Hughes475fc232011-10-25 15:00:35 -0700230void ThreadList::SuspendSelfForDebugger() {
231 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700232
Elliott Hughes475fc232011-10-25 15:00:35 -0700233 // The debugger thread must not suspend itself due to debugger activity!
234 Thread* debug_thread = Dbg::GetDebugThread();
235 CHECK(debug_thread != NULL);
236 CHECK(self != debug_thread);
237
238 // Collisions with other suspends aren't really interesting. We want
239 // to ensure that we're the only one fiddling with the suspend count
240 // though.
Elliott Hughes475fc232011-10-25 15:00:35 -0700241 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700242 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700243
244 // Suspend ourselves.
245 CHECK_GT(self->suspend_count_, 0);
246 self->SetState(Thread::kSuspended);
247 if (verbose_) {
248 LOG(INFO) << *self << " self-suspending (dbg)";
249 }
250
251 // Tell JDWP that we've completed suspension. The JDWP thread can't
252 // tell us to resume before we're fully asleep because we hold the
253 // suspend count lock.
254 Dbg::ClearWaitForEventThread();
255
256 while (self->suspend_count_ != 0) {
257 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
258 if (self->suspend_count_ != 0) {
259 // The condition was signaled but we're still suspended. This
260 // can happen if the debugger lets go while a SIGQUIT thread
261 // dump event is pending (assuming SignalCatcher was resumed for
262 // just long enough to try to grab the thread-suspend lock).
263 LOG(DEBUG) << *self << " still suspended after undo "
264 << "(suspend count=" << self->suspend_count_ << ")";
265 }
266 }
267 CHECK_EQ(self->suspend_count_, 0);
268 self->SetState(Thread::kRunnable);
269 if (verbose_) {
270 LOG(INFO) << *self << " self-reviving (dbg)";
271 }
272}
273
274void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700275 Thread* self = Thread::Current();
276
Elliott Hughes14357e82011-09-26 10:42:15 -0700277 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700278 LOG(INFO) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700279 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700280
281 // Decrement the suspend counts for all threads. No need for atomic
282 // writes, since nobody should be moving until we decrement the count.
283 // We do need to hold the thread list because of JNI attaches.
284 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800285 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700286 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700287 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700288 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
289 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700290 if (thread == self || (for_debugger && thread == debug_thread)) {
291 continue;
292 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700293 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700294 }
295 }
296
297 // Broadcast a notification to all suspended threads, some or all of
298 // which may choose to wake up. No need to wait for them.
299 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700300 if (verbose_) {
301 LOG(INFO) << *self << " ResumeAll waking others";
302 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700303 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700304 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700305 }
306
Elliott Hughes14357e82011-09-26 10:42:15 -0700307 if (verbose_) {
308 LOG(INFO) << *self << " ResumeAll complete";
309 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700310}
311
Elliott Hughes4e235312011-12-02 11:34:15 -0800312void ThreadList::Resume(Thread* thread, bool for_debugger) {
Elliott Hughes01158d72011-09-19 19:47:10 -0700313 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700314 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700315
Elliott Hughes14357e82011-09-26 10:42:15 -0700316 if (verbose_) {
Elliott Hughes4e235312011-12-02 11:34:15 -0800317 LOG(INFO) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700318 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700319
320 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700321 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700322 if (!Contains(thread)) {
323 return;
324 }
Elliott Hughes4e235312011-12-02 11:34:15 -0800325 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes01158d72011-09-19 19:47:10 -0700326 }
327
328 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700329 if (verbose_) {
330 LOG(INFO) << "Resume(" << *thread << ") waking others";
331 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700332 MutexLock mu(thread_suspend_count_lock_);
333 thread_suspend_count_cond_.Broadcast();
334 }
335
Elliott Hughes14357e82011-09-26 10:42:15 -0700336 if (verbose_) {
337 LOG(INFO) << "Resume(" << *thread << ") complete";
338 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700339}
340
341void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
342 DCHECK(thread != NULL);
343 Thread* self = Thread::Current();
344 if (thread != self) {
345 Suspend(thread);
346 }
347 callback(arg);
348 if (thread != self) {
349 Resume(thread);
350 }
351}
352
Elliott Hughes234ab152011-10-26 14:02:26 -0700353void ThreadList::UndoDebuggerSuspensions() {
354 Thread* self = Thread::Current();
355
356 if (verbose_) {
357 LOG(INFO) << *self << " UndoDebuggerSuspensions starting";
358 }
359
360 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800361 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700362 MutexLock mu(thread_suspend_count_lock_);
363 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
364 Thread* thread = *it;
365 if (thread == self || thread->debug_suspend_count_ == 0) {
366 continue;
367 }
368 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
369 }
370 }
371
372 {
373 MutexLock mu(thread_suspend_count_lock_);
374 thread_suspend_count_cond_.Broadcast();
375 }
376
377 if (verbose_) {
378 LOG(INFO) << "UndoDebuggerSuspensions(" << *self << ") complete";
379 }
380}
381
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700382void ThreadList::Register() {
383 Thread* self = Thread::Current();
384
Elliott Hughes14357e82011-09-26 10:42:15 -0700385 if (verbose_) {
Elliott Hughese0918552011-10-28 17:18:29 -0700386 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes14357e82011-09-26 10:42:15 -0700387 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700388
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800389 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700390 CHECK(!Contains(self));
391 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700392}
393
394void ThreadList::Unregister() {
395 Thread* self = Thread::Current();
396
Elliott Hughes14357e82011-09-26 10:42:15 -0700397 if (verbose_) {
398 LOG(INFO) << "ThreadList::Unregister() " << *self;
399 }
400
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700401 if (self->GetPeer() != NULL) {
402 self->SetState(Thread::kRunnable);
403
404 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
405 // down the Thread* and removing it from the thread list (or start taking any locks).
406 self->HandleUncaughtExceptions();
407
408 // Make sure we remove from ThreadGroup before taking the
409 // thread_list_lock_ since it allocates an Iterator which can cause
410 // a GC which will want to suspend.
411 self->RemoveFromThreadGroup();
412 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700413
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800414 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700415
416 // Remove this thread from the list.
417 CHECK(Contains(self));
418 list_.remove(self);
419
420 // Delete the Thread* and release the thin lock id.
421 uint32_t thin_lock_id = self->thin_lock_id_;
422 delete self;
423 ReleaseThreadId(thin_lock_id);
424
425 // Clear the TLS data, so that thread is recognizably detached.
426 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700427 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700428
429 // Signal that a thread just detached.
430 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700431}
432
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700433void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700434 thread_list_lock_.AssertHeld();
435 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700436 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700437 }
438}
439
Elliott Hughes8daa0922011-09-11 13:46:25 -0700440void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800441 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700442 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
443 (*it)->VisitRoots(visitor, arg);
444 }
445}
446
Elliott Hughes93e74e82011-09-13 11:07:03 -0700447/*
448 * Tell a new thread it's safe to start.
449 *
450 * We must hold the thread list lock before messing with another thread.
451 * In the general case we would also need to verify that the new thread was
452 * still in the thread list, but in our case the thread has not started
453 * executing user code and therefore has not had a chance to exit.
454 *
455 * We move it to kVmWait, and it then shifts itself to kRunning, which
456 * comes with a suspend-pending check. We do this after
457 */
458void ThreadList::SignalGo(Thread* child) {
459 Thread* self = Thread::Current();
460 CHECK(child != self);
461
462 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800463 ScopedThreadListLock thread_list_lock;
Elliott Hughes47179f72011-10-27 16:44:39 -0700464 if (verbose_) {
465 LOG(INFO) << *self << " waiting for child " << *child << " to be in thread list...";
466 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700467
468 // We wait for the child to tell us that it's in the thread list.
469 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700470 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700471 }
472 }
473
474 // If we switch out of runnable and then back in, we know there's no pending suspend.
475 self->SetState(Thread::kVmWait);
476 self->SetState(Thread::kRunnable);
477
478 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800479 ScopedThreadListLock thread_list_lock;
Elliott Hughes47179f72011-10-27 16:44:39 -0700480 if (verbose_) {
481 LOG(INFO) << *self << " telling child " << *child << " it's safe to proceed...";
482 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700483 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700484 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700485}
486
487void ThreadList::WaitForGo() {
488 Thread* self = Thread::Current();
489 DCHECK(Contains(self));
490
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700491 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800492 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700493
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700494 // Tell our parent that we're in the thread list.
Elliott Hughes47179f72011-10-27 16:44:39 -0700495 if (verbose_) {
496 LOG(INFO) << *self << " telling parent that we're now in thread list...";
497 }
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700498 self->SetState(Thread::kStarting);
499 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700500
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700501 // Wait until our parent tells us there's no suspend still pending
502 // from before we were on the thread list.
Elliott Hughes47179f72011-10-27 16:44:39 -0700503 if (verbose_) {
504 LOG(INFO) << *self << " waiting for parent's go-ahead...";
505 }
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700506 while (self->GetState() != Thread::kVmWait) {
507 thread_start_cond_.Wait(thread_list_lock_);
508 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700509 }
510
511 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes47179f72011-10-27 16:44:39 -0700512 if (verbose_) {
513 LOG(INFO) << *self << " entering runnable state...";
514 }
515 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
516 // started, we wait until it's over. Which means that if there's now another GC pending, our
517 // suspend count is non-zero, so switching to the runnable state will suspend us.
518 // TODO: find a better solution!
519 Heap::Lock();
520 Heap::Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700521 self->SetState(Thread::kRunnable);
522}
523
Elliott Hughes038a8062011-09-18 14:12:41 -0700524bool ThreadList::AllThreadsAreDaemons() {
525 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700526 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
527 // is null.
528 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700529 return false;
530 }
531 }
532 return true;
533}
534
535void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800536 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700537 while (!AllThreadsAreDaemons()) {
538 thread_exit_cond_.Wait(thread_list_lock_);
539 }
540}
541
542void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800543 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700544
545 // Tell all the daemons it's time to suspend. (At this point, we know
546 // all threads are daemons.)
547 {
548 MutexLock mu(thread_suspend_count_lock_);
549 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
550 Thread* thread = *it;
551 ++thread->suspend_count_;
552 }
553 }
554
555 // Give the threads a chance to suspend, complaining if they're slow.
556 bool have_complained = false;
557 for (int i = 0; i < 10; ++i) {
558 usleep(200 * 1000);
559 bool all_suspended = true;
560 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
561 Thread* thread = *it;
562 if (thread->GetState() == Thread::kRunnable) {
563 if (!have_complained) {
564 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
565 have_complained = true;
566 }
567 all_suspended = false;
568 }
569 }
570 if (all_suspended) {
571 return;
572 }
573 }
574}
575
Elliott Hughes8daa0922011-09-11 13:46:25 -0700576uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800577 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700578 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
579 if (!allocated_ids_[i]) {
580 allocated_ids_.set(i);
581 return i + 1; // Zero is reserved to mean "invalid".
582 }
583 }
584 LOG(FATAL) << "Out of internal thread ids";
585 return 0;
586}
587
588void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700589 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700590 --id; // Zero is reserved to mean "invalid".
591 DCHECK(allocated_ids_[id]) << id;
592 allocated_ids_.reset(id);
593}
594
595} // namespace art