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/scoped_thread_state_change.h b/src/scoped_thread_state_change.h
index b36922e..9da41e0 100644
--- a/src/scoped_thread_state_change.h
+++ b/src/scoped_thread_state_change.h
@@ -35,7 +35,9 @@
     if (self_ == NULL) {
       // Value chosen arbitrarily and won't be used in the destructor since thread_ == NULL.
       old_thread_state_ = kTerminated;
-      CHECK(!Runtime::Current()->IsStarted() || Runtime::Current()->IsShuttingDown());
+      MutexLock mu(*Locks::runtime_shutdown_lock_);
+      Runtime* runtime = Runtime::Current();
+      CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown());
     } else {
       bool runnable_transition;
       DCHECK_EQ(self, Thread::Current());
@@ -61,7 +63,10 @@
   ~ScopedThreadStateChange() LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) {
     if (self_ == NULL) {
       if (!expected_has_no_thread_) {
-        CHECK(Runtime::Current()->IsShuttingDown());
+        MutexLock mu(*Locks::runtime_shutdown_lock_);
+        Runtime* runtime = Runtime::Current();
+        bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown();
+        CHECK(shutting_down);
       }
     } else {
       if (old_thread_state_ != thread_state_) {