Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "monitor.h"
     18 
     19 #include <vector>
     20 
     21 #include "android-base/stringprintf.h"
     22 
     23 #include "art_method-inl.h"
     24 #include "base/mutex.h"
     25 #include "base/stl_util.h"
     26 #include "base/systrace.h"
     27 #include "base/time_utils.h"
     28 #include "class_linker.h"
     29 #include "dex_file-inl.h"
     30 #include "dex_instruction-inl.h"
     31 #include "lock_word-inl.h"
     32 #include "mirror/class-inl.h"
     33 #include "mirror/object-inl.h"
     34 #include "object_callbacks.h"
     35 #include "scoped_thread_state_change-inl.h"
     36 #include "stack.h"
     37 #include "thread.h"
     38 #include "thread_list.h"
     39 #include "verifier/method_verifier.h"
     40 #include "well_known_classes.h"
     41 
     42 namespace art {
     43 
     44 using android::base::StringPrintf;
     45 
     46 static constexpr uint64_t kLongWaitMs = 100;
     47 
     48 /*
     49  * Every Object has a monitor associated with it, but not every Object is actually locked.  Even
     50  * the ones that are locked do not need a full-fledged monitor until a) there is actual contention
     51  * or b) wait() is called on the Object.
     52  *
     53  * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s
     54  * "Thin locks: featherweight synchronization for Java" (ACM 1998).  Things are even easier for us,
     55  * though, because we have a full 32 bits to work with.
     56  *
     57  * The two states of an Object's lock are referred to as "thin" and "fat".  A lock may transition
     58  * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once
     59  * a lock has been inflated it remains in the "fat" state indefinitely.
     60  *
     61  * The lock value itself is stored in mirror::Object::monitor_ and the representation is described
     62  * in the LockWord value type.
     63  *
     64  * Monitors provide:
     65  *  - mutually exclusive access to resources
     66  *  - a way for multiple threads to wait for notification
     67  *
     68  * In effect, they fill the role of both mutexes and condition variables.
     69  *
     70  * Only one thread can own the monitor at any time.  There may be several threads waiting on it
     71  * (the wait call unlocks it).  One or more waiting threads may be getting interrupted or notified
     72  * at any given time.
     73  */
     74 
     75 uint32_t Monitor::lock_profiling_threshold_ = 0;
     76 uint32_t Monitor::stack_dump_lock_profiling_threshold_ = 0;
     77 
     78 void Monitor::Init(uint32_t lock_profiling_threshold,
     79                    uint32_t stack_dump_lock_profiling_threshold) {
     80   lock_profiling_threshold_ = lock_profiling_threshold;
     81   stack_dump_lock_profiling_threshold_ = stack_dump_lock_profiling_threshold;
     82 }
     83 
     84 Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
     85     : monitor_lock_("a monitor lock", kMonitorLock),
     86       monitor_contenders_("monitor contenders", monitor_lock_),
     87       num_waiters_(0),
     88       owner_(owner),
     89       lock_count_(0),
     90       obj_(GcRoot<mirror::Object>(obj)),
     91       wait_set_(nullptr),
     92       hash_code_(hash_code),
     93       locking_method_(nullptr),
     94       locking_dex_pc_(0),
     95       monitor_id_(MonitorPool::ComputeMonitorId(this, self)) {
     96 #ifdef __LP64__
     97   DCHECK(false) << "Should not be reached in 64b";
     98   next_free_ = nullptr;
     99 #endif
    100   // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
    101   // with the owner unlocking the thin-lock.
    102   CHECK(owner == nullptr || owner == self || owner->IsSuspended());
    103   // The identity hash code is set for the life time of the monitor.
    104 }
    105 
    106 Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code,
    107                  MonitorId id)
    108     : monitor_lock_("a monitor lock", kMonitorLock),
    109       monitor_contenders_("monitor contenders", monitor_lock_),
    110       num_waiters_(0),
    111       owner_(owner),
    112       lock_count_(0),
    113       obj_(GcRoot<mirror::Object>(obj)),
    114       wait_set_(nullptr),
    115       hash_code_(hash_code),
    116       locking_method_(nullptr),
    117       locking_dex_pc_(0),
    118       monitor_id_(id) {
    119 #ifdef __LP64__
    120   next_free_ = nullptr;
    121 #endif
    122   // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
    123   // with the owner unlocking the thin-lock.
    124   CHECK(owner == nullptr || owner == self || owner->IsSuspended());
    125   // The identity hash code is set for the life time of the monitor.
    126 }
    127 
    128 int32_t Monitor::GetHashCode() {
    129   while (!HasHashCode()) {
    130     if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) {
    131       break;
    132     }
    133   }
    134   DCHECK(HasHashCode());
    135   return hash_code_.LoadRelaxed();
    136 }
    137 
    138 bool Monitor::Install(Thread* self) {
    139   MutexLock mu(self, monitor_lock_);  // Uncontended mutex acquisition as monitor isn't yet public.
    140   CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended());
    141   // Propagate the lock state.
    142   LockWord lw(GetObject()->GetLockWord(false));
    143   switch (lw.GetState()) {
    144     case LockWord::kThinLocked: {
    145       CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner());
    146       lock_count_ = lw.ThinLockCount();
    147       break;
    148     }
    149     case LockWord::kHashCode: {
    150       CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode()));
    151       break;
    152     }
    153     case LockWord::kFatLocked: {
    154       // The owner_ is suspended but another thread beat us to install a monitor.
    155       return false;
    156     }
    157     case LockWord::kUnlocked: {
    158       LOG(FATAL) << "Inflating unlocked lock word";
    159       break;
    160     }
    161     default: {
    162       LOG(FATAL) << "Invalid monitor state " << lw.GetState();
    163       return false;
    164     }
    165   }
    166   LockWord fat(this, lw.GCState());
    167   // Publish the updated lock word, which may race with other threads.
    168   bool success = GetObject()->CasLockWordWeakRelease(lw, fat);
    169   // Lock profiling.
    170   if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) {
    171     // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on
    172     // abort.
    173     locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false);
    174   }
    175   return success;
    176 }
    177 
    178 Monitor::~Monitor() {
    179   // Deflated monitors have a null object.
    180 }
    181 
    182 void Monitor::AppendToWaitSet(Thread* thread) {
    183   DCHECK(owner_ == Thread::Current());
    184   DCHECK(thread != nullptr);
    185   DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext();
    186   if (wait_set_ == nullptr) {
    187     wait_set_ = thread;
    188     return;
    189   }
    190 
    191   // push_back.
    192   Thread* t = wait_set_;
    193   while (t->GetWaitNext() != nullptr) {
    194     t = t->GetWaitNext();
    195   }
    196   t->SetWaitNext(thread);
    197 }
    198 
    199 void Monitor::RemoveFromWaitSet(Thread *thread) {
    200   DCHECK(owner_ == Thread::Current());
    201   DCHECK(thread != nullptr);
    202   if (wait_set_ == nullptr) {
    203     return;
    204   }
    205   if (wait_set_ == thread) {
    206     wait_set_ = thread->GetWaitNext();
    207     thread->SetWaitNext(nullptr);
    208     return;
    209   }
    210 
    211   Thread* t = wait_set_;
    212   while (t->GetWaitNext() != nullptr) {
    213     if (t->GetWaitNext() == thread) {
    214       t->SetWaitNext(thread->GetWaitNext());
    215       thread->SetWaitNext(nullptr);
    216       return;
    217     }
    218     t = t->GetWaitNext();
    219   }
    220 }
    221 
    222 void Monitor::SetObject(mirror::Object* object) {
    223   obj_ = GcRoot<mirror::Object>(object);
    224 }
    225 
    226 // Note: Adapted from CurrentMethodVisitor in thread.cc. We must not resolve here.
    227 
    228 struct NthCallerWithDexPcVisitor FINAL : public StackVisitor {
    229   explicit NthCallerWithDexPcVisitor(Thread* thread, size_t frame)
    230       REQUIRES_SHARED(Locks::mutator_lock_)
    231       : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
    232         method_(nullptr),
    233         dex_pc_(0),
    234         current_frame_number_(0),
    235         wanted_frame_number_(frame) {}
    236   bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
    237     ArtMethod* m = GetMethod();
    238     if (m == nullptr || m->IsRuntimeMethod()) {
    239       // Runtime method, upcall, or resolution issue. Skip.
    240       return true;
    241     }
    242 
    243     // Is this the requested frame?
    244     if (current_frame_number_ == wanted_frame_number_) {
    245       method_ = m;
    246       dex_pc_ = GetDexPc(false /* abort_on_error*/);
    247       return false;
    248     }
    249 
    250     // Look for more.
    251     current_frame_number_++;
    252     return true;
    253   }
    254 
    255   ArtMethod* method_;
    256   uint32_t dex_pc_;
    257 
    258  private:
    259   size_t current_frame_number_;
    260   const size_t wanted_frame_number_;
    261 };
    262 
    263 // This function is inlined and just helps to not have the VLOG and ATRACE check at all the
    264 // potential tracing points.
    265 void Monitor::AtraceMonitorLock(Thread* self, mirror::Object* obj, bool is_wait) {
    266   if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging) && ATRACE_ENABLED())) {
    267     AtraceMonitorLockImpl(self, obj, is_wait);
    268   }
    269 }
    270 
    271 void Monitor::AtraceMonitorLockImpl(Thread* self, mirror::Object* obj, bool is_wait) {
    272   // Wait() requires a deeper call stack to be useful. Otherwise you'll see "Waiting at
    273   // Object.java". Assume that we'll wait a nontrivial amount, so it's OK to do a longer
    274   // stack walk than if !is_wait.
    275   NthCallerWithDexPcVisitor visitor(self, is_wait ? 1U : 0U);
    276   visitor.WalkStack(false);
    277   const char* prefix = is_wait ? "Waiting on " : "Locking ";
    278 
    279   const char* filename;
    280   int32_t line_number;
    281   TranslateLocation(visitor.method_, visitor.dex_pc_, &filename, &line_number);
    282 
    283   // It would be nice to have a stable "ID" for the object here. However, the only stable thing
    284   // would be the identity hashcode. But we cannot use IdentityHashcode here: For one, there are
    285   // times when it is unsafe to make that call (see stack dumping for an explanation). More
    286   // importantly, we would have to give up on thin-locking when adding systrace locks, as the
    287   // identity hashcode is stored in the lockword normally (so can't be used with thin-locks).
    288   //
    289   // Because of thin-locks we also cannot use the monitor id (as there is no monitor). Monitor ids
    290   // also do not have to be stable, as the monitor may be deflated.
    291   std::string tmp = StringPrintf("%s %d at %s:%d",
    292       prefix,
    293       (obj == nullptr ? -1 : static_cast<int32_t>(reinterpret_cast<uintptr_t>(obj))),
    294       (filename != nullptr ? filename : "null"),
    295       line_number);
    296   ATRACE_BEGIN(tmp.c_str());
    297 }
    298 
    299 void Monitor::AtraceMonitorUnlock() {
    300   if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging))) {
    301     ATRACE_END();
    302   }
    303 }
    304 
    305 std::string Monitor::PrettyContentionInfo(const std::string& owner_name,
    306                                           pid_t owner_tid,
    307                                           ArtMethod* owners_method,
    308                                           uint32_t owners_dex_pc,
    309                                           size_t num_waiters) {
    310   Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
    311   const char* owners_filename;
    312   int32_t owners_line_number = 0;
    313   if (owners_method != nullptr) {
    314     TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
    315   }
    316   std::ostringstream oss;
    317   oss << "monitor contention with owner " << owner_name << " (" << owner_tid << ")";
    318   if (owners_method != nullptr) {
    319     oss << " at " << owners_method->PrettyMethod();
    320     oss << "(" << owners_filename << ":" << owners_line_number << ")";
    321   }
    322   oss << " waiters=" << num_waiters;
    323   return oss.str();
    324 }
    325 
    326 bool Monitor::TryLockLocked(Thread* self) {
    327   if (owner_ == nullptr) {  // Unowned.
    328     owner_ = self;
    329     CHECK_EQ(lock_count_, 0);
    330     // When debugging, save the current monitor holder for future
    331     // acquisition failures to use in sampled logging.
    332     if (lock_profiling_threshold_ != 0) {
    333       locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
    334     }
    335   } else if (owner_ == self) {  // Recursive.
    336     lock_count_++;
    337   } else {
    338     return false;
    339   }
    340   AtraceMonitorLock(self, GetObject(), false /* is_wait */);
    341   return true;
    342 }
    343 
    344 bool Monitor::TryLock(Thread* self) {
    345   MutexLock mu(self, monitor_lock_);
    346   return TryLockLocked(self);
    347 }
    348 
    349 void Monitor::Lock(Thread* self) {
    350   MutexLock mu(self, monitor_lock_);
    351   while (true) {
    352     if (TryLockLocked(self)) {
    353       return;
    354     }
    355     // Contended.
    356     const bool log_contention = (lock_profiling_threshold_ != 0);
    357     uint64_t wait_start_ms = log_contention ? MilliTime() : 0;
    358     ArtMethod* owners_method = locking_method_;
    359     uint32_t owners_dex_pc = locking_dex_pc_;
    360     // Do this before releasing the lock so that we don't get deflated.
    361     size_t num_waiters = num_waiters_;
    362     ++num_waiters_;
    363 
    364     // If systrace logging is enabled, first look at the lock owner. Acquiring the monitor's
    365     // lock and then re-acquiring the mutator lock can deadlock.
    366     bool started_trace = false;
    367     if (ATRACE_ENABLED()) {
    368       if (owner_ != nullptr) {  // Did the owner_ give the lock up?
    369         std::ostringstream oss;
    370         std::string name;
    371         owner_->GetThreadName(name);
    372         oss << PrettyContentionInfo(name,
    373                                     owner_->GetTid(),
    374                                     owners_method,
    375                                     owners_dex_pc,
    376                                     num_waiters);
    377         // Add info for contending thread.
    378         uint32_t pc;
    379         ArtMethod* m = self->GetCurrentMethod(&pc);
    380         const char* filename;
    381         int32_t line_number;
    382         TranslateLocation(m, pc, &filename, &line_number);
    383         oss << " blocking from "
    384             << ArtMethod::PrettyMethod(m) << "(" << (filename != nullptr ? filename : "null")
    385             << ":" << line_number << ")";
    386         ATRACE_BEGIN(oss.str().c_str());
    387         started_trace = true;
    388       }
    389     }
    390 
    391     monitor_lock_.Unlock(self);  // Let go of locks in order.
    392     self->SetMonitorEnterObject(GetObject());
    393     {
    394       ScopedThreadSuspension tsc(self, kBlocked);  // Change to blocked and give up mutator_lock_.
    395       uint32_t original_owner_thread_id = 0u;
    396       {
    397         // Reacquire monitor_lock_ without mutator_lock_ for Wait.
    398         MutexLock mu2(self, monitor_lock_);
    399         if (owner_ != nullptr) {  // Did the owner_ give the lock up?
    400           original_owner_thread_id = owner_->GetThreadId();
    401           monitor_contenders_.Wait(self);  // Still contended so wait.
    402         }
    403       }
    404       if (original_owner_thread_id != 0u) {
    405         // Woken from contention.
    406         if (log_contention) {
    407           uint64_t wait_ms = MilliTime() - wait_start_ms;
    408           uint32_t sample_percent;
    409           if (wait_ms >= lock_profiling_threshold_) {
    410             sample_percent = 100;
    411           } else {
    412             sample_percent = 100 * wait_ms / lock_profiling_threshold_;
    413           }
    414           if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
    415             // Reacquire mutator_lock_ for logging.
    416             ScopedObjectAccess soa(self);
    417 
    418             bool owner_alive = false;
    419             pid_t original_owner_tid = 0;
    420             std::string original_owner_name;
    421 
    422             const bool should_dump_stacks = stack_dump_lock_profiling_threshold_ > 0 &&
    423                 wait_ms > stack_dump_lock_profiling_threshold_;
    424             std::string owner_stack_dump;
    425 
    426             // Acquire thread-list lock to find thread and keep it from dying until we've got all
    427             // the info we need.
    428             {
    429               MutexLock mu2(Thread::Current(), *Locks::thread_list_lock_);
    430 
    431               // Re-find the owner in case the thread got killed.
    432               Thread* original_owner = Runtime::Current()->GetThreadList()->FindThreadByThreadId(
    433                   original_owner_thread_id);
    434 
    435               if (original_owner != nullptr) {
    436                 owner_alive = true;
    437                 original_owner_tid = original_owner->GetTid();
    438                 original_owner->GetThreadName(original_owner_name);
    439 
    440                 if (should_dump_stacks) {
    441                   // Very long contention. Dump stacks.
    442                   struct CollectStackTrace : public Closure {
    443                     void Run(art::Thread* thread) OVERRIDE
    444                         REQUIRES_SHARED(art::Locks::mutator_lock_) {
    445                       thread->DumpJavaStack(oss);
    446                     }
    447 
    448                     std::ostringstream oss;
    449                   };
    450                   CollectStackTrace owner_trace;
    451                   original_owner->RequestSynchronousCheckpoint(&owner_trace);
    452                   owner_stack_dump = owner_trace.oss.str();
    453                 }
    454               }
    455               // This is all the data we need. Now drop the thread-list lock, it's OK for the
    456               // owner to go away now.
    457             }
    458 
    459             // If we found the owner (and thus have owner data), go and log now.
    460             if (owner_alive) {
    461               // Give the detailed traces for really long contention.
    462               if (should_dump_stacks) {
    463                 // This must be here (and not above) because we cannot hold the thread-list lock
    464                 // while running the checkpoint.
    465                 std::ostringstream self_trace_oss;
    466                 self->DumpJavaStack(self_trace_oss);
    467 
    468                 uint32_t pc;
    469                 ArtMethod* m = self->GetCurrentMethod(&pc);
    470 
    471                 LOG(WARNING) << "Long "
    472                     << PrettyContentionInfo(original_owner_name,
    473                                             original_owner_tid,
    474                                             owners_method,
    475                                             owners_dex_pc,
    476                                             num_waiters)
    477                     << " in " << ArtMethod::PrettyMethod(m) << " for "
    478                     << PrettyDuration(MsToNs(wait_ms)) << "\n"
    479                     << "Current owner stack:\n" << owner_stack_dump
    480                     << "Contender stack:\n" << self_trace_oss.str();
    481               } else if (wait_ms > kLongWaitMs && owners_method != nullptr) {
    482                 uint32_t pc;
    483                 ArtMethod* m = self->GetCurrentMethod(&pc);
    484                 // TODO: We should maybe check that original_owner is still a live thread.
    485                 LOG(WARNING) << "Long "
    486                     << PrettyContentionInfo(original_owner_name,
    487                                             original_owner_tid,
    488                                             owners_method,
    489                                             owners_dex_pc,
    490                                             num_waiters)
    491                     << " in " << ArtMethod::PrettyMethod(m) << " for "
    492                     << PrettyDuration(MsToNs(wait_ms));
    493               }
    494               LogContentionEvent(self,
    495                                  wait_ms,
    496                                  sample_percent,
    497                                  owners_method,
    498                                  owners_dex_pc);
    499             }
    500           }
    501         }
    502       }
    503     }
    504     if (started_trace) {
    505       ATRACE_END();
    506     }
    507     self->SetMonitorEnterObject(nullptr);
    508     monitor_lock_.Lock(self);  // Reacquire locks in order.
    509     --num_waiters_;
    510   }
    511 }
    512 
    513 static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
    514                                               __attribute__((format(printf, 1, 2)));
    515 
    516 static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
    517     REQUIRES_SHARED(Locks::mutator_lock_) {
    518   va_list args;
    519   va_start(args, fmt);
    520   Thread* self = Thread::Current();
    521   self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args);
    522   if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
    523     std::ostringstream ss;
    524     self->Dump(ss);
    525     LOG(Runtime::Current()->IsStarted() ? ::android::base::INFO : ::android::base::ERROR)
    526         << self->GetException()->Dump() << "\n" << ss.str();
    527   }
    528   va_end(args);
    529 }
    530 
    531 static std::string ThreadToString(Thread* thread) {
    532   if (thread == nullptr) {
    533     return "nullptr";
    534   }
    535   std::ostringstream oss;
    536   // TODO: alternatively, we could just return the thread's name.
    537   oss << *thread;
    538   return oss.str();
    539 }
    540 
    541 void Monitor::FailedUnlock(mirror::Object* o,
    542                            uint32_t expected_owner_thread_id,
    543                            uint32_t found_owner_thread_id,
    544                            Monitor* monitor) {
    545   // Acquire thread list lock so threads won't disappear from under us.
    546   std::string current_owner_string;
    547   std::string expected_owner_string;
    548   std::string found_owner_string;
    549   uint32_t current_owner_thread_id = 0u;
    550   {
    551     MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
    552     ThreadList* const thread_list = Runtime::Current()->GetThreadList();
    553     Thread* expected_owner = thread_list->FindThreadByThreadId(expected_owner_thread_id);
    554     Thread* found_owner = thread_list->FindThreadByThreadId(found_owner_thread_id);
    555 
    556     // Re-read owner now that we hold lock.
    557     Thread* current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr;
    558     if (current_owner != nullptr) {
    559       current_owner_thread_id = current_owner->GetThreadId();
    560     }
    561     // Get short descriptions of the threads involved.
    562     current_owner_string = ThreadToString(current_owner);
    563     expected_owner_string = expected_owner != nullptr ? ThreadToString(expected_owner) : "unnamed";
    564     found_owner_string = found_owner != nullptr ? ThreadToString(found_owner) : "unnamed";
    565   }
    566 
    567   if (current_owner_thread_id == 0u) {
    568     if (found_owner_thread_id == 0u) {
    569       ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
    570                                          " on thread '%s'",
    571                                          mirror::Object::PrettyTypeOf(o).c_str(),
    572                                          expected_owner_string.c_str());
    573     } else {
    574       // Race: the original read found an owner but now there is none
    575       ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
    576                                          " (where now the monitor appears unowned) on thread '%s'",
    577                                          found_owner_string.c_str(),
    578                                          mirror::Object::PrettyTypeOf(o).c_str(),
    579                                          expected_owner_string.c_str());
    580     }
    581   } else {
    582     if (found_owner_thread_id == 0u) {
    583       // Race: originally there was no owner, there is now
    584       ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
    585                                          " (originally believed to be unowned) on thread '%s'",
    586                                          current_owner_string.c_str(),
    587                                          mirror::Object::PrettyTypeOf(o).c_str(),
    588                                          expected_owner_string.c_str());
    589     } else {
    590       if (found_owner_thread_id != current_owner_thread_id) {
    591         // Race: originally found and current owner have changed
    592         ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
    593                                            " owned by '%s') on object of type '%s' on thread '%s'",
    594                                            found_owner_string.c_str(),
    595                                            current_owner_string.c_str(),
    596                                            mirror::Object::PrettyTypeOf(o).c_str(),
    597                                            expected_owner_string.c_str());
    598       } else {
    599         ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
    600                                            " on thread '%s",
    601                                            current_owner_string.c_str(),
    602                                            mirror::Object::PrettyTypeOf(o).c_str(),
    603                                            expected_owner_string.c_str());
    604       }
    605     }
    606   }
    607 }
    608 
    609 bool Monitor::Unlock(Thread* self) {
    610   DCHECK(self != nullptr);
    611   uint32_t owner_thread_id = 0u;
    612   {
    613     MutexLock mu(self, monitor_lock_);
    614     Thread* owner = owner_;
    615     if (owner != nullptr) {
    616       owner_thread_id = owner->GetThreadId();
    617     }
    618     if (owner == self) {
    619       // We own the monitor, so nobody else can be in here.
    620       AtraceMonitorUnlock();
    621       if (lock_count_ == 0) {
    622         owner_ = nullptr;
    623         locking_method_ = nullptr;
    624         locking_dex_pc_ = 0;
    625         // Wake a contender.
    626         monitor_contenders_.Signal(self);
    627       } else {
    628         --lock_count_;
    629       }
    630       return true;
    631     }
    632   }
    633   // We don't own this, so we're not allowed to unlock it.
    634   // The JNI spec says that we should throw IllegalMonitorStateException in this case.
    635   FailedUnlock(GetObject(), self->GetThreadId(), owner_thread_id, this);
    636   return false;
    637 }
    638 
    639 void Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
    640                    bool interruptShouldThrow, ThreadState why) {
    641   DCHECK(self != nullptr);
    642   DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
    643 
    644   monitor_lock_.Lock(self);
    645 
    646   // Make sure that we hold the lock.
    647   if (owner_ != self) {
    648     monitor_lock_.Unlock(self);
    649     ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
    650     return;
    651   }
    652 
    653   // We need to turn a zero-length timed wait into a regular wait because
    654   // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
    655   if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
    656     why = kWaiting;
    657   }
    658 
    659   // Enforce the timeout range.
    660   if (ms < 0 || ns < 0 || ns > 999999) {
    661     monitor_lock_.Unlock(self);
    662     self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
    663                              "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
    664     return;
    665   }
    666 
    667   /*
    668    * Add ourselves to the set of threads waiting on this monitor, and
    669    * release our hold.  We need to let it go even if we're a few levels
    670    * deep in a recursive lock, and we need to restore that later.
    671    *
    672    * We append to the wait set ahead of clearing the count and owner
    673    * fields so the subroutine can check that the calling thread owns
    674    * the monitor.  Aside from that, the order of member updates is
    675    * not order sensitive as we hold the pthread mutex.
    676    */
    677   AppendToWaitSet(self);
    678   ++num_waiters_;
    679   int prev_lock_count = lock_count_;
    680   lock_count_ = 0;
    681   owner_ = nullptr;
    682   ArtMethod* saved_method = locking_method_;
    683   locking_method_ = nullptr;
    684   uintptr_t saved_dex_pc = locking_dex_pc_;
    685   locking_dex_pc_ = 0;
    686 
    687   AtraceMonitorUnlock();  // For the implict Unlock() just above. This will only end the deepest
    688                           // nesting, but that is enough for the visualization, and corresponds to
    689                           // the single Lock() we do afterwards.
    690   AtraceMonitorLock(self, GetObject(), true /* is_wait */);
    691 
    692   bool was_interrupted = false;
    693   {
    694     // Update thread state. If the GC wakes up, it'll ignore us, knowing
    695     // that we won't touch any references in this state, and we'll check
    696     // our suspend mode before we transition out.
    697     ScopedThreadSuspension sts(self, why);
    698 
    699     // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
    700     MutexLock mu(self, *self->GetWaitMutex());
    701 
    702     // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
    703     // non-null a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
    704     // up.
    705     DCHECK(self->GetWaitMonitor() == nullptr);
    706     self->SetWaitMonitor(this);
    707 
    708     // Release the monitor lock.
    709     monitor_contenders_.Signal(self);
    710     monitor_lock_.Unlock(self);
    711 
    712     // Handle the case where the thread was interrupted before we called wait().
    713     if (self->IsInterrupted()) {
    714       was_interrupted = true;
    715     } else {
    716       // Wait for a notification or a timeout to occur.
    717       if (why == kWaiting) {
    718         self->GetWaitConditionVariable()->Wait(self);
    719       } else {
    720         DCHECK(why == kTimedWaiting || why == kSleeping) << why;
    721         self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
    722       }
    723       was_interrupted = self->IsInterrupted();
    724     }
    725   }
    726 
    727   {
    728     // We reset the thread's wait_monitor_ field after transitioning back to runnable so
    729     // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
    730     // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
    731     // are waiting on "null".)
    732     MutexLock mu(self, *self->GetWaitMutex());
    733     DCHECK(self->GetWaitMonitor() != nullptr);
    734     self->SetWaitMonitor(nullptr);
    735   }
    736 
    737   // Allocate the interrupted exception not holding the monitor lock since it may cause a GC.
    738   // If the GC requires acquiring the monitor for enqueuing cleared references, this would
    739   // cause a deadlock if the monitor is held.
    740   if (was_interrupted && interruptShouldThrow) {
    741     /*
    742      * We were interrupted while waiting, or somebody interrupted an
    743      * un-interruptible thread earlier and we're bailing out immediately.
    744      *
    745      * The doc sayeth: "The interrupted status of the current thread is
    746      * cleared when this exception is thrown."
    747      */
    748     self->SetInterrupted(false);
    749     self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr);
    750   }
    751 
    752   AtraceMonitorUnlock();  // End Wait().
    753 
    754   // Re-acquire the monitor and lock.
    755   Lock(self);
    756   monitor_lock_.Lock(self);
    757   self->GetWaitMutex()->AssertNotHeld(self);
    758 
    759   /*
    760    * We remove our thread from wait set after restoring the count
    761    * and owner fields so the subroutine can check that the calling
    762    * thread owns the monitor. Aside from that, the order of member
    763    * updates is not order sensitive as we hold the pthread mutex.
    764    */
    765   owner_ = self;
    766   lock_count_ = prev_lock_count;
    767   locking_method_ = saved_method;
    768   locking_dex_pc_ = saved_dex_pc;
    769   --num_waiters_;
    770   RemoveFromWaitSet(self);
    771 
    772   monitor_lock_.Unlock(self);
    773 }
    774 
    775 void Monitor::Notify(Thread* self) {
    776   DCHECK(self != nullptr);
    777   MutexLock mu(self, monitor_lock_);
    778   // Make sure that we hold the lock.
    779   if (owner_ != self) {
    780     ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
    781     return;
    782   }
    783   // Signal the first waiting thread in the wait set.
    784   while (wait_set_ != nullptr) {
    785     Thread* thread = wait_set_;
    786     wait_set_ = thread->GetWaitNext();
    787     thread->SetWaitNext(nullptr);
    788 
    789     // Check to see if the thread is still waiting.
    790     MutexLock wait_mu(self, *thread->GetWaitMutex());
    791     if (thread->GetWaitMonitor() != nullptr) {
    792       thread->GetWaitConditionVariable()->Signal(self);
    793       return;
    794     }
    795   }
    796 }
    797 
    798 void Monitor::NotifyAll(Thread* self) {
    799   DCHECK(self != nullptr);
    800   MutexLock mu(self, monitor_lock_);
    801   // Make sure that we hold the lock.
    802   if (owner_ != self) {
    803     ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
    804     return;
    805   }
    806   // Signal all threads in the wait set.
    807   while (wait_set_ != nullptr) {
    808     Thread* thread = wait_set_;
    809     wait_set_ = thread->GetWaitNext();
    810     thread->SetWaitNext(nullptr);
    811     thread->Notify();
    812   }
    813 }
    814 
    815 bool Monitor::Deflate(Thread* self, mirror::Object* obj) {
    816   DCHECK(obj != nullptr);
    817   // Don't need volatile since we only deflate with mutators suspended.
    818   LockWord lw(obj->GetLockWord(false));
    819   // If the lock isn't an inflated monitor, then we don't need to deflate anything.
    820   if (lw.GetState() == LockWord::kFatLocked) {
    821     Monitor* monitor = lw.FatLockMonitor();
    822     DCHECK(monitor != nullptr);
    823     MutexLock mu(self, monitor->monitor_lock_);
    824     // Can't deflate if we have anybody waiting on the CV.
    825     if (monitor->num_waiters_ > 0) {
    826       return false;
    827     }
    828     Thread* owner = monitor->owner_;
    829     if (owner != nullptr) {
    830       // Can't deflate if we are locked and have a hash code.
    831       if (monitor->HasHashCode()) {
    832         return false;
    833       }
    834       // Can't deflate if our lock count is too high.
    835       if (static_cast<uint32_t>(monitor->lock_count_) > LockWord::kThinLockMaxCount) {
    836         return false;
    837       }
    838       // Deflate to a thin lock.
    839       LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(),
    840                                                  monitor->lock_count_,
    841                                                  lw.GCState());
    842       // Assume no concurrent read barrier state changes as mutators are suspended.
    843       obj->SetLockWord(new_lw, false);
    844       VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / "
    845           << monitor->lock_count_;
    846     } else if (monitor->HasHashCode()) {
    847       LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.GCState());
    848       // Assume no concurrent read barrier state changes as mutators are suspended.
    849       obj->SetLockWord(new_lw, false);
    850       VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode();
    851     } else {
    852       // No lock and no hash, just put an empty lock word inside the object.
    853       LockWord new_lw = LockWord::FromDefault(lw.GCState());
    854       // Assume no concurrent read barrier state changes as mutators are suspended.
    855       obj->SetLockWord(new_lw, false);
    856       VLOG(monitor) << "Deflated" << obj << " to empty lock word";
    857     }
    858     // The monitor is deflated, mark the object as null so that we know to delete it during the
    859     // next GC.
    860     monitor->obj_ = GcRoot<mirror::Object>(nullptr);
    861   }
    862   return true;
    863 }
    864 
    865 void Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
    866   DCHECK(self != nullptr);
    867   DCHECK(obj != nullptr);
    868   // Allocate and acquire a new monitor.
    869   Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code);
    870   DCHECK(m != nullptr);
    871   if (m->Install(self)) {
    872     if (owner != nullptr) {
    873       VLOG(monitor) << "monitor: thread" << owner->GetThreadId()
    874           << " created monitor " << m << " for object " << obj;
    875     } else {
    876       VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code
    877           << " created monitor " << m << " for object " << obj;
    878     }
    879     Runtime::Current()->GetMonitorList()->Add(m);
    880     CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked);
    881   } else {
    882     MonitorPool::ReleaseMonitor(self, m);
    883   }
    884 }
    885 
    886 void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
    887                                 uint32_t hash_code) {
    888   DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
    889   uint32_t owner_thread_id = lock_word.ThinLockOwner();
    890   if (owner_thread_id == self->GetThreadId()) {
    891     // We own the monitor, we can easily inflate it.
    892     Inflate(self, self, obj.Get(), hash_code);
    893   } else {
    894     ThreadList* thread_list = Runtime::Current()->GetThreadList();
    895     // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
    896     self->SetMonitorEnterObject(obj.Get());
    897     bool timed_out;
    898     Thread* owner;
    899     {
    900       ScopedThreadSuspension sts(self, kBlocked);
    901       owner = thread_list->SuspendThreadByThreadId(owner_thread_id,
    902                                                    SuspendReason::kInternal,
    903                                                    &timed_out);
    904     }
    905     if (owner != nullptr) {
    906       // We succeeded in suspending the thread, check the lock's status didn't change.
    907       lock_word = obj->GetLockWord(true);
    908       if (lock_word.GetState() == LockWord::kThinLocked &&
    909           lock_word.ThinLockOwner() == owner_thread_id) {
    910         // Go ahead and inflate the lock.
    911         Inflate(self, owner, obj.Get(), hash_code);
    912       }
    913       bool resumed = thread_list->Resume(owner, SuspendReason::kInternal);
    914       DCHECK(resumed);
    915     }
    916     self->SetMonitorEnterObject(nullptr);
    917   }
    918 }
    919 
    920 // Fool annotalysis into thinking that the lock on obj is acquired.
    921 static mirror::Object* FakeLock(mirror::Object* obj)
    922     EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
    923   return obj;
    924 }
    925 
    926 // Fool annotalysis into thinking that the lock on obj is release.
    927 static mirror::Object* FakeUnlock(mirror::Object* obj)
    928     UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
    929   return obj;
    930 }
    931 
    932 mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj, bool trylock) {
    933   DCHECK(self != nullptr);
    934   DCHECK(obj != nullptr);
    935   self->AssertThreadSuspensionIsAllowable();
    936   obj = FakeLock(obj);
    937   uint32_t thread_id = self->GetThreadId();
    938   size_t contention_count = 0;
    939   StackHandleScope<1> hs(self);
    940   Handle<mirror::Object> h_obj(hs.NewHandle(obj));
    941   while (true) {
    942     // We initially read the lockword with ordinary Java/relaxed semantics. When stronger
    943     // semantics are needed, we address it below. Since GetLockWord bottoms out to a relaxed load,
    944     // we can fix it later, in an infrequently executed case, with a fence.
    945     LockWord lock_word = h_obj->GetLockWord(false);
    946     switch (lock_word.GetState()) {
    947       case LockWord::kUnlocked: {
    948         // No ordering required for preceding lockword read, since we retest.
    949         LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.GCState()));
    950         if (h_obj->CasLockWordWeakAcquire(lock_word, thin_locked)) {
    951           AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
    952           return h_obj.Get();  // Success!
    953         }
    954         continue;  // Go again.
    955       }
    956       case LockWord::kThinLocked: {
    957         uint32_t owner_thread_id = lock_word.ThinLockOwner();
    958         if (owner_thread_id == thread_id) {
    959           // No ordering required for initial lockword read.
    960           // We own the lock, increase the recursion count.
    961           uint32_t new_count = lock_word.ThinLockCount() + 1;
    962           if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
    963             LockWord thin_locked(LockWord::FromThinLockId(thread_id,
    964                                                           new_count,
    965                                                           lock_word.GCState()));
    966             // Only this thread pays attention to the count. Thus there is no need for stronger
    967             // than relaxed memory ordering.
    968             if (!kUseReadBarrier) {
    969               h_obj->SetLockWord(thin_locked, false /* volatile */);
    970               AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
    971               return h_obj.Get();  // Success!
    972             } else {
    973               // Use CAS to preserve the read barrier state.
    974               if (h_obj->CasLockWordWeakRelaxed(lock_word, thin_locked)) {
    975                 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
    976                 return h_obj.Get();  // Success!
    977               }
    978             }
    979             continue;  // Go again.
    980           } else {
    981             // We'd overflow the recursion count, so inflate the monitor.
    982             InflateThinLocked(self, h_obj, lock_word, 0);
    983           }
    984         } else {
    985           if (trylock) {
    986             return nullptr;
    987           }
    988           // Contention.
    989           contention_count++;
    990           Runtime* runtime = Runtime::Current();
    991           if (contention_count <= runtime->GetMaxSpinsBeforeThinLockInflation()) {
    992             // TODO: Consider switching the thread state to kBlocked when we are yielding.
    993             // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the
    994             // parameter you pass in. This can cause thread suspension to take excessively long
    995             // and make long pauses. See b/16307460.
    996             // TODO: We should literally spin first, without sched_yield. Sched_yield either does
    997             // nothing (at significant expense), or guarantees that we wait at least microseconds.
    998             // If the owner is running, I would expect the median lock hold time to be hundreds
    999             // of nanoseconds or less.
   1000             sched_yield();
   1001           } else {
   1002             contention_count = 0;
   1003             // No ordering required for initial lockword read. Install rereads it anyway.
   1004             InflateThinLocked(self, h_obj, lock_word, 0);
   1005           }
   1006         }
   1007         continue;  // Start from the beginning.
   1008       }
   1009       case LockWord::kFatLocked: {
   1010         // We should have done an acquire read of the lockword initially, to ensure
   1011         // visibility of the monitor data structure. Use an explicit fence instead.
   1012         QuasiAtomic::ThreadFenceAcquire();
   1013         Monitor* mon = lock_word.FatLockMonitor();
   1014         if (trylock) {
   1015           return mon->TryLock(self) ? h_obj.Get() : nullptr;
   1016         } else {
   1017           mon->Lock(self);
   1018           return h_obj.Get();  // Success!
   1019         }
   1020       }
   1021       case LockWord::kHashCode:
   1022         // Inflate with the existing hashcode.
   1023         // Again no ordering required for initial lockword read, since we don't rely
   1024         // on the visibility of any prior computation.
   1025         Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
   1026         continue;  // Start from the beginning.
   1027       default: {
   1028         LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
   1029         UNREACHABLE();
   1030       }
   1031     }
   1032   }
   1033 }
   1034 
   1035 bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
   1036   DCHECK(self != nullptr);
   1037   DCHECK(obj != nullptr);
   1038   self->AssertThreadSuspensionIsAllowable();
   1039   obj = FakeUnlock(obj);
   1040   StackHandleScope<1> hs(self);
   1041   Handle<mirror::Object> h_obj(hs.NewHandle(obj));
   1042   while (true) {
   1043     LockWord lock_word = obj->GetLockWord(true);
   1044     switch (lock_word.GetState()) {
   1045       case LockWord::kHashCode:
   1046         // Fall-through.
   1047       case LockWord::kUnlocked:
   1048         FailedUnlock(h_obj.Get(), self->GetThreadId(), 0u, nullptr);
   1049         return false;  // Failure.
   1050       case LockWord::kThinLocked: {
   1051         uint32_t thread_id = self->GetThreadId();
   1052         uint32_t owner_thread_id = lock_word.ThinLockOwner();
   1053         if (owner_thread_id != thread_id) {
   1054           FailedUnlock(h_obj.Get(), thread_id, owner_thread_id, nullptr);
   1055           return false;  // Failure.
   1056         } else {
   1057           // We own the lock, decrease the recursion count.
   1058           LockWord new_lw = LockWord::Default();
   1059           if (lock_word.ThinLockCount() != 0) {
   1060             uint32_t new_count = lock_word.ThinLockCount() - 1;
   1061             new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.GCState());
   1062           } else {
   1063             new_lw = LockWord::FromDefault(lock_word.GCState());
   1064           }
   1065           if (!kUseReadBarrier) {
   1066             DCHECK_EQ(new_lw.ReadBarrierState(), 0U);
   1067             // TODO: This really only needs memory_order_release, but we currently have
   1068             // no way to specify that. In fact there seem to be no legitimate uses of SetLockWord
   1069             // with a final argument of true. This slows down x86 and ARMv7, but probably not v8.
   1070             h_obj->SetLockWord(new_lw, true);
   1071             AtraceMonitorUnlock();
   1072             // Success!
   1073             return true;
   1074           } else {
   1075             // Use CAS to preserve the read barrier state.
   1076             if (h_obj->CasLockWordWeakRelease(lock_word, new_lw)) {
   1077               AtraceMonitorUnlock();
   1078               // Success!
   1079               return true;
   1080             }
   1081           }
   1082           continue;  // Go again.
   1083         }
   1084       }
   1085       case LockWord::kFatLocked: {
   1086         Monitor* mon = lock_word.FatLockMonitor();
   1087         return mon->Unlock(self);
   1088       }
   1089       default: {
   1090         LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
   1091         return false;
   1092       }
   1093     }
   1094   }
   1095 }
   1096 
   1097 void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
   1098                    bool interruptShouldThrow, ThreadState why) {
   1099   DCHECK(self != nullptr);
   1100   DCHECK(obj != nullptr);
   1101   LockWord lock_word = obj->GetLockWord(true);
   1102   while (lock_word.GetState() != LockWord::kFatLocked) {
   1103     switch (lock_word.GetState()) {
   1104       case LockWord::kHashCode:
   1105         // Fall-through.
   1106       case LockWord::kUnlocked:
   1107         ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
   1108         return;  // Failure.
   1109       case LockWord::kThinLocked: {
   1110         uint32_t thread_id = self->GetThreadId();
   1111         uint32_t owner_thread_id = lock_word.ThinLockOwner();
   1112         if (owner_thread_id != thread_id) {
   1113           ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
   1114           return;  // Failure.
   1115         } else {
   1116           // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so
   1117           // re-load.
   1118           Inflate(self, self, obj, 0);
   1119           lock_word = obj->GetLockWord(true);
   1120         }
   1121         break;
   1122       }
   1123       case LockWord::kFatLocked:  // Unreachable given the loop condition above. Fall-through.
   1124       default: {
   1125         LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
   1126         return;
   1127       }
   1128     }
   1129   }
   1130   Monitor* mon = lock_word.FatLockMonitor();
   1131   mon->Wait(self, ms, ns, interruptShouldThrow, why);
   1132 }
   1133 
   1134 void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
   1135   DCHECK(self != nullptr);
   1136   DCHECK(obj != nullptr);
   1137   LockWord lock_word = obj->GetLockWord(true);
   1138   switch (lock_word.GetState()) {
   1139     case LockWord::kHashCode:
   1140       // Fall-through.
   1141     case LockWord::kUnlocked:
   1142       ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
   1143       return;  // Failure.
   1144     case LockWord::kThinLocked: {
   1145       uint32_t thread_id = self->GetThreadId();
   1146       uint32_t owner_thread_id = lock_word.ThinLockOwner();
   1147       if (owner_thread_id != thread_id) {
   1148         ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
   1149         return;  // Failure.
   1150       } else {
   1151         // We own the lock but there's no Monitor and therefore no waiters.
   1152         return;  // Success.
   1153       }
   1154     }
   1155     case LockWord::kFatLocked: {
   1156       Monitor* mon = lock_word.FatLockMonitor();
   1157       if (notify_all) {
   1158         mon->NotifyAll(self);
   1159       } else {
   1160         mon->Notify(self);
   1161       }
   1162       return;  // Success.
   1163     }
   1164     default: {
   1165       LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
   1166       return;
   1167     }
   1168   }
   1169 }
   1170 
   1171 uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
   1172   DCHECK(obj != nullptr);
   1173   LockWord lock_word = obj->GetLockWord(true);
   1174   switch (lock_word.GetState()) {
   1175     case LockWord::kHashCode:
   1176       // Fall-through.
   1177     case LockWord::kUnlocked:
   1178       return ThreadList::kInvalidThreadId;
   1179     case LockWord::kThinLocked:
   1180       return lock_word.ThinLockOwner();
   1181     case LockWord::kFatLocked: {
   1182       Monitor* mon = lock_word.FatLockMonitor();
   1183       return mon->GetOwnerThreadId();
   1184     }
   1185     default: {
   1186       LOG(FATAL) << "Unreachable";
   1187       UNREACHABLE();
   1188     }
   1189   }
   1190 }
   1191 
   1192 void Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
   1193   // Determine the wait message and object we're waiting or blocked upon.
   1194   mirror::Object* pretty_object = nullptr;
   1195   const char* wait_message = nullptr;
   1196   uint32_t lock_owner = ThreadList::kInvalidThreadId;
   1197   ThreadState state = thread->GetState();
   1198   if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
   1199     wait_message = (state == kSleeping) ? "  - sleeping on " : "  - waiting on ";
   1200     Thread* self = Thread::Current();
   1201     MutexLock mu(self, *thread->GetWaitMutex());
   1202     Monitor* monitor = thread->GetWaitMonitor();
   1203     if (monitor != nullptr) {
   1204       pretty_object = monitor->GetObject();
   1205     }
   1206   } else if (state == kBlocked) {
   1207     wait_message = "  - waiting to lock ";
   1208     pretty_object = thread->GetMonitorEnterObject();
   1209     if (pretty_object != nullptr) {
   1210       if (kUseReadBarrier && Thread::Current()->GetIsGcMarking()) {
   1211         // We may call Thread::Dump() in the middle of the CC thread flip and this thread's stack
   1212         // may have not been flipped yet and "pretty_object" may be a from-space (stale) ref, in
   1213         // which case the GetLockOwnerThreadId() call below will crash. So explicitly mark/forward
   1214         // it here.
   1215         pretty_object = ReadBarrier::Mark(pretty_object);
   1216       }
   1217       lock_owner = pretty_object->GetLockOwnerThreadId();
   1218     }
   1219   }
   1220 
   1221   if (wait_message != nullptr) {
   1222     if (pretty_object == nullptr) {
   1223       os << wait_message << "an unknown object";
   1224     } else {
   1225       if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
   1226           Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
   1227         // Getting the identity hashcode here would result in lock inflation and suspension of the
   1228         // current thread, which isn't safe if this is the only runnable thread.
   1229         os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
   1230                                            reinterpret_cast<intptr_t>(pretty_object),
   1231                                            pretty_object->PrettyTypeOf().c_str());
   1232       } else {
   1233         // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
   1234         // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread
   1235         // suspension and move pretty_object.
   1236         const std::string pretty_type(pretty_object->PrettyTypeOf());
   1237         os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(),
   1238                                            pretty_type.c_str());
   1239       }
   1240     }
   1241     // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
   1242     if (lock_owner != ThreadList::kInvalidThreadId) {
   1243       os << " held by thread " << lock_owner;
   1244     }
   1245     os << "\n";
   1246   }
   1247 }
   1248 
   1249 mirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
   1250   // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
   1251   // definition of contended that includes a monitor a thread is trying to enter...
   1252   mirror::Object* result = thread->GetMonitorEnterObject();
   1253   if (result == nullptr) {
   1254     // ...but also a monitor that the thread is waiting on.
   1255     MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
   1256     Monitor* monitor = thread->GetWaitMonitor();
   1257     if (monitor != nullptr) {
   1258       result = monitor->GetObject();
   1259     }
   1260   }
   1261   return result;
   1262 }
   1263 
   1264 void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
   1265                          void* callback_context, bool abort_on_failure) {
   1266   ArtMethod* m = stack_visitor->GetMethod();
   1267   CHECK(m != nullptr);
   1268 
   1269   // Native methods are an easy special case.
   1270   // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
   1271   if (m->IsNative()) {
   1272     if (m->IsSynchronized()) {
   1273       mirror::Object* jni_this =
   1274           stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0);
   1275       callback(jni_this, callback_context);
   1276     }
   1277     return;
   1278   }
   1279 
   1280   // Proxy methods should not be synchronized.
   1281   if (m->IsProxyMethod()) {
   1282     CHECK(!m->IsSynchronized());
   1283     return;
   1284   }
   1285 
   1286   // Is there any reason to believe there's any synchronization in this method?
   1287   const DexFile::CodeItem* code_item = m->GetCodeItem();
   1288   CHECK(code_item != nullptr) << m->PrettyMethod();
   1289   if (code_item->tries_size_ == 0) {
   1290     return;  // No "tries" implies no synchronization, so no held locks to report.
   1291   }
   1292 
   1293   // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot
   1294   // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an
   1295   // inconsistent stack anyways.
   1296   uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure);
   1297   if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) {
   1298     LOG(ERROR) << "Could not find dex_pc for " << m->PrettyMethod();
   1299     return;
   1300   }
   1301 
   1302   // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
   1303   // the locks held in this stack frame.
   1304   std::vector<uint32_t> monitor_enter_dex_pcs;
   1305   verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs);
   1306   for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) {
   1307     // The verifier works in terms of the dex pcs of the monitor-enter instructions.
   1308     // We want the registers used by those instructions (so we can read the values out of them).
   1309     const Instruction* monitor_enter_instruction =
   1310         Instruction::At(&code_item->insns_[monitor_dex_pc]);
   1311 
   1312     // Quick sanity check.
   1313     CHECK_EQ(monitor_enter_instruction->Opcode(), Instruction::MONITOR_ENTER)
   1314       << "expected monitor-enter @" << monitor_dex_pc << "; was "
   1315       << reinterpret_cast<const void*>(monitor_enter_instruction);
   1316 
   1317     uint16_t monitor_register = monitor_enter_instruction->VRegA();
   1318     uint32_t value;
   1319     bool success = stack_visitor->GetVReg(m, monitor_register, kReferenceVReg, &value);
   1320     CHECK(success) << "Failed to read v" << monitor_register << " of kind "
   1321                    << kReferenceVReg << " in method " << m->PrettyMethod();
   1322     mirror::Object* o = reinterpret_cast<mirror::Object*>(value);
   1323     callback(o, callback_context);
   1324   }
   1325 }
   1326 
   1327 bool Monitor::IsValidLockWord(LockWord lock_word) {
   1328   switch (lock_word.GetState()) {
   1329     case LockWord::kUnlocked:
   1330       // Nothing to check.
   1331       return true;
   1332     case LockWord::kThinLocked:
   1333       // Basic sanity check of owner.
   1334       return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
   1335     case LockWord::kFatLocked: {
   1336       // Check the  monitor appears in the monitor list.
   1337       Monitor* mon = lock_word.FatLockMonitor();
   1338       MonitorList* list = Runtime::Current()->GetMonitorList();
   1339       MutexLock mu(Thread::Current(), list->monitor_list_lock_);
   1340       for (Monitor* list_mon : list->list_) {
   1341         if (mon == list_mon) {
   1342           return true;  // Found our monitor.
   1343         }
   1344       }
   1345       return false;  // Fail - unowned monitor in an object.
   1346     }
   1347     case LockWord::kHashCode:
   1348       return true;
   1349     default:
   1350       LOG(FATAL) << "Unreachable";
   1351       UNREACHABLE();
   1352   }
   1353 }
   1354 
   1355 bool Monitor::IsLocked() REQUIRES_SHARED(Locks::mutator_lock_) {
   1356   MutexLock mu(Thread::Current(), monitor_lock_);
   1357   return owner_ != nullptr;
   1358 }
   1359 
   1360 void Monitor::TranslateLocation(ArtMethod* method,
   1361                                 uint32_t dex_pc,
   1362                                 const char** source_file,
   1363                                 int32_t* line_number) {
   1364   // If method is null, location is unknown
   1365   if (method == nullptr) {
   1366     *source_file = "";
   1367     *line_number = 0;
   1368     return;
   1369   }
   1370   *source_file = method->GetDeclaringClassSourceFile();
   1371   if (*source_file == nullptr) {
   1372     *source_file = "";
   1373   }
   1374   *line_number = method->GetLineNumFromDexPC(dex_pc);
   1375 }
   1376 
   1377 uint32_t Monitor::GetOwnerThreadId() {
   1378   MutexLock mu(Thread::Current(), monitor_lock_);
   1379   Thread* owner = owner_;
   1380   if (owner != nullptr) {
   1381     return owner->GetThreadId();
   1382   } else {
   1383     return ThreadList::kInvalidThreadId;
   1384   }
   1385 }
   1386 
   1387 MonitorList::MonitorList()
   1388     : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
   1389       monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
   1390 }
   1391 
   1392 MonitorList::~MonitorList() {
   1393   Thread* self = Thread::Current();
   1394   MutexLock mu(self, monitor_list_lock_);
   1395   // Release all monitors to the pool.
   1396   // TODO: Is it an invariant that *all* open monitors are in the list? Then we could
   1397   // clear faster in the pool.
   1398   MonitorPool::ReleaseMonitors(self, &list_);
   1399 }
   1400 
   1401 void MonitorList::DisallowNewMonitors() {
   1402   CHECK(!kUseReadBarrier);
   1403   MutexLock mu(Thread::Current(), monitor_list_lock_);
   1404   allow_new_monitors_ = false;
   1405 }
   1406 
   1407 void MonitorList::AllowNewMonitors() {
   1408   CHECK(!kUseReadBarrier);
   1409   Thread* self = Thread::Current();
   1410   MutexLock mu(self, monitor_list_lock_);
   1411   allow_new_monitors_ = true;
   1412   monitor_add_condition_.Broadcast(self);
   1413 }
   1414 
   1415 void MonitorList::BroadcastForNewMonitors() {
   1416   Thread* self = Thread::Current();
   1417   MutexLock mu(self, monitor_list_lock_);
   1418   monitor_add_condition_.Broadcast(self);
   1419 }
   1420 
   1421 void MonitorList::Add(Monitor* m) {
   1422   Thread* self = Thread::Current();
   1423   MutexLock mu(self, monitor_list_lock_);
   1424   // CMS needs this to block for concurrent reference processing because an object allocated during
   1425   // the GC won't be marked and concurrent reference processing would incorrectly clear the JNI weak
   1426   // ref. But CC (kUseReadBarrier == true) doesn't because of the to-space invariant.
   1427   while (!kUseReadBarrier && UNLIKELY(!allow_new_monitors_)) {
   1428     // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the
   1429     // presence of threads blocking for weak ref access.
   1430     self->CheckEmptyCheckpointFromWeakRefAccess(&monitor_list_lock_);
   1431     monitor_add_condition_.WaitHoldingLocks(self);
   1432   }
   1433   list_.push_front(m);
   1434 }
   1435 
   1436 void MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) {
   1437   Thread* self = Thread::Current();
   1438   MutexLock mu(self, monitor_list_lock_);
   1439   for (auto it = list_.begin(); it != list_.end(); ) {
   1440     Monitor* m = *it;
   1441     // Disable the read barrier in GetObject() as this is called by GC.
   1442     mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
   1443     // The object of a monitor can be null if we have deflated it.
   1444     mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr;
   1445     if (new_obj == nullptr) {
   1446       VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
   1447                     << obj;
   1448       MonitorPool::ReleaseMonitor(self, m);
   1449       it = list_.erase(it);
   1450     } else {
   1451       m->SetObject(new_obj);
   1452       ++it;
   1453     }
   1454   }
   1455 }
   1456 
   1457 size_t MonitorList::Size() {
   1458   Thread* self = Thread::Current();
   1459   MutexLock mu(self, monitor_list_lock_);
   1460   return list_.size();
   1461 }
   1462 
   1463 class MonitorDeflateVisitor : public IsMarkedVisitor {
   1464  public:
   1465   MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {}
   1466 
   1467   virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE
   1468       REQUIRES_SHARED(Locks::mutator_lock_) {
   1469     if (Monitor::Deflate(self_, object)) {
   1470       DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
   1471       ++deflate_count_;
   1472       // If we deflated, return null so that the monitor gets removed from the array.
   1473       return nullptr;
   1474     }
   1475     return object;  // Monitor was not deflated.
   1476   }
   1477 
   1478   Thread* const self_;
   1479   size_t deflate_count_;
   1480 };
   1481 
   1482 size_t MonitorList::DeflateMonitors() {
   1483   MonitorDeflateVisitor visitor;
   1484   Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_);
   1485   SweepMonitorList(&visitor);
   1486   return visitor.deflate_count_;
   1487 }
   1488 
   1489 MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) {
   1490   DCHECK(obj != nullptr);
   1491   LockWord lock_word = obj->GetLockWord(true);
   1492   switch (lock_word.GetState()) {
   1493     case LockWord::kUnlocked:
   1494       // Fall-through.
   1495     case LockWord::kForwardingAddress:
   1496       // Fall-through.
   1497     case LockWord::kHashCode:
   1498       break;
   1499     case LockWord::kThinLocked:
   1500       owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
   1501       entry_count_ = 1 + lock_word.ThinLockCount();
   1502       // Thin locks have no waiters.
   1503       break;
   1504     case LockWord::kFatLocked: {
   1505       Monitor* mon = lock_word.FatLockMonitor();
   1506       owner_ = mon->owner_;
   1507       entry_count_ = 1 + mon->lock_count_;
   1508       for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) {
   1509         waiters_.push_back(waiter);
   1510       }
   1511       break;
   1512     }
   1513   }
   1514 }
   1515 
   1516 }  // namespace art
   1517