blob: 9a34426cd2f583508acab9601d705aa3a7adfe4d [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 Hughes8daa0922011-09-11 13:46:25 -0700221void ThreadList::Register(Thread* thread) {
222 //LOG(INFO) << "ThreadList::Register() " << *thread;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700223 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700224 CHECK(!Contains(thread));
225 list_.push_back(thread);
226}
227
228void ThreadList::Unregister() {
229 Thread* self = Thread::Current();
230
Elliott Hughes93e74e82011-09-13 11:07:03 -0700231 //LOG(INFO) << "ThreadList::Unregister() " << *self;
Elliott Hughes8d768a92011-09-14 16:35:25 -0700232 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700233
234 // Remove this thread from the list.
235 CHECK(Contains(self));
236 list_.remove(self);
237
238 // Delete the Thread* and release the thin lock id.
239 uint32_t thin_lock_id = self->thin_lock_id_;
240 delete self;
241 ReleaseThreadId(thin_lock_id);
242
243 // Clear the TLS data, so that thread is recognizably detached.
244 // (It may wish to reattach later.)
Elliott Hughes8d768a92011-09-14 16:35:25 -0700245 CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
Elliott Hughes038a8062011-09-18 14:12:41 -0700246
247 // Signal that a thread just detached.
248 thread_exit_cond_.Signal();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700249}
250
251void ThreadList::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700252 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700253 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
254 (*it)->VisitRoots(visitor, arg);
255 }
256}
257
Elliott Hughes93e74e82011-09-13 11:07:03 -0700258/*
259 * Tell a new thread it's safe to start.
260 *
261 * We must hold the thread list lock before messing with another thread.
262 * In the general case we would also need to verify that the new thread was
263 * still in the thread list, but in our case the thread has not started
264 * executing user code and therefore has not had a chance to exit.
265 *
266 * We move it to kVmWait, and it then shifts itself to kRunning, which
267 * comes with a suspend-pending check. We do this after
268 */
269void ThreadList::SignalGo(Thread* child) {
270 Thread* self = Thread::Current();
271 CHECK(child != self);
272
273 {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700274 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700275
276 // We wait for the child to tell us that it's in the thread list.
277 while (child->GetState() != Thread::kStarting) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700278 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700279 }
280 }
281
282 // If we switch out of runnable and then back in, we know there's no pending suspend.
283 self->SetState(Thread::kVmWait);
284 self->SetState(Thread::kRunnable);
285
286 // Tell the child that it's safe: it will see any future suspend request.
287 child->SetState(Thread::kVmWait);
Elliott Hughes5f791332011-09-15 17:45:30 -0700288 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700289}
290
291void ThreadList::WaitForGo() {
292 Thread* self = Thread::Current();
293 DCHECK(Contains(self));
294
Elliott Hughes8d768a92011-09-14 16:35:25 -0700295 MutexLock mu(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700296
297 // Tell our parent that we're in the thread list.
298 self->SetState(Thread::kStarting);
Elliott Hughes5f791332011-09-15 17:45:30 -0700299 thread_start_cond_.Broadcast();
Elliott Hughes93e74e82011-09-13 11:07:03 -0700300
301 // Wait until our parent tells us there's no suspend still pending
302 // from before we were on the thread list.
303 while (self->GetState() != Thread::kVmWait) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700304 thread_start_cond_.Wait(thread_list_lock_);
Elliott Hughes93e74e82011-09-13 11:07:03 -0700305 }
306
307 // Enter the runnable state. We know that any pending suspend will affect us now.
308 self->SetState(Thread::kRunnable);
309}
310
Elliott Hughes038a8062011-09-18 14:12:41 -0700311bool ThreadList::AllThreadsAreDaemons() {
312 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
Ian Rogerscbba6ac2011-09-22 16:28:37 -0700313 // TODO: there's a race here with thread exit that's being worked around by checking if the peer
314 // is null.
315 if ((*it)->GetPeer() != NULL && !(*it)->IsDaemon()) {
Elliott Hughes038a8062011-09-18 14:12:41 -0700316 return false;
317 }
318 }
319 return true;
320}
321
322void ThreadList::WaitForNonDaemonThreadsToExit() {
323 MutexLock mu(thread_list_lock_);
324 while (!AllThreadsAreDaemons()) {
325 thread_exit_cond_.Wait(thread_list_lock_);
326 }
327}
328
329void ThreadList::SuspendAllDaemonThreads() {
330 MutexLock mu(thread_list_lock_);
331
332 // Tell all the daemons it's time to suspend. (At this point, we know
333 // all threads are daemons.)
334 {
335 MutexLock mu(thread_suspend_count_lock_);
336 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
337 Thread* thread = *it;
338 ++thread->suspend_count_;
339 }
340 }
341
342 // Give the threads a chance to suspend, complaining if they're slow.
343 bool have_complained = false;
344 for (int i = 0; i < 10; ++i) {
345 usleep(200 * 1000);
346 bool all_suspended = true;
347 for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
348 Thread* thread = *it;
349 if (thread->GetState() == Thread::kRunnable) {
350 if (!have_complained) {
351 LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
352 have_complained = true;
353 }
354 all_suspended = false;
355 }
356 }
357 if (all_suspended) {
358 return;
359 }
360 }
361}
362
Elliott Hughes8daa0922011-09-11 13:46:25 -0700363uint32_t ThreadList::AllocThreadId() {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700364 MutexLock mu(thread_list_lock_);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700365 for (size_t i = 0; i < allocated_ids_.size(); ++i) {
366 if (!allocated_ids_[i]) {
367 allocated_ids_.set(i);
368 return i + 1; // Zero is reserved to mean "invalid".
369 }
370 }
371 LOG(FATAL) << "Out of internal thread ids";
372 return 0;
373}
374
375void ThreadList::ReleaseThreadId(uint32_t id) {
Elliott Hughes8d768a92011-09-14 16:35:25 -0700376 thread_list_lock_.AssertHeld();
Elliott Hughes8daa0922011-09-11 13:46:25 -0700377 --id; // Zero is reserved to mean "invalid".
378 DCHECK(allocated_ids_[id]) << id;
379 allocated_ids_.reset(id);
380}
381
382} // namespace art