Merge "Parellel mark stack processing" into dalvik-dev
diff --git a/src/dex2oat.cc b/src/dex2oat.cc
index 40218a8..5739057 100644
--- a/src/dex2oat.cc
+++ b/src/dex2oat.cc
@@ -493,8 +493,9 @@
   } while (false)
 
  public:
-  WatchDog() {
-    if (!kIsWatchDogEnabled) {
+  WatchDog(bool is_watch_dog_enabled) {
+    is_watch_dog_enabled_ = is_watch_dog_enabled;
+    if (!is_watch_dog_enabled_) {
       return;
     }
     shutting_down_ = false;
@@ -506,7 +507,7 @@
     CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason);
   }
   ~WatchDog() {
-    if (!kIsWatchDogEnabled) {
+    if (!is_watch_dog_enabled_) {
       return;
     }
     const char* reason = "dex2oat watch dog thread shutdown";
@@ -557,13 +558,13 @@
     CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
   }
 
-  static const bool kIsWatchDogEnabled = !kIsTargetBuild;
 #ifdef ART_USE_LLVM_COMPILER
   static const unsigned int kWatchDogTimeoutSeconds = 20 * 60; // 15 minutes + buffer
 #else
   static const unsigned int kWatchDogTimeoutSeconds = 2 * 60;  // 1 minute + buffer
 #endif
 
+  bool is_watch_dog_enabled_;
   bool shutting_down_;
   // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases
   pthread_mutex_t mutex_;
@@ -573,8 +574,6 @@
 };
 
 static int dex2oat(int argc, char** argv) {
-  WatchDog watch_dog;
-
   InitLogging(argv);
 
   // Skip over argv[0].
@@ -619,6 +618,7 @@
 #endif
   bool dump_stats = kIsDebugBuild;
   bool dump_timings = kIsDebugBuild;
+  bool watch_dog_enabled = !kIsTargetBuild;
 
   for (int i = 0; i < argc; i++) {
     const StringPiece option(argv[i]);
@@ -644,8 +644,12 @@
       if (!ParseInt(oat_fd_str, &oat_fd)) {
         Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
       }
-    } else if (option.starts_with("-g")) {
+    } else if (option == "-g") {
       support_debugging = true;
+    } else if (option == "--watch-dog") {
+      watch_dog_enabled = true;
+    } else if (option == "--no-watch-dog") {
+      watch_dog_enabled = false;
     } else if (option.starts_with("-j")) {
       const char* thread_count_str = option.substr(strlen("-j")).data();
       if (!ParseInt(thread_count_str, &thread_count)) {
@@ -781,6 +785,9 @@
     }
   }
 
+  // Done with usage checks, enable watchdog if requested
+  WatchDog watch_dog(watch_dog_enabled);
+
   // Check early that the result of compilation can be written
   UniquePtr<File> oat_file;
   bool create_file = !oat_filename.empty();  // as opposed to using open file descriptor
diff --git a/src/logging.cc b/src/logging.cc
index 5680ed0..9dde963 100644
--- a/src/logging.cc
+++ b/src/logging.cc
@@ -25,6 +25,8 @@
 
 LogVerbosity gLogVerbosity;
 
+bool gAborting = false;
+
 static LogSeverity gMinimumLogSeverity = INFO;
 static std::string* gCmdLine;
 static std::string* gProgramInvocationName;
diff --git a/src/logging.h b/src/logging.h
index 75782d5..d0d35ea 100644
--- a/src/logging.h
+++ b/src/logging.h
@@ -318,6 +318,11 @@
 };
 
 extern LogVerbosity gLogVerbosity;
+
+// Used on fatal exit. Prevents recursive aborts. Allows us to disable
+// some error checking to ensure fatal shutdown makes forward progress.
+extern bool gAborting;
+
 extern void InitLogging(char* argv[]);
 
 extern const char* GetCmdLine();
diff --git a/src/mutex.h b/src/mutex.h
index 4af2ad7..d0c6369 100644
--- a/src/mutex.h
+++ b/src/mutex.h
@@ -107,7 +107,7 @@
 
   // Assert that the Mutex is exclusively held by the current thread.
   void AssertExclusiveHeld(const Thread* self) {
-    if (kDebugLocking) {
+    if (kDebugLocking && !gAborting) {
       CHECK(IsExclusiveHeld(self)) << *this;
     }
   }
diff --git a/src/runtime.cc b/src/runtime.cc
index 3fa3123..100a198 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -155,8 +155,6 @@
   instance_ = NULL;
 }
 
-static bool gAborting = false;
-
 struct AbortState {
   void Dump(std::ostream& os) {
     if (gAborting) {
@@ -202,6 +200,8 @@
 };
 
 void Runtime::Abort() {
+  gAborting = true;  // set before taking any locks
+
   // Ensure that we don't have multiple threads trying to abort at once,
   // which would result in significantly worse diagnostics.
   MutexLock mu(Thread::Current(), *Locks::abort_lock_);
diff --git a/src/runtime_linux.cc b/src/runtime_linux.cc
index 01c08d3..430c70f 100644
--- a/src/runtime_linux.cc
+++ b/src/runtime_linux.cc
@@ -228,6 +228,7 @@
 };
 
 static void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context) {
+  gAborting = true;  // set before taking any locks
   MutexLock mu(Thread::Current(), *Locks::unexpected_signal_lock_);
 
   bool has_address = (signal_number == SIGILL || signal_number == SIGBUS ||