Fix failing check during JDWP invoke.

Fixes a race where the JDWP thread expects the invoke thread can be suspended
(suspend_count != 0) before invoke thread actually updates its suspend count.

It happens after the invoke thread signals the JDWP thread to notify invoke is
completed but before incrementing its suspend count. In the meantime, the JDWP
thread wakes up and checks whether invoke thread has updated its supend count.
But there is no way to prevent the JDWP thread to do so before the invoke
thread updates its suspend count.

We now move the invoke completion code after the suspend count update. Then the
JDWP thread wakes up at the right time.

Bug: 11247837
Bug: 12578041
Change-Id: Ib6079c6e330671b34217838e26b1e758706d3da6
diff --git a/runtime/jdwp/jdwp_event.cc b/runtime/jdwp/jdwp_event.cc
index e372c26..677b04b 100644
--- a/runtime/jdwp/jdwp_event.cc
+++ b/runtime/jdwp/jdwp_event.cc
@@ -576,14 +576,6 @@
     Dbg::ExecuteMethod(pReq);
 
     pReq->error = ERR_NONE;
-
-    /* clear this before signaling */
-    pReq->invoke_needed = false;
-
-    VLOG(jdwp) << "invoke complete, signaling and self-suspending";
-    Thread* self = Thread::Current();
-    MutexLock mu(self, pReq->lock);
-    pReq->cond.Signal(self);
   }
 }
 
diff --git a/runtime/thread_list.cc b/runtime/thread_list.cc
index 25f692d..9f67c96 100644
--- a/runtime/thread_list.cc
+++ b/runtime/thread_list.cc
@@ -577,6 +577,18 @@
 
   VLOG(threads) << *self << " self-suspending (debugger)";
 
+  // Tell JDWP we've completed invocation and are ready to suspend.
+  DebugInvokeReq* pReq = self->GetInvokeReq();
+  DCHECK(pReq != NULL);
+  if (pReq->invoke_needed) {
+    // Clear this before signaling.
+    pReq->invoke_needed = false;
+
+    VLOG(jdwp) << "invoke complete, signaling";
+    MutexLock mu(self, pReq->lock);
+    pReq->cond.Signal(self);
+  }
+
   // Tell JDWP that we've completed suspension. The JDWP thread can't
   // tell us to resume before we're fully asleep because we hold the
   // suspend count lock.