blob: 03442439ef811778ce48e893df837167ce8a26f8 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "thread_list.h"
18
Elliott Hughes038a8062011-09-18 14:12:41 -070019#include <unistd.h>
20
Elliott Hughes475fc232011-10-25 15:00:35 -070021#include "debugger.h"
22
Elliott Hughes8daa0922011-09-11 13:46:25 -070023namespace art {
24
Elliott Hughesbbd9d832011-11-07 14:40:00 -080025ScopedThreadListLock::ScopedThreadListLock() {
26 // Self may be null during shutdown.
27 Thread* self = Thread::Current();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070028
Elliott Hughesbbd9d832011-11-07 14:40:00 -080029 // We insist that anyone taking the thread list lock already has the heap lock,
30 // because pretty much any time someone takes the thread list lock, they may
31 // end up needing the heap lock (even removing a thread from the thread list calls
32 // back into managed code to remove the thread from its ThreadGroup, and that allocates
33 // an iterator).
34 // TODO: this makes the distinction between the two locks pretty pointless.
35 if (self != NULL) {
36 Heap::Lock();
37 }
38
39 // Avoid deadlock between two threads trying to SuspendAll
40 // simultaneously by going to kVmWait if the lock cannot be
41 // immediately acquired.
42 // TODO: is this needed if we took the heap lock? taking the heap lock will have done this,
43 // and the other thread will now be in kVmWait waiting for the heap lock.
44 ThreadList* thread_list = Runtime::Current()->GetThreadList();
45 if (!thread_list->thread_list_lock_.TryLock()) {
46 if (self == NULL) {
47 thread_list->thread_list_lock_.Lock();
48 } else {
49 ScopedThreadStateChange tsc(self, Thread::kVmWait);
50 thread_list->thread_list_lock_.Lock();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070051 }
52 }
53
Elliott Hughesbbd9d832011-11-07 14:40:00 -080054 if (self != NULL) {
55 Heap::Unlock();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070056 }
Elliott Hughesbbd9d832011-11-07 14:40:00 -080057}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070058
Elliott Hughesbbd9d832011-11-07 14:40:00 -080059ScopedThreadListLock::~ScopedThreadListLock() {
60 Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
61}
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070062
Elliott Hughes14357e82011-09-26 10:42:15 -070063ThreadList::ThreadList(bool verbose)
64 : verbose_(verbose),
65 thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070066 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070067 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070068 thread_suspend_count_lock_("thread suspend count lock"),
69 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Brian Carlstrom72db0d72011-11-10 17:58:56 -080070 if (verbose_) {
71 LOG(INFO) << "default stack size " << Runtime::Current()->GetDefaultStackSize() / KB << "kb";
72 }
Elliott Hughes8daa0922011-09-11 13:46:25 -070073}
74
75ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070076 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070077 if (Contains(Thread::Current())) {
78 Runtime::Current()->DetachCurrentThread();
79 }
80
Elliott Hughes038a8062011-09-18 14:12:41 -070081 WaitForNonDaemonThreadsToExit();
82 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070083}
84
85bool ThreadList::Contains(Thread* thread) {
86 return find(list_.begin(), list_.end(), thread) != list_.end();
87}
88
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070089pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070090 return thread_list_lock_.GetOwner();
91}
92
Elliott Hughes8daa0922011-09-11 13:46:25 -070093void ThreadList::Dump(std::ostream& os) {
Elliott Hughesbbd9d832011-11-07 14:40:00 -080094 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -070095 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070096 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
97 (*it)->Dump(os);
98 os << "\n";
99 }
100}
101
Elliott Hughes234ab152011-10-26 14:02:26 -0700102void ThreadList::ModifySuspendCount(Thread* thread, int delta, bool for_debugger) {
103#ifndef NDEBUG
Elliott Hughes47179f72011-10-27 16:44:39 -0700104 DCHECK(delta == -1 || delta == +1 || delta == thread->debug_suspend_count_) << delta << " "
105 << *thread;
106 DCHECK_GE(thread->suspend_count_, thread->debug_suspend_count_) << *thread;
Elliott Hughes234ab152011-10-26 14:02:26 -0700107#endif
Elliott Hughes47179f72011-10-27 16:44:39 -0700108 if (delta == -1 && thread->suspend_count_ <= 0) {
109 // This can happen if you attach a thread during a GC.
110 LOG(WARNING) << *thread << " suspend count already zero";
111 return;
112 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700113 thread->suspend_count_ += delta;
114 if (for_debugger) {
115 thread->debug_suspend_count_ += delta;
116 }
117}
118
Elliott Hughes8d768a92011-09-14 16:35:25 -0700119void ThreadList::FullSuspendCheck(Thread* thread) {
120 CHECK(thread != NULL);
121 CHECK_GE(thread->suspend_count_, 0);
122
123 MutexLock mu(thread_suspend_count_lock_);
124 if (thread->suspend_count_ == 0) {
125 return;
126 }
127
Elliott Hughes14357e82011-09-26 10:42:15 -0700128 if (verbose_) {
129 LOG(INFO) << *thread << " self-suspending";
130 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700131 {
132 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
133 while (thread->suspend_count_ != 0) {
134 /*
135 * Wait for wakeup signal, releasing lock. The act of releasing
136 * and re-acquiring the lock provides the memory barriers we
137 * need for correct behavior on SMP.
138 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700139 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700140 }
141 CHECK_EQ(thread->suspend_count_, 0);
142 }
Elliott Hughes14357e82011-09-26 10:42:15 -0700143 if (verbose_) {
144 LOG(INFO) << *thread << " self-reviving";
145 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700146}
147
Elliott Hughes475fc232011-10-25 15:00:35 -0700148void ThreadList::SuspendAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700149 Thread* self = Thread::Current();
150
Elliott Hughes14357e82011-09-26 10:42:15 -0700151 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700152 LOG(INFO) << *self << " SuspendAll starting..." << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700153 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700154
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700155 CHECK_EQ(self->GetState(), Thread::kRunnable);
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800156 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700157 Thread* debug_thread = Dbg::GetDebugThread();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700158
159 {
160 // Increment everybody's suspend count (except our own).
161 MutexLock mu(thread_suspend_count_lock_);
162 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
163 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700164 if (thread == self || (for_debugger && thread == debug_thread)) {
165 continue;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700166 }
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700167 if (verbose_) {
168 LOG(INFO) << "requesting thread suspend: " << *thread;
169 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700170 ModifySuspendCount(thread, +1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700171 }
172 }
173
174 /*
175 * Wait for everybody in kRunnable state to stop. Other states
176 * indicate the code is either running natively or sleeping quietly.
177 * Any attempt to transition back to kRunnable will cause a check
178 * for suspension, so it should be impossible for anything to execute
179 * interpreted code or modify objects (assuming native code plays nicely).
180 *
181 * It's also okay if the thread transitions to a non-kRunnable state.
182 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700183 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700184 * so if another thread is fiddling with its suspend count (perhaps
185 * self-suspending for the debugger) it won't block while we're waiting
186 * in here.
187 */
188 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
189 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700190 if (thread == self || (for_debugger && thread == debug_thread)) {
191 continue;
192 }
193 thread->WaitUntilSuspended();
194 if (verbose_) {
195 LOG(INFO) << "thread suspended: " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700196 }
197 }
198
Elliott Hughes14357e82011-09-26 10:42:15 -0700199 if (verbose_) {
200 LOG(INFO) << *self << " SuspendAll complete";
201 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700202}
203
Elliott Hughes01158d72011-09-19 19:47:10 -0700204void ThreadList::Suspend(Thread* thread) {
205 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700206 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700207
208 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
209
Elliott Hughes14357e82011-09-26 10:42:15 -0700210 if (verbose_) {
211 LOG(INFO) << "Suspend(" << *thread << ") starting...";
212 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700213
Elliott Hughes01158d72011-09-19 19:47:10 -0700214 if (!Contains(thread)) {
215 return;
216 }
217
218 {
219 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700220 ModifySuspendCount(thread, +1, false);
Elliott Hughes01158d72011-09-19 19:47:10 -0700221 }
222
223 thread->WaitUntilSuspended();
224
Elliott Hughes14357e82011-09-26 10:42:15 -0700225 if (verbose_) {
226 LOG(INFO) << "Suspend(" << *thread << ") complete";
227 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700228}
229
Elliott Hughes475fc232011-10-25 15:00:35 -0700230void ThreadList::SuspendSelfForDebugger() {
231 Thread* self = Thread::Current();
Elliott Hughes01158d72011-09-19 19:47:10 -0700232
Elliott Hughes475fc232011-10-25 15:00:35 -0700233 // The debugger thread must not suspend itself due to debugger activity!
234 Thread* debug_thread = Dbg::GetDebugThread();
235 CHECK(debug_thread != NULL);
236 CHECK(self != debug_thread);
237
238 // Collisions with other suspends aren't really interesting. We want
239 // to ensure that we're the only one fiddling with the suspend count
240 // though.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800241 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700242 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes234ab152011-10-26 14:02:26 -0700243 ModifySuspendCount(self, +1, true);
Elliott Hughes475fc232011-10-25 15:00:35 -0700244
245 // Suspend ourselves.
246 CHECK_GT(self->suspend_count_, 0);
247 self->SetState(Thread::kSuspended);
248 if (verbose_) {
249 LOG(INFO) << *self << " self-suspending (dbg)";
250 }
251
252 // Tell JDWP that we've completed suspension. The JDWP thread can't
253 // tell us to resume before we're fully asleep because we hold the
254 // suspend count lock.
255 Dbg::ClearWaitForEventThread();
256
257 while (self->suspend_count_ != 0) {
258 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
259 if (self->suspend_count_ != 0) {
260 // The condition was signaled but we're still suspended. This
261 // can happen if the debugger lets go while a SIGQUIT thread
262 // dump event is pending (assuming SignalCatcher was resumed for
263 // just long enough to try to grab the thread-suspend lock).
264 LOG(DEBUG) << *self << " still suspended after undo "
265 << "(suspend count=" << self->suspend_count_ << ")";
266 }
267 }
268 CHECK_EQ(self->suspend_count_, 0);
269 self->SetState(Thread::kRunnable);
270 if (verbose_) {
271 LOG(INFO) << *self << " self-reviving (dbg)";
272 }
273}
274
275void ThreadList::ResumeAll(bool for_debugger) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700276 Thread* self = Thread::Current();
277
Elliott Hughes14357e82011-09-26 10:42:15 -0700278 if (verbose_) {
Elliott Hughes475fc232011-10-25 15:00:35 -0700279 LOG(INFO) << *self << " ResumeAll starting" << (for_debugger ? " (debugger)" : "");
Elliott Hughes14357e82011-09-26 10:42:15 -0700280 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700281
282 // Decrement the suspend counts for all threads. No need for atomic
283 // writes, since nobody should be moving until we decrement the count.
284 // We do need to hold the thread list because of JNI attaches.
285 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800286 ScopedThreadListLock thread_list_lock;
Elliott Hughes475fc232011-10-25 15:00:35 -0700287 Thread* debug_thread = Dbg::GetDebugThread();
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700288 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700289 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
290 Thread* thread = *it;
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700291 if (thread == self || (for_debugger && thread == debug_thread)) {
292 continue;
293 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700294 ModifySuspendCount(thread, -1, for_debugger);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700295 }
296 }
297
298 // Broadcast a notification to all suspended threads, some or all of
299 // which may choose to wake up. No need to wait for them.
300 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700301 if (verbose_) {
302 LOG(INFO) << *self << " ResumeAll waking others";
303 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700304 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700305 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700306 }
307
Elliott Hughes14357e82011-09-26 10:42:15 -0700308 if (verbose_) {
309 LOG(INFO) << *self << " ResumeAll complete";
310 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700311}
312
Elliott Hughes01158d72011-09-19 19:47:10 -0700313void ThreadList::Resume(Thread* thread) {
314 DCHECK(thread != Thread::Current());
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700315 thread_list_lock_.AssertHeld();
Elliott Hughes01158d72011-09-19 19:47:10 -0700316
Elliott Hughes14357e82011-09-26 10:42:15 -0700317 if (verbose_) {
318 LOG(INFO) << "Resume(" << *thread << ") starting...";
319 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700320
321 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700322 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700323 if (!Contains(thread)) {
324 return;
325 }
Elliott Hughes234ab152011-10-26 14:02:26 -0700326 ModifySuspendCount(thread, -1, false);
Elliott Hughes01158d72011-09-19 19:47:10 -0700327 }
328
329 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700330 if (verbose_) {
331 LOG(INFO) << "Resume(" << *thread << ") waking others";
332 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700333 MutexLock mu(thread_suspend_count_lock_);
334 thread_suspend_count_cond_.Broadcast();
335 }
336
Elliott Hughes14357e82011-09-26 10:42:15 -0700337 if (verbose_) {
338 LOG(INFO) << "Resume(" << *thread << ") complete";
339 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700340}
341
342void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
343 DCHECK(thread != NULL);
344 Thread* self = Thread::Current();
345 if (thread != self) {
346 Suspend(thread);
347 }
348 callback(arg);
349 if (thread != self) {
350 Resume(thread);
351 }
352}
353
Elliott Hughes234ab152011-10-26 14:02:26 -0700354void ThreadList::UndoDebuggerSuspensions() {
355 Thread* self = Thread::Current();
356
357 if (verbose_) {
358 LOG(INFO) << *self << " UndoDebuggerSuspensions starting";
359 }
360
361 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800362 ScopedThreadListLock thread_list_lock;
Elliott Hughes234ab152011-10-26 14:02:26 -0700363 MutexLock mu(thread_suspend_count_lock_);
364 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
365 Thread* thread = *it;
366 if (thread == self || thread->debug_suspend_count_ == 0) {
367 continue;
368 }
369 ModifySuspendCount(thread, -thread->debug_suspend_count_, true);
370 }
371 }
372
373 {
374 MutexLock mu(thread_suspend_count_lock_);
375 thread_suspend_count_cond_.Broadcast();
376 }
377
378 if (verbose_) {
379 LOG(INFO) << "UndoDebuggerSuspensions(" << *self << ") complete";
380 }
381}
382
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700383void ThreadList::Register() {
384 Thread* self = Thread::Current();
385
Elliott Hughes14357e82011-09-26 10:42:15 -0700386 if (verbose_) {
Elliott Hughese0918552011-10-28 17:18:29 -0700387 LOG(INFO) << "ThreadList::Register() " << *self << "\n" << Dumpable<Thread>(*self);
Elliott Hughes14357e82011-09-26 10:42:15 -0700388 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700389
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800390 ScopedThreadListLock thread_list_lock;
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700391 CHECK(!Contains(self));
392 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700393}
394
395void ThreadList::Unregister() {
396 Thread* self = Thread::Current();
397
Elliott Hughes14357e82011-09-26 10:42:15 -0700398 if (verbose_) {
399 LOG(INFO) << "ThreadList::Unregister() " << *self;
400 }
401
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700402 if (self->GetPeer() != NULL) {
403 self->SetState(Thread::kRunnable);
404
405 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
406 // down the Thread* and removing it from the thread list (or start taking any locks).
407 self->HandleUncaughtExceptions();
408
409 // Make sure we remove from ThreadGroup before taking the
410 // thread_list_lock_ since it allocates an Iterator which can cause
411 // a GC which will want to suspend.
412 self->RemoveFromThreadGroup();
413 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700414
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800415 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700416
417 // Remove this thread from the list.
418 CHECK(Contains(self));
419 list_.remove(self);
420
421 // Delete the Thread* and release the thin lock id.
422 uint32_t thin_lock_id = self->thin_lock_id_;
423 delete self;
424 ReleaseThreadId(thin_lock_id);
425
426 // Clear the TLS data, so that thread is recognizably detached.
427 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700428 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700429
430 // Signal that a thread just detached.
431 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700432}
433
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700434void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700435 thread_list_lock_.AssertHeld();
436 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700437 callback(*it, context);
Elliott Hughes47fce012011-10-25 18:37:19 -0700438 }
439}
440
Elliott Hughes8daa0922011-09-11 13:46:25 -0700441void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800442 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700443 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
444 (*it)->VisitRoots(visitor, arg);
445 }
446}
447
Elliott Hughes93e74e82011-09-13 11:07:03 -0700448/*
449 * Tell a new thread it's safe to start.
450 *
451 * We must hold the thread list lock before messing with another thread.
452 * In the general case we would also need to verify that the new thread was
453 * still in the thread list, but in our case the thread has not started
454 * executing user code and therefore has not had a chance to exit.
455 *
456 * We move it to kVmWait, and it then shifts itself to kRunning, which
457 * comes with a suspend-pending check. We do this after
458 */
459void ThreadList::SignalGo(Thread* child) {
460 Thread* self = Thread::Current();
461 CHECK(child != self);
462
463 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800464 ScopedThreadListLock thread_list_lock;
Elliott Hughes47179f72011-10-27 16:44:39 -0700465 if (verbose_) {
466 LOG(INFO) << *self << " waiting for child " << *child << " to be in thread list...";
467 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700468
469 // We wait for the child to tell us that it's in the thread list.
470 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700471 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700472 }
473 }
474
475 // If we switch out of runnable and then back in, we know there's no pending suspend.
476 self->SetState(Thread::kVmWait);
477 self->SetState(Thread::kRunnable);
478
479 // Tell the child that it's safe: it will see any future suspend request.
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800480 ScopedThreadListLock thread_list_lock;
Elliott Hughes47179f72011-10-27 16:44:39 -0700481 if (verbose_) {
482 LOG(INFO) << *self << " telling child " << *child << " it's safe to proceed...";
483 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700484 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700485 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700486}
487
488void ThreadList::WaitForGo() {
489 Thread* self = Thread::Current();
490 DCHECK(Contains(self));
491
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700492 {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800493 ScopedThreadListLock thread_list_lock;
Elliott Hughes93e74e82011-09-13 11:07:03 -0700494
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700495 // Tell our parent that we're in the thread list.
Elliott Hughes47179f72011-10-27 16:44:39 -0700496 if (verbose_) {
497 LOG(INFO) << *self << " telling parent that we're now in thread list...";
498 }
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700499 self->SetState(Thread::kStarting);
500 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700501
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700502 // Wait until our parent tells us there's no suspend still pending
503 // from before we were on the thread list.
Elliott Hughes47179f72011-10-27 16:44:39 -0700504 if (verbose_) {
505 LOG(INFO) << *self << " waiting for parent's go-ahead...";
506 }
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700507 while (self->GetState() != Thread::kVmWait) {
508 thread_start_cond_.Wait(thread_list_lock_);
509 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700510 }
511
512 // Enter the runnable state. We know that any pending suspend will affect us now.
Elliott Hughes47179f72011-10-27 16:44:39 -0700513 if (verbose_) {
514 LOG(INFO) << *self << " entering runnable state...";
515 }
516 // Lock and unlock the heap lock. This ensures that if there was a GC in progress when we
517 // started, we wait until it's over. Which means that if there's now another GC pending, our
518 // suspend count is non-zero, so switching to the runnable state will suspend us.
519 // TODO: find a better solution!
520 Heap::Lock();
521 Heap::Unlock();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700522 self->SetState(Thread::kRunnable);
523}
524
Elliott Hughes038a8062011-09-18 14:12:41 -0700525bool ThreadList::AllThreadsAreDaemons() {
526 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700527 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
528 // is null.
529 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700530 return false;
531 }
532 }
533 return true;
534}
535
536void ThreadList::WaitForNonDaemonThreadsToExit() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800537 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700538 while (!AllThreadsAreDaemons()) {
539 thread_exit_cond_.Wait(thread_list_lock_);
540 }
541}
542
543void ThreadList::SuspendAllDaemonThreads() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800544 ScopedThreadListLock thread_list_lock;
Elliott Hughes038a8062011-09-18 14:12:41 -0700545
546 // Tell all the daemons it's time to suspend. (At this point, we know
547 // all threads are daemons.)
548 {
549 MutexLock mu(thread_suspend_count_lock_);
550 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
551 Thread* thread = *it;
552 ++thread->suspend_count_;
553 }
554 }
555
556 // Give the threads a chance to suspend, complaining if they're slow.
557 bool have_complained = false;
558 for (int i = 0; i < 10; ++i) {
559 usleep(200 * 1000);
560 bool all_suspended = true;
561 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
562 Thread* thread = *it;
563 if (thread->GetState() == Thread::kRunnable) {
564 if (!have_complained) {
565 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
566 have_complained = true;
567 }
568 all_suspended = false;
569 }
570 }
571 if (all_suspended) {
572 return;
573 }
574 }
575}
576
Elliott Hughes8daa0922011-09-11 13:46:25 -0700577uint32_t ThreadList::AllocThreadId() {
Elliott Hughesbbd9d832011-11-07 14:40:00 -0800578 ScopedThreadListLock thread_list_lock;
Elliott Hughes8daa0922011-09-11 13:46:25 -0700579 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
580 if (!allocated_ids_[i]) {
581 allocated_ids_.set(i);
582 return i + 1; // Zero is reserved to mean "invalid".
583 }
584 }
585 LOG(FATAL) << "Out of internal thread ids";
586 return 0;
587}
588
589void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700590 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700591 --id; // Zero is reserved to mean "invalid".
592 DCHECK(allocated_ids_[id]) << id;
593 allocated_ids_.reset(id);
594}
595
596} // namespace art