monitor.cc revision dd7624d2b9e599d57762d12031b10b89defc9807
15f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
25f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Copyright (C) 2008 The Android Open Source Project
35f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
45f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
55f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * you may not use this file except in compliance with the License.
65f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * You may obtain a copy of the License at
75f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
85f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
95f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Unless required by applicable law or agreed to in writing, software
115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * See the License for the specific language governing permissions and
145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * limitations under the License.
155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#include "monitor.h"
185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include <vector>
2008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
2176b6167407c2b6f5d40ad895b2793a6b037f54b2Elliott Hughes#include "base/mutex.h"
221aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h"
2333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao#include "class_linker.h"
244f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
2508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "dex_instruction.h"
26d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers#include "lock_word-inl.h"
27ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
284f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "mirror/class-inl.h"
2905f3057d6a4d23d712092ccd36a531590bff323bIan Rogers#include "mirror/object-inl.h"
302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
316d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h"
3200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "thread.h"
348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "thread_list.h"
3508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "verifier/method_verifier.h"
36044288fb8f69e154862763a6b3bfea2bac1b397eElliott Hughes#include "well_known_classes.h"
375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesnamespace art {
395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
41d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Every Object has a monitor associated with it, but not every Object is actually locked.  Even
42d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * the ones that are locked do not need a full-fledged monitor until a) there is actual contention
43d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * or b) wait() is called on the Object.
445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
45d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s
46d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * "Thin locks: featherweight synchronization for Java" (ACM 1998).  Things are even easier for us,
47d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * though, because we have a full 32 bits to work with.
485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
49d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * The two states of an Object's lock are referred to as "thin" and "fat".  A lock may transition
50d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once
51d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * a lock has been inflated it remains in the "fat" state indefinitely.
525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
53d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * The lock value itself is stored in mirror::Object::monitor_ and the representation is described
54d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * in the LockWord value type.
5554e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes *
565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Monitors provide:
575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - mutually exclusive access to resources
585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - a way for multiple threads to wait for notification
595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * In effect, they fill the role of both mutexes and condition variables.
615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
62d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Only one thread can own the monitor at any time.  There may be several threads waiting on it
63d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * (the wait call unlocks it).  One or more waiting threads may be getting interrupted or notified
64d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * at any given time.
655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
6654e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes
67fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesbool (*Monitor::is_sensitive_thread_hook_)() = NULL;
68fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesuint32_t Monitor::lock_profiling_threshold_ = 0;
6932d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes
70fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesbool Monitor::IsSensitiveThread() {
71fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  if (is_sensitive_thread_hook_ != NULL) {
72fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes    return (*is_sensitive_thread_hook_)();
73fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  }
74fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  return false;
75fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes}
76fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes
774dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughesvoid Monitor::Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()) {
78fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  lock_profiling_threshold_ = lock_profiling_threshold;
79fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  is_sensitive_thread_hook_ = is_sensitive_thread_hook;
8032d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes}
8132d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes
82ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersMonitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
8300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    : monitor_lock_("a monitor lock", kMonitorLock),
84d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      monitor_contenders_("monitor contenders", monitor_lock_),
8546bc778f1feed02b20d25e3d03470c93ca2c0506Mathieu Chartier      num_waiters_(0),
8600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      owner_(owner),
875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      lock_count_(0),
885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      obj_(obj),
895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      wait_set_(NULL),
90ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      hash_code_(hash_code),
9133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao      locking_method_(NULL),
92ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      locking_dex_pc_(0),
93ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      monitor_id_(MonitorPool::CreateMonitorId(self, this)) {
94d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
95d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // with the owner unlocking the thin-lock.
96ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  CHECK(owner == nullptr || owner == self || owner->IsSuspended());
97ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  // The identity hash code is set for the life time of the monitor.
98d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers}
99d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
1004e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartierint32_t Monitor::GetHashCode() {
1014e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier  while (!HasHashCode()) {
102b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers    if (hash_code_.CompareAndSwap(0, mirror::Object::GenerateIdentityHashCode())) {
1034e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier      break;
1044e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier    }
1054e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier  }
1064e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier  DCHECK(HasHashCode());
107b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers  return hash_code_.Load();
1084e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier}
1094e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier
110d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::Install(Thread* self) {
111d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);  // Uncontended mutex acquisition as monitor isn't yet public.
112ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended());
11300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Propagate the lock state.
114ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  LockWord lw(obj_->GetLockWord());
115ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  switch (lw.GetState()) {
116ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kThinLocked: {
117ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner());
118ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      lock_count_ = lw.ThinLockCount();
119ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      break;
120ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
121ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode: {
1224e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier      CHECK_EQ(hash_code_, static_cast<int32_t>(lw.GetHashCode()));
123ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      break;
124ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
125ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kFatLocked: {
126ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // The owner_ is suspended but another thread beat us to install a monitor.
127ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      return false;
128ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
129ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kUnlocked: {
130ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      LOG(FATAL) << "Inflating unlocked lock word";
131ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      break;
132ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
133590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    default: {
134590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      LOG(FATAL) << "Invalid monitor state " << lw.GetState();
135590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      return false;
136590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
137d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  }
138d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord fat(this);
139d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // Publish the updated lock word, which may race with other threads.
140ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  bool success = obj_->CasLockWord(lw, fat);
14100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Lock profiling.
1429728f91a63016136261231ff5213bde703bd27b6Mathieu Chartier  if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) {
143d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_);
14400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
145d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  return success;
1465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
1475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1485f79133a435ebcb20000370d56046fe01201dd80Elliott HughesMonitor::~Monitor() {
149ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  MonitorPool::ReleaseMonitorId(monitor_id_);
150590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  // Deflated monitors have a null object.
1515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
1525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
1545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Links a thread into a monitor's wait set.  The monitor lock must be
1555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * held by the caller of this routine.
1565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
1575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::AppendToWaitSet(Thread* thread) {
1585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(owner_ == Thread::Current());
1595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(thread != NULL);
160dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext();
1615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (wait_set_ == NULL) {
1625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    wait_set_ = thread;
1635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
1645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // push_back.
1675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  Thread* t = wait_set_;
168dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  while (t->GetWaitNext() != nullptr) {
169dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    t = t->GetWaitNext();
1705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
171dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  t->SetWaitNext(thread);
1725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
1735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
1755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Unlinks a thread from a monitor's wait set.  The monitor lock must
1765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * be held by the caller of this routine.
1775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
1785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::RemoveFromWaitSet(Thread *thread) {
1795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(owner_ == Thread::Current());
1805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(thread != NULL);
1815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (wait_set_ == NULL) {
1825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
1835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (wait_set_ == thread) {
185dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    wait_set_ = thread->GetWaitNext();
186dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    thread->SetWaitNext(nullptr);
1875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
1885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  Thread* t = wait_set_;
191dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  while (t->GetWaitNext() != NULL) {
192dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    if (t->GetWaitNext() == thread) {
193dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      t->SetWaitNext(thread->GetWaitNext());
194dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      thread->SetWaitNext(nullptr);
1955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      return;
1965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
197dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    t = t->GetWaitNext();
1985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
2005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2016aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartiervoid Monitor::SetObject(mirror::Object* object) {
2026aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier  obj_ = object;
2036aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier}
2046aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier
2055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Lock(Thread* self) {
206d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
207d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  while (true) {
208d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    if (owner_ == NULL) {  // Unowned.
209d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      owner_ = self;
210d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      CHECK_EQ(lock_count_, 0);
211d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // When debugging, save the current monitor holder for future
212d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // acquisition failures to use in sampled logging.
213d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (lock_profiling_threshold_ != 0) {
214d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
215fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes      }
216d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;
217d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    } else if (owner_ == self) {  // Recursive.
218d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      lock_count_++;
219d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;
220fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes    }
221d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    // Contended.
222d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    const bool log_contention = (lock_profiling_threshold_ != 0);
223d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    uint64_t wait_start_ms = log_contention ? 0 : MilliTime();
224ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::ArtMethod* owners_method = locking_method_;
225d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    uint32_t owners_dex_pc = locking_dex_pc_;
226d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);  // Let go of locks in order.
227d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    {
228d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      ScopedThreadStateChange tsc(self, kBlocked);  // Change to blocked and give up mutator_lock_.
229dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      self->SetMonitorEnterObject(obj_);
230d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      MutexLock mu2(self, monitor_lock_);  // Reacquire monitor_lock_ without mutator_lock_ for Wait.
231d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (owner_ != NULL) {  // Did the owner_ give the lock up?
23246bc778f1feed02b20d25e3d03470c93ca2c0506Mathieu Chartier        ++num_waiters_;
233d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        monitor_contenders_.Wait(self);  // Still contended so wait.
23446bc778f1feed02b20d25e3d03470c93ca2c0506Mathieu Chartier        --num_waiters_;
235d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // Woken from contention.
236d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (log_contention) {
237d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint64_t wait_ms = MilliTime() - wait_start_ms;
238d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint32_t sample_percent;
239d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (wait_ms >= lock_profiling_threshold_) {
240d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            sample_percent = 100;
241d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          } else {
242d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            sample_percent = 100 * wait_ms / lock_profiling_threshold_;
243d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          }
244d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
245d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            const char* owners_filename;
246d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            uint32_t owners_line_number;
247d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
248d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            LogContentionEvent(self, wait_ms, sample_percent, owners_filename, owners_line_number);
249d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          }
250d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
251fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes      }
252dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      self->SetMonitorEnterObject(nullptr);
2535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
254d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Lock(self);  // Reacquire locks in order.
255fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  }
2565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
2575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2586d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
2596d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                              __attribute__((format(printf, 1, 2)));
2606d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers
26100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
262b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2636d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  va_list args;
2646d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  va_start(args, fmt);
26562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
26662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowLocation throw_location = self->GetCurrentLocationForThrow();
26762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  self->ThrowNewExceptionV(throw_location, "Ljava/lang/IllegalMonitorStateException;", fmt, args);
268d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
26964277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom    std::ostringstream ss;
27062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    self->Dump(ss);
271d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    LOG(Runtime::Current()->IsStarted() ? INFO : ERROR)
272d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        << self->GetException(NULL)->Dump() << "\n" << ss.str();
27364277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom  }
2746d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  va_end(args);
2756d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers}
2766d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers
277d423741f91526cada9d081c64ee295ec9863032dElliott Hughesstatic std::string ThreadToString(Thread* thread) {
278d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  if (thread == NULL) {
279d423741f91526cada9d081c64ee295ec9863032dElliott Hughes    return "NULL";
280d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  }
281d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  std::ostringstream oss;
282d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  // TODO: alternatively, we could just return the thread's name.
283d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  oss << *thread;
284d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  return oss.str();
285d423741f91526cada9d081c64ee295ec9863032dElliott Hughes}
286d423741f91526cada9d081c64ee295ec9863032dElliott Hughes
2872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::FailedUnlock(mirror::Object* o, Thread* expected_owner, Thread* found_owner,
288ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                           Monitor* monitor) {
289ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  Thread* current_owner = NULL;
290ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  std::string current_owner_string;
291ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  std::string expected_owner_string;
292ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  std::string found_owner_string;
293ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  {
294ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // TODO: isn't this too late to prevent threads from disappearing?
295ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // Acquire thread list lock so threads won't disappear from under us.
29650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
297ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // Re-read owner now that we hold lock.
298d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    current_owner = (monitor != NULL) ? monitor->GetOwner() : NULL;
299ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // Get short descriptions of the threads involved.
300ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    current_owner_string = ThreadToString(current_owner);
301ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    expected_owner_string = ThreadToString(expected_owner);
302ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    found_owner_string = ThreadToString(found_owner);
303ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  }
3046d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  if (current_owner == NULL) {
3056d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    if (found_owner == NULL) {
3066d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
3076d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                         " on thread '%s'",
308ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         PrettyTypeOf(o).c_str(),
309ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         expected_owner_string.c_str());
3106d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    } else {
3116d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      // Race: the original read found an owner but now there is none
3126d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
3136d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                         " (where now the monitor appears unowned) on thread '%s'",
314ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         found_owner_string.c_str(),
315ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         PrettyTypeOf(o).c_str(),
316ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         expected_owner_string.c_str());
3176d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    }
3186d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  } else {
3196d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    if (found_owner == NULL) {
3206d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      // Race: originally there was no owner, there is now
3216d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
3226d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                         " (originally believed to be unowned) on thread '%s'",
323ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         current_owner_string.c_str(),
324ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         PrettyTypeOf(o).c_str(),
325ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         expected_owner_string.c_str());
3266d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    } else {
3276d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      if (found_owner != current_owner) {
3286d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers        // Race: originally found and current owner have changed
3296d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers        ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
3306d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                           " owned by '%s') on object of type '%s' on thread '%s'",
331ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           found_owner_string.c_str(),
332ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           current_owner_string.c_str(),
333ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           PrettyTypeOf(o).c_str(),
334ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           expected_owner_string.c_str());
3356d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      } else {
3366d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers        ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
3376d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                           " on thread '%s",
338ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           current_owner_string.c_str(),
339ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           PrettyTypeOf(o).c_str(),
340ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           expected_owner_string.c_str());
3416d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      }
3426d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    }
3436d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  }
3445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
3455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
346d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::Unlock(Thread* self) {
3475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
348d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
3496d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  Thread* owner = owner_;
3506d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  if (owner == self) {
3515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // We own the monitor, so nobody else can be in here.
3525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (lock_count_ == 0) {
3535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      owner_ = NULL;
35433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao      locking_method_ = NULL;
3550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      locking_dex_pc_ = 0;
356d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Wake a contender.
357d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      monitor_contenders_.Signal(self);
3585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    } else {
3595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      --lock_count_;
3605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
3615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  } else {
3625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // We don't own this, so we're not allowed to unlock it.
3635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // The JNI spec says that we should throw IllegalMonitorStateException
3645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // in this case.
3656d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    FailedUnlock(obj_, self, owner, this);
3665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return false;
3675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
3685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  return true;
3695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
3705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
3715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
3725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Wait on a monitor until timeout, interrupt, or notification.  Used for
3735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join().
3745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
3755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * If another thread calls Thread.interrupt(), we throw InterruptedException
3765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * and return immediately if one of the following are true:
3775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - blocked in wait(), wait(long), or wait(long, int) methods of Object
3785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - blocked in join(), join(long), or join(long, int) methods of Thread
3795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - blocked in sleep(long), or sleep(long, int) methods of Thread
3805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Otherwise, we set the "interrupted" flag.
3815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
3825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Checks to make sure that "ns" is in the range 0-999999
3835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * (i.e. fractions of a millisecond) and throws the appropriate
3845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * exception if it isn't.
3855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
3865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The spec allows "spurious wakeups", and recommends that all code using
3875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() do so in a loop.  This appears to derive from concerns
3885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * about pthread_cond_wait() on multiprocessor systems.  Some commentary
3895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * on the web casts doubt on whether these can/should occur.
3905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
3915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Since we're allowed to wake up "early", we clamp extremely long durations
3925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * to return at the end of the 32-bit time epoch.
3935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
3944cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughesvoid Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
3954cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes                   bool interruptShouldThrow, ThreadState why) {
3965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
3974cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes  DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
3985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
399d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  monitor_lock_.Lock(self);
400d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
4015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Make sure that we hold the lock.
4025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (owner_ != self) {
4036d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
404d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);
4055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
4065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4074cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes
408df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  // We need to turn a zero-length timed wait into a regular wait because
409df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
410df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
411df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes    why = kWaiting;
412df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  }
413df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes
4145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Enforce the timeout range.
4155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (ms < 0 || ns < 0 || ns > 999999) {
41662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation throw_location = self->GetCurrentLocationForThrow();
41762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    self->ThrowNewExceptionF(throw_location, "Ljava/lang/IllegalArgumentException;",
418ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
419d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);
4205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
4215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  /*
4245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * Add ourselves to the set of threads waiting on this monitor, and
4255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * release our hold.  We need to let it go even if we're a few levels
4265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * deep in a recursive lock, and we need to restore that later.
4275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   *
4285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * We append to the wait set ahead of clearing the count and owner
4295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * fields so the subroutine can check that the calling thread owns
4305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * the monitor.  Aside from that, the order of member updates is
4315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * not order sensitive as we hold the pthread mutex.
4325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   */
4335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  AppendToWaitSet(self);
4340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  int prev_lock_count = lock_count_;
4355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  lock_count_ = 0;
4365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  owner_ = NULL;
437ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  mirror::ArtMethod* saved_method = locking_method_;
43833dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  locking_method_ = NULL;
4390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  uintptr_t saved_dex_pc = locking_dex_pc_;
4400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  locking_dex_pc_ = 0;
4415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  /*
4434cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes   * Update thread state. If the GC wakes up, it'll ignore us, knowing
4445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * that we won't touch any references in this state, and we'll check
4455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * our suspend mode before we transition out.
4465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   */
4474cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes  self->TransitionFromRunnableToSuspended(why);
4485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
449b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  bool was_interrupted = false;
45000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
45100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
452dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    MutexLock mu(self, *self->GetWaitMutex());
4535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
45400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
45500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
45600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // up.
457dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    DCHECK(self->GetWaitMonitor() == nullptr);
458dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    self->SetWaitMonitor(this);
4595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
46000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Release the monitor lock.
461d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_contenders_.Signal(self);
462d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);
46300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
464b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // Handle the case where the thread was interrupted before we called wait().
465dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    if (self->IsInterruptedLocked()) {
466b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      was_interrupted = true;
46700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    } else {
46800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      // Wait for a notification or a timeout to occur.
4694cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes      if (why == kWaiting) {
470dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers        self->GetWaitConditionVariable()->Wait(self);
47100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      } else {
4724cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes        DCHECK(why == kTimedWaiting || why == kSleeping) << why;
473dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers        self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
47400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
475dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      if (self->IsInterruptedLocked()) {
476b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes        was_interrupted = true;
47700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
478dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      self->SetInterruptedLocked(false);
47900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
4805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
48200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Set self->status back to kRunnable, and self-suspend if needed.
48300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  self->TransitionFromSuspendedToRunnable();
4845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
485b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  {
486b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // We reset the thread's wait_monitor_ field after transitioning back to runnable so
487b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
488b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
489b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // are waiting on "null".)
490dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    MutexLock mu(self, *self->GetWaitMutex());
491dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    DCHECK(self->GetWaitMonitor() != nullptr);
492dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    self->SetWaitMonitor(nullptr);
493b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  }
494b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes
495d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // Re-acquire the monitor and lock.
49600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Lock(self);
497d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  monitor_lock_.Lock(self);
498dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  self->GetWaitMutex()->AssertNotHeld(self);
4995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  /*
5015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * We remove our thread from wait set after restoring the count
5025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * and owner fields so the subroutine can check that the calling
5035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * thread owns the monitor. Aside from that, the order of member
5045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * updates is not order sensitive as we hold the pthread mutex.
5055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   */
5065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  owner_ = self;
5070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  lock_count_ = prev_lock_count;
5080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  locking_method_ = saved_method;
5090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  locking_dex_pc_ = saved_dex_pc;
5105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  RemoveFromWaitSet(self);
5115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
512b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  if (was_interrupted) {
5135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    /*
5145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * We were interrupted while waiting, or somebody interrupted an
5155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * un-interruptible thread earlier and we're bailing out immediately.
5165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     *
5175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * The doc sayeth: "The interrupted status of the current thread is
5185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * cleared when this exception is thrown."
5195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     */
52000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
521dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      MutexLock mu(self, *self->GetWaitMutex());
522dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      self->SetInterruptedLocked(false);
52300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
5245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (interruptShouldThrow) {
52562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      ThrowLocation throw_location = self->GetCurrentLocationForThrow();
52662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      self->ThrowNewException(throw_location, "Ljava/lang/InterruptedException;", NULL);
5275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
5285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
529d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  monitor_lock_.Unlock(self);
5305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
5315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Notify(Thread* self) {
5335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
534d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
5355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Make sure that we hold the lock.
5365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (owner_ != self) {
5376d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
5385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
5395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Signal the first waiting thread in the wait set.
5415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  while (wait_set_ != NULL) {
5425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    Thread* thread = wait_set_;
543dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    wait_set_ = thread->GetWaitNext();
544dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    thread->SetWaitNext(nullptr);
5455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // Check to see if the thread is still waiting.
547dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    MutexLock mu(self, *thread->GetWaitMutex());
548dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    if (thread->GetWaitMonitor() != nullptr) {
549dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      thread->GetWaitConditionVariable()->Signal(self);
5505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      return;
5515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
5525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
5545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::NotifyAll(Thread* self) {
5565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
557d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
5585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Make sure that we hold the lock.
5595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (owner_ != self) {
5606d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
5615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
5625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Signal all threads in the wait set.
5645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  while (wait_set_ != NULL) {
5655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    Thread* thread = wait_set_;
566dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    wait_set_ = thread->GetWaitNext();
567dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    thread->SetWaitNext(nullptr);
5685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    thread->Notify();
5695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
5715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
572590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartierbool Monitor::Deflate(Thread* self, mirror::Object* obj) {
573590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  DCHECK(obj != nullptr);
574590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  LockWord lw(obj->GetLockWord());
575590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  // If the lock isn't an inflated monitor, then we don't need to deflate anything.
576590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  if (lw.GetState() == LockWord::kFatLocked) {
577590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    Monitor* monitor = lw.FatLockMonitor();
578590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    CHECK(monitor != nullptr);
579590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    MutexLock mu(self, monitor->monitor_lock_);
580590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    Thread* owner = monitor->owner_;
581590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (owner != nullptr) {
582590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // Can't deflate if we are locked and have a hash code.
583590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      if (monitor->HasHashCode()) {
584590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        return false;
585590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      }
586590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // Can't deflate if our lock count is too high.
587590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      if (monitor->lock_count_ > LockWord::kThinLockMaxCount) {
588590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        return false;
589590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      }
590590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // Can't deflate if we have anybody waiting on the CV.
59146bc778f1feed02b20d25e3d03470c93ca2c0506Mathieu Chartier      if (monitor->num_waiters_ > 0) {
592590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        return false;
593590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      }
594590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // Deflate to a thin lock.
595590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      obj->SetLockWord(LockWord::FromThinLockId(owner->GetTid(), monitor->lock_count_));
596590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    } else if (monitor->HasHashCode()) {
597590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      obj->SetLockWord(LockWord::FromHashCode(monitor->GetHashCode()));
598590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    } else {
599590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // No lock and no hash, just put an empty lock word inside the object.
600590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      obj->SetLockWord(LockWord());
601590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
602590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // The monitor is deflated, mark the object as nullptr so that we know to delete it during the
603590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // next GC.
604590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    monitor->obj_ = nullptr;
605590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  }
606590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  return true;
607590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier}
608590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
6095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
610d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Changes the shape of a monitor from thin to fat, preserving the internal lock state. The calling
611d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * thread must own the lock or the owner must be suspended. There's a race with other threads
612d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * inflating the lock and so the caller should read the monitor following the call.
6135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
614ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartiervoid Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
6155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
6165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(obj != NULL);
6175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Allocate and acquire a new monitor.
618ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  UniquePtr<Monitor> m(new Monitor(self, owner, obj, hash_code));
619d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (m->Install(self)) {
620d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    VLOG(monitor) << "monitor: thread " << owner->GetThreadId()
621d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                    << " created monitor " << m.get() << " for object " << obj;
622d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    Runtime::Current()->GetMonitorList()->Add(m.release());
623ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    CHECK_EQ(obj->GetLockWord().GetState(), LockWord::kFatLocked);
624ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  }
625ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier}
626ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier
627590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartiervoid Monitor::InflateThinLocked(Thread* self, SirtRef<mirror::Object>& obj, LockWord lock_word,
628ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier                                uint32_t hash_code) {
629ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
630ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  uint32_t owner_thread_id = lock_word.ThinLockOwner();
631ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  if (owner_thread_id == self->GetThreadId()) {
632ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    // We own the monitor, we can easily inflate it.
633590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    Inflate(self, self, obj.get(), hash_code);
634ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  } else {
635ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    ThreadList* thread_list = Runtime::Current()->GetThreadList();
636ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
637ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    ScopedThreadStateChange tsc(self, kBlocked);
638dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    self->SetMonitorEnterObject(obj.get());
639ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    if (lock_word == obj->GetLockWord()) {  // If lock word hasn't changed.
640ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      bool timed_out;
6415f51d4b80058236759fea1d932470a57f348c199Mathieu Chartier      Thread* owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out);
642ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      if (owner != nullptr) {
643ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier        // We succeeded in suspending the thread, check the lock's status didn't change.
644ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier        lock_word = obj->GetLockWord();
645ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier        if (lock_word.GetState() == LockWord::kThinLocked &&
646ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier            lock_word.ThinLockOwner() == owner_thread_id) {
647ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier          // Go ahead and inflate the lock.
648590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          Inflate(self, owner, obj.get(), hash_code);
649ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier        }
650ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier        thread_list->Resume(owner, false);
651ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      }
652ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
653dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    self->SetMonitorEnterObject(nullptr);
654d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  }
6555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
6565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
657719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Fool annotalysis into thinking that the lock on obj is acquired.
658719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic mirror::Object* FakeLock(mirror::Object* obj)
659719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
660719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return obj;
661719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}
662719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
663719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Fool annotalysis into thinking that the lock on obj is release.
664719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic mirror::Object* FakeUnlock(mirror::Object* obj)
665719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
666719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return obj;
667719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}
668719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
669e7e8a5fea2d852cccc840fa046151a16627f26cdMathieu Chartiermirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) {
6704681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes  DCHECK(self != NULL);
6714681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes  DCHECK(obj != NULL);
672719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  obj = FakeLock(obj);
673d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  uint32_t thread_id = self->GetThreadId();
674d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  size_t contention_count = 0;
675590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  SirtRef<mirror::Object> sirt_obj(self, obj);
676d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  while (true) {
677590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    LockWord lock_word = sirt_obj->GetLockWord();
678d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    switch (lock_word.GetState()) {
679d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      case LockWord::kUnlocked: {
680d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0));
681590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        if (sirt_obj->CasLockWord(lock_word, thin_locked)) {
682b122a4bbed34ab22b4c1541ee25e5cf22f12a926Ian Rogers          QuasiAtomic::MembarLoadLoad();
683e7e8a5fea2d852cccc840fa046151a16627f26cdMathieu Chartier          return sirt_obj.get();  // Success!
684d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
685d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        continue;  // Go again.
6865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      }
687d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      case LockWord::kThinLocked: {
688d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        uint32_t owner_thread_id = lock_word.ThinLockOwner();
689d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (owner_thread_id == thread_id) {
690d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          // We own the lock, increase the recursion count.
691d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint32_t new_count = lock_word.ThinLockCount() + 1;
692d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
693d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count));
694590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier            sirt_obj->SetLockWord(thin_locked);
695e7e8a5fea2d852cccc840fa046151a16627f26cdMathieu Chartier            return sirt_obj.get();  // Success!
696d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          } else {
697d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            // We'd overflow the recursion count, so inflate the monitor.
698590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier            InflateThinLocked(self, sirt_obj, lock_word, 0);
699d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          }
700d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        } else {
701d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          // Contention.
702d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          contention_count++;
703ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier          Runtime* runtime = Runtime::Current();
704ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier          if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) {
705d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            NanoSleep(1000);  // Sleep for 1us and re-attempt.
7065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes          } else {
707d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            contention_count = 0;
708590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier            InflateThinLocked(self, sirt_obj, lock_word, 0);
7095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes          }
7105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes        }
711d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        continue;  // Start from the beginning.
712d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
713d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      case LockWord::kFatLocked: {
714d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Monitor* mon = lock_word.FatLockMonitor();
715d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mon->Lock(self);
716e7e8a5fea2d852cccc840fa046151a16627f26cdMathieu Chartier        return sirt_obj.get();  // Success!
7175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      }
718719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      case LockWord::kHashCode:
719ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier        // Inflate with the existing hashcode.
720590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        Inflate(self, nullptr, sirt_obj.get(), lock_word.GetHashCode());
721719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers        continue;  // Start from the beginning.
722590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      default: {
723590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
724e7e8a5fea2d852cccc840fa046151a16627f26cdMathieu Chartier        return sirt_obj.get();
725590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      }
7265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
7275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
7285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
7295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
7302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersbool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
7315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
7325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(obj != NULL);
733719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  obj = FakeUnlock(obj);
734d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
735590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  SirtRef<mirror::Object> sirt_obj(self, obj);
736d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
737ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
738ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // Fall-through.
739d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
740590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      FailedUnlock(sirt_obj.get(), self, NULL, NULL);
741d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return false;  // Failure.
742d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked: {
743d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t thread_id = self->GetThreadId();
744d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t owner_thread_id = lock_word.ThinLockOwner();
745d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (owner_thread_id != thread_id) {
746d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // TODO: there's a race here with the owner dying while we unlock.
747d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Thread* owner =
748d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
749590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        FailedUnlock(sirt_obj.get(), self, owner, NULL);
750d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return false;  // Failure.
7515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      } else {
752d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // We own the lock, decrease the recursion count.
753d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (lock_word.ThinLockCount() != 0) {
754d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint32_t new_count = lock_word.ThinLockCount() - 1;
755d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count));
756590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          sirt_obj->SetLockWord(thin_locked);
757d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        } else {
758590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          sirt_obj->SetLockWord(LockWord());
759d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
760d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return true;  // Success!
7615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      }
7625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
763d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
764d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
765d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return mon->Unlock(self);
7665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
767590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    default: {
768590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
769d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return false;
770590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
7715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
7725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
7735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
7745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
7755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait().  Also called for class init.
7765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
7772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
7784cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes                   bool interruptShouldThrow, ThreadState why) {
779d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(self != NULL);
780d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(obj != NULL);
7815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
782d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
783d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
784ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
785ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // Fall-through.
786d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
7876d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
788d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;  // Failure.
789d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked: {
790d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t thread_id = self->GetThreadId();
791d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t owner_thread_id = lock_word.ThinLockOwner();
792d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (owner_thread_id != thread_id) {
793d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
794d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Failure.
795d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      } else {
796d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // We own the lock, inflate to enqueue ourself on the Monitor.
7974e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier        Inflate(self, self, obj, 0);
798d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        lock_word = obj->GetLockWord();
799d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
800d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
8015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
802d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked:
803d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;  // Already set for a wait.
804590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    default: {
805590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
806590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      return;
807590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
8085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
809d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Monitor* mon = lock_word.FatLockMonitor();
810d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  mon->Wait(self, ms, ns, interruptShouldThrow, why);
8115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
8125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
81313c479e7e9b62115fe0409e5273c1e976a1c09f9Ian Rogersvoid Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
814d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(self != NULL);
815d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(obj != NULL);
8165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
817d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
818d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
819ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
820ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // Fall-through.
821d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
8226d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
823d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;  // Failure.
824d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked: {
825d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t thread_id = self->GetThreadId();
826d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t owner_thread_id = lock_word.ThinLockOwner();
827d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (owner_thread_id != thread_id) {
828d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
829d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Failure.
830d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      } else {
831d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // We own the lock but there's no Monitor and therefore no waiters.
832d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Success.
833d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
834d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    }
835d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
836d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
837d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (notify_all) {
838d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mon->NotifyAll(self);
839d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      } else {
840d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mon->Notify(self);
841d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
842d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;  // Success.
8435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
844590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    default: {
845590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
846590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      return;
847590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
8485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
8495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
8505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
851d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersuint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
852d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(obj != NULL);
8535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
854d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
855d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
856ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
857ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // Fall-through.
858d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
859d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return ThreadList::kInvalidThreadId;
860d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked:
861d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return lock_word.ThinLockOwner();
862d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
863d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
864d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return mon->GetOwnerThreadId();
8655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
866590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    default: {
867d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      LOG(FATAL) << "Unreachable";
868d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return ThreadList::kInvalidThreadId;
869590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
8705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
8715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
8725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
8738e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesvoid Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
874f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ThreadState state = thread->GetState();
8758e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
876d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  int32_t object_identity_hashcode = 0;
877d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  uint32_t lock_owner = ThreadList::kInvalidThreadId;
878d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  std::string pretty_type;
879b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
880b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    if (state == kSleeping) {
881b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      os << "  - sleeping on ";
882b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    } else {
883b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      os << "  - waiting on ";
884b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    }
88500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
886f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes      Thread* self = Thread::Current();
887dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      MutexLock mu(self, *thread->GetWaitMutex());
888dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      Monitor* monitor = thread->GetWaitMonitor();
889b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      if (monitor != NULL) {
890d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mirror::Object* object = monitor->obj_;
891d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        object_identity_hashcode = object->IdentityHashCode();
892d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        pretty_type = PrettyTypeOf(object);
893b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      }
8948e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    }
89534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes  } else if (state == kBlocked) {
8968e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    os << "  - waiting to lock ";
897dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    mirror::Object* object = thread->GetMonitorEnterObject();
8988e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    if (object != NULL) {
899d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      object_identity_hashcode = object->IdentityHashCode();
900d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      lock_owner = object->GetLockOwnerThreadId();
901d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      pretty_type = PrettyTypeOf(object);
9028e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    }
9038e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  } else {
9048e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    // We're not waiting on anything.
9058e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    return;
9068e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  }
9078e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
9088e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
909d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  os << StringPrintf("<0x%08x> (a %s)", object_identity_hashcode, pretty_type.c_str());
9108e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
911c5dc2ff2d1beb3ff84f93d2c7ebe07c46b76956aElliott Hughes  // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
912d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (lock_owner != ThreadList::kInvalidThreadId) {
9138e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    os << " held by thread " << lock_owner;
9148e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  }
9158e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
9168e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  os << "\n";
9178e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes}
9188e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
9192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
920f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
921f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  // definition of contended that includes a monitor a thread is trying to enter...
922dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  mirror::Object* result = thread->GetMonitorEnterObject();
923d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (result == NULL) {
924d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    // ...but also a monitor that the thread is waiting on.
925dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
926dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    Monitor* monitor = thread->GetWaitMonitor();
927f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    if (monitor != NULL) {
928d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      result = monitor->GetObject();
929f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    }
930f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
931d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  return result;
932f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
933f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
9342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
9352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                         void* callback_context) {
936ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = stack_visitor->GetMethod();
93708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  CHECK(m != NULL);
93808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
93908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // Native methods are an easy special case.
94008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
94108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  if (m->IsNative()) {
94208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    if (m->IsSynchronized()) {
9432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* jni_this = stack_visitor->GetCurrentSirt()->GetReference(0);
9444993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      callback(jni_this, callback_context);
94508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    }
94608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    return;
94708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
94808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
94961f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao  // Proxy methods should not be synchronized.
95061f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao  if (m->IsProxyMethod()) {
95161f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao    CHECK(!m->IsSynchronized());
95261f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao    return;
95361f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao  }
95461f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao
95508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // <clinit> is another special case. The runtime holds the class lock while calling <clinit>.
95608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  MethodHelper mh(m);
95708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  if (mh.IsClassInitializer()) {
9584993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    callback(m->GetDeclaringClass(), callback_context);
95908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    // Fall through because there might be synchronization in the user code too.
96008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
96108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
96208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // Is there any reason to believe there's any synchronization in this method?
96308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  const DexFile::CodeItem* code_item = mh.GetCodeItem();
964caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  CHECK(code_item != NULL) << PrettyMethod(m);
96508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  if (code_item->tries_size_ == 0) {
9667934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return;  // No "tries" implies no synchronization, so no held locks to report.
96708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
96808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
96980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
97080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  // the locks held in this stack frame.
97180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  std::vector<uint32_t> monitor_enter_dex_pcs;
97280537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  verifier::MethodVerifier::FindLocksAtDexPc(m, stack_visitor->GetDexPc(), monitor_enter_dex_pcs);
97380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  if (monitor_enter_dex_pcs.empty()) {
97480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    return;
97580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  }
97608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
97780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  for (size_t i = 0; i < monitor_enter_dex_pcs.size(); ++i) {
97880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    // The verifier works in terms of the dex pcs of the monitor-enter instructions.
97980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    // We want the registers used by those instructions (so we can read the values out of them).
98080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    uint32_t dex_pc = monitor_enter_dex_pcs[i];
98180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    uint16_t monitor_enter_instruction = code_item->insns_[dex_pc];
98208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
98380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    // Quick sanity check.
98480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) {
98580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes      LOG(FATAL) << "expected monitor-enter @" << dex_pc << "; was "
98680537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes                 << reinterpret_cast<void*>(monitor_enter_instruction);
98708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    }
98880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes
98980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff);
9902dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* o = reinterpret_cast<mirror::Object*>(stack_visitor->GetVReg(m, monitor_register,
9912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                                                                 kReferenceVReg));
9924993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    callback(o, callback_context);
99308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
99408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes}
99508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
996d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::IsValidLockWord(LockWord lock_word) {
997d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
998d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
999d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Nothing to check.
1000d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return true;
1001d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked:
1002d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Basic sanity check of owner.
1003d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1004d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
1005d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Check the  monitor appears in the monitor list.
1006d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
1007d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      MonitorList* list = Runtime::Current()->GetMonitorList();
1008d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1009d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      for (Monitor* list_mon : list->list_) {
1010d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (mon == list_mon) {
1011d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          return true;  // Found our monitor.
1012d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
10137dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers      }
1014d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return false;  // Fail - unowned monitor in an object.
10157dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers    }
1016ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
1017ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      return true;
1018d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    default:
1019d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      LOG(FATAL) << "Unreachable";
1020d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return false;
10217dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers  }
10227dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers}
10237dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers
1024ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartierbool Monitor::IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1025ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  MutexLock mu(Thread::Current(), monitor_lock_);
1026ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  return owner_ != nullptr;
1027ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier}
1028ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier
1029ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersvoid Monitor::TranslateLocation(mirror::ArtMethod* method, uint32_t dex_pc,
1030d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                const char** source_file, uint32_t* line_number) const {
103133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  // If method is null, location is unknown
103233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  if (method == NULL) {
1033d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    *source_file = "";
1034d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    *line_number = 0;
103533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao    return;
103633dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  }
10376d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(method);
1038d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  *source_file = mh.GetDeclaringClassSourceFile();
1039d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (*source_file == NULL) {
1040d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    *source_file = "";
1041d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  }
1042d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  *line_number = mh.GetLineNumFromDexPC(dex_pc);
1043d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers}
1044d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
1045d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersuint32_t Monitor::GetOwnerThreadId() {
1046d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(Thread::Current(), monitor_lock_);
1047d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Thread* owner = owner_;
1048d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (owner != NULL) {
1049d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    return owner->GetThreadId();
1050d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  } else {
1051d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    return ThreadList::kInvalidThreadId;
105212c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes  }
105333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao}
105433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao
1055c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu ChartierMonitorList::MonitorList()
1056c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier    : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock"),
1057c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier      monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
1058c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
1059c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
1060c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesMonitorList::~MonitorList() {
106150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), monitor_list_lock_);
1062c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  STLDeleteElements(&list_);
1063c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
1064c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
1065c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::DisallowNewMonitors() {
106650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), monitor_list_lock_);
1067c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  allow_new_monitors_ = false;
1068c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier}
1069c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier
1070c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::AllowNewMonitors() {
1071c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  Thread* self = Thread::Current();
1072c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  MutexLock mu(self, monitor_list_lock_);
1073c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  allow_new_monitors_ = true;
1074c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  monitor_add_condition_.Broadcast(self);
1075c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier}
1076c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier
1077c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::Add(Monitor* m) {
1078c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  Thread* self = Thread::Current();
1079c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  MutexLock mu(self, monitor_list_lock_);
1080c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  while (UNLIKELY(!allow_new_monitors_)) {
1081c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier    monitor_add_condition_.WaitHoldingLocks(self);
1082c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  }
1083c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  list_.push_front(m);
1084c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
1085c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
108683c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartiervoid MonitorList::SweepMonitorList(IsMarkedCallback* callback, void* arg) {
108750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), monitor_list_lock_);
108802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier  for (auto it = list_.begin(); it != list_.end(); ) {
1089c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes    Monitor* m = *it;
10906aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier    mirror::Object* obj = m->GetObject();
1091590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // The object of a monitor can be null if we have deflated it.
109283c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier    mirror::Object* new_obj = obj != nullptr ? callback(obj, arg) : nullptr;
10936aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier    if (new_obj == nullptr) {
10946aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier      VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
10956aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier                    << m->GetObject();
1096c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes      delete m;
1097c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes      it = list_.erase(it);
1098c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes    } else {
10996aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier      m->SetObject(new_obj);
1100c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes      ++it;
1101c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes    }
1102c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  }
1103c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
1104c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
1105d9c4fc94fa618617f94e1de9af5f034549100753Ian RogersMonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(NULL), entry_count_(0) {
1106d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(obj != NULL);
1107d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
1108d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
1109d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
1110d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
1111ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // Fall-through.
1112590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    case LockWord::kForwardingAddress:
1113590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // Fall-through.
1114ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
1115d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
1116d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked:
1117d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
1118d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      entry_count_ = 1 + lock_word.ThinLockCount();
1119d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Thin locks have no waiters.
1120d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
1121d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
1122d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
1123d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      owner_ = mon->owner_;
1124d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      entry_count_ = 1 + mon->lock_count_;
1125dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      for (Thread* waiter = mon->wait_set_; waiter != NULL; waiter = waiter->GetWaitNext()) {
1126d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        waiters_.push_back(waiter);
1127d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
1128d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
1129f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    }
1130f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
1131f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
1132f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
11335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}  // namespace art
1134