Better detect PrimitiveArrayCritical issues

Crash if we exit a thread while holding an unreleased
PrimitiveArrayCritical.

Report that we're in such a "critical section" as part of the thread
state.

Bug: 231038541
Test: Build and boot AOSP
Change-Id: I8b76ca6f7b5ca79712b2431d60ad5e6795e484a6
diff --git a/runtime/thread.cc b/runtime/thread.cc
index 97cfb7a..f315b96 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -1981,6 +1981,9 @@
     if (thread->IsStillStarting()) {
       os << " (still starting up)";
     }
+    if (thread->GetDisableThreadFlipCount() != 0) {
+      os << " DisableFlipCount = " << thread->GetDisableThreadFlipCount();
+    }
     os << "\n";
   } else {
     os << '"' << ::art::GetThreadName(tid) << '"'
diff --git a/runtime/thread_list.cc b/runtime/thread_list.cc
index 6482e72..69f88d8 100644
--- a/runtime/thread_list.cc
+++ b/runtime/thread_list.cc
@@ -1291,6 +1291,10 @@
   DCHECK_EQ(self, Thread::Current());
   CHECK_NE(self->GetState(), ThreadState::kRunnable);
   Locks::mutator_lock_->AssertNotHeld(self);
+  if (self->GetDisableThreadFlipCount() != 0) {
+    LOG(FATAL) << "Incomplete PrimitiveArrayCritical section at exit: " << *self << "count = "
+               << self->GetDisableThreadFlipCount();
+  }
 
   VLOG(threads) << "ThreadList::Unregister() " << *self;