Pass self to lock methods.

This avoids frequent recomputation of
Thread::Current/pthread_getspecific.

Also add a futex based reader/writer mutex that is disabled.

Change-Id: I118fdb99ef1d1c4bfda6446ba3a0d8b6ab31eaee
diff --git a/src/jdwp/jdwp_event.cc b/src/jdwp/jdwp_event.cc
index 4f11a65..19f10eb 100644
--- a/src/jdwp/jdwp_event.cc
+++ b/src/jdwp/jdwp_event.cc
@@ -585,7 +585,8 @@
   bool waited = false;
 
   /* this is held for very brief periods; contention is unlikely */
-  MutexLock mu(event_thread_lock_);
+  Thread* self = Thread::Current();
+  MutexLock mu(self, event_thread_lock_);
 
   /*
    * If another thread is already doing stuff, wait for it.  This can
@@ -594,7 +595,7 @@
   while (event_thread_id_ != 0) {
     VLOG(jdwp) << StringPrintf("event in progress (%#llx), %#llx sleeping", event_thread_id_, threadId);
     waited = true;
-    event_thread_cond_.Wait(event_thread_lock_);
+    event_thread_cond_.Wait(self, event_thread_lock_);
   }
 
   if (waited || threadId != 0) {
@@ -1075,7 +1076,7 @@
   Thread* self = Thread::Current();
   bool safe_to_release_mutator_lock_over_send;
   for (size_t i=0; i < kMutatorLock; ++i) {
-    if (self->GetHeldMutex(static_cast<MutexLevel>(i)) != NULL) {
+    if (self->GetHeldMutex(static_cast<LockLevel>(i)) != NULL) {
       safe_to_release_mutator_lock_over_send = false;
       break;
     }
diff --git a/src/jdwp/jdwp_main.cc b/src/jdwp/jdwp_main.cc
index 4fec005..a09c488 100644
--- a/src/jdwp/jdwp_main.cc
+++ b/src/jdwp/jdwp_main.cc
@@ -118,7 +118,8 @@
  * the thread is accepting network connections.
  */
 JdwpState* JdwpState::Create(const JdwpOptions* options) {
-  Locks::mutator_lock_->AssertNotHeld();
+  Thread* self = Thread::Current();
+  Locks::mutator_lock_->AssertNotHeld(self);
   UniquePtr<JdwpState> state(new JdwpState(options));
   switch (options->transport) {
   case kJdwpTransportSocket:
@@ -157,7 +158,7 @@
        * Wait until the thread finishes basic initialization.
        * TODO: cond vars should be waited upon in a loop
        */
-      state->thread_start_cond_.Wait(state->thread_start_lock_);
+      state->thread_start_cond_.Wait(self, state->thread_start_lock_);
     } else {
       {
         MutexLock attach_locker(state->attach_lock_);
@@ -171,7 +172,7 @@
          * Wait until the thread finishes basic initialization.
          * TODO: cond vars should be waited upon in a loop
          */
-        state->thread_start_cond_.Wait(state->thread_start_lock_);
+        state->thread_start_cond_.Wait(self, state->thread_start_lock_);
 
         /*
          * For suspend=y, wait for the debugger to connect to us or for us to
@@ -182,8 +183,8 @@
          * when we wake up.
          */
         {
-          ScopedThreadStateChange tsc(Thread::Current(), kWaitingForDebuggerToAttach);
-          state->attach_cond_.Wait(state->attach_lock_);
+          ScopedThreadStateChange tsc(self, kWaitingForDebuggerToAttach);
+          state->attach_cond_.Wait(self, state->attach_lock_);
         }
       }
       if (!state->IsActive()) {
@@ -294,14 +295,15 @@
   thread_ = Thread::Current();
   run = true;
 
-  thread_start_lock_.Lock();
-  debug_thread_started_ = true;
-  thread_start_cond_.Broadcast();
-  thread_start_lock_.Unlock();
+  {
+    MutexLock locker(thread_, thread_start_lock_);
+    debug_thread_started_ = true;
+    thread_start_cond_.Broadcast();
+  }
 
   /* set the thread state to kWaitingInMainDebuggerLoop so GCs don't wait for us */
   {
-    MutexLock mu(*Locks::thread_suspend_count_lock_);
+    MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
     CHECK_EQ(thread_->GetState(), kNative);
     thread_->SetState(kWaitingInMainDebuggerLoop);
   }
@@ -346,7 +348,7 @@
     while (!Dbg::IsDisposed()) {
       {
         // sanity check -- shouldn't happen?
-        MutexLock mu(*Locks::thread_suspend_count_lock_);
+        MutexLock mu(thread_, *Locks::thread_suspend_count_lock_);
         CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
       }
 
@@ -366,7 +368,7 @@
         }
 
         /* wake anybody who's waiting for us */
-        MutexLock mu(attach_lock_);
+        MutexLock mu(thread_, attach_lock_);
         attach_cond_.Broadcast();
       }
     }
@@ -400,11 +402,8 @@
   }
 
   /* back to native, for thread shutdown */
-  {
-    MutexLock mu(*Locks::thread_suspend_count_lock_);
-    CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
-    thread_->SetState(kNative);
-  }
+  CHECK_EQ(thread_->GetState(), kWaitingInMainDebuggerLoop);
+  thread_->SetState(kNative);
 
   VLOG(jdwp) << "JDWP: thread detaching and exiting...";
   runtime->DetachCurrentThread();