Explicitly pass Thread::Current to MutexLock and Alloc.

Change-Id: I8b75bc0617915465f102815b32306aa7760dcae4
diff --git a/src/native/dalvik_system_VMRuntime.cc b/src/native/dalvik_system_VMRuntime.cc
index 7184e48..91b7b5f 100644
--- a/src/native/dalvik_system_VMRuntime.cc
+++ b/src/native/dalvik_system_VMRuntime.cc
@@ -54,11 +54,11 @@
 
   Class* element_class = soa.Decode<Class*>(javaElementClass);
   if (element_class == NULL) {
-    Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
+    soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "element class == null");
     return NULL;
   }
   if (length < 0) {
-    Thread::Current()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
+    soa.Self()->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", length);
     return NULL;
   }
 
@@ -67,7 +67,7 @@
   descriptor += "[";
   descriptor += ClassHelper(element_class).GetDescriptor();
   Class* array_class = class_linker->FindClass(descriptor.c_str(), NULL);
-  Array* result = Array::Alloc(array_class, length);
+  Array* result = Array::Alloc(soa.Self(), array_class, length);
   if (result == NULL) {
     return NULL;
   }
@@ -81,7 +81,7 @@
   ScopedObjectAccess soa(env);
   Array* array = soa.Decode<Array*>(javaArray);
   if (!array->IsArrayInstance()) {
-    Thread::Current()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
+    soa.Self()->ThrowNewException("Ljava/lang/IllegalArgumentException;", "not an array");
     return 0;
   }
   // TODO: we should also check that this is a non-movable array.
@@ -127,7 +127,7 @@
 }
 #endif
 
-static void VMRuntime_setTargetSdkVersion(JNIEnv*, jobject, jint targetSdkVersion) {
+static void VMRuntime_setTargetSdkVersion(JNIEnv* env, jobject, jint targetSdkVersion) {
   // This is the target SDK version of the app we're about to run.
   // Note that targetSdkVersion may be CUR_DEVELOPMENT (10000).
   // Note that targetSdkVersion may be 0, meaning "current".
@@ -138,7 +138,8 @@
 #if !defined(ART_USE_LLVM_COMPILER)
     if (vm->check_jni) {
       LOG(WARNING) << "Turning off CheckJNI so we can turn on JNI app bug workarounds...";
-      MutexLock mu(*Locks::thread_list_lock_);
+      Thread* self = static_cast<JNIEnvExt*>(env)->self;
+      MutexLock mu(self, *Locks::thread_list_lock_);
       vm->SetCheckJniEnabled(false);
       runtime->GetThreadList()->ForEach(DisableCheckJniCallback, NULL);
     }
@@ -148,6 +149,7 @@
 
     vm->work_around_app_jni_bugs = true;
 #else
+    UNUSED(env);
     LOG(WARNING) << "LLVM does not work-around app jni bugs.";
     vm->work_around_app_jni_bugs = false;
 #endif
diff --git a/src/native/java_lang_Class.cc b/src/native/java_lang_Class.cc
index c023b7e..488df80 100644
--- a/src/native/java_lang_Class.cc
+++ b/src/native/java_lang_Class.cc
@@ -199,7 +199,8 @@
     AbstractMethod* m = c->GetVirtualMethod(i);
     mh.ChangeMethod(m);
     if (IsVisibleMethod(m, publicOnly)) {
-      if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
+      // TODO: the use of GetParameterTypes creates an unused array here.
+      if (mh.GetReturnType() == NULL || mh.GetParameterTypes(soa.Self()) == NULL) {
         DCHECK(env->ExceptionOccurred());
         return NULL;
       }
@@ -213,7 +214,8 @@
     AbstractMethod* m = c->GetDirectMethod(i);
     mh.ChangeMethod(m);
     if (IsVisibleMethod(m, publicOnly)) {
-      if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) {
+      // TODO: the use of GetParameterTypes creates an unused array here.
+      if (mh.GetReturnType() == NULL || mh.GetParameterTypes(soa.Self()) == NULL) {
         DCHECK(env->ExceptionOccurred());
         return NULL;
       }
@@ -349,7 +351,7 @@
 static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
   ScopedObjectAccess soa(env);
   SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(soa, javaThis));
-  return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone());
+  return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self()));
 }
 
 static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) {
@@ -412,7 +414,7 @@
     return NULL;
   }
 
