Use a lock in RaceGenerateTask around dex2oat.

To avoid parallel dex2oat invocations, mimic the code from before
that used to lock in OatFileAssistant when invoking dex2oat.

Test: oat_file_assistant_test
Change-Id: I40988f8827539c9288090ebf1e4bc703aeae8e91
diff --git a/runtime/oat_file_assistant_test.cc b/runtime/oat_file_assistant_test.cc
index 5a29978..6ee3bdb 100644
--- a/runtime/oat_file_assistant_test.cc
+++ b/runtime/oat_file_assistant_test.cc
@@ -1159,8 +1159,13 @@
 // A task to generate a dex location. Used by the RaceToGenerate test.
 class RaceGenerateTask : public Task {
  public:
-  RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
-      : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
+  RaceGenerateTask(const std::string& dex_location,
+                   const std::string& oat_location,
+                   Mutex* lock)
+      : dex_location_(dex_location),
+        oat_location_(oat_location),
+        lock_(lock),
+        loaded_oat_file_(nullptr)
   {}
 
   void Run(Thread* self ATTRIBUTE_UNUSED) {
@@ -1170,18 +1175,13 @@
     std::vector<std::string> error_msgs;
     const OatFile* oat_file = nullptr;
     {
+      MutexLock mu(Thread::Current(), *lock_);
       // Create the oat file.
       std::vector<std::string> args;
       args.push_back("--dex-file=" + dex_location_);
       args.push_back("--oat-file=" + oat_location_);
       std::string error_msg;
-      if (kIsTargetBuild) {
-        // Don't check whether dex2oat is successful: given we're running kNumThreads in
-        // parallel, low memory killer might just kill some of the dex2oat invocations.
-        DexoptTest::Dex2Oat(args, &error_msg);
-      } else {
-        ASSERT_TRUE(DexoptTest::Dex2Oat(args, &error_msg)) << error_msg;
-      }
+      ASSERT_TRUE(DexoptTest::Dex2Oat(args, &error_msg)) << error_msg;
     }
 
     dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
@@ -1204,6 +1204,7 @@
  private:
   std::string dex_location_;
   std::string oat_location_;
+  Mutex* lock_;
   const OatFile* loaded_oat_file_;
 };
 
@@ -1225,8 +1226,9 @@
   Thread* self = Thread::Current();
   ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
   std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
+  Mutex lock("RaceToGenerate");
   for (size_t i = 0; i < kNumThreads; i++) {
-    std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
+    std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location, &lock));
     thread_pool.AddTask(self, task.get());
     tasks.push_back(std::move(task));
   }