Implement the Thread.sleep native method.

This makes returning TS_SLEEPING from JDWP simple and cheap, and
makes the stack dumps for sleeping threads more easily understood
by app developers (because there's no Object.wait magic going, and
the thread state is "Sleeping" rather than "TimedWaiting").

Also make Object.wait() a native method in its own right, so every call into
Monitor::Wait can explicitly pass the most appropriate ThreadState: kSleeping,
kWaiting, or kTimedWaiting.

Also add Thread.sleep and Object.wait(long, int) calls to the ThreadStress test.

Change-Id: I49adb45dbcd669eba7cf3def45e6cbfc461a3254
diff --git a/src/native/java_lang_Thread.cc b/src/native/java_lang_Thread.cc
index f14c03b..8db217e 100644
--- a/src/native/java_lang_Thread.cc
+++ b/src/native/java_lang_Thread.cc
@@ -16,6 +16,7 @@
 
 #include "debugger.h"
 #include "jni_internal.h"
+#include "monitor.h"
 #include "object.h"
 #include "scoped_thread_state_change.h"
 #include "ScopedUtfChars.h"
@@ -65,6 +66,7 @@
     case kTerminated:                     return kJavaTerminated;
     case kRunnable:                       return kJavaRunnable;
     case kTimedWaiting:                   return kJavaTimedWaiting;
+    case kSleeping:                       return kJavaTimedWaiting;
     case kBlocked:                        return kJavaBlocked;
     case kWaiting:                        return kJavaWaiting;
     case kStarting:                       return kJavaNew;
@@ -145,6 +147,12 @@
   }
 }
 
+static void Thread_sleep(JNIEnv* env, jclass, jobject java_lock, jlong ms, jint ns) {
+  ScopedObjectAccess soa(env);
+  Object* lock = soa.Decode<Object*>(java_lock);
+  Monitor::Wait(Thread::Current(), lock, ms, ns, true, kSleeping);
+}
+
 /*
  * Causes the thread to temporarily pause and allow other threads to execute.
  *
@@ -165,6 +173,7 @@
   NATIVE_METHOD(Thread, nativeInterrupt, "()V"),
   NATIVE_METHOD(Thread, nativeSetName, "(Ljava/lang/String;)V"),
   NATIVE_METHOD(Thread, nativeSetPriority, "(I)V"),
+  NATIVE_METHOD(Thread, sleep, "(Ljava/lang/Object;JI)V"),
   NATIVE_METHOD(Thread, yield, "()V"),
 };