-  Object* new_obj = c->AllocObject();
+  Object* new_obj = c->AllocObject(soa.Self());
   if (new_obj == NULL) {
     DCHECK(soa.Self()->IsExceptionPending());
     return NULL;
diff --git a/src/native/java_lang_Object.cc b/src/native/java_lang_Object.cc
index 89019f7..a6ae49d 100644
--- a/src/native/java_lang_Object.cc
+++ b/src/native/java_lang_Object.cc
@@ -23,7 +23,7 @@
 static jobject Object_internalClone(JNIEnv* env, jobject javaThis) {
   ScopedObjectAccess soa(env);
   Object* o = soa.Decode<Object*>(javaThis);
-  return soa.AddLocalReference<jobject>(o->Clone());
+  return soa.AddLocalReference<jobject>(o->Clone(soa.Self()));
 }
 
 static void Object_notify(JNIEnv* env, jobject javaThis) {
diff --git a/src/native/java_lang_Thread.cc b/src/native/java_lang_Thread.cc
index edf55c3..cf475e2 100644
--- a/src/native/java_lang_Thread.cc
+++ b/src/native/java_lang_Thread.cc
@@ -34,7 +34,7 @@
 
 static jboolean Thread_isInterrupted(JNIEnv* env, jobject java_thread) {
   ScopedObjectAccess soa(env);
-  MutexLock mu(*Locks::thread_list_lock_);
+  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
   Thread* thread = Thread::FromManagedThread(soa, java_thread);
   return (thread != NULL) ? thread->IsInterrupted() : JNI_FALSE;
 }
@@ -55,10 +55,9 @@
 
   ScopedObjectAccess soa(env);
   ThreadState internal_thread_state = (has_been_started ? kTerminated : kStarting);
-  MutexLock mu(*Locks::thread_list_lock_);
+  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
   Thread* thread = Thread::FromManagedThread(soa, java_thread);
   if (thread != NULL) {
-    MutexLock mu(*Locks::thread_suspend_count_lock_);
     internal_thread_state = thread->GetState();
   }
   switch (internal_thread_state) {
@@ -91,7 +90,7 @@
     Thread::Current()->ThrowNewException("Ljava/lang/NullPointerException;", "object == null");
     return JNI_FALSE;
   }
-  MutexLock mu(*Locks::thread_list_lock_);
+  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
   Thread* thread = Thread::FromManagedThread(soa, java_thread);
   return thread->HoldsLock(object);
 }
@@ -138,7 +137,7 @@
  */
 static void Thread_nativeSetPriority(JNIEnv* env, jobject java_thread, jint new_priority) {
   ScopedObjectAccess soa(env);
-  MutexLock mu(*Locks::thread_list_lock_);
+  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
   Thread* thread = Thread::FromManagedThread(soa, java_thread);
   if (thread != NULL) {
     thread->SetNativePriority(new_priority);
diff --git a/src/native/java_lang_reflect_Array.cc b/src/native/java_lang_reflect_Array.cc
index d3a57bf..863f9fc 100644
--- a/src/native/java_lang_reflect_Array.cc
+++ b/src/native/java_lang_reflect_Array.cc
@@ -29,7 +29,7 @@
                                IntArray* dimensions)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   int32_t array_length = dimensions->Get(current_dimension++);
-  SirtRef<Array> new_array(self, Array::Alloc(array_class, array_length));
+  SirtRef<Array> new_array(self, Array::Alloc(self, array_class, array_length));
   if (new_array.get() == NULL) {
     CHECK(self->IsExceptionPending());
     return NULL;
@@ -139,7 +139,7 @@
     return NULL;
   }
   DCHECK(array_class->IsArrayClass());
-  Array* new_array = Array::Alloc(array_class, length);
+  Array* new_array = Array::Alloc(soa.Self(), array_class, length);
   if (new_array == NULL) {
     CHECK(soa.Self()->IsExceptionPending());
     return NULL;
diff --git a/src/native/java_lang_reflect_Constructor.cc b/src/native/java_lang_reflect_Constructor.cc
index 44b459d..ed0d1f1 100644
--- a/src/native/java_lang_reflect_Constructor.cc
+++ b/src/native/java_lang_reflect_Constructor.cc
@@ -45,7 +45,7 @@
     return NULL;
   }
 
-  Object* receiver = c->AllocObject();
+  Object* receiver = c->AllocObject(soa.Self());
   if (receiver == NULL) {
     return NULL;
   }
diff --git a/src/native/java_lang_reflect_Method.cc b/src/native/java_lang_reflect_Method.cc
index 63a277b..b1eb6e0 100644
--- a/src/native/java_lang_reflect_Method.cc
+++ b/src/native/java_lang_reflect_Method.cc
@@ -44,7 +44,7 @@
   }
   CHECK_NE(throws_index, -1);
   ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
-  return soa.AddLocalReference<jobject>(declared_exceptions->Clone());
+  return soa.AddLocalReference<jobject>(declared_exceptions->Clone(soa.Self()));
 }
 
 static jobject Method_findOverriddenMethodNative(JNIEnv* env, jobject javaMethod) {
diff --git a/src/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.cc b/src/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.cc
index 66c27e4..0ed964b 100644
--- a/src/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.cc
+++ b/src/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.cc
@@ -39,7 +39,7 @@
   return Dbg::IsAllocTrackingEnabled();
 }
 
