thread_list.cc revision c96657c4243e04888d5948fa023d78cc9213800b
1/*
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
19#define ATRACE_TAG ATRACE_TAG_DALVIK
20
21#include <cutils/trace.h>
22#include <dirent.h>
23#include <ScopedLocalRef.h>
24#include <ScopedUtfChars.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28#include "base/mutex.h"
29#include "base/mutex-inl.h"
30#include "base/timing_logger.h"
31#include "debugger.h"
32#include "jni_internal.h"
33#include "lock_word.h"
34#include "monitor.h"
35#include "scoped_thread_state_change.h"
36#include "thread.h"
37#include "utils.h"
38#include "well_known_classes.h"
39
40namespace art {
41
42static constexpr uint64_t kLongThreadSuspendThreshold = MsToNs(5);
43
44ThreadList::ThreadList()
45    : suspend_all_count_(0), debug_suspend_all_count_(0),
46      thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) {
47  CHECK(Monitor::IsValidLockWord(LockWord::FromThinLockId(kMaxThreadId, 1)));
48}
49
50ThreadList::~ThreadList() {
51  // Detach the current thread if necessary. If we failed to start, there might not be any threads.
52  // We need to detach the current thread here in case there's another thread waiting to join with
53  // us.
54  if (Contains(Thread::Current())) {
55    Runtime::Current()->DetachCurrentThread();
56  }
57
58  WaitForOtherNonDaemonThreadsToExit();
59  // TODO: there's an unaddressed race here where a thread may attach during shutdown, see
60  //       Thread::Init.
61  SuspendAllDaemonThreads();
62}
63
64bool ThreadList::Contains(Thread* thread) {
65  return find(list_.begin(), list_.end(), thread) != list_.end();
66}
67
68bool ThreadList::Contains(pid_t tid) {
69  for (const auto& thread : list_) {
70    if (thread->GetTid() == tid) {
71      return true;
72    }
73  }
74  return false;
75}
76
77pid_t ThreadList::GetLockOwner() {
78  return Locks::thread_list_lock_->GetExclusiveOwnerTid();
79}
80
81void ThreadList::DumpNativeStacks(std::ostream& os) {
82  MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
83  for (const auto& thread : list_) {
84    os << "DUMPING THREAD " << thread->GetTid() << "\n";
85    DumpNativeStack(os, thread->GetTid(), "\t");
86    os << "\n";
87  }
88}
89
90void ThreadList::DumpForSigQuit(std::ostream& os) {
91  {
92    MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
93    DumpLocked(os);
94  }
95  DumpUnattachedThreads(os);
96}
97
98static void DumpUnattachedThread(std::ostream& os, pid_t tid) NO_THREAD_SAFETY_ANALYSIS {
99  // TODO: No thread safety analysis as DumpState with a NULL thread won't access fields, should
100  // refactor DumpState to avoid skipping analysis.
101  Thread::DumpState(os, NULL, tid);
102  DumpKernelStack(os, tid, "  kernel: ", false);
103  // TODO: Reenable this when the native code in system_server can handle it.
104  // Currently "adb shell kill -3 `pid system_server`" will cause it to exit.
105  if (false) {
106    DumpNativeStack(os, tid, "  native: ");
107  }
108  os << "\n";
109}
110
111void ThreadList::DumpUnattachedThreads(std::ostream& os) {
112  DIR* d = opendir("/proc/self/task");
113  if (!d) {
114    return;
115  }
116
117  Thread* self = Thread::Current();
118  dirent* e;
119  while ((e = readdir(d)) != NULL) {
120    char* end;
121    pid_t tid = strtol(e->d_name, &end, 10);
122    if (!*end) {
123      bool contains;
124      {
125        MutexLock mu(self, *Locks::thread_list_lock_);
126        contains = Contains(tid);
127      }
128      if (!contains) {
129        DumpUnattachedThread(os, tid);
130      }
131    }
132  }
133  closedir(d);
134}
135
136void ThreadList::DumpLocked(std::ostream& os) {
137  os << "DALVIK THREADS (" << list_.size() << "):\n";
138  for (const auto& thread : list_) {
139    thread->Dump(os);
140    os << "\n";
141  }
142}
143
144void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
145  MutexLock mu(self, *Locks::thread_list_lock_);
146  MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
147  for (const auto& thread : list_) {
148    if (thread != ignore1 && thread != ignore2) {
149      CHECK(thread->IsSuspended())
150            << "\nUnsuspended thread: <<" << *thread << "\n"
151            << "self: <<" << *Thread::Current();
152    }
153  }
154}
155
156#if HAVE_TIMED_RWLOCK
157// Attempt to rectify locks so that we dump thread list with required locks before exiting.
158static void UnsafeLogFatalForThreadSuspendAllTimeout() NO_THREAD_SAFETY_ANALYSIS __attribute__((noreturn));
159static void UnsafeLogFatalForThreadSuspendAllTimeout() {
160  Runtime* runtime = Runtime::Current();
161  std::ostringstream ss;
162  ss << "Thread suspend timeout\n";
163  runtime->GetThreadList()->DumpLocked(ss);
164  LOG(FATAL) << ss.str();
165  exit(0);
166}
167#endif
168
169// Unlike suspending all threads where we can wait to acquire the mutator_lock_, suspending an
170// individual thread requires polling. delay_us is the requested sleep and total_delay_us
171// accumulates the total time spent sleeping for timeouts. The first sleep is just a yield,
172// subsequently sleeps increase delay_us from 1ms to 500ms by doubling.
173static void ThreadSuspendSleep(Thread* self, useconds_t* delay_us, useconds_t* total_delay_us) {
174  useconds_t new_delay_us = (*delay_us) * 2;
175  CHECK_GE(new_delay_us, *delay_us);
176  if (new_delay_us < 500000) {  // Don't allow sleeping to be more than 0.5s.
177    *delay_us = new_delay_us;
178  }
179  if (*delay_us == 0) {
180    sched_yield();
181    // Default to 1 milliseconds (note that this gets multiplied by 2 before the first sleep).
182    *delay_us = 500;
183  } else {
184    usleep(*delay_us);
185    *total_delay_us += *delay_us;
186  }
187}
188
189size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) {
190  Thread* self = Thread::Current();
191  Locks::mutator_lock_->AssertNotExclusiveHeld(self);
192  Locks::thread_list_lock_->AssertNotHeld(self);
193  Locks::thread_suspend_count_lock_->AssertNotHeld(self);
194  if (kDebugLocking) {
195    CHECK_NE(self->GetState(), kRunnable);
196  }
197
198  std::vector<Thread*> suspended_count_modified_threads;
199  size_t count = 0;
200  {
201    // Call a checkpoint function for each thread, threads which are suspend get their checkpoint
202    // manually called.
203    MutexLock mu(self, *Locks::thread_list_lock_);
204    MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
205    for (const auto& thread : list_) {
206      if (thread != self) {
207        while (true) {
208          if (thread->RequestCheckpoint(checkpoint_function)) {
209            // This thread will run its checkpoint some time in the near future.
210            count++;
211            break;
212          } else {
213            // We are probably suspended, try to make sure that we stay suspended.
214            // The thread switched back to runnable.
215            if (thread->GetState() == kRunnable) {
216              // Spurious fail, try again.
217              continue;
218            }
219            thread->ModifySuspendCount(self, +1, false);
220            suspended_count_modified_threads.push_back(thread);
221            break;
222          }
223        }
224      }
225    }
226  }
227
228  // Run the checkpoint on ourself while we wait for threads to suspend.
229  checkpoint_function->Run(self);
230
231  // Run the checkpoint on the suspended threads.
232  for (const auto& thread : suspended_count_modified_threads) {
233    if (!thread->IsSuspended()) {
234      // Wait until the thread is suspended.
235      useconds_t total_delay_us = 0;
236      do {
237        useconds_t delay_us = 100;
238        ThreadSuspendSleep(self, &delay_us, &total_delay_us);
239      } while (!thread->IsSuspended());
240      // Shouldn't need to wait for longer than 1000 microseconds.
241      constexpr useconds_t kLongWaitThresholdUS = 1000;
242      if (UNLIKELY(total_delay_us > kLongWaitThresholdUS)) {
243        LOG(WARNING) << "Waited " << total_delay_us << " us for thread suspend!";
244      }
245    }
246    // We know for sure that the thread is suspended at this point.
247    checkpoint_function->Run(thread);
248    {
249      MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
250      thread->ModifySuspendCount(self, -1, false);
251    }
252  }
253
254  {
255    // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their
256    // suspend count. Now the suspend_count_ is lowered so we must do the broadcast.
257    MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
258    Thread::resume_cond_->Broadcast(self);
259  }
260
261  // Add one for self.
262  return count + suspended_count_modified_threads.size() + 1;
263}
264
265// Request that a checkpoint function be run on all active (non-suspended)
266// threads.  Returns the number of successful requests.
267size_t ThreadList::RunCheckpointOnRunnableThreads(Closure* checkpoint_function) {
268  Thread* self = Thread::Current();
269  if (kIsDebugBuild) {
270    Locks::mutator_lock_->AssertNotExclusiveHeld(self);
271    Locks::thread_list_lock_->AssertNotHeld(self);
272    Locks::thread_suspend_count_lock_->AssertNotHeld(self);
273    CHECK_NE(self->GetState(), kRunnable);
274  }
275
276  size_t count = 0;
277  {
278    // Call a checkpoint function for each non-suspended thread.
279    MutexLock mu(self, *Locks::thread_list_lock_);
280    MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
281    for (const auto& thread : list_) {
282      if (thread != self) {
283        if (thread->RequestCheckpoint(checkpoint_function)) {
284          // This thread will run its checkpoint some time in the near future.
285          count++;
286        }
287      }
288    }
289  }
290
291  // Return the number of threads that will run the checkpoint function.
292  return count;
293}
294
295void ThreadList::SuspendAll() {
296  Thread* self = Thread::Current();
297
298  if (self != nullptr) {
299    VLOG(threads) << *self << " SuspendAll starting...";
300  } else {
301    VLOG(threads) << "Thread[null] SuspendAll starting...";
302  }
303  ATRACE_BEGIN("Suspending mutator threads");
304  uint64_t start_time = NanoTime();
305
306  Locks::mutator_lock_->AssertNotHeld(self);
307  Locks::thread_list_lock_->AssertNotHeld(self);
308  Locks::thread_suspend_count_lock_->AssertNotHeld(self);
309  if (kDebugLocking && self != nullptr) {
310    CHECK_NE(self->GetState(), kRunnable);
311  }
312  {
313    MutexLock mu(self, *Locks::thread_list_lock_);
314    MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
315    // Update global suspend all state for attaching threads.
316    ++suspend_all_count_;
317    // Increment everybody's suspend count (except our own).
318    for (const auto& thread : list_) {
319      if (thread == self) {
320        continue;
321      }
322      VLOG(threads) << "requesting thread suspend: " << *thread;
323      thread->ModifySuspendCount(self, +1, false);
324    }
325  }
326
327  // Block on the mutator lock until all Runnable threads release their share of access.
328#if HAVE_TIMED_RWLOCK
329  // Timeout if we wait more than 30 seconds.
330  if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
331    UnsafeLogFatalForThreadSuspendAllTimeout();
332  }
333#else
334  Locks::mutator_lock_->ExclusiveLock(self);
335#endif
336
337  uint64_t end_time = NanoTime();
338  if (end_time - start_time > kLongThreadSuspendThreshold) {
339    LOG(WARNING) << "Suspending all threads took: " << PrettyDuration(end_time - start_time);
340  }
341
342  if (kDebugLocking) {
343    // Debug check that all threads are suspended.
344    AssertThreadsAreSuspended(self, self);
345  }
346
347  ATRACE_END();
348  ATRACE_BEGIN("Mutator threads suspended");
349
350  if (self != nullptr) {
351    VLOG(threads) << *self << " SuspendAll complete";
352  } else {
353    VLOG(threads) << "Thread[null] SuspendAll complete";
354  }
355}
356
357void ThreadList::ResumeAll() {
358  Thread* self = Thread::Current();
359
360  if (self != nullptr) {
361    VLOG(threads) << *self << " ResumeAll starting";
362  } else {
363    VLOG(threads) << "Thread[null] ResumeAll starting";
364  }
365
366  ATRACE_END();
367  ATRACE_BEGIN("Resuming mutator threads");
368
369  if (kDebugLocking) {
370    // Debug check that all threads are suspended.
371    AssertThreadsAreSuspended(self, self);
372  }
373
374  Locks::mutator_lock_->ExclusiveUnlock(self);
375  {
376    MutexLock mu(self, *Locks::thread_list_lock_);
377    MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
378    // Update global suspend all state for attaching threads.
379    --suspend_all_count_;
380    // Decrement the suspend counts for all threads.
381    for (const auto& thread : list_) {
382      if (thread == self) {
383        continue;
384      }
385      thread->ModifySuspendCount(self, -1, false);
386    }
387
388    // Broadcast a notification to all suspended threads, some or all of
389    // which may choose to wake up.  No need to wait for them.
390    if (self != nullptr) {
391      VLOG(threads) << *self << " ResumeAll waking others";
392    } else {
393      VLOG(threads) << "Thread[null] ResumeAll waking others";
394    }
395    Thread::resume_cond_->Broadcast(self);
396  }
397  ATRACE_END();
398
399  if (self != nullptr) {
400    VLOG(threads) << *self << " ResumeAll complete";
401  } else {
402    VLOG(threads) << "Thread[null] ResumeAll complete";
403  }
404}
405
406void ThreadList::Resume(Thread* thread, bool for_debugger) {
407  Thread* self = Thread::Current();
408  DCHECK_NE(thread, self);
409  VLOG(threads) << "Resume(" << *thread << ") starting..." << (for_debugger ? " (debugger)" : "");
410
411  {
412    // To check Contains.
413    MutexLock mu(self, *Locks::thread_list_lock_);
414    // To check IsSuspended.
415    MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
416    DCHECK(thread->IsSuspended());
417    if (!Contains(thread)) {
418      return;
419    }
420    thread->ModifySuspendCount(self, -1, for_debugger);
421  }
422
423  {
424    VLOG(threads) << "Resume(" << *thread << ") waking others";
425    MutexLock mu(self, *Locks::thread_suspend_count_lock_);
426    Thread::resume_cond_->Broadcast(self);
427  }
428
429  VLOG(threads) << "Resume(" << *thread << ") complete";
430}
431
432static void ThreadSuspendByPeerWarning(Thread* self, int level, const char* message, jobject peer) {
433  JNIEnvExt* env = self->GetJniEnv();
434  ScopedLocalRef<jstring>
435      scoped_name_string(env, (jstring)env->GetObjectField(peer,
436                                                          WellKnownClasses::java_lang_Thread_name));
437  ScopedUtfChars scoped_name_chars(env, scoped_name_string.get());
438  if (scoped_name_chars.c_str() == NULL) {
439      LOG(level) << message << ": " << peer;
440      env->ExceptionClear();
441  } else {
442      LOG(level) << message << ": " << peer << ":" << scoped_name_chars.c_str();
443  }
444}
445
446Thread* ThreadList::SuspendThreadByPeer(jobject peer, bool request_suspension,
447                                        bool debug_suspension, bool* timed_out) {
448  static const useconds_t kTimeoutUs = 30 * 1000000;  // 30s.
449  useconds_t total_delay_us = 0;
450  useconds_t delay_us = 0;
451  bool did_suspend_request = false;
452  *timed_out = false;
453  Thread* self = Thread::Current();
454  while (true) {
455    Thread* thread;
456    {
457      // Note: this will transition to runnable and potentially suspend. We ensure only one thread
458      // is requesting another suspend, to avoid deadlock, by requiring this function be called
459      // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
460      // than request thread suspension, to avoid potential cycles in threads requesting each other
461      // suspend.
462      ScopedObjectAccess soa(self);
463      MutexLock mu(self, *Locks::thread_list_lock_);
464      thread = Thread::FromManagedThread(soa, peer);
465      if (thread == NULL) {
466        ThreadSuspendByPeerWarning(self, WARNING, "No such thread for suspend", peer);
467        return NULL;
468      }
469      {
470        MutexLock mu(self, *Locks::thread_suspend_count_lock_);
471        if (request_suspension) {
472          thread->ModifySuspendCount(self, +1, debug_suspension);
473          request_suspension = false;
474          did_suspend_request = true;
475        } else {
476          // If the caller isn't requesting suspension, a suspension should have already occurred.
477          CHECK_GT(thread->GetSuspendCount(), 0);
478        }
479        // IsSuspended on the current thread will fail as the current thread is changed into
480        // Runnable above. As the suspend count is now raised if this is the current thread
481        // it will self suspend on transition to Runnable, making it hard to work with. It's simpler
482        // to just explicitly handle the current thread in the callers to this code.
483        CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
484        // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
485        // count, or else we've waited and it has self suspended) or is the current thread, we're
486        // done.
487        if (thread->IsSuspended()) {
488          return thread;
489        }
490        if (total_delay_us >= kTimeoutUs) {
491          ThreadSuspendByPeerWarning(self, FATAL, "Thread suspension timed out", peer);
492          if (did_suspend_request) {
493            thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
494          }
495          *timed_out = true;
496          return NULL;
497        }
498      }
499      // Release locks and come out of runnable state.
500    }
501    ThreadSuspendSleep(self, &delay_us, &total_delay_us);
502  }
503}
504
505static void ThreadSuspendByThreadIdWarning(int level, const char* message, uint32_t thread_id) {
506  LOG(level) << StringPrintf("%s: %d", message, thread_id);
507}
508
509Thread* ThreadList::SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension,
510                                            bool* timed_out) {
511  static const useconds_t kTimeoutUs = 30 * 1000000;  // 30s.
512  useconds_t total_delay_us = 0;
513  useconds_t delay_us = 0;
514  *timed_out = false;
515  Thread* suspended_thread = nullptr;
516  Thread* self = Thread::Current();
517  CHECK_NE(thread_id, kInvalidThreadId);
518  while (true) {
519    {
520      // Note: this will transition to runnable and potentially suspend. We ensure only one thread
521      // is requesting another suspend, to avoid deadlock, by requiring this function be called
522      // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
523      // than request thread suspension, to avoid potential cycles in threads requesting each other
524      // suspend.
525      ScopedObjectAccess soa(self);
526      MutexLock mu(self, *Locks::thread_list_lock_);
527      Thread* thread = nullptr;
528      for (const auto& it : list_) {
529        if (it->GetThreadId() == thread_id) {
530          thread = it;
531          break;
532        }
533      }
534      if (thread == nullptr) {
535        CHECK(suspended_thread == nullptr) << "Suspended thread " << suspended_thread
536            << " no longer in thread list";
537        // There's a race in inflating a lock and the owner giving up ownership and then dying.
538        ThreadSuspendByThreadIdWarning(WARNING, "No such thread id for suspend", thread_id);
539        return NULL;
540      }
541      {
542        MutexLock mu(self, *Locks::thread_suspend_count_lock_);
543        if (suspended_thread == nullptr) {
544          thread->ModifySuspendCount(self, +1, debug_suspension);
545          suspended_thread = thread;
546        } else {
547          CHECK_EQ(suspended_thread, thread);
548          // If the caller isn't requesting suspension, a suspension should have already occurred.
549          CHECK_GT(thread->GetSuspendCount(), 0);
550        }
551        // IsSuspended on the current thread will fail as the current thread is changed into
552        // Runnable above. As the suspend count is now raised if this is the current thread
553        // it will self suspend on transition to Runnable, making it hard to work with. It's simpler
554        // to just explicitly handle the current thread in the callers to this code.
555        CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
556        // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
557        // count, or else we've waited and it has self suspended) or is the current thread, we're
558        // done.
559        if (thread->IsSuspended()) {
560          return thread;
561        }
562        if (total_delay_us >= kTimeoutUs) {
563          ThreadSuspendByThreadIdWarning(WARNING, "Thread suspension timed out", thread_id);
564          if (suspended_thread != nullptr) {
565            thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
566          }
567          *timed_out = true;
568          return NULL;
569        }
570      }
571      // Release locks and come out of runnable state.
572    }
573    ThreadSuspendSleep(self, &delay_us, &total_delay_us);
574  }
575}
576
577Thread* ThreadList::FindThreadByThreadId(uint32_t thin_lock_id) {
578  Thread* self = Thread::Current();
579  MutexLock mu(self, *Locks::thread_list_lock_);
580  for (const auto& thread : list_) {
581    if (thread->GetThreadId() == thin_lock_id) {
582      CHECK(thread == self || thread->IsSuspended());
583      return thread;
584    }
585  }
586  return NULL;
587}
588
589void ThreadList::SuspendAllForDebugger() {
590  Thread* self = Thread::Current();
591  Thread* debug_thread = Dbg::GetDebugThread();
592
593  VLOG(threads) << *self << " SuspendAllForDebugger starting...";
594
595  {
596    MutexLock mu(self, *Locks::thread_list_lock_);
597    {
598      MutexLock mu(self, *Locks::thread_suspend_count_lock_);
599      // Update global suspend all state for attaching threads.
600      ++suspend_all_count_;
601      ++debug_suspend_all_count_;
602      // Increment everybody's suspend count (except our own).
603      for (const auto& thread : list_) {
604        if (thread == self || thread == debug_thread) {
605          continue;
606        }
607        VLOG(threads) << "requesting thread suspend: " << *thread;
608        thread->ModifySuspendCount(self, +1, true);
609      }
610    }
611  }
612
613  // Block on the mutator lock until all Runnable threads release their share of access then
614  // immediately unlock again.
615#if HAVE_TIMED_RWLOCK
616  // Timeout if we wait more than 30 seconds.
617  if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
618    UnsafeLogFatalForThreadSuspendAllTimeout();
619  } else {
620    Locks::mutator_lock_->ExclusiveUnlock(self);
621  }
622#else
623  Locks::mutator_lock_->ExclusiveLock(self);
624  Locks::mutator_lock_->ExclusiveUnlock(self);
625#endif
626  AssertThreadsAreSuspended(self, self, debug_thread);
627
628  VLOG(threads) << *self << " SuspendAll complete";
629}
630
631void ThreadList::SuspendSelfForDebugger() {
632  Thread* self = Thread::Current();
633
634  // The debugger thread must not suspend itself due to debugger activity!
635  Thread* debug_thread = Dbg::GetDebugThread();
636  CHECK(debug_thread != NULL);
637  CHECK(self != debug_thread);
638  CHECK_NE(self->GetState(), kRunnable);
639  Locks::mutator_lock_->AssertNotHeld(self);
640
641  {
642    // Collisions with other suspends aren't really interesting. We want
643    // to ensure that we're the only one fiddling with the suspend count
644    // though.
645    MutexLock mu(self, *Locks::thread_suspend_count_lock_);
646    self->ModifySuspendCount(self, +1, true);
647    CHECK_GT(self->GetSuspendCount(), 0);
648  }
649
650  VLOG(threads) << *self << " self-suspending (debugger)";
651
652  // Tell JDWP we've completed invocation and are ready to suspend.
653  DebugInvokeReq* pReq = self->GetInvokeReq();
654  DCHECK(pReq != NULL);
655  if (pReq->invoke_needed) {
656    // Clear this before signaling.
657    pReq->Clear();
658
659    VLOG(jdwp) << "invoke complete, signaling";
660    MutexLock mu(self, pReq->lock);
661    pReq->cond.Signal(self);
662  }
663
664  // Tell JDWP that we've completed suspension. The JDWP thread can't
665  // tell us to resume before we're fully asleep because we hold the
666  // suspend count lock.
667  Dbg::ClearWaitForEventThread();
668
669  {
670    MutexLock mu(self, *Locks::thread_suspend_count_lock_);
671    while (self->GetSuspendCount() != 0) {
672      Thread::resume_cond_->Wait(self);
673      if (self->GetSuspendCount() != 0) {
674        // The condition was signaled but we're still suspended. This
675        // can happen if the debugger lets go while a SIGQUIT thread
676        // dump event is pending (assuming SignalCatcher was resumed for
677        // just long enough to try to grab the thread-suspend lock).
678        LOG(WARNING) << *self << " still suspended after undo "
679                   << "(suspend count=" << self->GetSuspendCount() << ")";
680      }
681    }
682    CHECK_EQ(self->GetSuspendCount(), 0);
683  }
684
685  VLOG(threads) << *self << " self-reviving (debugger)";
686}
687
688void ThreadList::UndoDebuggerSuspensions() {
689  Thread* self = Thread::Current();
690
691  VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
692
693  {
694    MutexLock mu(self, *Locks::thread_list_lock_);
695    MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
696    // Update global suspend all state for attaching threads.
697    suspend_all_count_ -= debug_suspend_all_count_;
698    debug_suspend_all_count_ = 0;
699    // Update running threads.
700    for (const auto& thread : list_) {
701      if (thread == self || thread->GetDebugSuspendCount() == 0) {
702        continue;
703      }
704      thread->ModifySuspendCount(self, -thread->GetDebugSuspendCount(), true);
705    }
706  }
707
708  {
709    MutexLock mu(self, *Locks::thread_suspend_count_lock_);
710    Thread::resume_cond_->Broadcast(self);
711  }
712
713  VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
714}
715
716void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
717  Thread* self = Thread::Current();
718  Locks::mutator_lock_->AssertNotHeld(self);
719  bool all_threads_are_daemons;
720  do {
721    {
722      // No more threads can be born after we start to shutdown.
723      MutexLock mu(self, *Locks::runtime_shutdown_lock_);
724      CHECK(Runtime::Current()->IsShuttingDownLocked());
725      CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U);
726    }
727    all_threads_are_daemons = true;
728    MutexLock mu(self, *Locks::thread_list_lock_);
729    for (const auto& thread : list_) {
730      if (thread != self && !thread->IsDaemon()) {
731        all_threads_are_daemons = false;
732        break;
733      }
734    }
735    if (!all_threads_are_daemons) {
736      // Wait for another thread to exit before re-checking.
737      thread_exit_cond_.Wait(self);
738    }
739  } while (!all_threads_are_daemons);
740}
741
742void ThreadList::SuspendAllDaemonThreads() {
743  Thread* self = Thread::Current();
744  MutexLock mu(self, *Locks::thread_list_lock_);
745  {  // Tell all the daemons it's time to suspend.
746    MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
747    for (const auto& thread : list_) {
748      // This is only run after all non-daemon threads have exited, so the remainder should all be
749      // daemons.
750      CHECK(thread->IsDaemon()) << *thread;
751      if (thread != self) {
752        thread->ModifySuspendCount(self, +1, false);
753      }
754    }
755  }
756  // Give the threads a chance to suspend, complaining if they're slow.
757  bool have_complained = false;
758  for (int i = 0; i < 10; ++i) {
759    usleep(200 * 1000);
760    bool all_suspended = true;
761    for (const auto& thread : list_) {
762      if (thread != self && thread->GetState() == kRunnable) {
763        if (!have_complained) {
764          LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
765          have_complained = true;
766        }
767        all_suspended = false;
768      }
769    }
770    if (all_suspended) {
771      return;
772    }
773  }
774  LOG(ERROR) << "suspend all daemons failed";
775}
776void ThreadList::Register(Thread* self) {
777  DCHECK_EQ(self, Thread::Current());
778
779  if (VLOG_IS_ON(threads)) {
780    std::ostringstream oss;
781    self->ShortDump(oss);  // We don't hold the mutator_lock_ yet and so cannot call Dump.
782    LOG(INFO) << "ThreadList::Register() " << *self  << "\n" << oss.str();
783  }
784
785  // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
786  // SuspendAll requests.
787  MutexLock mu(self, *Locks::thread_list_lock_);
788  MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
789  CHECK_GE(suspend_all_count_, debug_suspend_all_count_);
790  // Modify suspend count in increments of 1 to maintain invariants in ModifySuspendCount. While
791  // this isn't particularly efficient the suspend counts are most commonly 0 or 1.
792  for (int delta = debug_suspend_all_count_; delta > 0; delta--) {
793    self->ModifySuspendCount(self, +1, true);
794  }
795  for (int delta = suspend_all_count_ - debug_suspend_all_count_; delta > 0; delta--) {
796    self->ModifySuspendCount(self, +1, false);
797  }
798  CHECK(!Contains(self));
799  list_.push_back(self);
800}
801
802void ThreadList::Unregister(Thread* self) {
803  DCHECK_EQ(self, Thread::Current());
804
805  VLOG(threads) << "ThreadList::Unregister() " << *self;
806
807  // Any time-consuming destruction, plus anything that can call back into managed code or
808  // suspend and so on, must happen at this point, and not in ~Thread.
809  self->Destroy();
810
811  uint32_t thin_lock_id = self->GetThreadId();
812  while (self != nullptr) {
813    // Remove and delete the Thread* while holding the thread_list_lock_ and
814    // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended.
815    // Note: deliberately not using MutexLock that could hold a stale self pointer.
816    Locks::thread_list_lock_->ExclusiveLock(self);
817    CHECK(Contains(self));
818    // Note: we don't take the thread_suspend_count_lock_ here as to be suspending a thread other
819    // than yourself you need to hold the thread_list_lock_ (see Thread::ModifySuspendCount).
820    if (!self->IsSuspended()) {
821      list_.remove(self);
822      delete self;
823      self = nullptr;
824    }
825    Locks::thread_list_lock_->ExclusiveUnlock(self);
826  }
827  // Release the thread ID after the thread is finished and deleted to avoid cases where we can
828  // temporarily have multiple threads with the same thread id. When this occurs, it causes
829  // problems in FindThreadByThreadId / SuspendThreadByThreadId.
830  ReleaseThreadId(nullptr, thin_lock_id);
831
832  // Clear the TLS data, so that the underlying native thread is recognizably detached.
833  // (It may wish to reattach later.)
834  CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
835
836  // Signal that a thread just detached.
837  MutexLock mu(NULL, *Locks::thread_list_lock_);
838  thread_exit_cond_.Signal(NULL);
839}
840
841void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
842  for (const auto& thread : list_) {
843    callback(thread, context);
844  }
845}
846
847void ThreadList::VisitRoots(RootCallback* callback, void* arg) const {
848  MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
849  for (const auto& thread : list_) {
850    thread->VisitRoots(callback, arg);
851  }
852}
853
854class VerifyRootWrapperArg {
855 public:
856  VerifyRootWrapperArg(VerifyRootCallback* callback, void* arg) : callback_(callback), arg_(arg) {
857  }
858  VerifyRootCallback* const callback_;
859  void* const arg_;
860};
861
862static void VerifyRootWrapperCallback(mirror::Object** root, void* arg, uint32_t /*thread_id*/,
863                                      RootType root_type) {
864  VerifyRootWrapperArg* wrapperArg = reinterpret_cast<VerifyRootWrapperArg*>(arg);
865  wrapperArg->callback_(*root, wrapperArg->arg_, 0, NULL, root_type);
866}
867
868void ThreadList::VerifyRoots(VerifyRootCallback* callback, void* arg) const {
869  VerifyRootWrapperArg wrapper(callback, arg);
870  MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
871  for (const auto& thread : list_) {
872    thread->VisitRoots(VerifyRootWrapperCallback, &wrapper);
873  }
874}
875
876uint32_t ThreadList::AllocThreadId(Thread* self) {
877  MutexLock mu(self, *Locks::allocated_thread_ids_lock_);
878  for (size_t i = 0; i < allocated_ids_.size(); ++i) {
879    if (!allocated_ids_[i]) {
880      allocated_ids_.set(i);
881      return i + 1;  // Zero is reserved to mean "invalid".
882    }
883  }
884  LOG(FATAL) << "Out of internal thread ids";
885  return 0;
886}
887
888void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) {
889  MutexLock mu(self, *Locks::allocated_thread_ids_lock_);
890  --id;  // Zero is reserved to mean "invalid".
891  DCHECK(allocated_ids_[id]) << id;
892  allocated_ids_.reset(id);
893}
894
895}  // namespace art
896