Merge "Use JNI to start the daemon threads." into dalvik-dev
diff --git a/src/thread.cc b/src/thread.cc
index 39be3d5..49200c2 100644
--- a/src/thread.cc
+++ b/src/thread.cc
@@ -623,14 +623,15 @@
 
   jni_env_ = new JNIEnvExt(this, runtime->GetJavaVM());
 
-  runtime->GetThreadList()->Register(this);
+  runtime->GetThreadList()->Register();
 }
 
 Thread* Thread::Attach(const Runtime* runtime, const char* name, bool as_daemon) {
+  LOG(INFO) << "Thread::Attach '" << name << "'";
   Thread* self = new Thread;
   self->Attach(runtime);
 
-  self->SetState(Thread::kRunnable);
+  self->SetState(Thread::kNative);
 
   SetThreadName(name);
 
@@ -653,9 +654,6 @@
 }
 
 void Thread::CreatePeer(const char* name, bool as_daemon) {
-  Thread* self = Thread::Current();
-  ScopedThreadStateChange tsc(self, Thread::kNative);
-
   JNIEnv* env = jni_env_;
 
   const char* field_name = (GetThinLockId() == ThreadList::kMainId) ? "mMain" : "mSystem";
@@ -669,7 +667,7 @@
 
   jobject peer = env->NewObject(c, mid, thread_group, thread_name, thread_priority, thread_is_daemon);
   peer_ = DecodeJObject(peer);
-  SetVmData(peer_, self);
+  SetVmData(peer_, Thread::Current());
 
   // Because we mostly run without code available (in the compiler, in tests), we
   // manually assign the fields the constructor should have set.
@@ -1026,6 +1024,8 @@
 }
 
 Thread::~Thread() {
+  SetState(Thread::kRunnable);
+
   // On thread detach, all monitors entered with JNI MonitorEnter are automatically exited.
   if (jni_env_ != NULL) {
     jni_env_->monitors.VisitRoots(MonitorExitVisitor, NULL);
diff --git a/src/thread_list.cc b/src/thread_list.cc
index 9a34426..f49c49d 100644
--- a/src/thread_list.cc
+++ b/src/thread_list.cc
@@ -218,11 +218,15 @@
   }
 }
 
-void ThreadList::Register(Thread* thread) {
-  //LOG(INFO) << "ThreadList::Register() " << *thread;
+void ThreadList::Register() {
+  Thread* self = Thread::Current();
+
+  //LOG(INFO) << "ThreadList::Register() " << *self;
+  self->Dump(std::cerr);
+
   MutexLock mu(thread_list_lock_);
-  CHECK(!Contains(thread));
-  list_.push_back(thread);
+  CHECK(!Contains(self));
+  list_.push_back(self);
 }
 
 void ThreadList::Unregister() {
diff --git a/src/thread_list.h b/src/thread_list.h
index 7840f71..e83b043 100644
--- a/src/thread_list.h
+++ b/src/thread_list.h
@@ -39,7 +39,7 @@
   void SuspendAll();
   void RunWhileSuspended(Thread* thread, void (*callback)(void*), void* arg);
 
-  void Register(Thread* thread);
+  void Register();
   void Unregister();
 
   void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;