ART: Add barrier to dex2oat watchdog startup

Ensure that the watchdog is running before progressing.

Bug: 63052624
Test: m test-art-host
Change-Id: I4ada6be7e46b5ee10f9f53805fa10efb15d6de1e
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 1574cd7..bef9d77 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -41,6 +41,7 @@
 #include "arch/instruction_set_features.h"
 #include "arch/mips/instruction_set_features_mips.h"
 #include "art_method-inl.h"
+#include "barrier.h"
 #include "base/callee_save_type.h"
 #include "base/dumpable.h"
 #include "base/file_utils.h"
@@ -480,7 +481,8 @@
 
  public:
   explicit WatchDog(int64_t timeout_in_milliseconds)
-      : timeout_in_milliseconds_(timeout_in_milliseconds),
+      : wait_barrier_(2),
+        timeout_in_milliseconds_(timeout_in_milliseconds),
         shutting_down_(false) {
     const char* reason = "dex2oat watch dog thread startup";
     CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason);
@@ -525,9 +527,14 @@
   static constexpr int64_t kDefaultWatchdogTimeoutInMS =
       kWatchdogVerifyMultiplier * kWatchDogTimeoutSeconds * 1000;
 
+  void WaitForCallBackStart(Thread* self) {
+    wait_barrier_.Wait(self);
+  }
+
  private:
   static void* CallBack(void* arg) {
     WatchDog* self = reinterpret_cast<WatchDog*>(arg);
+    self->wait_barrier_.Pass(nullptr);
     ::art::SetThreadName("dex2oat watch dog");
     self->Wait();
     return nullptr;
@@ -585,6 +592,8 @@
   pthread_attr_t attr_;
   pthread_t pthread_;
 
+  Barrier wait_barrier_;
+
   const int64_t timeout_in_milliseconds_;
   bool shutting_down_;
 };
@@ -929,6 +938,8 @@
                             ? parser_options->watch_dog_timeout_in_ms
                             : WatchDog::kDefaultWatchdogTimeoutInMS;
       watchdog_.reset(new WatchDog(timeout));
+      watchdog_->WaitForCallBackStart(nullptr);  // The runtime hasn't been started, yet. So
+                                                 // nullptr for current thread.
     }
 
     // Fill some values into the key-value store for the oat header.