blob: dfce2a255c78e4553acfe20130fd3a1183a9ebdc [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()) {
Brian Carlstrom4514d3c2011-10-21 17:01:31 -070032 Thread* self = Thread::Current();
33 if (self == NULL) {
34 thread_list_->thread_list_lock_.Lock();
35 } else {
36 ScopedThreadStateChange tsc(self, Thread::kVmWait);
37 thread_list_->thread_list_lock_.Lock();
38 }
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070039 }
40 }
41
42 ~ThreadListLocker() {
43 thread_list_->thread_list_lock_.Unlock();
44 }
45
46 private:
47 const ThreadList* thread_list_;
48 DISALLOW_COPY_AND_ASSIGN(ThreadListLocker);
49};
50
Elliott Hughes14357e82011-09-26 10:42:15 -070051ThreadList::ThreadList(bool verbose)
52 : verbose_(verbose),
53 thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070054 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070055 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070056 thread_suspend_count_lock_("thread suspend count lock"),
57 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes8daa0922011-09-11 13:46:25 -070058}
59
60ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070061 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070062 if (Contains(Thread::Current())) {
63 Runtime::Current()->DetachCurrentThread();
64 }
65
Elliott Hughes038a8062011-09-18 14:12:41 -070066 WaitForNonDaemonThreadsToExit();
67 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070068}
69
70bool ThreadList::Contains(Thread* thread) {
71 return find(list_.begin(), list_.end(), thread) != list_.end();
72}
73
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070074pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070075 return thread_list_lock_.GetOwner();
76}
77
Elliott Hughes8daa0922011-09-11 13:46:25 -070078void ThreadList::Dump(std::ostream& os) {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -070079 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -070080 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070081 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
82 (*it)->Dump(os);
83 os << "\n";
84 }
85}
86
Elliott Hughes8d768a92011-09-14 16:35:25 -070087void ThreadList::FullSuspendCheck(Thread* thread) {
88 CHECK(thread != NULL);
89 CHECK_GE(thread->suspend_count_, 0);
90
91 MutexLock mu(thread_suspend_count_lock_);
92 if (thread->suspend_count_ == 0) {
93 return;
94 }
95
Elliott Hughes14357e82011-09-26 10:42:15 -070096 if (verbose_) {
97 LOG(INFO) << *thread << " self-suspending";
98 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070099 {
100 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
101 while (thread->suspend_count_ != 0) {
102 /*
103 * Wait for wakeup signal, releasing lock. The act of releasing
104 * and re-acquiring the lock provides the memory barriers we
105 * need for correct behavior on SMP.
106 */
Elliott Hughes5f791332011-09-15 17:45:30 -0700107 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700108 }
109 CHECK_EQ(thread->suspend_count_, 0);
110 }
Elliott Hughes14357e82011-09-26 10:42:15 -0700111 if (verbose_) {
112 LOG(INFO) << *thread << " self-reviving";
113 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700114}
115
116void ThreadList::SuspendAll() {
117 Thread* self = Thread::Current();
118
119 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
120
Elliott Hughes14357e82011-09-26 10:42:15 -0700121 if (verbose_) {
122 LOG(INFO) << *self << " SuspendAll starting...";
123 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700124
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700125 ThreadListLocker locker(this);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700126
127 {
128 // Increment everybody's suspend count (except our own).
129 MutexLock mu(thread_suspend_count_lock_);
130 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
131 Thread* thread = *it;
132 if (thread != self) {
Elliott Hughes14357e82011-09-26 10:42:15 -0700133 if (verbose_) {
134 LOG(INFO) << "requesting thread suspend: " << *thread;
135 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700136 ++thread->suspend_count_;
137 }
138 }
139 }
140
141 /*
142 * Wait for everybody in kRunnable state to stop. Other states
143 * indicate the code is either running natively or sleeping quietly.
144 * Any attempt to transition back to kRunnable will cause a check
145 * for suspension, so it should be impossible for anything to execute
146 * interpreted code or modify objects (assuming native code plays nicely).
147 *
148 * It's also okay if the thread transitions to a non-kRunnable state.
149 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700150 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700151 * so if another thread is fiddling with its suspend count (perhaps
152 * self-suspending for the debugger) it won't block while we're waiting
153 * in here.
154 */
155 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
156 Thread* thread = *it;
157 if (thread != self) {
158 thread->WaitUntilSuspended();
Elliott Hughes14357e82011-09-26 10:42:15 -0700159 if (verbose_) {
160 LOG(INFO) << "thread suspended: " << *thread;
161 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700162 }
163 }
164
Elliott Hughes14357e82011-09-26 10:42:15 -0700165 if (verbose_) {
166 LOG(INFO) << *self << " SuspendAll complete";
167 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700168}
169
Elliott Hughes01158d72011-09-19 19:47:10 -0700170void ThreadList::Suspend(Thread* thread) {
171 DCHECK(thread != Thread::Current());
172
173 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
174
Elliott Hughes14357e82011-09-26 10:42:15 -0700175 if (verbose_) {
176 LOG(INFO) << "Suspend(" << *thread << ") starting...";
177 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700178
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700179 ThreadListLocker locker(this);
Elliott Hughes01158d72011-09-19 19:47:10 -0700180 if (!Contains(thread)) {
181 return;
182 }
183
184 {
185 MutexLock mu(thread_suspend_count_lock_);
186 ++thread->suspend_count_;
187 }
188
189 thread->WaitUntilSuspended();
190
Elliott Hughes14357e82011-09-26 10:42:15 -0700191 if (verbose_) {
192 LOG(INFO) << "Suspend(" << *thread << ") complete";
193 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700194}
195
196
Elliott Hughes8d768a92011-09-14 16:35:25 -0700197void ThreadList::ResumeAll() {
198 Thread* self = Thread::Current();
199
Elliott Hughes14357e82011-09-26 10:42:15 -0700200 if (verbose_) {
201 LOG(INFO) << *self << " ResumeAll starting";
202 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700203
204 // Decrement the suspend counts for all threads. No need for atomic
205 // writes, since nobody should be moving until we decrement the count.
206 // We do need to hold the thread list because of JNI attaches.
207 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700208 ThreadListLocker locker(this);
209 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -0700210 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
211 Thread* thread = *it;
212 if (thread != self) {
213 if (thread->suspend_count_ > 0) {
214 --thread->suspend_count_;
215 } else {
216 LOG(WARNING) << *thread << " suspend count already zero";
217 }
218 }
219 }
220 }
221
222 // Broadcast a notification to all suspended threads, some or all of
223 // which may choose to wake up. No need to wait for them.
224 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700225 if (verbose_) {
226 LOG(INFO) << *self << " ResumeAll waking others";
227 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700228 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700229 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700230 }
231
Elliott Hughes14357e82011-09-26 10:42:15 -0700232 if (verbose_) {
233 LOG(INFO) << *self << " ResumeAll complete";
234 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700235}
236
Elliott Hughes01158d72011-09-19 19:47:10 -0700237void ThreadList::Resume(Thread* thread) {
238 DCHECK(thread != Thread::Current());
239
Elliott Hughes14357e82011-09-26 10:42:15 -0700240 if (verbose_) {
241 LOG(INFO) << "Resume(" << *thread << ") starting...";
242 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700243
244 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700245 ThreadListLocker locker(this);
246 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes01158d72011-09-19 19:47:10 -0700247 if (!Contains(thread)) {
248 return;
249 }
250 if (thread->suspend_count_ > 0) {
251 --thread->suspend_count_;
252 } else {
253 LOG(WARNING) << *thread << " suspend count already zero";
254 }
255 }
256
257 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700258 if (verbose_) {
259 LOG(INFO) << "Resume(" << *thread << ") waking others";
260 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700261 MutexLock mu(thread_suspend_count_lock_);
262 thread_suspend_count_cond_.Broadcast();
263 }
264
Elliott Hughes14357e82011-09-26 10:42:15 -0700265 if (verbose_) {
266 LOG(INFO) << "Resume(" << *thread << ") complete";
267 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700268}
269
270void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
271 DCHECK(thread != NULL);
272 Thread* self = Thread::Current();
273 if (thread != self) {
274 Suspend(thread);
275 }
276 callback(arg);
277 if (thread != self) {
278 Resume(thread);
279 }
280}
281
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700282void ThreadList::Register() {
283 Thread* self = Thread::Current();
284
Elliott Hughes14357e82011-09-26 10:42:15 -0700285 if (verbose_) {
286 LOG(INFO) << "ThreadList::Register() " << *self;
287 self->Dump(std::cerr);
288 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700289
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700290 ThreadListLocker locker(this);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700291 CHECK(!Contains(self));
292 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700293}
294
295void ThreadList::Unregister() {
296 Thread* self = Thread::Current();
297
Elliott Hughes14357e82011-09-26 10:42:15 -0700298 if (verbose_) {
299 LOG(INFO) << "ThreadList::Unregister() " << *self;
300 }
301
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700302 if (self->GetPeer() != NULL) {
303 self->SetState(Thread::kRunnable);
304
305 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
306 // down the Thread* and removing it from the thread list (or start taking any locks).
307 self->HandleUncaughtExceptions();
308
309 // Make sure we remove from ThreadGroup before taking the
310 // thread_list_lock_ since it allocates an Iterator which can cause
311 // a GC which will want to suspend.
312 self->RemoveFromThreadGroup();
313 }
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700314
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700315 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700316
317 // Remove this thread from the list.
318 CHECK(Contains(self));
319 list_.remove(self);
320
321 // Delete the Thread* and release the thin lock id.
322 uint32_t thin_lock_id = self->thin_lock_id_;
323 delete self;
324 ReleaseThreadId(thin_lock_id);
325
326 // Clear the TLS data, so that thread is recognizably detached.
327 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700328 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700329
330 // Signal that a thread just detached.
331 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700332}
333
334void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700335 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700336 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
337 (*it)->VisitRoots(visitor, arg);
338 }
339}
340
Elliott Hughes93e74e82011-09-13 11:07:03 -0700341/*
342 * Tell a new thread it's safe to start.
343 *
344 * We must hold the thread list lock before messing with another thread.
345 * In the general case we would also need to verify that the new thread was
346 * still in the thread list, but in our case the thread has not started
347 * executing user code and therefore has not had a chance to exit.
348 *
349 * We move it to kVmWait, and it then shifts itself to kRunning, which
350 * comes with a suspend-pending check. We do this after
351 */
352void ThreadList::SignalGo(Thread* child) {
353 Thread* self = Thread::Current();
354 CHECK(child != self);
355
356 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700357 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700358
359 // We wait for the child to tell us that it's in the thread list.
360 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700361 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700362 }
363 }
364
365 // If we switch out of runnable and then back in, we know there's no pending suspend.
366 self->SetState(Thread::kVmWait);
367 self->SetState(Thread::kRunnable);
368
369 // Tell the child that it's safe: it will see any future suspend request.
370 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700371 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700372}
373
374void ThreadList::WaitForGo() {
375 Thread* self = Thread::Current();
376 DCHECK(Contains(self));
377
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700378 {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700379 ThreadListLocker locker(this);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700380
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700381 // Tell our parent that we're in the thread list.
382 self->SetState(Thread::kStarting);
383 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700384
Brian Carlstrom6fbb5162011-10-20 20:55:38 -0700385 // Wait until our parent tells us there's no suspend still pending
386 // from before we were on the thread list.
387 while (self->GetState() != Thread::kVmWait) {
388 thread_start_cond_.Wait(thread_list_lock_);
389 }
Elliott Hughes93e74e82011-09-13 11:07:03 -0700390 }
391
392 // Enter the runnable state. We know that any pending suspend will affect us now.
393 self->SetState(Thread::kRunnable);
394}
395
Elliott Hughes038a8062011-09-18 14:12:41 -0700396bool ThreadList::AllThreadsAreDaemons() {
397 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700398 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
399 // is null.
400 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700401 return false;
402 }
403 }
404 return true;
405}
406
407void ThreadList::WaitForNonDaemonThreadsToExit() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700408 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700409 while (!AllThreadsAreDaemons()) {
410 thread_exit_cond_.Wait(thread_list_lock_);
411 }
412}
413
414void ThreadList::SuspendAllDaemonThreads() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700415 ThreadListLocker locker(this);
Elliott Hughes038a8062011-09-18 14:12:41 -0700416
417 // Tell all the daemons it's time to suspend. (At this point, we know
418 // all threads are daemons.)
419 {
420 MutexLock mu(thread_suspend_count_lock_);
421 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
422 Thread* thread = *it;
423 ++thread->suspend_count_;
424 }
425 }
426
427 // Give the threads a chance to suspend, complaining if they're slow.
428 bool have_complained = false;
429 for (int i = 0; i < 10; ++i) {
430 usleep(200 * 1000);
431 bool all_suspended = true;
432 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
433 Thread* thread = *it;
434 if (thread->GetState() == Thread::kRunnable) {
435 if (!have_complained) {
436 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
437 have_complained = true;
438 }
439 all_suspended = false;
440 }
441 }
442 if (all_suspended) {
443 return;
444 }
445 }
446}
447
Elliott Hughes8daa0922011-09-11 13:46:25 -0700448uint32_t ThreadList::AllocThreadId() {
Brian Carlstrom4f20aef2011-10-21 00:16:18 -0700449 ThreadListLocker locker(this);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700450 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
451 if (!allocated_ids_[i]) {
452 allocated_ids_.set(i);
453 return i + 1; // Zero is reserved to mean "invalid".
454 }
455 }
456 LOG(FATAL) << "Out of internal thread ids";
457 return 0;
458}
459
460void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700461 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700462 --id; // Zero is reserved to mean "invalid".
463 DCHECK(allocated_ids_[id]) << id;
464 allocated_ids_.reset(id);
465}
466
467} // namespace art