Fix a deadlock caused by my big threading change yesterday.

Also add even more locking diagnostics that catch this class of error and
explain what you've done in clear terms. In this case:

03-02 16:42:46.040 20768 20785 E art     : holding ThreadListLock while doing condition variable wait on ThreadSuspendCountLock
...
03-02 16:42:46.493 23421 23421 I DEBUG   :     #01  pc 00149e5f  /system/lib/libartd.so (art::Runtime::Abort(char const*, int)+338)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #02  pc 00113c0b  /system/lib/libartd.so (art::LogMessage::~LogMessage()+1026)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #03  pc 0015a723  /system/lib/libartd.so (art::Thread::CheckSafeToWait(art::MutexRank)+290)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #04  pc 0012481f  /system/lib/libartd.so (art::ConditionVariable::Wait(art::Mutex&)+26)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #05  pc 001631fb  /system/lib/libartd.so (art::ThreadList::FullSuspendCheck(art::Thread*)+154)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #06  pc 00159ffd  /system/lib/libartd.so (art::Thread::SetState(art::Thread::State)+128)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #07  pc 00161bb5  /system/lib/libartd.so (art::ScopedThreadListLock::ScopedThreadListLock()+64)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #08  pc 00164a31  /system/lib/libartd.so (art::ThreadList::Unregister()+92)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #09  pc 0015e537  /system/lib/libartd.so (art::Thread::CreateCallback(void*)+230)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #10  pc 00013b08  /system/lib/libc.so (__thread_entry+48)
03-02 16:42:46.493 23421 23421 I DEBUG   :     #11  pc 0001363c  /system/lib/libc.so (pthread_create+180)

Change-Id: I9cdb770e766f63359ab7d11ee1993dd7a6fc1c90
diff --git a/src/mutex.cc b/src/mutex.cc
index 963ac99..f7c3143 100644
--- a/src/mutex.cc
+++ b/src/mutex.cc
@@ -27,14 +27,23 @@
 
 namespace art {
 
-static inline void CheckRank(MutexRank rank, bool is_locking) {
+static inline void CheckSafeToLockOrUnlock(MutexRank rank, bool is_locking) {
 #ifndef NDEBUG
   if (rank == -1) {
     return;
   }
   Thread* self = Thread::Current();
   if (self != NULL) {
-    self->CheckRank(rank, is_locking);
+    self->CheckSafeToLockOrUnlock(rank, is_locking);
+  }
+#endif
+}
+
+static inline void CheckSafeToWait(MutexRank rank) {
+#ifndef NDEBUG
+  Thread* self = Thread::Current();
+  if (self != NULL) {
+    self->CheckSafeToWait(rank);
   }
 #endif
 }
@@ -58,8 +67,8 @@
 }
 
 void Mutex::Lock() {
+  CheckSafeToLockOrUnlock(rank_, true);
   CHECK_MUTEX_CALL(pthread_mutex_lock, (&mutex_));
-  CheckRank(rank_, true);
   AssertHeld();
 }
 
@@ -72,15 +81,15 @@
     errno = result;
     PLOG(FATAL) << "pthread_mutex_trylock failed for " << name_;
   }
-  CheckRank(rank_, true);
+  CheckSafeToLockOrUnlock(rank_, true);
   AssertHeld();
   return true;
 }
 
 void Mutex::Unlock() {
   AssertHeld();
+  CheckSafeToLockOrUnlock(rank_, false);
   CHECK_MUTEX_CALL(pthread_mutex_unlock, (&mutex_));
-  CheckRank(rank_, false);
 }
 
 pid_t Mutex::GetOwner() {
@@ -151,6 +160,7 @@
 }
 
 void ConditionVariable::Wait(Mutex& mutex) {
+  CheckSafeToWait(mutex.GetRank());
   CHECK_MUTEX_CALL(pthread_cond_wait, (&cond_, mutex.GetImpl()));
 }
 
@@ -160,6 +170,7 @@
 #else
 #define TIMEDWAIT pthread_cond_timedwait
 #endif
+  CheckSafeToWait(mutex.GetRank());
   int rc = TIMEDWAIT(&cond_, mutex.GetImpl(), &ts);
   if (rc != 0 && rc != ETIMEDOUT) {
     errno = rc;