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