Thread.join, GC daemons, suspend daemons on shutdown, and wait for non-daemon threads to exit.

(I've been testing with a modified SystemMethods test that does various thread
operations.)

Change-Id: I3087087546f90c43da7a0e63fae918ff0a6e7005
diff --git a/src/runtime.cc b/src/runtime.cc
index de94b3d..99f5e3a 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -40,8 +40,9 @@
 Runtime::~Runtime() {
   // Make sure our internal threads are dead before we start tearing down things they're using.
   delete signal_catcher_;
+  // TODO: GC thread.
 
-  // Make sure all other threads have terminated too.
+  // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
   delete thread_list_;
 
   delete class_linker_;
@@ -355,11 +356,9 @@
 void Runtime::Start() {
   started_ = true;
 
-  // Initialize both the built-in and libcore native methods.
-  InitLibraries();
+  InitNativeMethods();
 
-  // Finish attaching the main thread.
-  Thread::Current()->CreatePeer("main", false);
+  Thread::FinishStartup();
 
   RunImageClinits();
 
@@ -382,7 +381,7 @@
   CHECK(c != NULL);
   Method* m = c->FindDirectMethod("start", "()V");
   CHECK(m != NULL);
-//  m->Invoke(Thread::Current(), NULL, NULL, NULL);
+  m->Invoke(Thread::Current(), NULL, NULL, NULL);
 }
 
 bool Runtime::IsStarted() {
@@ -432,7 +431,7 @@
   return true;
 }
 
-void Runtime::InitLibraries() {
+void Runtime::InitNativeMethods() {
   Thread* self = Thread::Current();
   JNIEnv* env = self->GetJniEnv();
 
@@ -516,6 +515,8 @@
 }
 
 void Runtime::DetachCurrentThread() {
+  // TODO: check we're not calling DetachCurrentThread from a call stack that
+  // includes managed frames. (It's only valid if the stack is all-native.)
   thread_list_->Unregister();
 }