monitor.cc revision 97509954404d031594b2ecbda607314d169d512e
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
19f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier#define ATRACE_TAG ATRACE_TAG_DALVIK
20f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier
21f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier#include <cutils/trace.h>
2208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include <vector>
2308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
24e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
2576b6167407c2b6f5d40ad895b2793a6b037f54b2Elliott Hughes#include "base/mutex.h"
261aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h"
2780afd02024d20e60b197d3adfbb43cc303cf29e0Vladimir Marko#include "base/time_utils.h"
2833dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao#include "class_linker.h"
294f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
3008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "dex_instruction.h"
31d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers#include "lock_word-inl.h"
324f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "mirror/class-inl.h"
3305f3057d6a4d23d712092ccd36a531590bff323bIan Rogers#include "mirror/object-inl.h"
342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
3500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "thread.h"
378e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "thread_list.h"
3808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "verifier/method_verifier.h"
39044288fb8f69e154862763a6b3bfea2bac1b397eElliott Hughes#include "well_known_classes.h"
405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesnamespace art {
425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
43b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartierstatic constexpr uint64_t kLongWaitMs = 100;
44b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier
455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
46d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Every Object has a monitor associated with it, but not every Object is actually locked.  Even
47d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * the ones that are locked do not need a full-fledged monitor until a) there is actual contention
48d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * or b) wait() is called on the Object.
495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
50d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s
51d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * "Thin locks: featherweight synchronization for Java" (ACM 1998).  Things are even easier for us,
52d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * though, because we have a full 32 bits to work with.
535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
54d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * The two states of an Object's lock are referred to as "thin" and "fat".  A lock may transition
55d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once
56d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * a lock has been inflated it remains in the "fat" state indefinitely.
575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
58d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * The lock value itself is stored in mirror::Object::monitor_ and the representation is described
59d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * in the LockWord value type.
6054e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes *
615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Monitors provide:
625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - mutually exclusive access to resources
635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - a way for multiple threads to wait for notification
645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * In effect, they fill the role of both mutexes and condition variables.
665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
67d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Only one thread can own the monitor at any time.  There may be several threads waiting on it
68d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * (the wait call unlocks it).  One or more waiting threads may be getting interrupted or notified
69d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * at any given time.
705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
7154e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes
722cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartierbool (*Monitor::is_sensitive_thread_hook_)() = nullptr;
73fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesuint32_t Monitor::lock_profiling_threshold_ = 0;
7432d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes
75fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesbool Monitor::IsSensitiveThread() {
762cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (is_sensitive_thread_hook_ != nullptr) {
77fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes    return (*is_sensitive_thread_hook_)();
78fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  }
79fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  return false;
80fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes}
81fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes
824dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughesvoid Monitor::Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()) {
83fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  lock_profiling_threshold_ = lock_profiling_threshold;
84fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  is_sensitive_thread_hook_ = is_sensitive_thread_hook;
8532d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes}
8632d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes
87ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersMonitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
8800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    : monitor_lock_("a monitor lock", kMonitorLock),
89d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      monitor_contenders_("monitor contenders", monitor_lock_),
9046bc778f1feed02b20d25e3d03470c93ca2c0506Mathieu Chartier      num_waiters_(0),
9100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      owner_(owner),
925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      lock_count_(0),
9394f7b49578b6aaa80de8ffed230648d601393905Hiroshi Yamauchi      obj_(GcRoot<mirror::Object>(obj)),
942cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      wait_set_(nullptr),
95ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      hash_code_(hash_code),
962cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      locking_method_(nullptr),
97ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      locking_dex_pc_(0),
9874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe      monitor_id_(MonitorPool::ComputeMonitorId(this, self)) {
9974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe#ifdef __LP64__
10074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  DCHECK(false) << "Should not be reached in 64b";
10174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  next_free_ = nullptr;
10274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe#endif
10374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
10474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // with the owner unlocking the thin-lock.
10574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  CHECK(owner == nullptr || owner == self || owner->IsSuspended());
10674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // The identity hash code is set for the life time of the monitor.
10774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe}
10874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe
10974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas GampeMonitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code,
11074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe                 MonitorId id)
11174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    : monitor_lock_("a monitor lock", kMonitorLock),
11274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe      monitor_contenders_("monitor contenders", monitor_lock_),
11374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe      num_waiters_(0),
11474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe      owner_(owner),
11574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe      lock_count_(0),
11694f7b49578b6aaa80de8ffed230648d601393905Hiroshi Yamauchi      obj_(GcRoot<mirror::Object>(obj)),
1172cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      wait_set_(nullptr),
11874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe      hash_code_(hash_code),
1192cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      locking_method_(nullptr),
12074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe      locking_dex_pc_(0),
12174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe      monitor_id_(id) {
12274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe#ifdef __LP64__
12374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  next_free_ = nullptr;
12474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe#endif
125d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
126d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // with the owner unlocking the thin-lock.
127ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  CHECK(owner == nullptr || owner == self || owner->IsSuspended());
128ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  // The identity hash code is set for the life time of the monitor.
129d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers}
130d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
1314e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartierint32_t Monitor::GetHashCode() {
1324e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier  while (!HasHashCode()) {
1333e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers    if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) {
1344e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier      break;
1354e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier    }
1364e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier  }
1374e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier  DCHECK(HasHashCode());
1383e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers  return hash_code_.LoadRelaxed();
1394e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier}
1404e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier
141d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::Install(Thread* self) {
142d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);  // Uncontended mutex acquisition as monitor isn't yet public.
143ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended());
14400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Propagate the lock state.
1454cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi  LockWord lw(GetObject()->GetLockWord(false));
146ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  switch (lw.GetState()) {
147ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kThinLocked: {
148ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner());
149ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      lock_count_ = lw.ThinLockCount();
150ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      break;
151ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
152ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode: {
1533e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers      CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode()));
154ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      break;
155ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
156ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kFatLocked: {
157ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // The owner_ is suspended but another thread beat us to install a monitor.
158ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      return false;
159ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
160ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kUnlocked: {
161ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      LOG(FATAL) << "Inflating unlocked lock word";
162ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      break;
163ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
164590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    default: {
165590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      LOG(FATAL) << "Invalid monitor state " << lw.GetState();
166590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      return false;
167590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
168d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  }
169e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi  LockWord fat(this, lw.ReadBarrierState());
170d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // Publish the updated lock word, which may race with other threads.
171228602f562f1d130d06e60a98752d99c2d467d6aIan Rogers  bool success = GetObject()->CasLockWordWeakSequentiallyConsistent(lw, fat);
17200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Lock profiling.
1739728f91a63016136261231ff5213bde703bd27b6Mathieu Chartier  if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) {
1746ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe    // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on
1756ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe    // abort.
1766ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe    locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false);
17700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
178d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  return success;
1795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
1805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1815f79133a435ebcb20000370d56046fe01201dd80Elliott HughesMonitor::~Monitor() {
182590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  // Deflated monitors have a null object.
1835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
1845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::AppendToWaitSet(Thread* thread) {
1865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(owner_ == Thread::Current());
1872cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(thread != nullptr);
188dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext();
1892cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (wait_set_ == nullptr) {
1905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    wait_set_ = thread;
1915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
1925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // push_back.
1955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  Thread* t = wait_set_;
196dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  while (t->GetWaitNext() != nullptr) {
197dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    t = t->GetWaitNext();
1985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
199dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  t->SetWaitNext(thread);
2005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
2015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::RemoveFromWaitSet(Thread *thread) {
2035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(owner_ == Thread::Current());
2042cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(thread != nullptr);
2052cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (wait_set_ == nullptr) {
2065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
2075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
2085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (wait_set_ == thread) {
209dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    wait_set_ = thread->GetWaitNext();
210dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    thread->SetWaitNext(nullptr);
2115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
2125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
2135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  Thread* t = wait_set_;
2152cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  while (t->GetWaitNext() != nullptr) {
216dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    if (t->GetWaitNext() == thread) {
217dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      t->SetWaitNext(thread->GetWaitNext());
218dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      thread->SetWaitNext(nullptr);
2195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      return;
2205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
221dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    t = t->GetWaitNext();
2225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
2235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
2245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2256aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartiervoid Monitor::SetObject(mirror::Object* object) {
22694f7b49578b6aaa80de8ffed230648d601393905Hiroshi Yamauchi  obj_ = GcRoot<mirror::Object>(object);
2276aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier}
2286aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier
2295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Lock(Thread* self) {
230d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
231d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  while (true) {
232440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier    if (owner_ == nullptr) {  // Unowned.
233d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      owner_ = self;
234d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      CHECK_EQ(lock_count_, 0);
235d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // When debugging, save the current monitor holder for future
236d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // acquisition failures to use in sampled logging.
237d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (lock_profiling_threshold_ != 0) {
238d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
239fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes      }
240d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;
241d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    } else if (owner_ == self) {  // Recursive.
242d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      lock_count_++;
243d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;
244fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes    }
245d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    // Contended.
246d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    const bool log_contention = (lock_profiling_threshold_ != 0);
247b894a19dfd668b6779de939cf5265b7e409d8809Xin Guan    uint64_t wait_start_ms = log_contention ? MilliTime() : 0;
248e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    ArtMethod* owners_method = locking_method_;
249d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    uint32_t owners_dex_pc = locking_dex_pc_;
250440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier    // Do this before releasing the lock so that we don't get deflated.
251b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier    size_t num_waiters = num_waiters_;
252440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier    ++num_waiters_;
253d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);  // Let go of locks in order.
254a6e7f0872c42009ecbee82d7fbe452deef9ae65bMathieu Chartier    self->SetMonitorEnterObject(GetObject());
255d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    {
256d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      ScopedThreadStateChange tsc(self, kBlocked);  // Change to blocked and give up mutator_lock_.
2572cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      // Reacquire monitor_lock_ without mutator_lock_ for Wait.
2582cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      MutexLock mu2(self, monitor_lock_);
259f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier      if (owner_ != nullptr) {  // Did the owner_ give the lock up?
260f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier        if (ATRACE_ENABLED()) {
261f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier          std::string name;
262f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier          owner_->GetThreadName(name);
263f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier          ATRACE_BEGIN(("Contended on monitor with owner " + name).c_str());
264f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier        }
265d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        monitor_contenders_.Wait(self);  // Still contended so wait.
266d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // Woken from contention.
267d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (log_contention) {
268d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint64_t wait_ms = MilliTime() - wait_start_ms;
269d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint32_t sample_percent;
270d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (wait_ms >= lock_profiling_threshold_) {
271d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            sample_percent = 100;
272d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          } else {
273d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            sample_percent = 100 * wait_ms / lock_profiling_threshold_;
274d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          }
275d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
276d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            const char* owners_filename;
277d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            uint32_t owners_line_number;
278d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
279b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier            if (wait_ms > kLongWaitMs && owners_method != nullptr) {
280b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier              LOG(WARNING) << "Long monitor contention event with owner method="
281b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier                  << PrettyMethod(owners_method) << " from " << owners_filename << ":"
282b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier                  << owners_line_number << " waiters=" << num_waiters << " for "
283b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier                  << PrettyDuration(MsToNs(wait_ms));
284b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier            }
285d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            LogContentionEvent(self, wait_ms, sample_percent, owners_filename, owners_line_number);
286d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          }
287d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
288f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier        ATRACE_END();
289fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes      }
2905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
291a6e7f0872c42009ecbee82d7fbe452deef9ae65bMathieu Chartier    self->SetMonitorEnterObject(nullptr);
292d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Lock(self);  // Reacquire locks in order.
293440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier    --num_waiters_;
294fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  }
2955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
2965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2976d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
2986d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                              __attribute__((format(printf, 1, 2)));
2996d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers
30000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
301b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3026d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  va_list args;
3036d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  va_start(args, fmt);
30462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
3050aa50ce2fb75bfc2e815a0c33adf9b049561923bNicolas Geoffray  self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args);
306d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
30764277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom    std::ostringstream ss;
30862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    self->Dump(ss);
309d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    LOG(Runtime::Current()->IsStarted() ? INFO : ERROR)
31014691c5e786e8c2c5734f687e4c96217340771beNicolas Geoffray        << self->GetException()->Dump() << "\n" << ss.str();
31164277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom  }
3126d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  va_end(args);
3136d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers}
3146d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers
315d423741f91526cada9d081c64ee295ec9863032dElliott Hughesstatic std::string ThreadToString(Thread* thread) {
3162cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (thread == nullptr) {
3172cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier    return "nullptr";
318d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  }
319d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  std::ostringstream oss;
320d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  // TODO: alternatively, we could just return the thread's name.
321d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  oss << *thread;
322d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  return oss.str();
323d423741f91526cada9d081c64ee295ec9863032dElliott Hughes}
324d423741f91526cada9d081c64ee295ec9863032dElliott Hughes
3252dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::FailedUnlock(mirror::Object* o, Thread* expected_owner, Thread* found_owner,
326ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                           Monitor* monitor) {
3272cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  Thread* current_owner = nullptr;
328ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  std::string current_owner_string;
329ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  std::string expected_owner_string;
330ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  std::string found_owner_string;
331ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  {
332ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // TODO: isn't this too late to prevent threads from disappearing?
333ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // Acquire thread list lock so threads won't disappear from under us.
33450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
335ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // Re-read owner now that we hold lock.
3362cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier    current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr;
337ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // Get short descriptions of the threads involved.
338ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    current_owner_string = ThreadToString(current_owner);
339ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    expected_owner_string = ThreadToString(expected_owner);
340ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    found_owner_string = ThreadToString(found_owner);
341ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  }
3422cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (current_owner == nullptr) {
3432cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier    if (found_owner == nullptr) {
3446d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
3456d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                         " on thread '%s'",
346ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         PrettyTypeOf(o).c_str(),
347ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         expected_owner_string.c_str());
3486d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    } else {
3496d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      // Race: the original read found an owner but now there is none
3506d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
3516d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                         " (where now the monitor appears unowned) on thread '%s'",
352ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         found_owner_string.c_str(),
353ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         PrettyTypeOf(o).c_str(),
354ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         expected_owner_string.c_str());
3556d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    }
3566d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  } else {
3572cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier    if (found_owner == nullptr) {
3586d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      // Race: originally there was no owner, there is now
3596d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
3606d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                         " (originally believed to be unowned) on thread '%s'",
361ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         current_owner_string.c_str(),
362ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         PrettyTypeOf(o).c_str(),
363ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         expected_owner_string.c_str());
3646d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    } else {
3656d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      if (found_owner != current_owner) {
3666d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers        // Race: originally found and current owner have changed
3676d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers        ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
3686d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                           " owned by '%s') on object of type '%s' on thread '%s'",
369ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           found_owner_string.c_str(),
370ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           current_owner_string.c_str(),
371ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           PrettyTypeOf(o).c_str(),
372ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           expected_owner_string.c_str());
3736d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      } else {
3746d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers        ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
3756d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                           " on thread '%s",
376ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           current_owner_string.c_str(),
377ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           PrettyTypeOf(o).c_str(),
378ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           expected_owner_string.c_str());
3796d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      }
3806d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    }
3816d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  }
3825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
3835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
384d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::Unlock(Thread* self) {
3852cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(self != nullptr);
386d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
3876d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  Thread* owner = owner_;
3886d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  if (owner == self) {
3895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // We own the monitor, so nobody else can be in here.
3905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (lock_count_ == 0) {
3912cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      owner_ = nullptr;
3922cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      locking_method_ = nullptr;
3930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      locking_dex_pc_ = 0;
394d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Wake a contender.
395d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      monitor_contenders_.Signal(self);
3965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    } else {
3975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      --lock_count_;
3985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
3995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  } else {
4005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // We don't own this, so we're not allowed to unlock it.
4015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // The JNI spec says that we should throw IllegalMonitorStateException
4025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // in this case.
4034cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi    FailedUnlock(GetObject(), self, owner, this);
4045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return false;
4055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  return true;
4075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
4085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4094cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughesvoid Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
4104cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes                   bool interruptShouldThrow, ThreadState why) {
4112cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(self != nullptr);
4124cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes  DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
4135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
414d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  monitor_lock_.Lock(self);
415d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
4165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Make sure that we hold the lock.
4175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (owner_ != self) {
418d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);
4191af6a1fa35ff7dc0a5c653f19dbc8a91c914aa42Elena Sayapina    ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
4205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
4215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4224cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes
423df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  // We need to turn a zero-length timed wait into a regular wait because
424df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
425df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
426df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes    why = kWaiting;
427df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  }
428df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes
4295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Enforce the timeout range.
4305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (ms < 0 || ns < 0 || ns > 999999) {
4311af6a1fa35ff7dc0a5c653f19dbc8a91c914aa42Elena Sayapina    monitor_lock_.Unlock(self);
4320aa50ce2fb75bfc2e815a0c33adf9b049561923bNicolas Geoffray    self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
433ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
4345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
4355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  /*
4385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * Add ourselves to the set of threads waiting on this monitor, and
4395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * release our hold.  We need to let it go even if we're a few levels
4405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * deep in a recursive lock, and we need to restore that later.
4415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   *
4425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * We append to the wait set ahead of clearing the count and owner
4435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * fields so the subroutine can check that the calling thread owns
4445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * the monitor.  Aside from that, the order of member updates is
4455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * not order sensitive as we hold the pthread mutex.
4465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   */
4475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  AppendToWaitSet(self);
448440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier  ++num_waiters_;
4490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  int prev_lock_count = lock_count_;
4505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  lock_count_ = 0;
4512cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  owner_ = nullptr;
452e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  ArtMethod* saved_method = locking_method_;
4532cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  locking_method_ = nullptr;
4540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  uintptr_t saved_dex_pc = locking_dex_pc_;
4550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  locking_dex_pc_ = 0;
4565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  /*
4584cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes   * Update thread state. If the GC wakes up, it'll ignore us, knowing
4595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * that we won't touch any references in this state, and we'll check
4605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * our suspend mode before we transition out.
4615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   */
4624cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes  self->TransitionFromRunnableToSuspended(why);
4635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
464b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  bool was_interrupted = false;
46500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
46600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
467dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    MutexLock mu(self, *self->GetWaitMutex());
4685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
46900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
4702cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier    // non-null a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
47100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // up.
472dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    DCHECK(self->GetWaitMonitor() == nullptr);
473dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    self->SetWaitMonitor(this);
4745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
47500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Release the monitor lock.
476d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_contenders_.Signal(self);
477d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);
47800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
479b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // Handle the case where the thread was interrupted before we called wait().
480dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    if (self->IsInterruptedLocked()) {
481b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      was_interrupted = true;
48200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    } else {
48300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      // Wait for a notification or a timeout to occur.
4844cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes      if (why == kWaiting) {
485dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers        self->GetWaitConditionVariable()->Wait(self);
48600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      } else {
4874cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes        DCHECK(why == kTimedWaiting || why == kSleeping) << why;
488dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers        self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
48900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
490dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      if (self->IsInterruptedLocked()) {
491b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes        was_interrupted = true;
49200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
493dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      self->SetInterruptedLocked(false);
49400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
4955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
49700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Set self->status back to kRunnable, and self-suspend if needed.
49800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  self->TransitionFromSuspendedToRunnable();
4995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
500b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  {
501b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // We reset the thread's wait_monitor_ field after transitioning back to runnable so
502b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
503b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
504b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // are waiting on "null".)
505dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    MutexLock mu(self, *self->GetWaitMutex());
506dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    DCHECK(self->GetWaitMonitor() != nullptr);
507dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    self->SetWaitMonitor(nullptr);
508b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  }
509b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes
510d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // Re-acquire the monitor and lock.
51100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Lock(self);
512d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  monitor_lock_.Lock(self);
513dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  self->GetWaitMutex()->AssertNotHeld(self);
5145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  /*
5165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * We remove our thread from wait set after restoring the count
5175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * and owner fields so the subroutine can check that the calling
5185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * thread owns the monitor. Aside from that, the order of member
5195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * updates is not order sensitive as we hold the pthread mutex.
5205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   */
5215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  owner_ = self;
5220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  lock_count_ = prev_lock_count;
5230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  locking_method_ = saved_method;
5240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  locking_dex_pc_ = saved_dex_pc;
525440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier  --num_waiters_;
5265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  RemoveFromWaitSet(self);
5275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5281af6a1fa35ff7dc0a5c653f19dbc8a91c914aa42Elena Sayapina  monitor_lock_.Unlock(self);
5291af6a1fa35ff7dc0a5c653f19dbc8a91c914aa42Elena Sayapina
530b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  if (was_interrupted) {
5315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    /*
5325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * We were interrupted while waiting, or somebody interrupted an
5335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * un-interruptible thread earlier and we're bailing out immediately.
5345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     *
5355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * The doc sayeth: "The interrupted status of the current thread is
5365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * cleared when this exception is thrown."
5375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     */
53800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
539dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      MutexLock mu(self, *self->GetWaitMutex());
540dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      self->SetInterruptedLocked(false);
54100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
5425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (interruptShouldThrow) {
5432cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr);
5445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
5455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
5475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Notify(Thread* self) {
5492cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(self != nullptr);
550d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
5515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Make sure that we hold the lock.
5525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (owner_ != self) {
5536d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
5545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
5555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Signal the first waiting thread in the wait set.
5572cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  while (wait_set_ != nullptr) {
5585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    Thread* thread = wait_set_;
559dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    wait_set_ = thread->GetWaitNext();
560dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    thread->SetWaitNext(nullptr);
5615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // Check to see if the thread is still waiting.
563277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    MutexLock wait_mu(self, *thread->GetWaitMutex());
564dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    if (thread->GetWaitMonitor() != nullptr) {
565dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers      thread->GetWaitConditionVariable()->Signal(self);
5665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      return;
5675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
5685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
5705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::NotifyAll(Thread* self) {
5722cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(self != nullptr);
573d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
5745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Make sure that we hold the lock.
5755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (owner_ != self) {
5766d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
5775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
5785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Signal all threads in the wait set.
5802cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  while (wait_set_ != nullptr) {
5815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    Thread* thread = wait_set_;
582dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    wait_set_ = thread->GetWaitNext();
583dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    thread->SetWaitNext(nullptr);
5845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    thread->Notify();
5855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
5875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
588590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartierbool Monitor::Deflate(Thread* self, mirror::Object* obj) {
589590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  DCHECK(obj != nullptr);
5904d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  // Don't need volatile since we only deflate with mutators suspended.
5914d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  LockWord lw(obj->GetLockWord(false));
592590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  // If the lock isn't an inflated monitor, then we don't need to deflate anything.
593590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  if (lw.GetState() == LockWord::kFatLocked) {
594590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    Monitor* monitor = lw.FatLockMonitor();
595440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier    DCHECK(monitor != nullptr);
596590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    MutexLock mu(self, monitor->monitor_lock_);
597440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier    // Can't deflate if we have anybody waiting on the CV.
598440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier    if (monitor->num_waiters_ > 0) {
599440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier      return false;
600440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier    }
601590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    Thread* owner = monitor->owner_;
602590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (owner != nullptr) {
603590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // Can't deflate if we are locked and have a hash code.
604590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      if (monitor->HasHashCode()) {
605590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        return false;
606590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      }
607590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // Can't deflate if our lock count is too high.
608590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      if (monitor->lock_count_ > LockWord::kThinLockMaxCount) {
609590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        return false;
610590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      }
611590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // Deflate to a thin lock.
612e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_,
613e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi                                                 lw.ReadBarrierState());
614e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      // Assume no concurrent read barrier state changes as mutators are suspended.
615e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      obj->SetLockWord(new_lw, false);
6164d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier      VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / "
6174d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier          << monitor->lock_count_;
618590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    } else if (monitor->HasHashCode()) {
619e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.ReadBarrierState());
620e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      // Assume no concurrent read barrier state changes as mutators are suspended.
621e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      obj->SetLockWord(new_lw, false);
622440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier      VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode();
623590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    } else {
624590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // No lock and no hash, just put an empty lock word inside the object.
625e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      LockWord new_lw = LockWord::FromDefault(lw.ReadBarrierState());
626e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      // Assume no concurrent read barrier state changes as mutators are suspended.
627e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      obj->SetLockWord(new_lw, false);
628440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier      VLOG(monitor) << "Deflated" << obj << " to empty lock word";
629590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
6302cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier    // The monitor is deflated, mark the object as null so that we know to delete it during the
631590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // next GC.
63294f7b49578b6aaa80de8ffed230648d601393905Hiroshi Yamauchi    monitor->obj_ = GcRoot<mirror::Object>(nullptr);
633590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  }
634590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  return true;
635590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier}
636590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
637ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartiervoid Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
63874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  DCHECK(self != nullptr);
63974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  DCHECK(obj != nullptr);
6405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Allocate and acquire a new monitor.
64174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code);
64274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  DCHECK(m != nullptr);
643d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (m->Install(self)) {
64486ab7912423f63541073af5c487b509e9b2b5420Haifeng Li    if (owner != nullptr) {
64586ab7912423f63541073af5c487b509e9b2b5420Haifeng Li      VLOG(monitor) << "monitor: thread" << owner->GetThreadId()
64674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe          << " created monitor " << m << " for object " << obj;
64786ab7912423f63541073af5c487b509e9b2b5420Haifeng Li    } else {
64886ab7912423f63541073af5c487b509e9b2b5420Haifeng Li      VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code
64974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe          << " created monitor " << m << " for object " << obj;
65086ab7912423f63541073af5c487b509e9b2b5420Haifeng Li    }
65174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    Runtime::Current()->GetMonitorList()->Add(m);
6524d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier    CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked);
65374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  } else {
65474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe    MonitorPool::ReleaseMonitor(self, m);
655ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  }
656ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier}
657ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier
6580cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartiervoid Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
659ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier                                uint32_t hash_code) {
660ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
661ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  uint32_t owner_thread_id = lock_word.ThinLockOwner();
662ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  if (owner_thread_id == self->GetThreadId()) {
663ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    // We own the monitor, we can easily inflate it.
664eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Inflate(self, self, obj.Get(), hash_code);
665ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  } else {
666ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    ThreadList* thread_list = Runtime::Current()->GetThreadList();
667ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
668eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    self->SetMonitorEnterObject(obj.Get());
669a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier    bool timed_out;
670a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier    Thread* owner;
671a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier    {
672a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier      ScopedThreadStateChange tsc(self, kBlocked);
673a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier      owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out);
674a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier    }
675a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier    if (owner != nullptr) {
676a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier      // We succeeded in suspending the thread, check the lock's status didn't change.
677a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier      lock_word = obj->GetLockWord(true);
678a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier      if (lock_word.GetState() == LockWord::kThinLocked &&
679a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier          lock_word.ThinLockOwner() == owner_thread_id) {
680a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier        // Go ahead and inflate the lock.
681a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier        Inflate(self, owner, obj.Get(), hash_code);
682ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      }
683a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier      thread_list->Resume(owner, false);
684ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    }
685dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    self->SetMonitorEnterObject(nullptr);
686d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  }
6875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
6885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
689719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Fool annotalysis into thinking that the lock on obj is acquired.
690719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic mirror::Object* FakeLock(mirror::Object* obj)
691719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
692719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return obj;
693719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}
694719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
695719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Fool annotalysis into thinking that the lock on obj is release.
696719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic mirror::Object* FakeUnlock(mirror::Object* obj)
697719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
698719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return obj;
699719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers}
700719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers
701e7e8a5fea2d852cccc840fa046151a16627f26cdMathieu Chartiermirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) {
7022cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(self != nullptr);
7032cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(obj != nullptr);
704719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  obj = FakeLock(obj);
705d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  uint32_t thread_id = self->GetThreadId();
706d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  size_t contention_count = 0;
707eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<1> hs(self);
708eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::Object> h_obj(hs.NewHandle(obj));
709d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  while (true) {
710eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    LockWord lock_word = h_obj->GetLockWord(true);
711d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    switch (lock_word.GetState()) {
712d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      case LockWord::kUnlocked: {
713e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.ReadBarrierState()));
714228602f562f1d130d06e60a98752d99c2d467d6aIan Rogers        if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) {
7153035961cb41865b80b927546be0c708b6389cec6Hans Boehm          // CasLockWord enforces more than the acquire ordering we need here.
716eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier          return h_obj.Get();  // Success!
717d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
718d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        continue;  // Go again.
7195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      }
720d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      case LockWord::kThinLocked: {
721d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        uint32_t owner_thread_id = lock_word.ThinLockOwner();
722d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (owner_thread_id == thread_id) {
723d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          // We own the lock, increase the recursion count.
724d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint32_t new_count = lock_word.ThinLockCount() + 1;
725d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
726e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count,
727e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi                                                          lock_word.ReadBarrierState()));
728e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            if (!kUseReadBarrier) {
729e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi              h_obj->SetLockWord(thin_locked, true);
730e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi              return h_obj.Get();  // Success!
731e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            } else {
732e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi              // Use CAS to preserve the read barrier state.
733e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi              if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) {
734e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi                return h_obj.Get();  // Success!
735e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi              }
736e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            }
737e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            continue;  // Go again.
738d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          } else {
739d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            // We'd overflow the recursion count, so inflate the monitor.
740eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier            InflateThinLocked(self, h_obj, lock_word, 0);
741d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          }
742d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        } else {
743d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          // Contention.
744d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          contention_count++;
745ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier          Runtime* runtime = Runtime::Current();
746ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier          if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) {
747b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier            // TODO: Consider switching the thread state to kBlocked when we are yielding.
748251755cd511463260e60be98bf138b6aa1c14bf3Mathieu Chartier            // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the
749251755cd511463260e60be98bf138b6aa1c14bf3Mathieu Chartier            // parameter you pass in. This can cause thread suspension to take excessively long
750b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier            // and make long pauses. See b/16307460.
751251755cd511463260e60be98bf138b6aa1c14bf3Mathieu Chartier            sched_yield();
7525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes          } else {
753d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            contention_count = 0;
754eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier            InflateThinLocked(self, h_obj, lock_word, 0);
7555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes          }
7565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes        }
757d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        continue;  // Start from the beginning.
758d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
759d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      case LockWord::kFatLocked: {
760d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Monitor* mon = lock_word.FatLockMonitor();
761d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mon->Lock(self);
762eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        return h_obj.Get();  // Success!
7635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      }
764719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      case LockWord::kHashCode:
765ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier        // Inflate with the existing hashcode.
766eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
767719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers        continue;  // Start from the beginning.
768590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      default: {
769590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier        LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
770eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier        return h_obj.Get();
771590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      }
7725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
7735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
7745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
7755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
7762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersbool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
7772cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(self != nullptr);
7782cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  DCHECK(obj != nullptr);
779719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  obj = FakeUnlock(obj);
780eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<1> hs(self);
781eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::Object> h_obj(hs.NewHandle(obj));
782e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi  while (true) {
783e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi    LockWord lock_word = obj->GetLockWord(true);
784e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi    switch (lock_word.GetState()) {
785e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      case LockWord::kHashCode:
786e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        // Fall-through.
787e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      case LockWord::kUnlocked:
788e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        FailedUnlock(h_obj.Get(), self, nullptr, nullptr);
789d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return false;  // Failure.
790e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      case LockWord::kThinLocked: {
791e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        uint32_t thread_id = self->GetThreadId();
792e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        uint32_t owner_thread_id = lock_word.ThinLockOwner();
793e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        if (owner_thread_id != thread_id) {
794e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          // TODO: there's a race here with the owner dying while we unlock.
795e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          Thread* owner =
796e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi              Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
797e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          FailedUnlock(h_obj.Get(), self, owner, nullptr);
798e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          return false;  // Failure.
799d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        } else {
800e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          // We own the lock, decrease the recursion count.
801e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          LockWord new_lw = LockWord::Default();
802e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          if (lock_word.ThinLockCount() != 0) {
803e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            uint32_t new_count = lock_word.ThinLockCount() - 1;
804e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.ReadBarrierState());
805e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          } else {
806e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            new_lw = LockWord::FromDefault(lock_word.ReadBarrierState());
807e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          }
808e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          if (!kUseReadBarrier) {
809e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            DCHECK_EQ(new_lw.ReadBarrierState(), 0U);
810e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            h_obj->SetLockWord(new_lw, true);
811e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            // Success!
812e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            return true;
813e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          } else {
814e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            // Use CAS to preserve the read barrier state.
815e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, new_lw)) {
816e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi              // Success!
817e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi              return true;
818e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi            }
819e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          }
820e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi          continue;  // Go again.
821d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
8225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      }
823e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      case LockWord::kFatLocked: {
824e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        Monitor* mon = lock_word.FatLockMonitor();
825e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        return mon->Unlock(self);
826e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      }
827e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      default: {
828e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
829e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi        return false;
830e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi      }
831590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
8325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
8335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
8345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
8352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
8364cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes                   bool interruptShouldThrow, ThreadState why) {
8374d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  DCHECK(self != nullptr);
8384d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  DCHECK(obj != nullptr);
8394d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  LockWord lock_word = obj->GetLockWord(true);
84043c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers  while (lock_word.GetState() != LockWord::kFatLocked) {
84143c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers    switch (lock_word.GetState()) {
84243c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers      case LockWord::kHashCode:
84343c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers        // Fall-through.
84443c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers      case LockWord::kUnlocked:
845d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
846d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Failure.
84743c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers      case LockWord::kThinLocked: {
84843c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers        uint32_t thread_id = self->GetThreadId();
84943c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers        uint32_t owner_thread_id = lock_word.ThinLockOwner();
85043c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers        if (owner_thread_id != thread_id) {
85143c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers          ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
85243c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers          return;  // Failure.
85343c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers        } else {
85443c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers          // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so
85543c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers          // re-load.
85643c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers          Inflate(self, self, obj, 0);
85743c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers          lock_word = obj->GetLockWord(true);
85843c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers        }
85943c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers        break;
86043c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers      }
86143c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers      case LockWord::kFatLocked:  // Unreachable given the loop condition above. Fall-through.
86243c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers      default: {
86343c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers        LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
86443c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers        return;
865d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
866590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
8675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
868d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Monitor* mon = lock_word.FatLockMonitor();
869d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  mon->Wait(self, ms, ns, interruptShouldThrow, why);
8705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
8715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
87213c479e7e9b62115fe0409e5273c1e976a1c09f9Ian Rogersvoid Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
8734d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  DCHECK(self != nullptr);
8744d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  DCHECK(obj != nullptr);
8754d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  LockWord lock_word = obj->GetLockWord(true);
876d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
877ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
878ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // Fall-through.
879d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
8806d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
881d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;  // Failure.
882d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked: {
883d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t thread_id = self->GetThreadId();
884d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t owner_thread_id = lock_word.ThinLockOwner();
885d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (owner_thread_id != thread_id) {
886d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
887d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Failure.
888d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      } else {
889d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // We own the lock but there's no Monitor and therefore no waiters.
890d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Success.
891d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
892d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    }
893d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
894d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
895d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (notify_all) {
896d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mon->NotifyAll(self);
897d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      } else {
898d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mon->Notify(self);
899d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
900d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;  // Success.
9015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
902590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    default: {
903590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
904590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      return;
905590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
9065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
9075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
9085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
909d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersuint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
9104d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  DCHECK(obj != nullptr);
9114d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  LockWord lock_word = obj->GetLockWord(true);
912d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
913ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
914ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // Fall-through.
915d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
916d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return ThreadList::kInvalidThreadId;
917d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked:
918d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return lock_word.ThinLockOwner();
919d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
920d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
921d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return mon->GetOwnerThreadId();
9225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
923590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    default: {
924d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      LOG(FATAL) << "Unreachable";
9252c4257be8191c5eefde744e8965fcefc80a0a97dIan Rogers      UNREACHABLE();
926590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    }
9275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
9285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
9295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
9308e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesvoid Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
931d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers  // Determine the wait message and object we're waiting or blocked upon.
932d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers  mirror::Object* pretty_object = nullptr;
933d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers  const char* wait_message = nullptr;
934d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  uint32_t lock_owner = ThreadList::kInvalidThreadId;
935d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers  ThreadState state = thread->GetState();
936b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
937d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    wait_message = (state == kSleeping) ? "  - sleeping on " : "  - waiting on ";
938d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    Thread* self = Thread::Current();
939d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    MutexLock mu(self, *thread->GetWaitMutex());
940d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    Monitor* monitor = thread->GetWaitMonitor();
941d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    if (monitor != nullptr) {
9424cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi      pretty_object = monitor->GetObject();
9438e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    }
94434e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes  } else if (state == kBlocked) {
945d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    wait_message = "  - waiting to lock ";
946d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    pretty_object = thread->GetMonitorEnterObject();
947d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    if (pretty_object != nullptr) {
948d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers      lock_owner = pretty_object->GetLockOwnerThreadId();
9498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    }
9508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  }
9518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
952d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers  if (wait_message != nullptr) {
953d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    if (pretty_object == nullptr) {
954d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers      os << wait_message << "an unknown object";
955d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    } else {
9564d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier      if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
957d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers          Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
958d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers        // Getting the identity hashcode here would result in lock inflation and suspension of the
959d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers        // current thread, which isn't safe if this is the only runnable thread.
960d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers        os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
961d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers                                           reinterpret_cast<intptr_t>(pretty_object),
962d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers                                           PrettyTypeOf(pretty_object).c_str());
963d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers      } else {
964d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers        // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
9654936159997132d7706d9700c646f35ef0283df4bMathieu Chartier        // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread
9664936159997132d7706d9700c646f35ef0283df4bMathieu Chartier        // suspension and move pretty_object.
9674936159997132d7706d9700c646f35ef0283df4bMathieu Chartier        const std::string pretty_type(PrettyTypeOf(pretty_object));
968d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers        os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(),
9694936159997132d7706d9700c646f35ef0283df4bMathieu Chartier                                           pretty_type.c_str());
970d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers      }
971d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    }
972d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
973d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    if (lock_owner != ThreadList::kInvalidThreadId) {
974d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers      os << " held by thread " << lock_owner;
975d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    }
976d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers    os << "\n";
9778e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  }
9788e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes}
9798e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
9802dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
981f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
982f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  // definition of contended that includes a monitor a thread is trying to enter...
983dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  mirror::Object* result = thread->GetMonitorEnterObject();
9842cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (result == nullptr) {
985d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    // ...but also a monitor that the thread is waiting on.
986dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
987dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers    Monitor* monitor = thread->GetWaitMonitor();
9882cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier    if (monitor != nullptr) {
989d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      result = monitor->GetObject();
990f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    }
991f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
992d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  return result;
993f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
994f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
9952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
996760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe                         void* callback_context, bool abort_on_failure) {
997e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  ArtMethod* m = stack_visitor->GetMethod();
9982cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  CHECK(m != nullptr);
99908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
100008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // Native methods are an easy special case.
100108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
100208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  if (m->IsNative()) {
100308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    if (m->IsSynchronized()) {
1004e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      mirror::Object* jni_this =
1005e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0);
10064993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      callback(jni_this, callback_context);
100708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    }
100808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    return;
100908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
101008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
101161f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao  // Proxy methods should not be synchronized.
101261f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao  if (m->IsProxyMethod()) {
101361f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao    CHECK(!m->IsSynchronized());
101461f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao    return;
101561f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao  }
101661f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao
101708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // Is there any reason to believe there's any synchronization in this method?
1018bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
10192cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  CHECK(code_item != nullptr) << PrettyMethod(m);
102008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  if (code_item->tries_size_ == 0) {
10217934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return;  // No "tries" implies no synchronization, so no held locks to report.
102208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
102308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
1024760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe  // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot
1025760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe  // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an
1026760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe  // inconsistent stack anyways.
1027760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe  uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure);
1028760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe  if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) {
1029760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe    LOG(ERROR) << "Could not find dex_pc for " << PrettyMethod(m);
1030760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe    return;
1031760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe  }
1032760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe
103380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
103480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  // the locks held in this stack frame.
103580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  std::vector<uint32_t> monitor_enter_dex_pcs;
1036760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe  verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs);
1037e6a8eec3a5db28de7d5db6d78e38033b80740e49Mathieu Chartier  for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) {
103880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    // The verifier works in terms of the dex pcs of the monitor-enter instructions.
103980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    // We want the registers used by those instructions (so we can read the values out of them).
1040277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    uint16_t monitor_enter_instruction = code_item->insns_[monitor_dex_pc];
104108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
104280537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    // Quick sanity check.
104380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) {
1044277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      LOG(FATAL) << "expected monitor-enter @" << monitor_dex_pc << "; was "
104580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes                 << reinterpret_cast<void*>(monitor_enter_instruction);
104608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    }
104780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes
104880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff);
104915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray    uint32_t value;
105015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray    bool success = stack_visitor->GetVReg(m, monitor_register, kReferenceVReg, &value);
105115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray    CHECK(success) << "Failed to read v" << monitor_register << " of kind "
105215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray                   << kReferenceVReg << " in method " << PrettyMethod(m);
105315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray    mirror::Object* o = reinterpret_cast<mirror::Object*>(value);
10544993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    callback(o, callback_context);
105508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
105608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes}
105708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
1058d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::IsValidLockWord(LockWord lock_word) {
1059d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
1060d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
1061d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Nothing to check.
1062d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return true;
1063d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked:
1064d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Basic sanity check of owner.
1065d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1066d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
1067d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Check the  monitor appears in the monitor list.
1068d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
1069d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      MonitorList* list = Runtime::Current()->GetMonitorList();
1070d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1071d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      for (Monitor* list_mon : list->list_) {
1072d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (mon == list_mon) {
1073d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          return true;  // Found our monitor.
1074d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
10757dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers      }
1076d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return false;  // Fail - unowned monitor in an object.
10777dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers    }
1078ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
1079ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      return true;
1080d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    default:
1081d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      LOG(FATAL) << "Unreachable";
10822c4257be8191c5eefde744e8965fcefc80a0a97dIan Rogers      UNREACHABLE();
10837dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers  }
10847dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers}
10857dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers
1086ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartierbool Monitor::IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1087ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  MutexLock mu(Thread::Current(), monitor_lock_);
1088ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier  return owner_ != nullptr;
1089ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier}
1090ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier
1091e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartiervoid Monitor::TranslateLocation(ArtMethod* method, uint32_t dex_pc,
1092d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                const char** source_file, uint32_t* line_number) const {
109333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  // If method is null, location is unknown
10942cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (method == nullptr) {
1095d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    *source_file = "";
1096d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    *line_number = 0;
109733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao    return;
109833dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  }
1099bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  *source_file = method->GetDeclaringClassSourceFile();
11002cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (*source_file == nullptr) {
1101d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    *source_file = "";
1102d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  }
1103bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  *line_number = method->GetLineNumFromDexPC(dex_pc);
1104d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers}
1105d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
1106d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersuint32_t Monitor::GetOwnerThreadId() {
1107d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(Thread::Current(), monitor_lock_);
1108d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Thread* owner = owner_;
11092cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier  if (owner != nullptr) {
1110d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    return owner->GetThreadId();
1111d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  } else {
1112d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    return ThreadList::kInvalidThreadId;
111312c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes  }
111433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao}
111533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao
1116c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu ChartierMonitorList::MonitorList()
1117440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier    : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
1118c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier      monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
1119c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
1120c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
1121c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesMonitorList::~MonitorList() {
112274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  Thread* self = Thread::Current();
112374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  MutexLock mu(self, monitor_list_lock_);
112474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // Release all monitors to the pool.
112574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // TODO: Is it an invariant that *all* open monitors are in the list? Then we could
112674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  // clear faster in the pool.
112774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  MonitorPool::ReleaseMonitors(self, &list_);
1128c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
1129c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
1130c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::DisallowNewMonitors() {
113150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), monitor_list_lock_);
1132c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  allow_new_monitors_ = false;
1133c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier}
1134c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier
1135c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::AllowNewMonitors() {
1136c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  Thread* self = Thread::Current();
1137c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  MutexLock mu(self, monitor_list_lock_);
1138c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  allow_new_monitors_ = true;
1139c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  monitor_add_condition_.Broadcast(self);
1140c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier}
1141c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier
11422cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchivoid MonitorList::EnsureNewMonitorsDisallowed() {
11432cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi  // Lock and unlock once to ensure that no threads are still in the
11442cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi  // middle of adding new monitors.
11452cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi  MutexLock mu(Thread::Current(), monitor_list_lock_);
11462cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi  CHECK(!allow_new_monitors_);
11472cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi}
11482cd334ae2d4287216523882f0d298cf3901b7ab1Hiroshi Yamauchi
11490b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchivoid MonitorList::BroadcastForNewMonitors() {
11500b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi  CHECK(kUseReadBarrier);
11510b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi  Thread* self = Thread::Current();
11520b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi  MutexLock mu(self, monitor_list_lock_);
11530b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi  monitor_add_condition_.Broadcast(self);
11540b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi}
11550b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi
1156c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::Add(Monitor* m) {
1157c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  Thread* self = Thread::Current();
1158c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  MutexLock mu(self, monitor_list_lock_);
11590b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi  while (UNLIKELY((!kUseReadBarrier && !allow_new_monitors_) ||
11600b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi                  (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) {
1161c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier    monitor_add_condition_.WaitHoldingLocks(self);
1162c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  }
1163c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  list_.push_front(m);
1164c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
1165c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
116697509954404d031594b2ecbda607314d169d512eMathieu Chartiervoid MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) {
116774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  Thread* self = Thread::Current();
116874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe  MutexLock mu(self, monitor_list_lock_);
116902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier  for (auto it = list_.begin(); it != list_.end(); ) {
1170c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes    Monitor* m = *it;
11714cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi    // Disable the read barrier in GetObject() as this is called by GC.
11724cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi    mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
1173590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // The object of a monitor can be null if we have deflated it.
117497509954404d031594b2ecbda607314d169d512eMathieu Chartier    mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr;
11756aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier    if (new_obj == nullptr) {
11766aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier      VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
11774cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi                    << obj;
117874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe      MonitorPool::ReleaseMonitor(self, m);
1179c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes      it = list_.erase(it);
1180c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes    } else {
11816aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier      m->SetObject(new_obj);
1182c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes      ++it;
1183c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes    }
1184c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  }
1185c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
1186c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
118797509954404d031594b2ecbda607314d169d512eMathieu Chartierclass MonitorDeflateVisitor : public IsMarkedVisitor {
118897509954404d031594b2ecbda607314d169d512eMathieu Chartier public:
118997509954404d031594b2ecbda607314d169d512eMathieu Chartier  MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {}
119097509954404d031594b2ecbda607314d169d512eMathieu Chartier
119197509954404d031594b2ecbda607314d169d512eMathieu Chartier  virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE
119297509954404d031594b2ecbda607314d169d512eMathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
119397509954404d031594b2ecbda607314d169d512eMathieu Chartier    if (Monitor::Deflate(self_, object)) {
119497509954404d031594b2ecbda607314d169d512eMathieu Chartier      DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
119597509954404d031594b2ecbda607314d169d512eMathieu Chartier      ++deflate_count_;
119697509954404d031594b2ecbda607314d169d512eMathieu Chartier      // If we deflated, return null so that the monitor gets removed from the array.
119797509954404d031594b2ecbda607314d169d512eMathieu Chartier      return nullptr;
119897509954404d031594b2ecbda607314d169d512eMathieu Chartier    }
119997509954404d031594b2ecbda607314d169d512eMathieu Chartier    return object;  // Monitor was not deflated.
1200440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier  }
120197509954404d031594b2ecbda607314d169d512eMathieu Chartier
120297509954404d031594b2ecbda607314d169d512eMathieu Chartier  Thread* const self_;
120397509954404d031594b2ecbda607314d169d512eMathieu Chartier  size_t deflate_count_;
120497509954404d031594b2ecbda607314d169d512eMathieu Chartier};
1205440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier
120648ab687d1f864fec93c2682de6fdc44ab784e2f8Mathieu Chartiersize_t MonitorList::DeflateMonitors() {
120797509954404d031594b2ecbda607314d169d512eMathieu Chartier  MonitorDeflateVisitor visitor;
120897509954404d031594b2ecbda607314d169d512eMathieu Chartier  Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_);
120997509954404d031594b2ecbda607314d169d512eMathieu Chartier  SweepMonitorList(&visitor);
121097509954404d031594b2ecbda607314d169d512eMathieu Chartier  return visitor.deflate_count_;
1211440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier}
1212440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier
12132cebb24bfc3247d3e9be138a3350106737455918Mathieu ChartierMonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) {
12144d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  DCHECK(obj != nullptr);
12154d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier  LockWord lock_word = obj->GetLockWord(true);
1216d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
1217d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
1218ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier      // Fall-through.
1219590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    case LockWord::kForwardingAddress:
1220590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      // Fall-through.
1221ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier    case LockWord::kHashCode:
1222d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
1223d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked:
1224d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
1225d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      entry_count_ = 1 + lock_word.ThinLockCount();
1226d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Thin locks have no waiters.
1227d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
1228d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
1229d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
1230d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      owner_ = mon->owner_;
1231d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      entry_count_ = 1 + mon->lock_count_;
12322cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) {
1233d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        waiters_.push_back(waiter);
1234d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
1235d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
1236f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    }
1237f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
1238f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
1239f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
12405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}  // namespace art
1241