Change Thread::peer_ to be a jobject instead of an Object*

Fixes issue where GC was freeing the java peer if the parent thread exited before the child thread got registered.

Change-Id: I6fbe74865d5827d243ac55fc396679afa97ff2f9
diff --git a/src/native/dalvik_system_VMStack.cc b/src/native/dalvik_system_VMStack.cc
index 091ecd6..24aa730 100644
--- a/src/native/dalvik_system_VMStack.cc
+++ b/src/native/dalvik_system_VMStack.cc
@@ -25,12 +25,10 @@
 
 static jobject GetThreadStack(JNIEnv* env, jobject peer) {
   bool timeout;
-  {
+  Thread* self = Thread::Current();
+  if (env->IsSameObject(peer, self->GetPeer())) {
     ScopedObjectAccess soa(env);
-    Thread* self = Thread::Current();
-    if (soa.Decode<Object*>(peer) == self->GetPeer()) {
-      return self->CreateInternalStackTrace(soa);
-    }
+    return self->CreateInternalStackTrace(soa);
   }
   // Suspend thread to build stack trace.
   Thread* thread = Thread::SuspendForDebugger(peer, true, &timeout);
diff --git a/src/native/java_lang_Thread.cc b/src/native/java_lang_Thread.cc
index adc246a..2a6f177 100644
--- a/src/native/java_lang_Thread.cc
+++ b/src/native/java_lang_Thread.cc
@@ -25,8 +25,7 @@
 namespace art {
 
 static jobject Thread_currentThread(JNIEnv* env, jclass) {
-  ScopedObjectAccess soa(env);
-  return soa.AddLocalReference<jobject>(soa.Self()->GetPeer());
+  return reinterpret_cast<JNIEnvExt*>(env)->self->GetPeer();
 }
 
 static jboolean Thread_interrupted(JNIEnv* env, jclass) {
@@ -110,9 +109,8 @@
   ScopedUtfChars name(env, java_name);
   {
     ScopedObjectAccess soa(env);
-    Thread* self = Thread::Current();
-    if (soa.Decode<Object*>(peer) == self->GetPeer()) {
-      self->SetThreadName(name.c_str());
+    if (soa.Env()->IsSameObject(peer, soa.Self()->GetPeer())) {
+      soa.Self()->SetThreadName(name.c_str());
       return;
     }
   }
diff --git a/src/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.cc b/src/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.cc
index be92068..66c27e4 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* env, uint32_t thin_lock_id) {
+static jobject FindThreadByThinLockId(JNIEnv*, uint32_t thin_lock_id) {
   struct ThreadFinder {
     explicit ThreadFinder(uint32_t thin_lock_id) : thin_lock_id(thin_lock_id), thread(NULL) {
     }
@@ -60,8 +60,7 @@
     Runtime::Current()->GetThreadList()->ForEach(ThreadFinder::Callback, &finder);
   }
   if (finder.thread != NULL) {
-    ScopedObjectAccess soa(env);
-    return soa.AddLocalReference<jobject>(finder.thread->GetPeer());
+    return finder.thread->GetPeer();
   } else {
     return NULL;
   }