blob: 896986819c0665dfbc9583da3eb5eee03c4a6b9c [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 Hughes8daa0922011-09-11 13:46:25 -070021namespace art {
22
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070023// TODO: merge with ThreadListLock?
24class ThreadListLocker {
25 public:
26
27 explicit ThreadListLocker(const ThreadList* thread_list) : thread_list_(thread_list) {
28 // Avoid deadlock between two threads trying to SuspendAll
29 // simultaneously by going to kVmWait if the lock cannot be
30 // immediately acquired.
31 if (!thread_list_->thread_list_lock_.TryLock()) {
32 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
33 thread_list_->thread_list_lock_.Lock();
34 }
35 }
36
37 ~ThreadListLocker() {
38 thread_list_->thread_list_lock_.Unlock();
39 }
40
41 private:
42 const ThreadList* thread_list_;
43 DISALLOW_COPY_AND_ASSIGN(ThreadListLocker);
44};
45
Elliott Hughes14357e82011-09-26 10:42:15 -070046ThreadList::ThreadList(bool verbose)
47 : verbose_(verbose),
48 thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070049 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070050 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070051 thread_suspend_count_lock_("thread suspend count lock"),
52 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes8daa0922011-09-11 13:46:25 -070053}
54
55ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070056 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070057 if (Contains(Thread::Current())) {
58 Runtime::Current()->DetachCurrentThread();
59 }
60
Elliott Hughes038a8062011-09-18 14:12:41 -070061 WaitForNonDaemonThreadsToExit();
62 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070063}
64
65bool ThreadList::Contains(Thread* thread) {
66 return find(list_.begin(), list_.end(), thread) != list_.end();
67}
68
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070069pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070070 return thread_list_lock_.GetOwner();
71}
72
Elliott Hughes8daa0922011-09-11 13:46:25 -070073void ThreadList::Dump(std::ostream& os) {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070074 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -070075 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070076 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
77 (*it)->Dump(os);
78 os << "\n";
79 }
80}
81
Elliott Hughes8d768a92011-09-14 16:35:25 -070082void ThreadList::FullSuspendCheck(Thread* thread) {
83 CHECK(thread != NULL);
84 CHECK_GE(thread->suspend_count_, 0);
85
86 MutexLock mu(thread_suspend_count_lock_);
87 if (thread->suspend_count_ == 0) {
88 return;
89 }
90
Elliott Hughes14357e82011-09-26 10:42:15 -070091 if (verbose_) {
92 LOG(INFO) << *thread << " self-suspending";
93 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070094 {
95 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
96 while (thread->suspend_count_ != 0) {
97 /*
98 * Wait for wakeup signal, releasing lock. The act of releasing
99 * and re-acquiring the lock provides the memory barriers we
100 * need for correct behavior on SMP.
101 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700102 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700103 }
104 CHECK_EQ(thread->suspend_count_, 0);
105 }
Elliott Hughes14357e82011-09-26 10:42:15 -0700106 if (verbose_) {
107 LOG(INFO) << *thread << " self-reviving";
108 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700109}
110
111void ThreadList::SuspendAll() {
112 Thread* self = Thread::Current();
113
114 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
115
Elliott Hughes14357e82011-09-26 10:42:15 -0700116 if (verbose_) {
117 LOG(INFO) << *self << " SuspendAll starting...";
118 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700119
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700120 ThreadListLocker locker(this);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700121
122 {
123 // Increment everybody's suspend count (except our own).
124 MutexLock mu(thread_suspend_count_lock_);
125 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
126 Thread* thread = *it;
127 if (thread != self) {
Elliott Hughes14357e82011-09-26 10:42:15 -0700128 if (verbose_) {
129 LOG(INFO) << "requesting thread suspend: " << *thread;
130 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700131 ++thread->suspend_count_;
132 }
133 }
134 }
135
136 /*
137 * Wait for everybody in kRunnable state to stop. Other states
138 * indicate the code is either running natively or sleeping quietly.
139 * Any attempt to transition back to kRunnable will cause a check
140 * for suspension, so it should be impossible for anything to execute
141 * interpreted code or modify objects (assuming native code plays nicely).
142 *
143 * It's also okay if the thread transitions to a non-kRunnable state.
144 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700145 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700146 * so if another thread is fiddling with its suspend count (perhaps
147 * self-suspending for the debugger) it won't block while we're waiting
148 * in here.
149 */
150 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
151 Thread* thread = *it;
152 if (thread != self) {
153 thread->WaitUntilSuspended();
Elliott Hughes14357e82011-09-26 10:42:15 -0700154 if (verbose_) {
155 LOG(INFO) << "thread suspended: " << *thread;
156 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700157 }
158 }
159
Elliott Hughes14357e82011-09-26 10:42:15 -0700160 if (verbose_) {
161 LOG(INFO) << *self << " SuspendAll complete";
162 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700163}
164
Elliott Hughes01158d72011-09-19 19:47:10 -0700165void ThreadList::Suspend(Thread* thread) {
166 DCHECK(thread != Thread::Current());
167
168 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
169
Elliott Hughes14357e82011-09-26 10:42:15 -0700170 if (verbose_) {
171 LOG(INFO) << "Suspend(" << *thread << ") starting...";
172 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700173
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700174 ThreadListLocker locker(this);
Elliott Hughes01158d72011-09-19 19:47:10 -0700175 if (!Contains(thread)) {
176 return;
177 }
178
179 {
180 MutexLock mu(thread_suspend_count_lock_);
181 ++thread->suspend_count_;
182 }
183
184 thread->WaitUntilSuspended();
185
Elliott Hughes14357e82011-09-26 10:42:15 -0700186 if (verbose_) {
187 LOG(INFO) << "Suspend(" << *thread << ") complete";
188 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700189}
190
191
Elliott Hughes8d768a92011-09-14 16:35:25 -0700192void ThreadList::ResumeAll() {
193 Thread* self = Thread::Current();
194
Elliott Hughes14357e82011-09-26 10:42:15 -0700195 if (verbose_) {
196 LOG(INFO) << *self << " ResumeAll starting";
197 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700198
199 // Decrement the suspend counts for all threads. No need for atomic
200 // writes, since nobody should be moving until we decrement the count.
201 // We do need to hold the thread list because of JNI attaches.
202 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700203 ThreadListLocker locker(this);
204 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700205 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
206 Thread* thread = *it;
207 if (thread != self) {
208 if (thread->suspend_count_ > 0) {
209 --thread->suspend_count_;
210 } else {
211 LOG(WARNING) << *thread << " suspend count already zero";
212 }
213 }
214 }
215 }
216
217 // Broadcast a notification to all suspended threads, some or all of
218 // which may choose to wake up. No need to wait for them.
219 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700220 if (verbose_) {
221 LOG(INFO) << *self << " ResumeAll waking others";
222 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700223 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700224 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700225 }
226
Elliott Hughes14357e82011-09-26 10:42:15 -0700227 if (verbose_) {
228 LOG(INFO) << *self << " ResumeAll complete";
229 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700230}
231
Elliott Hughes01158d72011-09-19 19:47:10 -0700232void ThreadList::Resume(Thread* thread) {
233 DCHECK(thread != Thread::Current());
234
Elliott Hughes14357e82011-09-26 10:42:15 -0700235 if (verbose_) {
236 LOG(INFO) << "Resume(" << *thread << ") starting...";
237 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700238
239 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700240 ThreadListLocker locker(this);
241 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700242 if (!Contains(thread)) {
243 return;
244 }
245 if (thread->suspend_count_ > 0) {
246 --thread->suspend_count_;
247 } else {
248 LOG(WARNING) << *thread << " suspend count already zero";
249 }
250 }
251
252 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700253 if (verbose_) {
254 LOG(INFO) << "Resume(" << *thread << ") waking others";
255 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700256 MutexLock mu(thread_suspend_count_lock_);
257 thread_suspend_count_cond_.Broadcast();
258 }
259
Elliott Hughes14357e82011-09-26 10:42:15 -0700260 if (verbose_) {
261 LOG(INFO) << "Resume(" << *thread << ") complete";
262 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700263}
264
265void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
266 DCHECK(thread != NULL);
267 Thread* self = Thread::Current();
268 if (thread != self) {
269 Suspend(thread);
270 }
271 callback(arg);
272 if (thread != self) {
273 Resume(thread);
274 }
275}
276
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700277void ThreadList::Register() {
278 Thread* self = Thread::Current();
279
Elliott Hughes14357e82011-09-26 10:42:15 -0700280 if (verbose_) {
281 LOG(INFO) << "ThreadList::Register() " << *self;
282 self->Dump(std::cerr);
283 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700284
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700285 ThreadListLocker locker(this);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700286 CHECK(!Contains(self));
287 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700288}
289
290void ThreadList::Unregister() {
291 Thread* self = Thread::Current();
292
Elliott Hughes14357e82011-09-26 10:42:15 -0700293 if (verbose_) {
294 LOG(INFO) << "ThreadList::Unregister() " << *self;
295 }
296
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700297 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
298 // down the Thread* and removing it from the thread list (or start taking any locks).
299 self->HandleUncaughtExceptions();
300
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700301 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700302
303 // Remove this thread from the list.
304 CHECK(Contains(self));
305 list_.remove(self);
306
307 // Delete the Thread* and release the thin lock id.
308 uint32_t thin_lock_id = self->thin_lock_id_;
309 delete self;
310 ReleaseThreadId(thin_lock_id);
311
312 // Clear the TLS data, so that thread is recognizably detached.
313 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700314 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700315
316 // Signal that a thread just detached.
317 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700318}
319
320void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700321 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700322 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
323 (*it)->VisitRoots(visitor, arg);
324 }
325}
326
Elliott Hughes93e74e82011-09-13 11:07:03 -0700327/*
328 * Tell a new thread it's safe to start.
329 *
330 * We must hold the thread list lock before messing with another thread.
331 * In the general case we would also need to verify that the new thread was
332 * still in the thread list, but in our case the thread has not started
333 * executing user code and therefore has not had a chance to exit.
334 *
335 * We move it to kVmWait, and it then shifts itself to kRunning, which
336 * comes with a suspend-pending check. We do this after
337 */
338void ThreadList::SignalGo(Thread* child) {
339 Thread* self = Thread::Current();
340 CHECK(child != self);
341
342 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700343 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700344
345 // We wait for the child to tell us that it's in the thread list.
346 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700347 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700348 }
349 }
350
351 // If we switch out of runnable and then back in, we know there's no pending suspend.
352 self->SetState(Thread::kVmWait);
353 self->SetState(Thread::kRunnable);
354
355 // Tell the child that it's safe: it will see any future suspend request.
356 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700357 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700358}
359
360void ThreadList::WaitForGo() {
361 Thread* self = Thread::Current();
362 DCHECK(Contains(self));
363
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700364 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700365 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700366
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700367 // Tell our parent that we're in the thread list.
368 self->SetState(Thread::kStarting);
369 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700370
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700371 // Wait until our parent tells us there's no suspend still pending
372 // from before we were on the thread list.
373 while (self->GetState() != Thread::kVmWait) {
374 thread_start_cond_.Wait(thread_list_lock_);
375 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700376 }
377
378 // Enter the runnable state. We know that any pending suspend will affect us now.
379 self->SetState(Thread::kRunnable);
380}
381
Elliott Hughes038a8062011-09-18 14:12:41 -0700382bool ThreadList::AllThreadsAreDaemons() {
383 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700384 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
385 // is null.
386 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700387 return false;
388 }
389 }
390 return true;
391}
392
393void ThreadList::WaitForNonDaemonThreadsToExit() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700394 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700395 while (!AllThreadsAreDaemons()) {
396 thread_exit_cond_.Wait(thread_list_lock_);
397 }
398}
399
400void ThreadList::SuspendAllDaemonThreads() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700401 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700402
403 // Tell all the daemons it's time to suspend. (At this point, we know
404 // all threads are daemons.)
405 {
406 MutexLock mu(thread_suspend_count_lock_);
407 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
408 Thread* thread = *it;
409 ++thread->suspend_count_;
410 }
411 }
412
413 // Give the threads a chance to suspend, complaining if they're slow.
414 bool have_complained = false;
415 for (int i = 0; i < 10; ++i) {
416 usleep(200 * 1000);
417 bool all_suspended = true;
418 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
419 Thread* thread = *it;
420 if (thread->GetState() == Thread::kRunnable) {
421 if (!have_complained) {
422 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
423 have_complained = true;
424 }
425 all_suspended = false;
426 }
427 }
428 if (all_suspended) {
429 return;
430 }
431 }
432}
433
Elliott Hughes8daa0922011-09-11 13:46:25 -0700434uint32_t ThreadList::AllocThreadId() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700435 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700436 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
437 if (!allocated_ids_[i]) {
438 allocated_ids_.set(i);
439 return i + 1; // Zero is reserved to mean "invalid".
440 }
441 }
442 LOG(FATAL) << "Out of internal thread ids";
443 return 0;
444}
445
446void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700447 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700448 --id; // Zero is reserved to mean "invalid".
449 DCHECK(allocated_ids_[id]) << id;
450 allocated_ids_.reset(id);
451}
452
453} // namespace art