blob: 84edf46749130aa37a390b87245d26e5a703ee6e [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
Elliott Hughes14357e82011-09-26 10:42:15 -070023ThreadList::ThreadList(bool verbose)
24 : verbose_(verbose),
25 thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070026 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070027 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070028 thread_suspend_count_lock_("thread suspend count lock"),
29 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes8daa0922011-09-11 13:46:25 -070030}
31
32ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070033 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070034 if (Contains(Thread::Current())) {
35 Runtime::Current()->DetachCurrentThread();
36 }
37
Elliott Hughes038a8062011-09-18 14:12:41 -070038 WaitForNonDaemonThreadsToExit();
39 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070040}
41
42bool ThreadList::Contains(Thread* thread) {
43 return find(list_.begin(), list_.end(), thread) != list_.end();
44}
45
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070046pid_t ThreadList::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -070047 return thread_list_lock_.GetOwner();
48}
49
Elliott Hughes8daa0922011-09-11 13:46:25 -070050void ThreadList::Dump(std::ostream& os) {
Elliott Hughes8d768a92011-09-14 16:35:25 -070051 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070052 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070053 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
54 (*it)->Dump(os);
55 os << "\n";
56 }
57}
58
Elliott Hughes8d768a92011-09-14 16:35:25 -070059void ThreadList::FullSuspendCheck(Thread* thread) {
60 CHECK(thread != NULL);
61 CHECK_GE(thread->suspend_count_, 0);
62
63 MutexLock mu(thread_suspend_count_lock_);
64 if (thread->suspend_count_ == 0) {
65 return;
66 }
67
Elliott Hughes14357e82011-09-26 10:42:15 -070068 if (verbose_) {
69 LOG(INFO) << *thread << " self-suspending";
70 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070071 {
72 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
73 while (thread->suspend_count_ != 0) {
74 /*
75 * Wait for wakeup signal, releasing lock. The act of releasing
76 * and re-acquiring the lock provides the memory barriers we
77 * need for correct behavior on SMP.
78 */
Elliott Hughes5f791332011-09-15 17:45:30 -070079 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -070080 }
81 CHECK_EQ(thread->suspend_count_, 0);
82 }
Elliott Hughes14357e82011-09-26 10:42:15 -070083 if (verbose_) {
84 LOG(INFO) << *thread << " self-reviving";
85 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070086}
87
88void ThreadList::SuspendAll() {
89 Thread* self = Thread::Current();
90
91 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
92
Elliott Hughes14357e82011-09-26 10:42:15 -070093 if (verbose_) {
94 LOG(INFO) << *self << " SuspendAll starting...";
95 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070096
97 MutexLock mu(thread_list_lock_);
98
99 {
100 // Increment everybody's suspend count (except our own).
101 MutexLock mu(thread_suspend_count_lock_);
102 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
103 Thread* thread = *it;
104 if (thread != self) {
Elliott Hughes14357e82011-09-26 10:42:15 -0700105 if (verbose_) {
106 LOG(INFO) << "requesting thread suspend: " << *thread;
107 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700108 ++thread->suspend_count_;
109 }
110 }
111 }
112
113 /*
114 * Wait for everybody in kRunnable state to stop. Other states
115 * indicate the code is either running natively or sleeping quietly.
116 * Any attempt to transition back to kRunnable will cause a check
117 * for suspension, so it should be impossible for anything to execute
118 * interpreted code or modify objects (assuming native code plays nicely).
119 *
120 * It's also okay if the thread transitions to a non-kRunnable state.
121 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700122 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700123 * so if another thread is fiddling with its suspend count (perhaps
124 * self-suspending for the debugger) it won't block while we're waiting
125 * in here.
126 */
127 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
128 Thread* thread = *it;
129 if (thread != self) {
130 thread->WaitUntilSuspended();
Elliott Hughes14357e82011-09-26 10:42:15 -0700131 if (verbose_) {
132 LOG(INFO) << "thread suspended: " << *thread;
133 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700134 }
135 }
136
Elliott Hughes14357e82011-09-26 10:42:15 -0700137 if (verbose_) {
138 LOG(INFO) << *self << " SuspendAll complete";
139 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700140}
141
Elliott Hughes01158d72011-09-19 19:47:10 -0700142void ThreadList::Suspend(Thread* thread) {
143 DCHECK(thread != Thread::Current());
144
145 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
146
Elliott Hughes14357e82011-09-26 10:42:15 -0700147 if (verbose_) {
148 LOG(INFO) << "Suspend(" << *thread << ") starting...";
149 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700150
151 MutexLock mu(thread_list_lock_);
152 if (!Contains(thread)) {
153 return;
154 }
155
156 {
157 MutexLock mu(thread_suspend_count_lock_);
158 ++thread->suspend_count_;
159 }
160
161 thread->WaitUntilSuspended();
162
Elliott Hughes14357e82011-09-26 10:42:15 -0700163 if (verbose_) {
164 LOG(INFO) << "Suspend(" << *thread << ") complete";
165 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700166}
167
168
Elliott Hughes8d768a92011-09-14 16:35:25 -0700169void ThreadList::ResumeAll() {
170 Thread* self = Thread::Current();
171
Elliott Hughes14357e82011-09-26 10:42:15 -0700172 if (verbose_) {
173 LOG(INFO) << *self << " ResumeAll starting";
174 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700175
176 // Decrement the suspend counts for all threads. No need for atomic
177 // writes, since nobody should be moving until we decrement the count.
178 // We do need to hold the thread list because of JNI attaches.
179 {
180 MutexLock mu1(thread_list_lock_);
181 MutexLock mu2(thread_suspend_count_lock_);
182 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
183 Thread* thread = *it;
184 if (thread != self) {
185 if (thread->suspend_count_ > 0) {
186 --thread->suspend_count_;
187 } else {
188 LOG(WARNING) << *thread << " suspend count already zero";
189 }
190 }
191 }
192 }
193
194 // Broadcast a notification to all suspended threads, some or all of
195 // which may choose to wake up. No need to wait for them.
196 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700197 if (verbose_) {
198 LOG(INFO) << *self << " ResumeAll waking others";
199 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700200 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700201 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700202 }
203
Elliott Hughes14357e82011-09-26 10:42:15 -0700204 if (verbose_) {
205 LOG(INFO) << *self << " ResumeAll complete";
206 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700207}
208
Elliott Hughes01158d72011-09-19 19:47:10 -0700209void ThreadList::Resume(Thread* thread) {
210 DCHECK(thread != Thread::Current());
211
Elliott Hughes14357e82011-09-26 10:42:15 -0700212 if (verbose_) {
213 LOG(INFO) << "Resume(" << *thread << ") starting...";
214 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700215
216 {
217 MutexLock mu1(thread_list_lock_);
218 MutexLock mu2(thread_suspend_count_lock_);
219 if (!Contains(thread)) {
220 return;
221 }
222 if (thread->suspend_count_ > 0) {
223 --thread->suspend_count_;
224 } else {
225 LOG(WARNING) << *thread << " suspend count already zero";
226 }
227 }
228
229 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700230 if (verbose_) {
231 LOG(INFO) << "Resume(" << *thread << ") waking others";
232 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700233 MutexLock mu(thread_suspend_count_lock_);
234 thread_suspend_count_cond_.Broadcast();
235 }
236
Elliott Hughes14357e82011-09-26 10:42:15 -0700237 if (verbose_) {
238 LOG(INFO) << "Resume(" << *thread << ") complete";
239 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700240}
241
242void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
243 DCHECK(thread != NULL);
244 Thread* self = Thread::Current();
245 if (thread != self) {
246 Suspend(thread);
247 }
248 callback(arg);
249 if (thread != self) {
250 Resume(thread);
251 }
252}
253
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700254void ThreadList::Register() {
255 Thread* self = Thread::Current();
256
Elliott Hughes14357e82011-09-26 10:42:15 -0700257 if (verbose_) {
258 LOG(INFO) << "ThreadList::Register() " << *self;
259 self->Dump(std::cerr);
260 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700261
Elliott Hughes8d768a92011-09-14 16:35:25 -0700262 MutexLock mu(thread_list_lock_);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700263 CHECK(!Contains(self));
264 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700265}
266
267void ThreadList::Unregister() {
268 Thread* self = Thread::Current();
269
Elliott Hughes14357e82011-09-26 10:42:15 -0700270 if (verbose_) {
271 LOG(INFO) << "ThreadList::Unregister() " << *self;
272 }
273
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700274 // This may need to call user-supplied managed code. Make sure we do this before we start tearing
275 // down the Thread* and removing it from the thread list (or start taking any locks).
276 self->HandleUncaughtExceptions();
277
Elliott Hughes8d768a92011-09-14 16:35:25 -0700278 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700279
280 // Remove this thread from the list.
281 CHECK(Contains(self));
282 list_.remove(self);
283
284 // Delete the Thread* and release the thin lock id.
285 uint32_t thin_lock_id = self->thin_lock_id_;
286 delete self;
287 ReleaseThreadId(thin_lock_id);
288
289 // Clear the TLS data, so that thread is recognizably detached.
290 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700291 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700292
293 // Signal that a thread just detached.
294 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700295}
296
297void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700298 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700299 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
300 (*it)->VisitRoots(visitor, arg);
301 }
302}
303
Elliott Hughes93e74e82011-09-13 11:07:03 -0700304/*
305 * Tell a new thread it's safe to start.
306 *
307 * We must hold the thread list lock before messing with another thread.
308 * In the general case we would also need to verify that the new thread was
309 * still in the thread list, but in our case the thread has not started
310 * executing user code and therefore has not had a chance to exit.
311 *
312 * We move it to kVmWait, and it then shifts itself to kRunning, which
313 * comes with a suspend-pending check. We do this after
314 */
315void ThreadList::SignalGo(Thread* child) {
316 Thread* self = Thread::Current();
317 CHECK(child != self);
318
319 {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700320 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700321
322 // We wait for the child to tell us that it's in the thread list.
323 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700324 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700325 }
326 }
327
328 // If we switch out of runnable and then back in, we know there's no pending suspend.
329 self->SetState(Thread::kVmWait);
330 self->SetState(Thread::kRunnable);
331
332 // Tell the child that it's safe: it will see any future suspend request.
333 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700334 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700335}
336
337void ThreadList::WaitForGo() {
338 Thread* self = Thread::Current();
339 DCHECK(Contains(self));
340
Elliott Hughes8d768a92011-09-14 16:35:25 -0700341 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700342
343 // Tell our parent that we're in the thread list.
344 self->SetState(Thread::kStarting);
Elliott Hughes5f791332011-09-15 17:45:30 -0700345 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700346
347 // Wait until our parent tells us there's no suspend still pending
348 // from before we were on the thread list.
349 while (self->GetState() != Thread::kVmWait) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700350 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700351 }
352
353 // Enter the runnable state. We know that any pending suspend will affect us now.
354 self->SetState(Thread::kRunnable);
355}
356
Elliott Hughes038a8062011-09-18 14:12:41 -0700357bool ThreadList::AllThreadsAreDaemons() {
358 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700359 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
360 // is null.
361 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700362 return false;
363 }
364 }
365 return true;
366}
367
368void ThreadList::WaitForNonDaemonThreadsToExit() {
369 MutexLock mu(thread_list_lock_);
370 while (!AllThreadsAreDaemons()) {
371 thread_exit_cond_.Wait(thread_list_lock_);
372 }
373}
374
375void ThreadList::SuspendAllDaemonThreads() {
376 MutexLock mu(thread_list_lock_);
377
378 // Tell all the daemons it's time to suspend. (At this point, we know
379 // all threads are daemons.)
380 {
381 MutexLock mu(thread_suspend_count_lock_);
382 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
383 Thread* thread = *it;
384 ++thread->suspend_count_;
385 }
386 }
387
388 // Give the threads a chance to suspend, complaining if they're slow.
389 bool have_complained = false;
390 for (int i = 0; i < 10; ++i) {
391 usleep(200 * 1000);
392 bool all_suspended = true;
393 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
394 Thread* thread = *it;
395 if (thread->GetState() == Thread::kRunnable) {
396 if (!have_complained) {
397 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
398 have_complained = true;
399 }
400 all_suspended = false;
401 }
402 }
403 if (all_suspended) {
404 return;
405 }
406 }
407}
408
Elliott Hughes8daa0922011-09-11 13:46:25 -0700409uint32_t ThreadList::AllocThreadId() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700410 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700411 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
412 if (!allocated_ids_[i]) {
413 allocated_ids_.set(i);
414 return i + 1; // Zero is reserved to mean "invalid".
415 }
416 }
417 LOG(FATAL) << "Out of internal thread ids";
418 return 0;
419}
420
421void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700422 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700423 --id; // Zero is reserved to mean "invalid".
424 DCHECK(allocated_ids_[id]) << id;
425 allocated_ids_.reset(id);
426}
427
428} // namespace art