Add additional support for Thread::Current() == NULL.

Added checks for Thread::Current() == NULL so that we don't crash in Runtime::~Runtime() when we have to wait for a concurrent GC to complete.

Change-Id: I3164c01f3eb668c7e8fa29c8a0d5a48b8f0e4fb1
diff --git a/src/heap.cc b/src/heap.cc
index 55d9c03..459597a 100644
--- a/src/heap.cc
+++ b/src/heap.cc
@@ -946,6 +946,9 @@
 }
 
 void Heap::ConcurrentGC() {
+  if (Runtime::Current()->IsShuttingDown()) {
+    return;
+  }
   ScopedHeapLock heap_lock;
   // We shouldn't need a WaitForConcurrentGcToComplete here since only
   // concurrent GC resumes threads before the GC is completed and this function
diff --git a/src/monitor.cc b/src/monitor.cc
index 8bb7e45..50deba6 100644
--- a/src/monitor.cc
+++ b/src/monitor.cc
@@ -184,8 +184,9 @@
     return;
   }
 
-  uint64_t waitStart, waitEnd;
   if (!lock_.TryLock()) {
+    uint64_t waitStart = 0;
+    uint64_t waitEnd = 0;
     uint32_t wait_threshold = lock_profiling_threshold_;
     const Method* current_locking_method = NULL;
     uint32_t current_locking_dex_pc = 0;
diff --git a/src/scoped_thread_list_lock_releaser.cc b/src/scoped_thread_list_lock_releaser.cc
index 3ac22a5..d15eae5 100644
--- a/src/scoped_thread_list_lock_releaser.cc
+++ b/src/scoped_thread_list_lock_releaser.cc
@@ -21,12 +21,15 @@
 
 namespace art {
 
-ScopedThreadListLockReleaser::ScopedThreadListLockReleaser() {
+ScopedThreadListLockReleaser::ScopedThreadListLockReleaser() : unlocked_(false) {
+  if (Thread::Current() == NULL) {
+    CHECK(Runtime::Current()->IsShuttingDown());
+    return;
+  }
+
   if (Thread::Current()->held_mutexes_[kThreadListLock] > 0) {
     Runtime::Current()->GetThreadList()->thread_list_lock_.Unlock();
     unlocked_ = true;
-  } else {
-    unlocked_ = false;
   }
 }
 
diff --git a/src/thread.h b/src/thread.h
index e48f444..6904d4a 100644
--- a/src/thread.h
+++ b/src/thread.h
@@ -655,10 +655,20 @@
 class ScopedThreadStateChange {
  public:
   ScopedThreadStateChange(Thread* thread, ThreadState new_state) : thread_(thread) {
+    if (thread_ == NULL) {
+      // Value chosen arbitrarily and won't be used in the destructor since thread_ == NULL.
+      old_thread_state_ = kTerminated;
+      CHECK(Runtime::Current()->IsShuttingDown());
+      return;
+    }
     old_thread_state_ = thread_->SetState(new_state);
   }
 
   ~ScopedThreadStateChange() {
+    if (thread_ == NULL) {
+      CHECK(Runtime::Current()->IsShuttingDown());
+      return;
+    }
     thread_->SetState(old_thread_state_);
   }