blob: 6da5e682f0ac438a5500f821c98be3ead2c3592b [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
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070025// TODO: merge with ThreadListLock?
26class ThreadListLocker {
27 public:
28
29 explicit ThreadListLocker(const ThreadList* thread_list) : thread_list_(thread_list) {
30 // Avoid deadlock between two threads trying to SuspendAll
31 // simultaneously by going to kVmWait if the lock cannot be
32 // immediately acquired.
33 if (!thread_list_->thread_list_lock_.TryLock()) {
Brian Carlstrom4514d3c2011-10-21 17:01:31 -070034 Thread* self = Thread::Current();
35 if (self == NULL) {
36 thread_list_->thread_list_lock_.Lock();
37 } else {
38 ScopedThreadStateChange tsc(self, Thread::kVmWait);
39 thread_list_->thread_list_lock_.Lock();
40 }
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070041 }
42 }
43
44 ~ThreadListLocker() {
45 thread_list_->thread_list_lock_.Unlock();
46 }
47
48 private:
49 const ThreadList* thread_list_;
50 DISALLOW_COPY_AND_ASSIGN(ThreadListLocker);
51};
52
Elliott Hughes14357e82011-09-26 10:42:15 -070053ThreadList::ThreadList(bool verbose)
54 : verbose_(verbose),
55 thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070056 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070057 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070058 thread_suspend_count_lock_("thread suspend count lock"),
59 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes8daa0922011-09-11 13:46:25 -070060}
61
62ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070063 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070064 if (Contains(Thread::Current())) {
65 Runtime::Current()->DetachCurrentThread();
66 }
67
Elliott Hughes038a8062011-09-18 14:12:41 -070068 WaitForNonDaemonThreadsToExit();
69 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070070}
71
72bool ThreadList::Contains(Thread* thread) {
73 return find(list_.begin(), list_.end(), thread) != list_.end();
74}
75
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070076pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070077 return thread_list_lock_.GetOwner();
78}
79
Elliott Hughes8daa0922011-09-11 13:46:25 -070080void ThreadList::Dump(std::ostream& os) {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070081 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -070082 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070083 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
84 (*it)->Dump(os);
85 os << "\n";
86 }
87}
88
Elliott Hughes234ab152011-10-26 14:02:26 -070089void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
90#ifndef NDEBUG
Elliott Hughes47179f72011-10-27 16:44:39 -070091 DCHECK(delta == -1 || delta == +1 || delta == thread->debug_suspend_count_) << delta << " "
92 << *thread;
93 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -070094#endif
Elliott Hughes47179f72011-10-27 16:44:39 -070095 if (delta == -1 && thread->suspend_count_ <= 0) {
96 // This can happen if you attach a thread during a GC.
97 LOG(WARNING) << *thread << " suspend count already zero";
98 return;
99 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700100 thread->suspend_count_ += delta;
101 if (for_debugger) {
102 thread->debug_suspend_count_ += delta;
103 }
104}
105
Elliott Hughes8d768a92011-09-14 16:35:25 -0700106void ThreadList::FullSuspendCheck(Thread* thread) {
107 CHECK(thread != NULL);
108 CHECK_GE(thread->suspend_count_, 0);
109
110 MutexLock mu(thread_suspend_count_lock_);
111 if (thread->suspend_count_ == 0) {
112 return;
113 }
114
Elliott Hughes14357e82011-09-26 10:42:15 -0700115 if (verbose_) {
116 LOG(INFO) << *thread << " self-suspending";
117 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700118 {
119 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
120 while (thread->suspend_count_ != 0) {
121 /*
122 * Wait for wakeup signal, releasing lock. The act of releasing
123 * and re-acquiring the lock provides the memory barriers we
124 * need for correct behavior on SMP.
125 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700126 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700127 }
128 CHECK_EQ(thread->suspend_count_, 0);
129 }
Elliott Hughes14357e82011-09-26 10:42:15 -0700130 if (verbose_) {
131 LOG(INFO) << *thread << " self-reviving";
132 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700133}
134
Elliott Hughes475fc232011-10-25 15:00:35 -0700135void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700136 Thread* self = Thread::Current();
137
Elliott Hughes14357e82011-09-26 10:42:15 -0700138 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700139 LOG(INFO) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700140 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700141
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700142 CHECK_EQ(self->GetState(), Thread::kRunnable);
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700143 ThreadListLocker locker(this);
Elliott Hughes475fc232011-10-25 15:00:35 -0700144 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700145
146 {
147 // Increment everybody's suspend count (except our own).
148 MutexLock mu(thread_suspend_count_lock_);
149 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
150 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700151 if (thread == self || (for_debugger && thread == debug_thread)) {
152 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700153 }
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700154 if (verbose_) {
155 LOG(INFO) << "requesting thread suspend: " << *thread;
156 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700157 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700158 }
159 }
160
161 /*
162 * Wait for everybody in kRunnable state to stop. Other states
163 * indicate the code is either running natively or sleeping quietly.
164 * Any attempt to transition back to kRunnable will cause a check
165 * for suspension, so it should be impossible for anything to execute
166 * interpreted code or modify objects (assuming native code plays nicely).
167 *
168 * It's also okay if the thread transitions to a non-kRunnable state.
169 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700170 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700171 * so if another thread is fiddling with its suspend count (perhaps
172 * self-suspending for the debugger) it won't block while we're waiting
173 * in here.
174 */
175 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
176 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700177 if (thread == self || (for_debugger && thread == debug_thread)) {
178 continue;
179 }
180 thread->WaitUntilSuspended();
181 if (verbose_) {
182 LOG(INFO) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700183 }
184 }
185
Elliott Hughes14357e82011-09-26 10:42:15 -0700186 if (verbose_) {
187 LOG(INFO) << *self << " SuspendAll complete";
188 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700189}
190
Elliott Hughes01158d72011-09-19 19:47:10 -0700191void ThreadList::Suspend(Thread* thread) {
192 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700193 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700194
195 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
196
Elliott Hughes14357e82011-09-26 10:42:15 -0700197 if (verbose_) {
198 LOG(INFO) << "Suspend(" << *thread << ") starting...";
199 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700200
Elliott Hughes01158d72011-09-19 19:47:10 -0700201 if (!Contains(thread)) {
202 return;
203 }
204
205 {
206 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700207 ModifySuspendCount(thread, +1, false);
Elliott Hughes01158d72011-09-19 19:47:10 -0700208 }
209
210 thread->WaitUntilSuspended();
211
Elliott Hughes14357e82011-09-26 10:42:15 -0700212 if (verbose_) {
213 LOG(INFO) << "Suspend(" << *thread << ") complete";
214 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700215}
216
Elliott Hughes475fc232011-10-25 15:00:35 -0700217void ThreadList::SuspendSelfForDebugger() {
218 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700219
Elliott Hughes475fc232011-10-25 15:00:35 -0700220 // The debugger thread must not suspend itself due to debugger activity!
221 Thread* debug_thread = Dbg::GetDebugThread();
222 CHECK(debug_thread != NULL);
223 CHECK(self != debug_thread);
224
225 // Collisions with other suspends aren't really interesting. We want
226 // to ensure that we're the only one fiddling with the suspend count
227 // though.
228 ThreadListLocker locker(this);
229 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700230 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700231
232 // Suspend ourselves.
233 CHECK_GT(self->suspend_count_, 0);
234 self->SetState(Thread::kSuspended);
235 if (verbose_) {
236 LOG(INFO) << *self << " self-suspending (dbg)";
237 }
238
239 // Tell JDWP that we've completed suspension. The JDWP thread can't
240 // tell us to resume before we're fully asleep because we hold the
241 // suspend count lock.
242 Dbg::ClearWaitForEventThread();
243
244 while (self->suspend_count_ != 0) {
245 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
246 if (self->suspend_count_ != 0) {
247 // The condition was signaled but we're still suspended. This
248 // can happen if the debugger lets go while a SIGQUIT thread
249 // dump event is pending (assuming SignalCatcher was resumed for
250 // just long enough to try to grab the thread-suspend lock).
251 LOG(DEBUG) << *self << " still suspended after undo "
252 << "(suspend count=" << self->suspend_count_ << ")";
253 }
254 }
255 CHECK_EQ(self->suspend_count_, 0);
256 self->SetState(Thread::kRunnable);
257 if (verbose_) {
258 LOG(INFO) << *self << " self-reviving (dbg)";
259 }
260}
261
262void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700263 Thread* self = Thread::Current();
264
Elliott Hughes14357e82011-09-26 10:42:15 -0700265 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700266 LOG(INFO) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700267 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700268
269 // Decrement the suspend counts for all threads. No need for atomic
270 // writes, since nobody should be moving until we decrement the count.
271 // We do need to hold the thread list because of JNI attaches.
272 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700273 ThreadListLocker locker(this);
Elliott Hughes475fc232011-10-25 15:00:35 -0700274 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700275 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700276 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
277 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700278 if (thread == self || (for_debugger && thread == debug_thread)) {
279 continue;
280 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700281 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700282 }
283 }
284
285 // Broadcast a notification to all suspended threads, some or all of
286 // which may choose to wake up. No need to wait for them.
287 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700288 if (verbose_) {
289 LOG(INFO) << *self << " ResumeAll waking others";
290 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700291 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700292 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700293 }
294
Elliott Hughes14357e82011-09-26 10:42:15 -0700295 if (verbose_) {
296 LOG(INFO) << *self << " ResumeAll complete";
297 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700298}
299
Elliott Hughes01158d72011-09-19 19:47:10 -0700300void ThreadList::Resume(Thread* thread) {
301 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700302 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700303
Elliott Hughes14357e82011-09-26 10:42:15 -0700304 if (verbose_) {
305 LOG(INFO) << "Resume(" << *thread << ") starting...";
306 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700307
308 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700309 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700310 if (!Contains(thread)) {
311 return;
312 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700313 ModifySuspendCount(thread, -1, false);
Elliott Hughes01158d72011-09-19 19:47:10 -0700314 }
315
316 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700317 if (verbose_) {
318 LOG(INFO) << "Resume(" << *thread << ") waking others";
319 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700320 MutexLock mu(thread_suspend_count_lock_);
321 thread_suspend_count_cond_.Broadcast();
322 }
323
Elliott Hughes14357e82011-09-26 10:42:15 -0700324 if (verbose_) {
325 LOG(INFO) << "Resume(" << *thread << ") complete";
326 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700327}
328
329void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
330 DCHECK(thread != NULL);
331 Thread* self = Thread::Current();
332 if (thread != self) {
333 Suspend(thread);
334 }
335 callback(arg);
336 if (thread != self) {
337 Resume(thread);
338 }
339}
340
Elliott Hughes234ab152011-10-26 14:02:26 -0700341void ThreadList::UndoDebuggerSuspensions() {
342 Thread* self = Thread::Current();
343
344 if (verbose_) {
345 LOG(INFO) << *self << " UndoDebuggerSuspensions starting";
346 }
347
348 {
349 ThreadListLocker locker(this);
350 MutexLock mu(thread_suspend_count_lock_);
351 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
352 Thread* thread = *it;
353 if (thread == self || thread->debug_suspend_count_ == 0) {
354 continue;
355 }
356 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
357 }
358 }
359
360 {
361 MutexLock mu(thread_suspend_count_lock_);
362 thread_suspend_count_cond_.Broadcast();
363 }
364
365 if (verbose_) {
366 LOG(INFO) << "UndoDebuggerSuspensions(" << *self << ") complete";
367 }
368}
369
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700370void ThreadList::Register() {
371 Thread* self = Thread::Current();
372
Elliott Hughes14357e82011-09-26 10:42:15 -0700373 if (verbose_) {
Elliott Hughese0918552011-10-28 17:18:29 -0700374 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes14357e82011-09-26 10:42:15 -0700375 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700376
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700377 ThreadListLocker locker(this);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700378 CHECK(!Contains(self));
379 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700380}
381
382void ThreadList::Unregister() {
383 Thread* self = Thread::Current();
384
Elliott Hughes14357e82011-09-26 10:42:15 -0700385 if (verbose_) {
386 LOG(INFO) << "ThreadList::Unregister() " << *self;
387 }
388
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700389 if (self->GetPeer() != NULL) {
390 self->SetState(Thread::kRunnable);
391
392 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
393 // down the Thread* and removing it from the thread list (or start taking any locks).
394 self->HandleUncaughtExceptions();
395
396 // Make sure we remove from ThreadGroup before taking the
397 // thread_list_lock_ since it allocates an Iterator which can cause
398 // a GC which will want to suspend.
399 self->RemoveFromThreadGroup();
400 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700401
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700402 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700403
404 // Remove this thread from the list.
405 CHECK(Contains(self));
406 list_.remove(self);
407
408 // Delete the Thread* and release the thin lock id.
409 uint32_t thin_lock_id = self->thin_lock_id_;
410 delete self;
411 ReleaseThreadId(thin_lock_id);
412
413 // Clear the TLS data, so that thread is recognizably detached.
414 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700415 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700416
417 // Signal that a thread just detached.
418 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700419}
420
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700421void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700422 thread_list_lock_.AssertHeld();
423 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700424 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700425 }
426}
427
Elliott Hughes8daa0922011-09-11 13:46:25 -0700428void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700429 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700430 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
431 (*it)->VisitRoots(visitor, arg);
432 }
433}
434
Elliott Hughes93e74e82011-09-13 11:07:03 -0700435/*
436 * Tell a new thread it's safe to start.
437 *
438 * We must hold the thread list lock before messing with another thread.
439 * In the general case we would also need to verify that the new thread was
440 * still in the thread list, but in our case the thread has not started
441 * executing user code and therefore has not had a chance to exit.
442 *
443 * We move it to kVmWait, and it then shifts itself to kRunning, which
444 * comes with a suspend-pending check. We do this after
445 */
446void ThreadList::SignalGo(Thread* child) {
447 Thread* self = Thread::Current();
448 CHECK(child != self);
449
450 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700451 ThreadListLocker locker(this);
Elliott Hughes47179f72011-10-27 16:44:39 -0700452 if (verbose_) {
453 LOG(INFO) << *self << " waiting for child " << *child << " to be in thread list...";
454 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700455
456 // We wait for the child to tell us that it's in the thread list.
457 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700458 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700459 }
460 }
461
462 // If we switch out of runnable and then back in, we know there's no pending suspend.
463 self->SetState(Thread::kVmWait);
464 self->SetState(Thread::kRunnable);
465
466 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughes47179f72011-10-27 16:44:39 -0700467 ThreadListLocker locker(this);
468 if (verbose_) {
469 LOG(INFO) << *self << " telling child " << *child << " it's safe to proceed...";
470 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700471 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700472 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700473}
474
475void ThreadList::WaitForGo() {
476 Thread* self = Thread::Current();
477 DCHECK(Contains(self));
478
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700479 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700480 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700481
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700482 // Tell our parent that we're in the thread list.
Elliott Hughes47179f72011-10-27 16:44:39 -0700483 if (verbose_) {
484 LOG(INFO) << *self << " telling parent that we're now in thread list...";
485 }
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700486 self->SetState(Thread::kStarting);
487 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700488
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700489 // Wait until our parent tells us there's no suspend still pending
490 // from before we were on the thread list.
Elliott Hughes47179f72011-10-27 16:44:39 -0700491 if (verbose_) {
492 LOG(INFO) << *self << " waiting for parent's go-ahead...";
493 }
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700494 while (self->GetState() != Thread::kVmWait) {
495 thread_start_cond_.Wait(thread_list_lock_);
496 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700497 }
498
499 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes47179f72011-10-27 16:44:39 -0700500 if (verbose_) {
501 LOG(INFO) << *self << " entering runnable state...";
502 }
503 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
504 // started, we wait until it's over. Which means that if there's now another GC pending, our
505 // suspend count is non-zero, so switching to the runnable state will suspend us.
506 // TODO: find a better solution!
507 Heap::Lock();
508 Heap::Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700509 self->SetState(Thread::kRunnable);
510}
511
Elliott Hughes038a8062011-09-18 14:12:41 -0700512bool ThreadList::AllThreadsAreDaemons() {
513 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700514 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
515 // is null.
516 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700517 return false;
518 }
519 }
520 return true;
521}
522
523void ThreadList::WaitForNonDaemonThreadsToExit() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700524 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700525 while (!AllThreadsAreDaemons()) {
526 thread_exit_cond_.Wait(thread_list_lock_);
527 }
528}
529
530void ThreadList::SuspendAllDaemonThreads() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700531 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700532
533 // Tell all the daemons it's time to suspend. (At this point, we know
534 // all threads are daemons.)
535 {
536 MutexLock mu(thread_suspend_count_lock_);
537 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
538 Thread* thread = *it;
539 ++thread->suspend_count_;
540 }
541 }
542
543 // Give the threads a chance to suspend, complaining if they're slow.
544 bool have_complained = false;
545 for (int i = 0; i < 10; ++i) {
546 usleep(200 * 1000);
547 bool all_suspended = true;
548 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
549 Thread* thread = *it;
550 if (thread->GetState() == Thread::kRunnable) {
551 if (!have_complained) {
552 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
553 have_complained = true;
554 }
555 all_suspended = false;
556 }
557 }
558 if (all_suspended) {
559 return;
560 }
561 }
562}
563
Elliott Hughes8daa0922011-09-11 13:46:25 -0700564uint32_t ThreadList::AllocThreadId() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700565 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700566 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
567 if (!allocated_ids_[i]) {
568 allocated_ids_.set(i);
569 return i + 1; // Zero is reserved to mean "invalid".
570 }
571 }
572 LOG(FATAL) << "Out of internal thread ids";
573 return 0;
574}
575
576void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700577 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700578 --id; // Zero is reserved to mean "invalid".
579 DCHECK(allocated_ids_[id]) << id;
580 allocated_ids_.reset(id);
581}
582
583} // namespace art