blob: 4b33908e4a9e40777e39bc5b9bcdc9b69542c040 [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_") {
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 Hughes47179f72011-10-27 16:44:39 -0700101 DCHECK(delta == -1 || delta == +1 || delta == thread->debug_suspend_count_) << delta << " "
102 << *thread;
103 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 Hughes14357e82011-09-26 10:42:15 -0700125 if (verbose_) {
126 LOG(INFO) << *thread << " self-suspending";
127 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700128 {
129 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
130 while (thread->suspend_count_ != 0) {
131 /*
132 * Wait for wakeup signal, releasing lock. The act of releasing
133 * and re-acquiring the lock provides the memory barriers we
134 * need for correct behavior on SMP.
135 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700136 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700137 }
138 CHECK_EQ(thread->suspend_count_, 0);
139 }
Elliott Hughes14357e82011-09-26 10:42:15 -0700140 if (verbose_) {
141 LOG(INFO) << *thread << " self-reviving";
142 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700143}
144
Elliott Hughes475fc232011-10-25 15:00:35 -0700145void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700146 Thread* self = Thread::Current();
147
Elliott Hughes14357e82011-09-26 10:42:15 -0700148 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700149 LOG(INFO) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700150 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700151
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700152 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800153 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700154 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700155
156 {
157 // Increment everybody's suspend count (except our own).
158 MutexLock mu(thread_suspend_count_lock_);
159 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
160 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700161 if (thread == self || (for_debugger && thread == debug_thread)) {
162 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700163 }
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700164 if (verbose_) {
165 LOG(INFO) << "requesting thread suspend: " << *thread;
166 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700167 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700168 }
169 }
170
171 /*
172 * Wait for everybody in kRunnable state to stop. Other states
173 * indicate the code is either running natively or sleeping quietly.
174 * Any attempt to transition back to kRunnable will cause a check
175 * for suspension, so it should be impossible for anything to execute
176 * interpreted code or modify objects (assuming native code plays nicely).
177 *
178 * It's also okay if the thread transitions to a non-kRunnable state.
179 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700180 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700181 * so if another thread is fiddling with its suspend count (perhaps
182 * self-suspending for the debugger) it won't block while we're waiting
183 * in here.
184 */
185 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
186 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700187 if (thread == self || (for_debugger && thread == debug_thread)) {
188 continue;
189 }
190 thread->WaitUntilSuspended();
191 if (verbose_) {
192 LOG(INFO) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700193 }
194 }
195
Elliott Hughes14357e82011-09-26 10:42:15 -0700196 if (verbose_) {
197 LOG(INFO) << *self << " SuspendAll complete";
198 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700199}
200
Elliott Hughes01158d72011-09-19 19:47:10 -0700201void ThreadList::Suspend(Thread* thread) {
202 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700203 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700204
205 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
206
Elliott Hughes14357e82011-09-26 10:42:15 -0700207 if (verbose_) {
208 LOG(INFO) << "Suspend(" << *thread << ") starting...";
209 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700210
Elliott Hughes01158d72011-09-19 19:47:10 -0700211 if (!Contains(thread)) {
212 return;
213 }
214
215 {
216 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700217 ModifySuspendCount(thread, +1, false);
Elliott Hughes01158d72011-09-19 19:47:10 -0700218 }
219
220 thread->WaitUntilSuspended();
221
Elliott Hughes14357e82011-09-26 10:42:15 -0700222 if (verbose_) {
223 LOG(INFO) << "Suspend(" << *thread << ") complete";
224 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700225}
226
Elliott Hughes475fc232011-10-25 15:00:35 -0700227void ThreadList::SuspendSelfForDebugger() {
228 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700229
Elliott Hughes475fc232011-10-25 15:00:35 -0700230 // The debugger thread must not suspend itself due to debugger activity!
231 Thread* debug_thread = Dbg::GetDebugThread();
232 CHECK(debug_thread != NULL);
233 CHECK(self != debug_thread);
234
235 // Collisions with other suspends aren't really interesting. We want
236 // to ensure that we're the only one fiddling with the suspend count
237 // though.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800238 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700239 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700240 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700241
242 // Suspend ourselves.
243 CHECK_GT(self->suspend_count_, 0);
244 self->SetState(Thread::kSuspended);
245 if (verbose_) {
246 LOG(INFO) << *self << " self-suspending (dbg)";
247 }
248
249 // Tell JDWP that we've completed suspension. The JDWP thread can't
250 // tell us to resume before we're fully asleep because we hold the
251 // suspend count lock.
252 Dbg::ClearWaitForEventThread();
253
254 while (self->suspend_count_ != 0) {
255 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
256 if (self->suspend_count_ != 0) {
257 // The condition was signaled but we're still suspended. This
258 // can happen if the debugger lets go while a SIGQUIT thread
259 // dump event is pending (assuming SignalCatcher was resumed for
260 // just long enough to try to grab the thread-suspend lock).
261 LOG(DEBUG) << *self << " still suspended after undo "
262 << "(suspend count=" << self->suspend_count_ << ")";
263 }
264 }
265 CHECK_EQ(self->suspend_count_, 0);
266 self->SetState(Thread::kRunnable);
267 if (verbose_) {
268 LOG(INFO) << *self << " self-reviving (dbg)";
269 }
270}
271
272void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700273 Thread* self = Thread::Current();
274
Elliott Hughes14357e82011-09-26 10:42:15 -0700275 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700276 LOG(INFO) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700277 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700278
279 // Decrement the suspend counts for all threads. No need for atomic
280 // writes, since nobody should be moving until we decrement the count.
281 // We do need to hold the thread list because of JNI attaches.
282 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800283 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700284 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700285 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700286 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
287 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700288 if (thread == self || (for_debugger && thread == debug_thread)) {
289 continue;
290 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700291 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700292 }
293 }
294
295 // Broadcast a notification to all suspended threads, some or all of
296 // which may choose to wake up. No need to wait for them.
297 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700298 if (verbose_) {
299 LOG(INFO) << *self << " ResumeAll waking others";
300 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700301 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700302 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700303 }
304
Elliott Hughes14357e82011-09-26 10:42:15 -0700305 if (verbose_) {
306 LOG(INFO) << *self << " ResumeAll complete";
307 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700308}
309
Elliott Hughes01158d72011-09-19 19:47:10 -0700310void ThreadList::Resume(Thread* thread) {
311 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700312 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700313
Elliott Hughes14357e82011-09-26 10:42:15 -0700314 if (verbose_) {
315 LOG(INFO) << "Resume(" << *thread << ") starting...";
316 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700317
318 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700319 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700320 if (!Contains(thread)) {
321 return;
322 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700323 ModifySuspendCount(thread, -1, false);
Elliott Hughes01158d72011-09-19 19:47:10 -0700324 }
325
326 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700327 if (verbose_) {
328 LOG(INFO) << "Resume(" << *thread << ") waking others";
329 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700330 MutexLock mu(thread_suspend_count_lock_);
331 thread_suspend_count_cond_.Broadcast();
332 }
333
Elliott Hughes14357e82011-09-26 10:42:15 -0700334 if (verbose_) {
335 LOG(INFO) << "Resume(" << *thread << ") complete";
336 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700337}
338
339void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
340 DCHECK(thread != NULL);
341 Thread* self = Thread::Current();
342 if (thread != self) {
343 Suspend(thread);
344 }
345 callback(arg);
346 if (thread != self) {
347 Resume(thread);
348 }
349}
350
Elliott Hughes234ab152011-10-26 14:02:26 -0700351void ThreadList::UndoDebuggerSuspensions() {
352 Thread* self = Thread::Current();
353
354 if (verbose_) {
355 LOG(INFO) << *self << " UndoDebuggerSuspensions starting";
356 }
357
358 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800359 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700360 MutexLock mu(thread_suspend_count_lock_);
361 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
362 Thread* thread = *it;
363 if (thread == self || thread->debug_suspend_count_ == 0) {
364 continue;
365 }
366 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
367 }
368 }
369
370 {
371 MutexLock mu(thread_suspend_count_lock_);
372 thread_suspend_count_cond_.Broadcast();
373 }
374
375 if (verbose_) {
376 LOG(INFO) << "UndoDebuggerSuspensions(" << *self << ") complete";
377 }
378}
379
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700380void ThreadList::Register() {
381 Thread* self = Thread::Current();
382
Elliott Hughes14357e82011-09-26 10:42:15 -0700383 if (verbose_) {
Elliott Hughese0918552011-10-28 17:18:29 -0700384 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes14357e82011-09-26 10:42:15 -0700385 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700386
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800387 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700388 CHECK(!Contains(self));
389 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700390}
391
392void ThreadList::Unregister() {
393 Thread* self = Thread::Current();
394
Elliott Hughes14357e82011-09-26 10:42:15 -0700395 if (verbose_) {
396 LOG(INFO) << "ThreadList::Unregister() " << *self;
397 }
398
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700399 if (self->GetPeer() != NULL) {
400 self->SetState(Thread::kRunnable);
401
402 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
403 // down the Thread* and removing it from the thread list (or start taking any locks).
404 self->HandleUncaughtExceptions();
405
406 // Make sure we remove from ThreadGroup before taking the
407 // thread_list_lock_ since it allocates an Iterator which can cause
408 // a GC which will want to suspend.
409 self->RemoveFromThreadGroup();
410 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700411
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800412 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700413
414 // Remove this thread from the list.
415 CHECK(Contains(self));
416 list_.remove(self);
417
418 // Delete the Thread* and release the thin lock id.
419 uint32_t thin_lock_id = self->thin_lock_id_;
420 delete self;
421 ReleaseThreadId(thin_lock_id);
422
423 // Clear the TLS data, so that thread is recognizably detached.
424 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700425 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700426
427 // Signal that a thread just detached.
428 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700429}
430
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700431void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700432 thread_list_lock_.AssertHeld();
433 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700434 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700435 }
436}
437
Elliott Hughes8daa0922011-09-11 13:46:25 -0700438void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800439 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700440 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
441 (*it)->VisitRoots(visitor, arg);
442 }
443}
444
Elliott Hughes93e74e82011-09-13 11:07:03 -0700445/*
446 * Tell a new thread it's safe to start.
447 *
448 * We must hold the thread list lock before messing with another thread.
449 * In the general case we would also need to verify that the new thread was
450 * still in the thread list, but in our case the thread has not started
451 * executing user code and therefore has not had a chance to exit.
452 *
453 * We move it to kVmWait, and it then shifts itself to kRunning, which
454 * comes with a suspend-pending check. We do this after
455 */
456void ThreadList::SignalGo(Thread* child) {
457 Thread* self = Thread::Current();
458 CHECK(child != self);
459
460 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800461 ScopedThreadListLock thread_list_lock;
Elliott Hughes47179f72011-10-27 16:44:39 -0700462 if (verbose_) {
463 LOG(INFO) << *self << " waiting for child " << *child << " to be in thread list...";
464 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700465
466 // We wait for the child to tell us that it's in the thread list.
467 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700468 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700469 }
470 }
471
472 // If we switch out of runnable and then back in, we know there's no pending suspend.
473 self->SetState(Thread::kVmWait);
474 self->SetState(Thread::kRunnable);
475
476 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800477 ScopedThreadListLock thread_list_lock;
Elliott Hughes47179f72011-10-27 16:44:39 -0700478 if (verbose_) {
479 LOG(INFO) << *self << " telling child " << *child << " it's safe to proceed...";
480 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700481 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700482 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700483}
484
485void ThreadList::WaitForGo() {
486 Thread* self = Thread::Current();
487 DCHECK(Contains(self));
488
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700489 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800490 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700491
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700492 // Tell our parent that we're in the thread list.
Elliott Hughes47179f72011-10-27 16:44:39 -0700493 if (verbose_) {
494 LOG(INFO) << *self << " telling parent that we're now in thread list...";
495 }
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700496 self->SetState(Thread::kStarting);
497 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700498
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700499 // Wait until our parent tells us there's no suspend still pending
500 // from before we were on the thread list.
Elliott Hughes47179f72011-10-27 16:44:39 -0700501 if (verbose_) {
502 LOG(INFO) << *self << " waiting for parent's go-ahead...";
503 }
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700504 while (self->GetState() != Thread::kVmWait) {
505 thread_start_cond_.Wait(thread_list_lock_);
506 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700507 }
508
509 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes47179f72011-10-27 16:44:39 -0700510 if (verbose_) {
511 LOG(INFO) << *self << " entering runnable state...";
512 }
513 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
514 // started, we wait until it's over. Which means that if there's now another GC pending, our
515 // suspend count is non-zero, so switching to the runnable state will suspend us.
516 // TODO: find a better solution!
517 Heap::Lock();
518 Heap::Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700519 self->SetState(Thread::kRunnable);
520}
521
Elliott Hughes038a8062011-09-18 14:12:41 -0700522bool ThreadList::AllThreadsAreDaemons() {
523 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700524 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
525 // is null.
526 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700527 return false;
528 }
529 }
530 return true;
531}
532
533void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800534 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700535 while (!AllThreadsAreDaemons()) {
536 thread_exit_cond_.Wait(thread_list_lock_);
537 }
538}
539
540void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800541 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700542
543 // Tell all the daemons it's time to suspend. (At this point, we know
544 // all threads are daemons.)
545 {
546 MutexLock mu(thread_suspend_count_lock_);
547 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
548 Thread* thread = *it;
549 ++thread->suspend_count_;
550 }
551 }
552
553 // Give the threads a chance to suspend, complaining if they're slow.
554 bool have_complained = false;
555 for (int i = 0; i < 10; ++i) {
556 usleep(200 * 1000);
557 bool all_suspended = true;
558 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
559 Thread* thread = *it;
560 if (thread->GetState() == Thread::kRunnable) {
561 if (!have_complained) {
562 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
563 have_complained = true;
564 }
565 all_suspended = false;
566 }
567 }
568 if (all_suspended) {
569 return;
570 }
571 }
572}
573
Elliott Hughes8daa0922011-09-11 13:46:25 -0700574uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800575 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700576 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
577 if (!allocated_ids_[i]) {
578 allocated_ids_.set(i);
579 return i + 1; // Zero is reserved to mean "invalid".
580 }
581 }
582 LOG(FATAL) << "Out of internal thread ids";
583 return 0;
584}
585
586void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700587 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700588 --id; // Zero is reserved to mean "invalid".
589 DCHECK(allocated_ids_[id]) << id;
590 allocated_ids_.reset(id);
591}
592
593} // namespace art