blob: 7b6d3baec5ed153ffa5b94b94947a36a96110909 [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
46void ThreadList::Dump(std::ostream& os) {
Elliott Hughes8d768a92011-09-14 16:35:25 -070047 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070048 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070049 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
50 (*it)->Dump(os);
51 os << "\n";
52 }
53}
54
Elliott Hughes8d768a92011-09-14 16:35:25 -070055void ThreadList::FullSuspendCheck(Thread* thread) {
56 CHECK(thread != NULL);
57 CHECK_GE(thread->suspend_count_, 0);
58
59 MutexLock mu(thread_suspend_count_lock_);
60 if (thread->suspend_count_ == 0) {
61 return;
62 }
63
Elliott Hughes14357e82011-09-26 10:42:15 -070064 if (verbose_) {
65 LOG(INFO) << *thread << " self-suspending";
66 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070067 {
68 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
69 while (thread->suspend_count_ != 0) {
70 /*
71 * Wait for wakeup signal, releasing lock. The act of releasing
72 * and re-acquiring the lock provides the memory barriers we
73 * need for correct behavior on SMP.
74 */
Elliott Hughes5f791332011-09-15 17:45:30 -070075 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -070076 }
77 CHECK_EQ(thread->suspend_count_, 0);
78 }
Elliott Hughes14357e82011-09-26 10:42:15 -070079 if (verbose_) {
80 LOG(INFO) << *thread << " self-reviving";
81 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070082}
83
84void ThreadList::SuspendAll() {
85 Thread* self = Thread::Current();
86
87 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
88
Elliott Hughes14357e82011-09-26 10:42:15 -070089 if (verbose_) {
90 LOG(INFO) << *self << " SuspendAll starting...";
91 }
Elliott Hughes8d768a92011-09-14 16:35:25 -070092
93 MutexLock mu(thread_list_lock_);
94
95 {
96 // Increment everybody's suspend count (except our own).
97 MutexLock mu(thread_suspend_count_lock_);
98 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
99 Thread* thread = *it;
100 if (thread != self) {
Elliott Hughes14357e82011-09-26 10:42:15 -0700101 if (verbose_) {
102 LOG(INFO) << "requesting thread suspend: " << *thread;
103 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700104 ++thread->suspend_count_;
105 }
106 }
107 }
108
109 /*
110 * Wait for everybody in kRunnable state to stop. Other states
111 * indicate the code is either running natively or sleeping quietly.
112 * Any attempt to transition back to kRunnable will cause a check
113 * for suspension, so it should be impossible for anything to execute
114 * interpreted code or modify objects (assuming native code plays nicely).
115 *
116 * It's also okay if the thread transitions to a non-kRunnable state.
117 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700118 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700119 * so if another thread is fiddling with its suspend count (perhaps
120 * self-suspending for the debugger) it won't block while we're waiting
121 * in here.
122 */
123 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
124 Thread* thread = *it;
125 if (thread != self) {
126 thread->WaitUntilSuspended();
Elliott Hughes14357e82011-09-26 10:42:15 -0700127 if (verbose_) {
128 LOG(INFO) << "thread suspended: " << *thread;
129 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700130 }
131 }
132
Elliott Hughes14357e82011-09-26 10:42:15 -0700133 if (verbose_) {
134 LOG(INFO) << *self << " SuspendAll complete";
135 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700136}
137
Elliott Hughes01158d72011-09-19 19:47:10 -0700138void ThreadList::Suspend(Thread* thread) {
139 DCHECK(thread != Thread::Current());
140
141 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
142
Elliott Hughes14357e82011-09-26 10:42:15 -0700143 if (verbose_) {
144 LOG(INFO) << "Suspend(" << *thread << ") starting...";
145 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700146
147 MutexLock mu(thread_list_lock_);
148 if (!Contains(thread)) {
149 return;
150 }
151
152 {
153 MutexLock mu(thread_suspend_count_lock_);
154 ++thread->suspend_count_;
155 }
156
157 thread->WaitUntilSuspended();
158
Elliott Hughes14357e82011-09-26 10:42:15 -0700159 if (verbose_) {
160 LOG(INFO) << "Suspend(" << *thread << ") complete";
161 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700162}
163
164
Elliott Hughes8d768a92011-09-14 16:35:25 -0700165void ThreadList::ResumeAll() {
166 Thread* self = Thread::Current();
167
Elliott Hughes14357e82011-09-26 10:42:15 -0700168 if (verbose_) {
169 LOG(INFO) << *self << " ResumeAll starting";
170 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700171
172 // Decrement the suspend counts for all threads. No need for atomic
173 // writes, since nobody should be moving until we decrement the count.
174 // We do need to hold the thread list because of JNI attaches.
175 {
176 MutexLock mu1(thread_list_lock_);
177 MutexLock mu2(thread_suspend_count_lock_);
178 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
179 Thread* thread = *it;
180 if (thread != self) {
181 if (thread->suspend_count_ > 0) {
182 --thread->suspend_count_;
183 } else {
184 LOG(WARNING) << *thread << " suspend count already zero";
185 }
186 }
187 }
188 }
189
190 // Broadcast a notification to all suspended threads, some or all of
191 // which may choose to wake up. No need to wait for them.
192 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700193 if (verbose_) {
194 LOG(INFO) << *self << " ResumeAll waking others";
195 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700196 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700197 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700198 }
199
Elliott Hughes14357e82011-09-26 10:42:15 -0700200 if (verbose_) {
201 LOG(INFO) << *self << " ResumeAll complete";
202 }
Elliott Hughes8d768a92011-09-14 16:35:25 -0700203}
204
Elliott Hughes01158d72011-09-19 19:47:10 -0700205void ThreadList::Resume(Thread* thread) {
206 DCHECK(thread != Thread::Current());
207
Elliott Hughes14357e82011-09-26 10:42:15 -0700208 if (verbose_) {
209 LOG(INFO) << "Resume(" << *thread << ") starting...";
210 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700211
212 {
213 MutexLock mu1(thread_list_lock_);
214 MutexLock mu2(thread_suspend_count_lock_);
215 if (!Contains(thread)) {
216 return;
217 }
218 if (thread->suspend_count_ > 0) {
219 --thread->suspend_count_;
220 } else {
221 LOG(WARNING) << *thread << " suspend count already zero";
222 }
223 }
224
225 {
Elliott Hughes14357e82011-09-26 10:42:15 -0700226 if (verbose_) {
227 LOG(INFO) << "Resume(" << *thread << ") waking others";
228 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700229 MutexLock mu(thread_suspend_count_lock_);
230 thread_suspend_count_cond_.Broadcast();
231 }
232
Elliott Hughes14357e82011-09-26 10:42:15 -0700233 if (verbose_) {
234 LOG(INFO) << "Resume(" << *thread << ") complete";
235 }
Elliott Hughes01158d72011-09-19 19:47:10 -0700236}
237
238void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
239 DCHECK(thread != NULL);
240 Thread* self = Thread::Current();
241 if (thread != self) {
242 Suspend(thread);
243 }
244 callback(arg);
245 if (thread != self) {
246 Resume(thread);
247 }
248}
249
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700250void ThreadList::Register() {
251 Thread* self = Thread::Current();
252
Elliott Hughes14357e82011-09-26 10:42:15 -0700253 if (verbose_) {
254 LOG(INFO) << "ThreadList::Register() " << *self;
255 self->Dump(std::cerr);
256 }
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700257
Elliott Hughes8d768a92011-09-14 16:35:25 -0700258 MutexLock mu(thread_list_lock_);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700259 CHECK(!Contains(self));
260 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700261}
262
263void ThreadList::Unregister() {
264 Thread* self = Thread::Current();
265
Elliott Hughes14357e82011-09-26 10:42:15 -0700266 if (verbose_) {
267 LOG(INFO) << "ThreadList::Unregister() " << *self;
268 }
269
Elliott Hughes8d768a92011-09-14 16:35:25 -0700270 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700271
272 // Remove this thread from the list.
273 CHECK(Contains(self));
274 list_.remove(self);
275
276 // Delete the Thread* and release the thin lock id.
277 uint32_t thin_lock_id = self->thin_lock_id_;
278 delete self;
279 ReleaseThreadId(thin_lock_id);
280
281 // Clear the TLS data, so that thread is recognizably detached.
282 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700283 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700284
285 // Signal that a thread just detached.
286 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700287}
288
289void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700290 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700291 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
292 (*it)->VisitRoots(visitor, arg);
293 }
294}
295
Elliott Hughes93e74e82011-09-13 11:07:03 -0700296/*
297 * Tell a new thread it's safe to start.
298 *
299 * We must hold the thread list lock before messing with another thread.
300 * In the general case we would also need to verify that the new thread was
301 * still in the thread list, but in our case the thread has not started
302 * executing user code and therefore has not had a chance to exit.
303 *
304 * We move it to kVmWait, and it then shifts itself to kRunning, which
305 * comes with a suspend-pending check. We do this after
306 */
307void ThreadList::SignalGo(Thread* child) {
308 Thread* self = Thread::Current();
309 CHECK(child != self);
310
311 {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700312 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700313
314 // We wait for the child to tell us that it's in the thread list.
315 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700316 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700317 }
318 }
319
320 // If we switch out of runnable and then back in, we know there's no pending suspend.
321 self->SetState(Thread::kVmWait);
322 self->SetState(Thread::kRunnable);
323
324 // Tell the child that it's safe: it will see any future suspend request.
325 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700326 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700327}
328
329void ThreadList::WaitForGo() {
330 Thread* self = Thread::Current();
331 DCHECK(Contains(self));
332
Elliott Hughes8d768a92011-09-14 16:35:25 -0700333 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700334
335 // Tell our parent that we're in the thread list.
336 self->SetState(Thread::kStarting);
Elliott Hughes5f791332011-09-15 17:45:30 -0700337 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700338
339 // Wait until our parent tells us there's no suspend still pending
340 // from before we were on the thread list.
341 while (self->GetState() != Thread::kVmWait) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700342 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700343 }
344
345 // Enter the runnable state. We know that any pending suspend will affect us now.
346 self->SetState(Thread::kRunnable);
347}
348
Elliott Hughes038a8062011-09-18 14:12:41 -0700349bool ThreadList::AllThreadsAreDaemons() {
350 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700351 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
352 // is null.
353 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700354 return false;
355 }
356 }
357 return true;
358}
359
360void ThreadList::WaitForNonDaemonThreadsToExit() {
361 MutexLock mu(thread_list_lock_);
362 while (!AllThreadsAreDaemons()) {
363 thread_exit_cond_.Wait(thread_list_lock_);
364 }
365}
366
367void ThreadList::SuspendAllDaemonThreads() {
368 MutexLock mu(thread_list_lock_);
369
370 // Tell all the daemons it's time to suspend. (At this point, we know
371 // all threads are daemons.)
372 {
373 MutexLock mu(thread_suspend_count_lock_);
374 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
375 Thread* thread = *it;
376 ++thread->suspend_count_;
377 }
378 }
379
380 // Give the threads a chance to suspend, complaining if they're slow.
381 bool have_complained = false;
382 for (int i = 0; i < 10; ++i) {
383 usleep(200 * 1000);
384 bool all_suspended = true;
385 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
386 Thread* thread = *it;
387 if (thread->GetState() == Thread::kRunnable) {
388 if (!have_complained) {
389 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
390 have_complained = true;
391 }
392 all_suspended = false;
393 }
394 }
395 if (all_suspended) {
396 return;
397 }
398 }
399}
400
Elliott Hughes8daa0922011-09-11 13:46:25 -0700401uint32_t ThreadList::AllocThreadId() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700402 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700403 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
404 if (!allocated_ids_[i]) {
405 allocated_ids_.set(i);
406 return i + 1; // Zero is reserved to mean "invalid".
407 }
408 }
409 LOG(FATAL) << "Out of internal thread ids";
410 return 0;
411}
412
413void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700414 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700415 --id; // Zero is reserved to mean "invalid".
416 DCHECK(allocated_ids_[id]) << id;
417 allocated_ids_.reset(id);
418}
419
420} // namespace art