Stop verifying barrier count for thread dumping

For ANRs, some threads may not pass through the barrier. Prevent
a crash for this case.

This behavior is consistent with checks in the dump code.

Bug: 128490284
Test: test-art-host
Change-Id: I5dc681d00c29d20755020c35a18fb9912cc08d57
diff --git a/runtime/barrier.cc b/runtime/barrier.cc
index a1a3659..f0fa66a 100644
--- a/runtime/barrier.cc
+++ b/runtime/barrier.cc
@@ -25,10 +25,11 @@
 
 namespace art {
 
-Barrier::Barrier(int count)
+Barrier::Barrier(int count, bool verify_count_on_shutdown)
     : count_(count),
       lock_(new Mutex("GC barrier lock", kThreadSuspendCountLock)),
-      condition_(new ConditionVariable("GC barrier condition", *lock_)) {
+      condition_(new ConditionVariable("GC barrier condition", *lock_)),
+      verify_count_on_shutdown_(verify_count_on_shutdown) {
 }
 
 template void Barrier::Increment<Barrier::kAllowHoldingLocks>(Thread* self, int delta);
@@ -103,13 +104,10 @@
 }
 
 Barrier::~Barrier() {
-  if (gAborting == 0) {
-    // Only check when not aborting.
-    CHECK_EQ(count_, 0) << "Attempted to destroy barrier with non zero count";
-  } else {
-    if (count_ != 0) {
-      LOG(WARNING) << "Attempted to destroy barrier with non zero count " << count_;
-    }
+  if (count_ != 0) {
+    // Only check when not aborting and if we verify the count on shutdown.
+    LOG((gAborting == 0 && verify_count_on_shutdown_) ? FATAL : WARNING)
+        << "Attempted to destroy barrier with non zero count" << count_;
   }
 }
 
diff --git a/runtime/barrier.h b/runtime/barrier.h
index e21627e..704ecb0 100644
--- a/runtime/barrier.h
+++ b/runtime/barrier.h
@@ -44,7 +44,9 @@
     kDisallowHoldingLocks,
   };
 
-  explicit Barrier(int count);
+  // If verify_count_on_shutdown is true, the destructor verifies that the count is zero in the
+  // destructor. This means that all expected threads have went through the barrier.
+  explicit Barrier(int count, bool verify_count_on_shutdown = true);
   virtual ~Barrier();
 
   // Pass through the barrier, decrement the count but do not block.
@@ -86,6 +88,7 @@
 
   std::unique_ptr<Mutex> lock_ ACQUIRED_AFTER(Locks::abort_lock_);
   std::unique_ptr<ConditionVariable> condition_ GUARDED_BY(GetLock());
+  const bool verify_count_on_shutdown_;
 };
 
 }  // namespace art
diff --git a/runtime/thread_list.cc b/runtime/thread_list.cc
index 609bfb0..f07dca1 100644
--- a/runtime/thread_list.cc
+++ b/runtime/thread_list.cc
@@ -203,7 +203,7 @@
  public:
   DumpCheckpoint(std::ostream* os, bool dump_native_stack)
       : os_(os),
-        barrier_(0),
+        barrier_(0, /*verify_count_on_shutdown=*/false),
         backtrace_map_(dump_native_stack ? BacktraceMap::Create(getpid()) : nullptr),
         dump_native_stack_(dump_native_stack) {
     if (backtrace_map_ != nullptr) {