blob: f49c49da202800226d8d33aa1b3783aae744d37f [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 Hughes8d768a92011-09-14 16:35:25 -070023ThreadList::ThreadList()
24 : thread_list_lock_("thread list lock"),
Elliott Hughes5f791332011-09-15 17:45:30 -070025 thread_start_cond_("thread_start_cond_"),
Elliott Hughes038a8062011-09-18 14:12:41 -070026 thread_exit_cond_("thread_exit_cond_"),
Elliott Hughes5f791332011-09-15 17:45:30 -070027 thread_suspend_count_lock_("thread suspend count lock"),
28 thread_suspend_count_cond_("thread_suspend_count_cond_") {
Elliott Hughes8daa0922011-09-11 13:46:25 -070029}
30
31ThreadList::~ThreadList() {
Elliott Hughes038a8062011-09-18 14:12:41 -070032 // Detach the current thread if necessary.
Elliott Hughes8daa0922011-09-11 13:46:25 -070033 if (Contains(Thread::Current())) {
34 Runtime::Current()->DetachCurrentThread();
35 }
36
Elliott Hughes038a8062011-09-18 14:12:41 -070037 WaitForNonDaemonThreadsToExit();
38 SuspendAllDaemonThreads();
Elliott Hughes8daa0922011-09-11 13:46:25 -070039}
40
41bool ThreadList::Contains(Thread* thread) {
42 return find(list_.begin(), list_.end(), thread) != list_.end();
43}
44
45void ThreadList::Dump(std::ostream& os) {
Elliott Hughes8d768a92011-09-14 16:35:25 -070046 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -070047 os << "DALVIK THREADS (" << list_.size() << "):\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -070048 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
49 (*it)->Dump(os);
50 os << "\n";
51 }
52}
53
Elliott Hughes8d768a92011-09-14 16:35:25 -070054void ThreadList::FullSuspendCheck(Thread* thread) {
55 CHECK(thread != NULL);
56 CHECK_GE(thread->suspend_count_, 0);
57
58 MutexLock mu(thread_suspend_count_lock_);
59 if (thread->suspend_count_ == 0) {
60 return;
61 }
62
63 //LOG(INFO) << *thread << " self-suspending";
64 {
65 ScopedThreadStateChange tsc(thread, Thread::kSuspended);
66 while (thread->suspend_count_ != 0) {
67 /*
68 * Wait for wakeup signal, releasing lock. The act of releasing
69 * and re-acquiring the lock provides the memory barriers we
70 * need for correct behavior on SMP.
71 */
Elliott Hughes5f791332011-09-15 17:45:30 -070072 thread_suspend_count_cond_.Wait(thread_suspend_count_lock_);
Elliott Hughes8d768a92011-09-14 16:35:25 -070073 }
74 CHECK_EQ(thread->suspend_count_, 0);
75 }
76 //LOG(INFO) << *thread << " self-reviving";
77}
78
79void ThreadList::SuspendAll() {
80 Thread* self = Thread::Current();
81
82 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
83
84 //LOG(INFO) << *self << " SuspendAll starting...";
85
86 MutexLock mu(thread_list_lock_);
87
88 {
89 // Increment everybody's suspend count (except our own).
90 MutexLock mu(thread_suspend_count_lock_);
91 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
92 Thread* thread = *it;
93 if (thread != self) {
94 //LOG(INFO) << "requesting thread suspend: " << *thread;
95 ++thread->suspend_count_;
96 }
97 }
98 }
99
100 /*
101 * Wait for everybody in kRunnable state to stop. Other states
102 * indicate the code is either running natively or sleeping quietly.
103 * Any attempt to transition back to kRunnable will cause a check
104 * for suspension, so it should be impossible for anything to execute
105 * interpreted code or modify objects (assuming native code plays nicely).
106 *
107 * It's also okay if the thread transitions to a non-kRunnable state.
108 *
Elliott Hughes038a8062011-09-18 14:12:41 -0700109 * Note we released the thread_suspend_count_lock_ before getting here,
Elliott Hughes8d768a92011-09-14 16:35:25 -0700110 * so if another thread is fiddling with its suspend count (perhaps
111 * self-suspending for the debugger) it won't block while we're waiting
112 * in here.
113 */
114 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
115 Thread* thread = *it;
116 if (thread != self) {
117 thread->WaitUntilSuspended();
118 //LOG(INFO) << "thread suspended: " << *thread;
119 }
120 }
121
122 //LOG(INFO) << *self << " SuspendAll complete";
123}
124
Elliott Hughes01158d72011-09-19 19:47:10 -0700125void ThreadList::Suspend(Thread* thread) {
126 DCHECK(thread != Thread::Current());
127
128 // TODO: add another thread_suspend_lock_ to avoid GC/debugger races.
129
130 //LOG(INFO) << "Suspend(" << *thread << ") starting...";
131
132 MutexLock mu(thread_list_lock_);
133 if (!Contains(thread)) {
134 return;
135 }
136
137 {
138 MutexLock mu(thread_suspend_count_lock_);
139 ++thread->suspend_count_;
140 }
141
142 thread->WaitUntilSuspended();
143
144 //LOG(INFO) << "Suspend(" << *thread << ") complete";
145}
146
147
Elliott Hughes8d768a92011-09-14 16:35:25 -0700148void ThreadList::ResumeAll() {
149 Thread* self = Thread::Current();
150
151 //LOG(INFO) << *self << " ResumeAll starting";
152
153 // Decrement the suspend counts for all threads. No need for atomic
154 // writes, since nobody should be moving until we decrement the count.
155 // We do need to hold the thread list because of JNI attaches.
156 {
157 MutexLock mu1(thread_list_lock_);
158 MutexLock mu2(thread_suspend_count_lock_);
159 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
160 Thread* thread = *it;
161 if (thread != self) {
162 if (thread->suspend_count_ > 0) {
163 --thread->suspend_count_;
164 } else {
165 LOG(WARNING) << *thread << " suspend count already zero";
166 }
167 }
168 }
169 }
170
171 // Broadcast a notification to all suspended threads, some or all of
172 // which may choose to wake up. No need to wait for them.
173 {
174 //LOG(INFO) << *self << " ResumeAll waking others";
175 MutexLock mu(thread_suspend_count_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700176 thread_suspend_count_cond_.Broadcast();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700177 }
178
179 //LOG(INFO) << *self << " ResumeAll complete";
180}
181
Elliott Hughes01158d72011-09-19 19:47:10 -0700182void ThreadList::Resume(Thread* thread) {
183 DCHECK(thread != Thread::Current());
184
185 //LOG(INFO) << "Resume(" << *thread << ") starting...";
186
187 {
188 MutexLock mu1(thread_list_lock_);
189 MutexLock mu2(thread_suspend_count_lock_);
190 if (!Contains(thread)) {
191 return;
192 }
193 if (thread->suspend_count_ > 0) {
194 --thread->suspend_count_;
195 } else {
196 LOG(WARNING) << *thread << " suspend count already zero";
197 }
198 }
199
200 {
201 //LOG(INFO) << "Resume(" << *thread << ") waking others";
202 MutexLock mu(thread_suspend_count_lock_);
203 thread_suspend_count_cond_.Broadcast();
204 }
205
206 //LOG(INFO) << "Resume(" << *thread << ") complete";
207}
208
209void ThreadList::RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg) {
210 DCHECK(thread != NULL);
211 Thread* self = Thread::Current();
212 if (thread != self) {
213 Suspend(thread);
214 }
215 callback(arg);
216 if (thread != self) {
217 Resume(thread);
218 }
219}
220
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700221void ThreadList::Register() {
222 Thread* self = Thread::Current();
223
224 //LOG(INFO) << "ThreadList::Register() " << *self;
225 self->Dump(std::cerr);
226
Elliott Hughes8d768a92011-09-14 16:35:25 -0700227 MutexLock mu(thread_list_lock_);
Elliott Hughes7a3aeb42011-09-25 17:39:47 -0700228 CHECK(!Contains(self));
229 list_.push_back(self);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700230}
231
232void ThreadList::Unregister() {
233 Thread* self = Thread::Current();
234
Elliott Hughes93e74e82011-09-13 11:07:03 -0700235 //LOG(INFO) << "ThreadList::Unregister() " << *self;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700236 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700237
238 // Remove this thread from the list.
239 CHECK(Contains(self));
240 list_.remove(self);
241
242 // Delete the Thread* and release the thin lock id.
243 uint32_t thin_lock_id = self->thin_lock_id_;
244 delete self;
245 ReleaseThreadId(thin_lock_id);
246
247 // Clear the TLS data, so that thread is recognizably detached.
248 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700249 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700250
251 // Signal that a thread just detached.
252 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700253}
254
255void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700256 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700257 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
258 (*it)->VisitRoots(visitor, arg);
259 }
260}
261
Elliott Hughes93e74e82011-09-13 11:07:03 -0700262/*
263 * Tell a new thread it's safe to start.
264 *
265 * We must hold the thread list lock before messing with another thread.
266 * In the general case we would also need to verify that the new thread was
267 * still in the thread list, but in our case the thread has not started
268 * executing user code and therefore has not had a chance to exit.
269 *
270 * We move it to kVmWait, and it then shifts itself to kRunning, which
271 * comes with a suspend-pending check. We do this after
272 */
273void ThreadList::SignalGo(Thread* child) {
274 Thread* self = Thread::Current();
275 CHECK(child != self);
276
277 {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700278 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700279
280 // We wait for the child to tell us that it's in the thread list.
281 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700282 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700283 }
284 }
285
286 // If we switch out of runnable and then back in, we know there's no pending suspend.
287 self->SetState(Thread::kVmWait);
288 self->SetState(Thread::kRunnable);
289
290 // Tell the child that it's safe: it will see any future suspend request.
291 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700292 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700293}
294
295void ThreadList::WaitForGo() {
296 Thread* self = Thread::Current();
297 DCHECK(Contains(self));
298
Elliott Hughes8d768a92011-09-14 16:35:25 -0700299 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700300
301 // Tell our parent that we're in the thread list.
302 self->SetState(Thread::kStarting);
Elliott Hughes5f791332011-09-15 17:45:30 -0700303 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700304
305 // Wait until our parent tells us there's no suspend still pending
306 // from before we were on the thread list.
307 while (self->GetState() != Thread::kVmWait) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700308 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700309 }
310
311 // Enter the runnable state. We know that any pending suspend will affect us now.
312 self->SetState(Thread::kRunnable);
313}
314
Elliott Hughes038a8062011-09-18 14:12:41 -0700315bool ThreadList::AllThreadsAreDaemons() {
316 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700317 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
318 // is null.
319 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700320 return false;
321 }
322 }
323 return true;
324}
325
326void ThreadList::WaitForNonDaemonThreadsToExit() {
327 MutexLock mu(thread_list_lock_);
328 while (!AllThreadsAreDaemons()) {
329 thread_exit_cond_.Wait(thread_list_lock_);
330 }
331}
332
333void ThreadList::SuspendAllDaemonThreads() {
334 MutexLock mu(thread_list_lock_);
335
336 // Tell all the daemons it's time to suspend. (At this point, we know
337 // all threads are daemons.)
338 {
339 MutexLock mu(thread_suspend_count_lock_);
340 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
341 Thread* thread = *it;
342 ++thread->suspend_count_;
343 }
344 }
345
346 // Give the threads a chance to suspend, complaining if they're slow.
347 bool have_complained = false;
348 for (int i = 0; i < 10; ++i) {
349 usleep(200 * 1000);
350 bool all_suspended = true;
351 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
352 Thread* thread = *it;
353 if (thread->GetState() == Thread::kRunnable) {
354 if (!have_complained) {
355 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
356 have_complained = true;
357 }
358 all_suspended = false;
359 }
360 }
361 if (all_suspended) {
362 return;
363 }
364 }
365}
366
Elliott Hughes8daa0922011-09-11 13:46:25 -0700367uint32_t ThreadList::AllocThreadId() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700368 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700369 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
370 if (!allocated_ids_[i]) {
371 allocated_ids_.set(i);
372 return i + 1; // Zero is reserved to mean "invalid".
373 }
374 }
375 LOG(FATAL) << "Out of internal thread ids";
376 return 0;
377}
378
379void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700380 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700381 --id; // Zero is reserved to mean "invalid".
382 DCHECK(allocated_ids_[id]) << id;
383 allocated_ids_.reset(id);
384}
385
386} // namespace art