Fail threads attaching during runtime shutdown.

Introduce counters to indicate that threads are being born. Don't allow
thread birth to occur during runtime shutdown.

Bug: 7000936

Change-Id: Ib0d78f78c0ff126a4b5d3b5a6f1a2ff8f5061ae9
diff --git a/src/thread_list.cc b/src/thread_list.cc
index 56912ac..83f2658 100644
--- a/src/thread_list.cc
+++ b/src/thread_list.cc
@@ -374,10 +374,16 @@
 void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
   Thread* self = Thread::Current();
   Locks::mutator_lock_->AssertNotHeld(self);
-  MutexLock mu(self, *Locks::thread_list_lock_);
   bool all_threads_are_daemons;
   do {
+    {
+      // No more threads can be born after we start to shutdown.
+      MutexLock mu(self, *Locks::runtime_shutdown_lock_);
+      CHECK(Runtime::Current()->IsShuttingDown());
+      CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U);
+    }
     all_threads_are_daemons = true;
+    MutexLock mu(self, *Locks::thread_list_lock_);
     for (It it = list_.begin(), end = list_.end(); it != end; ++it) {
       // TODO: there's a race here with thread exit that's being worked around by checking if the
       // thread has a peer.