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/monitor.cc b/src/monitor.cc
index 33383eb..7cdccac 100644
--- a/src/monitor.cc
+++ b/src/monitor.cc
@@ -379,8 +379,11 @@
  * Since we're allowed to wake up "early", we clamp extremely long durations
  * to return at the end of the 32-bit time epoch.
  */
-void Monitor::Wait(Thread* self, int64_t ms, int32_t ns, bool interruptShouldThrow) {
+void Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
+                   bool interruptShouldThrow, ThreadState why) {
   DCHECK(self != NULL);
+  DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
+  DCHECK(why != kWaiting || (ms == 0 && ns == 0));
 
   // Make sure that we hold the lock.
   if (owner_ != self) {
@@ -388,10 +391,12 @@
     return;
   }
   monitor_lock_.AssertHeld(self);
-  WaitWithLock(self, ms, ns, interruptShouldThrow);
+
+  WaitWithLock(self, ms, ns, interruptShouldThrow, why);
 }
 
-void Monitor::WaitWithLock(Thread* self, int64_t ms, int32_t ns, bool interruptShouldThrow) {
+void Monitor::WaitWithLock(Thread* self, int64_t ms, int32_t ns,
+                           bool interruptShouldThrow, ThreadState why) {
   // Enforce the timeout range.
   if (ms < 0 || ns < 0 || ns > 999999) {
     Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
@@ -419,12 +424,11 @@
   locking_dex_pc_ = 0;
 
   /*
-   * Update thread status.  If the GC wakes up, it'll ignore us, knowing
+   * Update thread state. If the GC wakes up, it'll ignore us, knowing
    * that we won't touch any references in this state, and we'll check
    * our suspend mode before we transition out.
    */
-  bool timed = (ms != 0 || ns != 0);
-  self->TransitionFromRunnableToSuspended(timed ? kTimedWaiting : kWaiting);
+  self->TransitionFromRunnableToSuspended(why);
 
   bool wasInterrupted = false;
   {
@@ -448,9 +452,10 @@
       wasInterrupted = true;
     } else {
       // Wait for a notification or a timeout to occur.
-      if (!timed) {
+      if (why == kWaiting) {
         self->wait_cond_->Wait(self);
       } else {
+        DCHECK(why == kTimedWaiting || why == kSleeping) << why;
         self->wait_cond_->TimedWait(self, ms, ns);
       }
       if (self->interrupted_) {
@@ -733,7 +738,8 @@
 /*
  * Object.wait().  Also called for class init.
  */
-void Monitor::Wait(Thread* self, Object *obj, int64_t ms, int32_t ns, bool interruptShouldThrow) {
+void Monitor::Wait(Thread* self, Object *obj, int64_t ms, int32_t ns,
+                   bool interruptShouldThrow, ThreadState why) {
   volatile int32_t* thinp = obj->GetRawLockWordAddress();
 
   // If the lock is still thin, we need to fatten it.
@@ -753,7 +759,7 @@
     Inflate(self, obj);
     VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p by wait()", self->GetThinLockId(), thinp);
   }
-  LW_MONITOR(*thinp)->Wait(self, ms, ns, interruptShouldThrow);
+  LW_MONITOR(*thinp)->Wait(self, ms, ns, interruptShouldThrow, why);
 }
 
 void Monitor::Notify(Thread* self, Object *obj) {