Add infrastructure for registering built-in native methods.

While I'm here, make fiddling with Thread state easier.

Change-Id: I3d215a3a852aa8970c3974b2edefce9dd261ccd7
diff --git a/src/signal_catcher.cc b/src/signal_catcher.cc
index b83e71d..5979a0f 100644
--- a/src/signal_catcher.cc
+++ b/src/signal_catcher.cc
@@ -74,6 +74,24 @@
   Heap::CollectGarbage();
 }
 
+int WaitForSignal(Thread* thread, sigset_t& mask) {
+  ScopedThreadStateChange tsc(thread, Thread::kWaiting); // TODO: VMWAIT
+
+  // Signals for sigwait() must be blocked but not ignored.  We
+  // block signals like SIGQUIT for all threads, so the condition
+  // is met.  When the signal hits, we wake up, without any signal
+  // handlers being invoked.
+
+  // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
+  int signal_number;
+  int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
+  if (rc != 0) {
+    PLOG(FATAL) << "sigwait failed";
+  }
+
+  return signal_number;
+}
+
 void* SignalCatcher::Run(void*) {
   CHECK(Runtime::Current()->AttachCurrentThread("Signal Catcher", NULL, true));
   Thread* self = Thread::Current();
@@ -88,31 +106,12 @@
   sigaddset(&mask, SIGUSR1);
 
   while (true) {
-    self->SetState(Thread::kWaiting); // TODO: VMWAIT
-
-    // Signals for sigwait() must be blocked but not ignored.  We
-    // block signals like SIGQUIT for all threads, so the condition
-    // is met.  When the signal hits, we wake up, without any signal
-    // handlers being invoked.
-
-    // Sleep in sigwait() until a signal arrives. gdb causes EINTR failures.
-    int signal_number;
-    int rc = TEMP_FAILURE_RETRY(sigwait(&mask, &signal_number));
-    if (rc != 0) {
-      PLOG(FATAL) << "sigwait failed";
-    }
-
-    if (!halt_) {
-      LOG(INFO) << *self << ": reacting to signal " << signal_number;
-    }
-
-    // Set our status to runnable, self-suspending if GC in progress.
-    self->SetState(Thread::kRunnable);
-
+    int signal_number = WaitForSignal(self, mask);
     if (halt_) {
       return NULL;
     }
 
+    LOG(INFO) << *self << ": reacting to signal " << signal_number;
     switch (signal_number) {
     case SIGQUIT:
       HandleSigQuit();