blob: 91fc0f21a36b8461d84f5ce20e5b22fb527c73ae [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
91 DCHECK(delta == -1 || delta == +1 || delta == thread->debug_suspend_count_) << delta;
92 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_);
93 if (delta == -1) {
94 DCHECK_GT(thread->suspend_count_, 0);
95 if (for_debugger) {
96 DCHECK_GT(thread->debug_suspend_count_, 0);
97 }
98 }
99#else
100 if (delta == -1 && thread->suspend_count_ <= 0) {
101 LOG(FATAL) << *thread << " suspend count already zero";
102 }
103#endif
104 thread->suspend_count_ += delta;
105 if (for_debugger) {
106 thread->debug_suspend_count_ += delta;
107 }
108}
109
Elliott Hughes8d768a92011-09-14 16:35:25 -0700110void ThreadList::FullSuspendCheck(Thread* thread) {
111 CHECK(thread != NULL);
112 CHECK_GE(thread->suspend_count_, 0);
113
114 MutexLock mu(thread_suspend_count_lock_);
115 if (thread->suspend_count_ == 0) {
116 return;
117 }
118
Elliott Hughes14357e82011-09-26 10:42:15 -0700119 if (verbose_) {
120 LOG(INFO) << *thread << " self-suspending";
121 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700122 {
123 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
124 while (thread->suspend_count_ != 0) {
125 /*
126 * Wait for wakeup signal, releasing lock. The act of releasing
127 * and re-acquiring the lock provides the memory barriers we
128 * need for correct behavior on SMP.
129 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700130 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700131 }
132 CHECK_EQ(thread->suspend_count_, 0);
133 }
Elliott Hughes14357e82011-09-26 10:42:15 -0700134 if (verbose_) {
135 LOG(INFO) << *thread << " self-reviving";
136 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700137}
138
Elliott Hughes475fc232011-10-25 15:00:35 -0700139void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700140 Thread* self = Thread::Current();
141
Elliott Hughes14357e82011-09-26 10:42:15 -0700142 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700143 LOG(INFO) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700144 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700145
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700146 CHECK_EQ(self->GetState(), Thread::kRunnable);
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700147 ThreadListLocker locker(this);
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 Hughesf6a1e1e2011-10-25 16:28:04 -0700158 if (verbose_) {
159 LOG(INFO) << "requesting thread suspend: " << *thread;
160 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700161 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700162 }
163 }
164
165 /*
166 * Wait for everybody in kRunnable state to stop. Other states
167 * indicate the code is either running natively or sleeping quietly.
168 * Any attempt to transition back to kRunnable will cause a check
169 * for suspension, so it should be impossible for anything to execute
170 * interpreted code or modify objects (assuming native code plays nicely).
171 *
172 * It's also okay if the thread transitions to a non-kRunnable state.
173 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700174 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700175 * so if another thread is fiddling with its suspend count (perhaps
176 * self-suspending for the debugger) it won't block while we're waiting
177 * in here.
178 */
179 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
180 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700181 if (thread == self || (for_debugger && thread == debug_thread)) {
182 continue;
183 }
184 thread->WaitUntilSuspended();
185 if (verbose_) {
186 LOG(INFO) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700187 }
188 }
189
Elliott Hughes14357e82011-09-26 10:42:15 -0700190 if (verbose_) {
191 LOG(INFO) << *self << " SuspendAll complete";
192 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700193}
194
Elliott Hughes01158d72011-09-19 19:47:10 -0700195void ThreadList::Suspend(Thread* thread) {
196 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700197 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700198
199 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
200
Elliott Hughes14357e82011-09-26 10:42:15 -0700201 if (verbose_) {
202 LOG(INFO) << "Suspend(" << *thread << ") starting...";
203 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700204
Elliott Hughes01158d72011-09-19 19:47:10 -0700205 if (!Contains(thread)) {
206 return;
207 }
208
209 {
210 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700211 ModifySuspendCount(thread, +1, false);
Elliott Hughes01158d72011-09-19 19:47:10 -0700212 }
213
214 thread->WaitUntilSuspended();
215
Elliott Hughes14357e82011-09-26 10:42:15 -0700216 if (verbose_) {
217 LOG(INFO) << "Suspend(" << *thread << ") complete";
218 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700219}
220
Elliott Hughes475fc232011-10-25 15:00:35 -0700221void ThreadList::SuspendSelfForDebugger() {
222 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700223
Elliott Hughes475fc232011-10-25 15:00:35 -0700224 // The debugger thread must not suspend itself due to debugger activity!
225 Thread* debug_thread = Dbg::GetDebugThread();
226 CHECK(debug_thread != NULL);
227 CHECK(self != debug_thread);
228
229 // Collisions with other suspends aren't really interesting. We want
230 // to ensure that we're the only one fiddling with the suspend count
231 // though.
232 ThreadListLocker locker(this);
233 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700234 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700235
236 // Suspend ourselves.
237 CHECK_GT(self->suspend_count_, 0);
238 self->SetState(Thread::kSuspended);
239 if (verbose_) {
240 LOG(INFO) << *self << " self-suspending (dbg)";
241 }
242
243 // Tell JDWP that we've completed suspension. The JDWP thread can't
244 // tell us to resume before we're fully asleep because we hold the
245 // suspend count lock.
246 Dbg::ClearWaitForEventThread();
247
248 while (self->suspend_count_ != 0) {
249 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
250 if (self->suspend_count_ != 0) {
251 // The condition was signaled but we're still suspended. This
252 // can happen if the debugger lets go while a SIGQUIT thread
253 // dump event is pending (assuming SignalCatcher was resumed for
254 // just long enough to try to grab the thread-suspend lock).
255 LOG(DEBUG) << *self << " still suspended after undo "
256 << "(suspend count=" << self->suspend_count_ << ")";
257 }
258 }
259 CHECK_EQ(self->suspend_count_, 0);
260 self->SetState(Thread::kRunnable);
261 if (verbose_) {
262 LOG(INFO) << *self << " self-reviving (dbg)";
263 }
264}
265
266void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700267 Thread* self = Thread::Current();
268
Elliott Hughes14357e82011-09-26 10:42:15 -0700269 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700270 LOG(INFO) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700271 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700272
273 // Decrement the suspend counts for all threads. No need for atomic
274 // writes, since nobody should be moving until we decrement the count.
275 // We do need to hold the thread list because of JNI attaches.
276 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700277 ThreadListLocker locker(this);
Elliott Hughes475fc232011-10-25 15:00:35 -0700278 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700279 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700280 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
281 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700282 if (thread == self || (for_debugger && thread == debug_thread)) {
283 continue;
284 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700285 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700286 }
287 }
288
289 // Broadcast a notification to all suspended threads, some or all of
290 // which may choose to wake up. No need to wait for them.
291 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700292 if (verbose_) {
293 LOG(INFO) << *self << " ResumeAll waking others";
294 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700295 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700296 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700297 }
298
Elliott Hughes14357e82011-09-26 10:42:15 -0700299 if (verbose_) {
300 LOG(INFO) << *self << " ResumeAll complete";
301 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700302}
303
Elliott Hughes01158d72011-09-19 19:47:10 -0700304void ThreadList::Resume(Thread* thread) {
305 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700306 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700307
Elliott Hughes14357e82011-09-26 10:42:15 -0700308 if (verbose_) {
309 LOG(INFO) << "Resume(" << *thread << ") starting...";
310 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700311
312 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700313 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700314 if (!Contains(thread)) {
315 return;
316 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700317 ModifySuspendCount(thread, -1, false);
Elliott Hughes01158d72011-09-19 19:47:10 -0700318 }
319
320 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700321 if (verbose_) {
322 LOG(INFO) << "Resume(" << *thread << ") waking others";
323 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700324 MutexLock mu(thread_suspend_count_lock_);
325 thread_suspend_count_cond_.Broadcast();
326 }
327
Elliott Hughes14357e82011-09-26 10:42:15 -0700328 if (verbose_) {
329 LOG(INFO) << "Resume(" << *thread << ") complete";
330 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700331}
332
333void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
334 DCHECK(thread != NULL);
335 Thread* self = Thread::Current();
336 if (thread != self) {
337 Suspend(thread);
338 }
339 callback(arg);
340 if (thread != self) {
341 Resume(thread);
342 }
343}
344
Elliott Hughes234ab152011-10-26 14:02:26 -0700345void ThreadList::UndoDebuggerSuspensions() {
346 Thread* self = Thread::Current();
347
348 if (verbose_) {
349 LOG(INFO) << *self << " UndoDebuggerSuspensions starting";
350 }
351
352 {
353 ThreadListLocker locker(this);
354 MutexLock mu(thread_suspend_count_lock_);
355 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
356 Thread* thread = *it;
357 if (thread == self || thread->debug_suspend_count_ == 0) {
358 continue;
359 }
360 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
361 }
362 }
363
364 {
365 MutexLock mu(thread_suspend_count_lock_);
366 thread_suspend_count_cond_.Broadcast();
367 }
368
369 if (verbose_) {
370 LOG(INFO) << "UndoDebuggerSuspensions(" << *self << ") complete";
371 }
372}
373
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700374void ThreadList::Register() {
375 Thread* self = Thread::Current();
376
Elliott Hughes14357e82011-09-26 10:42:15 -0700377 if (verbose_) {
378 LOG(INFO) << "ThreadList::Register() " << *self;
379 self->Dump(std::cerr);
380 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700381
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700382 ThreadListLocker locker(this);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700383 CHECK(!Contains(self));
384 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700385}
386
387void ThreadList::Unregister() {
388 Thread* self = Thread::Current();
389
Elliott Hughes14357e82011-09-26 10:42:15 -0700390 if (verbose_) {
391 LOG(INFO) << "ThreadList::Unregister() " << *self;
392 }
393
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700394 if (self->GetPeer() != NULL) {
395 self->SetState(Thread::kRunnable);
396
397 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
398 // down the Thread* and removing it from the thread list (or start taking any locks).
399 self->HandleUncaughtExceptions();
400
401 // Make sure we remove from ThreadGroup before taking the
402 // thread_list_lock_ since it allocates an Iterator which can cause
403 // a GC which will want to suspend.
404 self->RemoveFromThreadGroup();
405 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700406
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700407 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700408
409 // Remove this thread from the list.
410 CHECK(Contains(self));
411 list_.remove(self);
412
413 // Delete the Thread* and release the thin lock id.
414 uint32_t thin_lock_id = self->thin_lock_id_;
415 delete self;
416 ReleaseThreadId(thin_lock_id);
417
418 // Clear the TLS data, so that thread is recognizably detached.
419 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700420 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700421
422 // Signal that a thread just detached.
423 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700424}
425
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700426void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700427 thread_list_lock_.AssertHeld();
428 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700429 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700430 }
431}
432
Elliott Hughes8daa0922011-09-11 13:46:25 -0700433void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700434 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700435 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
436 (*it)->VisitRoots(visitor, arg);
437 }
438}
439
Elliott Hughes93e74e82011-09-13 11:07:03 -0700440/*
441 * Tell a new thread it's safe to start.
442 *
443 * We must hold the thread list lock before messing with another thread.
444 * In the general case we would also need to verify that the new thread was
445 * still in the thread list, but in our case the thread has not started
446 * executing user code and therefore has not had a chance to exit.
447 *
448 * We move it to kVmWait, and it then shifts itself to kRunning, which
449 * comes with a suspend-pending check. We do this after
450 */
451void ThreadList::SignalGo(Thread* child) {
452 Thread* self = Thread::Current();
453 CHECK(child != self);
454
455 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700456 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700457
458 // We wait for the child to tell us that it's in the thread list.
459 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700460 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700461 }
462 }
463
464 // If we switch out of runnable and then back in, we know there's no pending suspend.
465 self->SetState(Thread::kVmWait);
466 self->SetState(Thread::kRunnable);
467
468 // Tell the child that it's safe: it will see any future suspend request.
469 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700470 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700471}
472
473void ThreadList::WaitForGo() {
474 Thread* self = Thread::Current();
475 DCHECK(Contains(self));
476
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700477 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700478 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700479
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700480 // Tell our parent that we're in the thread list.
481 self->SetState(Thread::kStarting);
482 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700483
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700484 // Wait until our parent tells us there's no suspend still pending
485 // from before we were on the thread list.
486 while (self->GetState() != Thread::kVmWait) {
487 thread_start_cond_.Wait(thread_list_lock_);
488 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700489 }
490
491 // Enter the runnable state. We know that any pending suspend will affect us now.
492 self->SetState(Thread::kRunnable);
493}
494
Elliott Hughes038a8062011-09-18 14:12:41 -0700495bool ThreadList::AllThreadsAreDaemons() {
496 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700497 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
498 // is null.
499 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700500 return false;
501 }
502 }
503 return true;
504}
505
506void ThreadList::WaitForNonDaemonThreadsToExit() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700507 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700508 while (!AllThreadsAreDaemons()) {
509 thread_exit_cond_.Wait(thread_list_lock_);
510 }
511}
512
513void ThreadList::SuspendAllDaemonThreads() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700514 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700515
516 // Tell all the daemons it's time to suspend. (At this point, we know
517 // all threads are daemons.)
518 {
519 MutexLock mu(thread_suspend_count_lock_);
520 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
521 Thread* thread = *it;
522 ++thread->suspend_count_;
523 }
524 }
525
526 // Give the threads a chance to suspend, complaining if they're slow.
527 bool have_complained = false;
528 for (int i = 0; i < 10; ++i) {
529 usleep(200 * 1000);
530 bool all_suspended = true;
531 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
532 Thread* thread = *it;
533 if (thread->GetState() == Thread::kRunnable) {
534 if (!have_complained) {
535 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
536 have_complained = true;
537 }
538 all_suspended = false;
539 }
540 }
541 if (all_suspended) {
542 return;
543 }
544 }
545}
546
Elliott Hughes8daa0922011-09-11 13:46:25 -0700547uint32_t ThreadList::AllocThreadId() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700548 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700549 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
550 if (!allocated_ids_[i]) {
551 allocated_ids_.set(i);
552 return i + 1; // Zero is reserved to mean "invalid".
553 }
554 }
555 LOG(FATAL) << "Out of internal thread ids";
556 return 0;
557}
558
559void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700560 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700561 --id; // Zero is reserved to mean "invalid".
562 DCHECK(allocated_ids_[id]) << id;
563 allocated_ids_.reset(id);
564}
565
566} // namespace art