-static jobject FindThreadByThinLockId(JNIEnv*, uint32_t thin_lock_id) {
+static jobject FindThreadByThinLockId(JNIEnv* env, uint32_t thin_lock_id) {
   struct ThreadFinder {
     explicit ThreadFinder(uint32_t thin_lock_id) : thin_lock_id(thin_lock_id), thread(NULL) {
     }
@@ -56,7 +56,8 @@
   };
   ThreadFinder finder(thin_lock_id);
   {
-    MutexLock mu(*Locks::thread_list_lock_);
+    Thread* self = static_cast<JNIEnvExt*>(env)->self;
+    MutexLock mu(self, *Locks::thread_list_lock_);
     Runtime::Current()->GetThreadList()->ForEach(ThreadFinder::Callback, &finder);
   }
   if (finder.thread != NULL) {
@@ -133,10 +134,7 @@
 
   std::vector<uint8_t>& bytes = *reinterpret_cast<std::vector<uint8_t>*>(context);
   JDWP::Append4BE(bytes, t->GetThinLockId());
-  {
-    MutexLock mu(*Locks::thread_suspend_count_lock_);
-    JDWP::Append1BE(bytes, t->GetState());
-  }
+  JDWP::Append1BE(bytes, t->GetState());
   JDWP::Append4BE(bytes, t->GetTid());
   JDWP::Append4BE(bytes, utime);
   JDWP::Append4BE(bytes, stime);
@@ -145,8 +143,9 @@
 
 static jbyteArray DdmVmInternal_getThreadStats(JNIEnv* env, jclass) {
   std::vector<uint8_t> bytes;
+  Thread* self = static_cast<JNIEnvExt*>(env)->self;
   {
-    MutexLock mu(*Locks::thread_list_lock_);
+    MutexLock mu(self, *Locks::thread_list_lock_);
     ThreadList* thread_list = Runtime::Current()->GetThreadList();
 
     uint16_t thread_count = 0;