monitor.cc revision d9c4fc94fa618617f94e1de9af5f034549100753
15f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
25f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Copyright (C) 2008 The Android Open Source Project
35f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
45f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
55f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * you may not use this file except in compliance with the License.
65f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * You may obtain a copy of the License at
75f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
85f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
95f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Unless required by applicable law or agreed to in writing, software
115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * See the License for the specific language governing permissions and
145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * limitations under the License.
155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#include "monitor.h"
185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include <vector>
2008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
2176b6167407c2b6f5d40ad895b2793a6b037f54b2Elliott Hughes#include "base/mutex.h"
221aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h"
2333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao#include "class_linker.h"
244f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
2508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "dex_instruction.h"
26d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers#include "lock_word-inl.h"
27ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
284f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "mirror/class-inl.h"
2905f3057d6a4d23d712092ccd36a531590bff323bIan Rogers#include "mirror/object-inl.h"
302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
316d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h"
3200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "thread.h"
348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "thread_list.h"
3508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "verifier/method_verifier.h"
36044288fb8f69e154862763a6b3bfea2bac1b397eElliott Hughes#include "well_known_classes.h"
375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesnamespace art {
395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
41d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Every Object has a monitor associated with it, but not every Object is actually locked.  Even
42d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * the ones that are locked do not need a full-fledged monitor until a) there is actual contention
43d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * or b) wait() is called on the Object.
445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
45d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s
46d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * "Thin locks: featherweight synchronization for Java" (ACM 1998).  Things are even easier for us,
47d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * though, because we have a full 32 bits to work with.
485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
49d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * The two states of an Object's lock are referred to as "thin" and "fat".  A lock may transition
50d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once
51d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * a lock has been inflated it remains in the "fat" state indefinitely.
525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
53d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * The lock value itself is stored in mirror::Object::monitor_ and the representation is described
54d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * in the LockWord value type.
5554e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes *
565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Monitors provide:
575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - mutually exclusive access to resources
585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - a way for multiple threads to wait for notification
595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * In effect, they fill the role of both mutexes and condition variables.
615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
62d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Only one thread can own the monitor at any time.  There may be several threads waiting on it
63d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * (the wait call unlocks it).  One or more waiting threads may be getting interrupted or notified
64d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * at any given time.
655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
6654e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes
67fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesbool (*Monitor::is_sensitive_thread_hook_)() = NULL;
68fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesuint32_t Monitor::lock_profiling_threshold_ = 0;
6932d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes
70fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesbool Monitor::IsSensitiveThread() {
71fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  if (is_sensitive_thread_hook_ != NULL) {
72fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes    return (*is_sensitive_thread_hook_)();
73fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  }
74fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  return false;
75fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes}
76fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes
774dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughesvoid Monitor::Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()) {
78fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  lock_profiling_threshold_ = lock_profiling_threshold;
79fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  is_sensitive_thread_hook_ = is_sensitive_thread_hook;
8032d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes}
8132d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes
822dd0e2cea360bc9206eb88ecc40d259e796c239dIan RogersMonitor::Monitor(Thread* owner, mirror::Object* obj)
8300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    : monitor_lock_("a monitor lock", kMonitorLock),
84d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      monitor_contenders_("monitor contenders", monitor_lock_),
8500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      owner_(owner),
865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      lock_count_(0),
875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      obj_(obj),
885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      wait_set_(NULL),
8933dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao      locking_method_(NULL),
900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      locking_dex_pc_(0) {
91d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
92d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // with the owner unlocking the thin-lock.
93d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  CHECK(owner == Thread::Current() || owner->IsSuspended());
94d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers}
95d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
96d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::Install(Thread* self) {
97d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);  // Uncontended mutex acquisition as monitor isn't yet public.
98d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  CHECK(owner_ == self || owner_->IsSuspended());
9900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Propagate the lock state.
100d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord thin(obj_->GetLockWord());
101d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (thin.GetState() != LockWord::kThinLocked) {
102d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    // The owner_ is suspended but another thread beat us to install a monitor.
103d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    CHECK_EQ(thin.GetState(), LockWord::kFatLocked);
104d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    return false;
105d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  }
106d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  CHECK_EQ(owner_->GetThreadId(), thin.ThinLockOwner());
107d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  lock_count_ = thin.ThinLockCount();
108d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord fat(this);
109d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // Publish the updated lock word, which may race with other threads.
110d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  bool success = obj_->CasLockWord(thin, fat);
11100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Lock profiling.
112d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (success && lock_profiling_threshold_ != 0) {
113d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_);
11400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
115d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  return success;
1165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
1175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1185f79133a435ebcb20000370d56046fe01201dd80Elliott HughesMonitor::~Monitor() {
119d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  CHECK(obj_ != NULL);
120d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  CHECK_EQ(obj_->GetLockWord().GetState(), LockWord::kFatLocked);
1215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
1225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
1245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Links a thread into a monitor's wait set.  The monitor lock must be
1255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * held by the caller of this routine.
1265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
1275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::AppendToWaitSet(Thread* thread) {
1285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(owner_ == Thread::Current());
1295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(thread != NULL);
130dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes  DCHECK(thread->wait_next_ == NULL) << thread->wait_next_;
1315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (wait_set_ == NULL) {
1325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    wait_set_ = thread;
1335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
1345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // push_back.
1375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  Thread* t = wait_set_;
1385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  while (t->wait_next_ != NULL) {
1395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    t = t->wait_next_;
1405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  t->wait_next_ = thread;
1425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
1435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
1455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Unlinks a thread from a monitor's wait set.  The monitor lock must
1465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * be held by the caller of this routine.
1475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
1485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::RemoveFromWaitSet(Thread *thread) {
1495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(owner_ == Thread::Current());
1505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(thread != NULL);
1515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (wait_set_ == NULL) {
1525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
1535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (wait_set_ == thread) {
1555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    wait_set_ = thread->wait_next_;
1565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    thread->wait_next_ = NULL;
1575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
1585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  Thread* t = wait_set_;
1615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  while (t->wait_next_ != NULL) {
1625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (t->wait_next_ == thread) {
1635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      t->wait_next_ = thread->wait_next_;
1645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      thread->wait_next_ = NULL;
1655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      return;
1665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
1675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    t = t->wait_next_;
1685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
1695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
1705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
1716aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartiervoid Monitor::SetObject(mirror::Object* object) {
1726aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier  obj_ = object;
1736aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier}
1746aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier
1755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Lock(Thread* self) {
176d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
177d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  while (true) {
178d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    if (owner_ == NULL) {  // Unowned.
179d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      owner_ = self;
180d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      CHECK_EQ(lock_count_, 0);
181d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // When debugging, save the current monitor holder for future
182d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // acquisition failures to use in sampled logging.
183d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (lock_profiling_threshold_ != 0) {
184d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
185fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes      }
186d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;
187d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    } else if (owner_ == self) {  // Recursive.
188d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      lock_count_++;
189d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;
190fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes    }
191d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    // Contended.
192d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    const bool log_contention = (lock_profiling_threshold_ != 0);
193d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    uint64_t wait_start_ms = log_contention ? 0 : MilliTime();
194d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    const mirror::ArtMethod* owners_method = locking_method_;
195d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    uint32_t owners_dex_pc = locking_dex_pc_;
196d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);  // Let go of locks in order.
197d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    {
198d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      ScopedThreadStateChange tsc(self, kBlocked);  // Change to blocked and give up mutator_lock_.
199d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      MutexLock mu2(self, monitor_lock_);  // Reacquire monitor_lock_ without mutator_lock_ for Wait.
200d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (owner_ != NULL) {  // Did the owner_ give the lock up?
201d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        monitor_contenders_.Wait(self);  // Still contended so wait.
202d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // Woken from contention.
203d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (log_contention) {
204d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint64_t wait_ms = MilliTime() - wait_start_ms;
205d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint32_t sample_percent;
206d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (wait_ms >= lock_profiling_threshold_) {
207d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            sample_percent = 100;
208d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          } else {
209d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            sample_percent = 100 * wait_ms / lock_profiling_threshold_;
210d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          }
211d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
212d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            const char* owners_filename;
213d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            uint32_t owners_line_number;
214d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
215d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            LogContentionEvent(self, wait_ms, sample_percent, owners_filename, owners_line_number);
216d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          }
217d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
218fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes      }
2195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
220d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Lock(self);  // Reacquire locks in order.
221fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes  }
2225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
2235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
2246d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
2256d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                              __attribute__((format(printf, 1, 2)));
2266d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers
22700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
228b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2296d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  va_list args;
2306d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  va_start(args, fmt);
23162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
23262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThrowLocation throw_location = self->GetCurrentLocationForThrow();
23362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  self->ThrowNewExceptionV(throw_location, "Ljava/lang/IllegalMonitorStateException;", fmt, args);
234d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
23564277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom    std::ostringstream ss;
23662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    self->Dump(ss);
237d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    LOG(Runtime::Current()->IsStarted() ? INFO : ERROR)
238d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        << self->GetException(NULL)->Dump() << "\n" << ss.str();
23964277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom  }
2406d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  va_end(args);
2416d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers}
2426d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers
243d423741f91526cada9d081c64ee295ec9863032dElliott Hughesstatic std::string ThreadToString(Thread* thread) {
244d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  if (thread == NULL) {
245d423741f91526cada9d081c64ee295ec9863032dElliott Hughes    return "NULL";
246d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  }
247d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  std::ostringstream oss;
248d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  // TODO: alternatively, we could just return the thread's name.
249d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  oss << *thread;
250d423741f91526cada9d081c64ee295ec9863032dElliott Hughes  return oss.str();
251d423741f91526cada9d081c64ee295ec9863032dElliott Hughes}
252d423741f91526cada9d081c64ee295ec9863032dElliott Hughes
2532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::FailedUnlock(mirror::Object* o, Thread* expected_owner, Thread* found_owner,
254ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                           Monitor* monitor) {
255ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  Thread* current_owner = NULL;
256ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  std::string current_owner_string;
257ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  std::string expected_owner_string;
258ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  std::string found_owner_string;
259ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  {
260ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // TODO: isn't this too late to prevent threads from disappearing?
261ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // Acquire thread list lock so threads won't disappear from under us.
26250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
263ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // Re-read owner now that we hold lock.
264d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    current_owner = (monitor != NULL) ? monitor->GetOwner() : NULL;
265ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    // Get short descriptions of the threads involved.
266ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    current_owner_string = ThreadToString(current_owner);
267ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    expected_owner_string = ThreadToString(expected_owner);
268ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes    found_owner_string = ThreadToString(found_owner);
269ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes  }
2706d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  if (current_owner == NULL) {
2716d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    if (found_owner == NULL) {
2726d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
2736d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                         " on thread '%s'",
274ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         PrettyTypeOf(o).c_str(),
275ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         expected_owner_string.c_str());
2766d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    } else {
2776d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      // Race: the original read found an owner but now there is none
2786d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
2796d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                         " (where now the monitor appears unowned) on thread '%s'",
280ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         found_owner_string.c_str(),
281ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         PrettyTypeOf(o).c_str(),
282ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         expected_owner_string.c_str());
2836d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    }
2846d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  } else {
2856d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    if (found_owner == NULL) {
2866d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      // Race: originally there was no owner, there is now
2876d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
2886d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                         " (originally believed to be unowned) on thread '%s'",
289ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         current_owner_string.c_str(),
290ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         PrettyTypeOf(o).c_str(),
291ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                         expected_owner_string.c_str());
2926d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    } else {
2936d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      if (found_owner != current_owner) {
2946d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers        // Race: originally found and current owner have changed
2956d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers        ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
2966d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                           " owned by '%s') on object of type '%s' on thread '%s'",
297ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           found_owner_string.c_str(),
298ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           current_owner_string.c_str(),
299ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           PrettyTypeOf(o).c_str(),
300ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           expected_owner_string.c_str());
3016d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      } else {
3026d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers        ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
3036d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers                                           " on thread '%s",
304ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           current_owner_string.c_str(),
305ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           PrettyTypeOf(o).c_str(),
306ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes                                           expected_owner_string.c_str());
3076d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      }
3086d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    }
3096d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  }
3105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
3115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
312d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::Unlock(Thread* self) {
3135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
314d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
3156d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  Thread* owner = owner_;
3166d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers  if (owner == self) {
3175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // We own the monitor, so nobody else can be in here.
3185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (lock_count_ == 0) {
3195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      owner_ = NULL;
32033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao      locking_method_ = NULL;
3210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      locking_dex_pc_ = 0;
322d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Wake a contender.
323d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      monitor_contenders_.Signal(self);
3245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    } else {
3255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      --lock_count_;
3265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
3275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  } else {
3285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // We don't own this, so we're not allowed to unlock it.
3295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // The JNI spec says that we should throw IllegalMonitorStateException
3305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // in this case.
3316d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    FailedUnlock(obj_, self, owner, this);
3325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return false;
3335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
3345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  return true;
3355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
3365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
3375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
3385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Wait on a monitor until timeout, interrupt, or notification.  Used for
3395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join().
3405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
3415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * If another thread calls Thread.interrupt(), we throw InterruptedException
3425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * and return immediately if one of the following are true:
3435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - blocked in wait(), wait(long), or wait(long, int) methods of Object
3445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - blocked in join(), join(long), or join(long, int) methods of Thread
3455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *  - blocked in sleep(long), or sleep(long, int) methods of Thread
3465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Otherwise, we set the "interrupted" flag.
3475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
3485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Checks to make sure that "ns" is in the range 0-999999
3495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * (i.e. fractions of a millisecond) and throws the appropriate
3505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * exception if it isn't.
3515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
3525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The spec allows "spurious wakeups", and recommends that all code using
3535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() do so in a loop.  This appears to derive from concerns
3545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * about pthread_cond_wait() on multiprocessor systems.  Some commentary
3555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * on the web casts doubt on whether these can/should occur.
3565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *
3575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Since we're allowed to wake up "early", we clamp extremely long durations
3585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * to return at the end of the 32-bit time epoch.
3595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
3604cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughesvoid Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
3614cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes                   bool interruptShouldThrow, ThreadState why) {
3625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
3634cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes  DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
3645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
365d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  monitor_lock_.Lock(self);
366d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
3675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Make sure that we hold the lock.
3685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (owner_ != self) {
3696d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
370d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);
3715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
3725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
3734cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes
374df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  // We need to turn a zero-length timed wait into a regular wait because
375df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
376df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
377df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes    why = kWaiting;
378df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes  }
379df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes
3805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Enforce the timeout range.
3815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (ms < 0 || ns < 0 || ns > 999999) {
38262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation throw_location = self->GetCurrentLocationForThrow();
38362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    self->ThrowNewExceptionF(throw_location, "Ljava/lang/IllegalArgumentException;",
38462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                             "timeout arguments out of range: ms=%lld ns=%d", ms, ns);
385d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);
3865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
3875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
3885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
3895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  /*
3905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * Add ourselves to the set of threads waiting on this monitor, and
3915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * release our hold.  We need to let it go even if we're a few levels
3925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * deep in a recursive lock, and we need to restore that later.
3935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   *
3945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * We append to the wait set ahead of clearing the count and owner
3955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * fields so the subroutine can check that the calling thread owns
3965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * the monitor.  Aside from that, the order of member updates is
3975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * not order sensitive as we hold the pthread mutex.
3985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   */
3995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  AppendToWaitSet(self);
4000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  int prev_lock_count = lock_count_;
4015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  lock_count_ = 0;
4025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  owner_ = NULL;
403ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  const mirror::ArtMethod* saved_method = locking_method_;
40433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  locking_method_ = NULL;
4050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  uintptr_t saved_dex_pc = locking_dex_pc_;
4060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  locking_dex_pc_ = 0;
4075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  /*
4094cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes   * Update thread state. If the GC wakes up, it'll ignore us, knowing
4105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * that we won't touch any references in this state, and we'll check
4115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * our suspend mode before we transition out.
4125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   */
4134cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes  self->TransitionFromRunnableToSuspended(why);
4145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
415b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  bool was_interrupted = false;
41600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
41700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
41850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(self, *self->wait_mutex_);
4195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
42000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
42100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
42200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // up.
42300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    DCHECK(self->wait_monitor_ == NULL);
42400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->wait_monitor_ = this;
4255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
42600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Release the monitor lock.
427d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_contenders_.Signal(self);
428d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    monitor_lock_.Unlock(self);
42900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
430b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // Handle the case where the thread was interrupted before we called wait().
43100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (self->interrupted_) {
432b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      was_interrupted = true;
43300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    } else {
43400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      // Wait for a notification or a timeout to occur.
4354cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes      if (why == kWaiting) {
436c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers        self->wait_cond_->Wait(self);
43700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      } else {
4384cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes        DCHECK(why == kTimedWaiting || why == kSleeping) << why;
439c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers        self->wait_cond_->TimedWait(self, ms, ns);
44000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
44100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      if (self->interrupted_) {
442b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes        was_interrupted = true;
44300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
44400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      self->interrupted_ = false;
44500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
4465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
4475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
44800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Set self->status back to kRunnable, and self-suspend if needed.
44900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  self->TransitionFromSuspendedToRunnable();
4505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
451b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  {
452b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // We reset the thread's wait_monitor_ field after transitioning back to runnable so
453b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
454b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
455b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    // are waiting on "null".)
456b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    MutexLock mu(self, *self->wait_mutex_);
457b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    DCHECK(self->wait_monitor_ != NULL);
458b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    self->wait_monitor_ = NULL;
459b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  }
460b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes
461d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  // Re-acquire the monitor and lock.
46200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Lock(self);
463d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  monitor_lock_.Lock(self);
46481d425b0b232962441616f8b14f73620bffef5e5Ian Rogers  self->wait_mutex_->AssertNotHeld(self);
4655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  /*
4675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * We remove our thread from wait set after restoring the count
4685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * and owner fields so the subroutine can check that the calling
4695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * thread owns the monitor. Aside from that, the order of member
4705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   * updates is not order sensitive as we hold the pthread mutex.
4715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes   */
4725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  owner_ = self;
4730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  lock_count_ = prev_lock_count;
4740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  locking_method_ = saved_method;
4750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  locking_dex_pc_ = saved_dex_pc;
4765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  RemoveFromWaitSet(self);
4775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
478b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  if (was_interrupted) {
4795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    /*
4805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * We were interrupted while waiting, or somebody interrupted an
4815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * un-interruptible thread earlier and we're bailing out immediately.
4825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     *
4835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * The doc sayeth: "The interrupted status of the current thread is
4845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     * cleared when this exception is thrown."
4855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes     */
48600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
48750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, *self->wait_mutex_);
48800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      self->interrupted_ = false;
48900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
4905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (interruptShouldThrow) {
49162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      ThrowLocation throw_location = self->GetCurrentLocationForThrow();
49262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      self->ThrowNewException(throw_location, "Ljava/lang/InterruptedException;", NULL);
4935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
4945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
495d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  monitor_lock_.Unlock(self);
4965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
4975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
4985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Notify(Thread* self) {
4995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
500d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
5015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Make sure that we hold the lock.
5025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (owner_ != self) {
5036d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
5045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
5055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Signal the first waiting thread in the wait set.
5075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  while (wait_set_ != NULL) {
5085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    Thread* thread = wait_set_;
5095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    wait_set_ = thread->wait_next_;
5105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    thread->wait_next_ = NULL;
5115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    // Check to see if the thread is still waiting.
51350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(self, *thread->wait_mutex_);
5145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    if (thread->wait_monitor_ != NULL) {
515c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers      thread->wait_cond_->Signal(self);
5165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      return;
5175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
5185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
5205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::NotifyAll(Thread* self) {
5225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
523d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(self, monitor_lock_);
5245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Make sure that we hold the lock.
5255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  if (owner_ != self) {
5266d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers    ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
5275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    return;
5285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Signal all threads in the wait set.
5305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  while (wait_set_ != NULL) {
5315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    Thread* thread = wait_set_;
5325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    wait_set_ = thread->wait_next_;
5335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    thread->wait_next_ = NULL;
5345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    thread->Notify();
5355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
5365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
5375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
539d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Changes the shape of a monitor from thin to fat, preserving the internal lock state. The calling
540d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * thread must own the lock or the owner must be suspended. There's a race with other threads
541d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * inflating the lock and so the caller should read the monitor following the call.
5425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
543d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersvoid Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj) {
5445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
545d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(owner != NULL);
5465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(obj != NULL);
5475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  // Allocate and acquire a new monitor.
549d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  UniquePtr<Monitor> m(new Monitor(owner, obj));
550d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (m->Install(self)) {
551d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    VLOG(monitor) << "monitor: thread " << owner->GetThreadId()
552d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                    << " created monitor " << m.get() << " for object " << obj;
553d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    Runtime::Current()->GetMonitorList()->Add(m.release());
554d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  }
555d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  CHECK_EQ(obj->GetLockWord().GetState(), LockWord::kFatLocked);
5565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
5575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
5582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::MonitorEnter(Thread* self, mirror::Object* obj) {
5594681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes  DCHECK(self != NULL);
5604681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes  DCHECK(obj != NULL);
561d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  uint32_t thread_id = self->GetThreadId();
562d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  size_t contention_count = 0;
563d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
564d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  while (true) {
565d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    LockWord lock_word = obj->GetLockWord();
566d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    switch (lock_word.GetState()) {
567d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      case LockWord::kUnlocked: {
568d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0));
569d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (obj->CasLockWord(lock_word, thin_locked)) {
570d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          return;  // Success!
571d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
572d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        continue;  // Go again.
5735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      }
574d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      case LockWord::kThinLocked: {
575d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        uint32_t owner_thread_id = lock_word.ThinLockOwner();
576d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (owner_thread_id == thread_id) {
577d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          // We own the lock, increase the recursion count.
578d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint32_t new_count = lock_word.ThinLockCount() + 1;
579d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
580d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count));
581d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            obj->SetLockWord(thin_locked);
582d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            return;  // Success!
583d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          } else {
584d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            // We'd overflow the recursion count, so inflate the monitor.
585d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            Inflate(self, self, obj);
586d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          }
587d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        } else {
588d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          // Contention.
589d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          contention_count++;
590d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          if (contention_count <= Runtime::Current()->GetMaxSpinsBeforeThinkLockInflation()) {
591d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            NanoSleep(1000);  // Sleep for 1us and re-attempt.
5925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes          } else {
593d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            contention_count = 0;
594d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
595d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            ScopedThreadStateChange tsc(self, kBlocked);
596d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            bool timed_out;
597d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            ThreadList* thread_list = Runtime::Current()->GetThreadList();
598d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            if (lock_word == obj->GetLockWord()) {  // If lock word hasn't changed.
599d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers              Thread* owner = thread_list->SuspendThreadByThreadId(lock_word.ThinLockOwner(), false,
600d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                                   &timed_out);
601d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers              if (owner != NULL) {
602d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                // We succeeded in suspending the thread, check the lock's status didn't change.
603d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                lock_word = obj->GetLockWord();
604d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                if (lock_word.GetState() == LockWord::kThinLocked &&
605d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                    lock_word.ThinLockOwner() == owner_thread_id) {
606d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                  // Go ahead and inflate the lock.
607d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                  Inflate(self, owner, obj);
608d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                }
609d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                thread_list->Resume(owner, false);
6105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes              }
6115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes            }
6125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes          }
6135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes        }
614d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        continue;  // Start from the beginning.
615d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
616d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      case LockWord::kFatLocked: {
617d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Monitor* mon = lock_word.FatLockMonitor();
618d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mon->Lock(self);
619d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Success!
6205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      }
6215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
6225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
6235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
6245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
6252dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersbool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
6265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(self != NULL);
6275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  DCHECK(obj != NULL);
6285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
629d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
630d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
631d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
632d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      FailedUnlock(obj, self, NULL, NULL);
633d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return false;  // Failure.
634d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked: {
635d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t thread_id = self->GetThreadId();
636d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t owner_thread_id = lock_word.ThinLockOwner();
637d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (owner_thread_id != thread_id) {
638d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // TODO: there's a race here with the owner dying while we unlock.
639d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Thread* owner =
640d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers            Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
641d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        FailedUnlock(obj, self, owner, NULL);
642d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return false;  // Failure.
6435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      } else {
644d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // We own the lock, decrease the recursion count.
645d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (lock_word.ThinLockCount() != 0) {
646d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          uint32_t new_count = lock_word.ThinLockCount() - 1;
647d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count));
648d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          obj->SetLockWord(thin_locked);
649d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        } else {
650d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          obj->SetLockWord(LockWord());
651d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
652d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return true;  // Success!
6535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes      }
6545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
655d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
656d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
657d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return mon->Unlock(self);
6585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
659d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    default:
660d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      LOG(FATAL) << "Unreachable";
661d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return false;
6625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
6635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
6645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
6655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/*
6665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait().  Also called for class init.
6675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */
6682dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
6694cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes                   bool interruptShouldThrow, ThreadState why) {
670d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(self != NULL);
671d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(obj != NULL);
6725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
673d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
674d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
675d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
6766d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
677d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;  // Failure.
678d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked: {
679d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t thread_id = self->GetThreadId();
680d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t owner_thread_id = lock_word.ThinLockOwner();
681d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (owner_thread_id != thread_id) {
682d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
683d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Failure.
684d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      } else {
685d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // We own the lock, inflate to enqueue ourself on the Monitor.
686d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Inflate(self, self, obj);
687d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        lock_word = obj->GetLockWord();
688d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
689d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
6905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
691d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked:
692d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;  // Already set for a wait.
6935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
694d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Monitor* mon = lock_word.FatLockMonitor();
695d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  mon->Wait(self, ms, ns, interruptShouldThrow, why);
6965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
6975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
698d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersvoid Monitor::InflateAndNotify(Thread* self, mirror::Object* obj, bool notify_all) {
699d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(self != NULL);
700d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(obj != NULL);
7015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
702d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
703d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
704d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
7056d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers      ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
706d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;  // Failure.
707d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked: {
708d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t thread_id = self->GetThreadId();
709d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      uint32_t owner_thread_id = lock_word.ThinLockOwner();
710d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (owner_thread_id != thread_id) {
711d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
712d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Failure.
713d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      } else {
714d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        // We own the lock but there's no Monitor and therefore no waiters.
715d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        return;  // Success.
716d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
717d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    }
718d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
719d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
720d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      if (notify_all) {
721d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mon->NotifyAll(self);
722d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      } else {
723d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mon->Notify(self);
724d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
725d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return;  // Success.
7265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
7275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
7285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
7295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
730d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersuint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
731d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(obj != NULL);
7325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
733d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
734d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
735d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
736d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return ThreadList::kInvalidThreadId;
737d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked:
738d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return lock_word.ThinLockOwner();
739d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
740d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
741d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return mon->GetOwnerThreadId();
7425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes    }
743d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    default:
744d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      LOG(FATAL) << "Unreachable";
745d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return ThreadList::kInvalidThreadId;
7465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes  }
7475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}
7485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes
7498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesvoid Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
750f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ThreadState state = thread->GetState();
7518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
752d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  int32_t object_identity_hashcode = 0;
753d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  uint32_t lock_owner = ThreadList::kInvalidThreadId;
754d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  std::string pretty_type;
755b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes  if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
756b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    if (state == kSleeping) {
757b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      os << "  - sleeping on ";
758b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    } else {
759b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      os << "  - waiting on ";
760b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes    }
76100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
762f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes      Thread* self = Thread::Current();
763f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes      MutexLock mu(self, *thread->wait_mutex_);
764b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      Monitor* monitor = thread->wait_monitor_;
765b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      if (monitor != NULL) {
766d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        mirror::Object* object = monitor->obj_;
767d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        object_identity_hashcode = object->IdentityHashCode();
768d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        pretty_type = PrettyTypeOf(object);
769b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes      }
7708e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    }
77134e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes  } else if (state == kBlocked) {
7728e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    os << "  - waiting to lock ";
773d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    mirror::Object* object = thread->monitor_enter_object_;
7748e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    if (object != NULL) {
775d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      object_identity_hashcode = object->IdentityHashCode();
776d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      lock_owner = object->GetLockOwnerThreadId();
777d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      pretty_type = PrettyTypeOf(object);
7788e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    }
7798e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  } else {
7808e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    // We're not waiting on anything.
7818e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    return;
7828e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  }
7838e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
7848e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
785d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  os << StringPrintf("<0x%08x> (a %s)", object_identity_hashcode, pretty_type.c_str());
7868e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
787c5dc2ff2d1beb3ff84f93d2c7ebe07c46b76956aElliott Hughes  // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
788d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (lock_owner != ThreadList::kInvalidThreadId) {
7898e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes    os << " held by thread " << lock_owner;
7908e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  }
7918e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
7928e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes  os << "\n";
7938e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes}
7948e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes
7952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
796f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
797f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  // definition of contended that includes a monitor a thread is trying to enter...
7982dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* result = thread->monitor_enter_object_;
799d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (result == NULL) {
800d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    // ...but also a monitor that the thread is waiting on.
801f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    MutexLock mu(Thread::Current(), *thread->wait_mutex_);
802f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    Monitor* monitor = thread->wait_monitor_;
803f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    if (monitor != NULL) {
804d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      result = monitor->GetObject();
805f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    }
806f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
807d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  return result;
808f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
809f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
8102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
8112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                         void* callback_context) {
812ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = stack_visitor->GetMethod();
81308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  CHECK(m != NULL);
81408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
81508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // Native methods are an easy special case.
81608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
81708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  if (m->IsNative()) {
81808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    if (m->IsSynchronized()) {
8192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* jni_this = stack_visitor->GetCurrentSirt()->GetReference(0);
8204993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      callback(jni_this, callback_context);
82108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    }
82208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    return;
82308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
82408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
82561f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao  // Proxy methods should not be synchronized.
82661f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao  if (m->IsProxyMethod()) {
82761f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao    CHECK(!m->IsSynchronized());
82861f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao    return;
82961f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao  }
83061f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao
83108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // <clinit> is another special case. The runtime holds the class lock while calling <clinit>.
83208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  MethodHelper mh(m);
83308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  if (mh.IsClassInitializer()) {
8344993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    callback(m->GetDeclaringClass(), callback_context);
83508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    // Fall through because there might be synchronization in the user code too.
83608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
83708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
83808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  // Is there any reason to believe there's any synchronization in this method?
83908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  const DexFile::CodeItem* code_item = mh.GetCodeItem();
840caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  CHECK(code_item != NULL) << PrettyMethod(m);
84108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  if (code_item->tries_size_ == 0) {
8427934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return;  // No "tries" implies no synchronization, so no held locks to report.
84308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
84408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
84580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
84680537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  // the locks held in this stack frame.
84780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  std::vector<uint32_t> monitor_enter_dex_pcs;
84880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  verifier::MethodVerifier::FindLocksAtDexPc(m, stack_visitor->GetDexPc(), monitor_enter_dex_pcs);
84980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  if (monitor_enter_dex_pcs.empty()) {
85080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    return;
85180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  }
85208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
85380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes  for (size_t i = 0; i < monitor_enter_dex_pcs.size(); ++i) {
85480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    // The verifier works in terms of the dex pcs of the monitor-enter instructions.
85580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    // We want the registers used by those instructions (so we can read the values out of them).
85680537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    uint32_t dex_pc = monitor_enter_dex_pcs[i];
85780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    uint16_t monitor_enter_instruction = code_item->insns_[dex_pc];
85808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
85980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    // Quick sanity check.
86080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) {
86180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes      LOG(FATAL) << "expected monitor-enter @" << dex_pc << "; was "
86280537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes                 << reinterpret_cast<void*>(monitor_enter_instruction);
86308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes    }
86480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes
86580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes    uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff);
8662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* o = reinterpret_cast<mirror::Object*>(stack_visitor->GetVReg(m, monitor_register,
8672dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                                                                 kReferenceVReg));
8684993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    callback(o, callback_context);
86908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  }
87008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes}
87108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
872d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::IsValidLockWord(LockWord lock_word) {
873d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
874d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
875d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Nothing to check.
876d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return true;
877d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked:
878d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Basic sanity check of owner.
879d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
880d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
881d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Check the  monitor appears in the monitor list.
882d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
883d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      MonitorList* list = Runtime::Current()->GetMonitorList();
884d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      MutexLock mu(Thread::Current(), list->monitor_list_lock_);
885d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      for (Monitor* list_mon : list->list_) {
886d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        if (mon == list_mon) {
887d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers          return true;  // Found our monitor.
888d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        }
8897dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers      }
890d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return false;  // Fail - unowned monitor in an object.
8917dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers    }
892d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    default:
893d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      LOG(FATAL) << "Unreachable";
894d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      return false;
8957dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers  }
8967dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers}
8977dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers
898ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromvoid Monitor::TranslateLocation(const mirror::ArtMethod* method, uint32_t dex_pc,
899d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                const char** source_file, uint32_t* line_number) const {
90033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  // If method is null, location is unknown
90133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  if (method == NULL) {
902d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    *source_file = "";
903d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    *line_number = 0;
90433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao    return;
90533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao  }
9066d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(method);
907d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  *source_file = mh.GetDeclaringClassSourceFile();
908d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (*source_file == NULL) {
909d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    *source_file = "";
910d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  }
911d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  *line_number = mh.GetLineNumFromDexPC(dex_pc);
912d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers}
913d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
914d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersuint32_t Monitor::GetOwnerThreadId() {
915d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  MutexLock mu(Thread::Current(), monitor_lock_);
916d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Thread* owner = owner_;
917d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (owner != NULL) {
918d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    return owner->GetThreadId();
919d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  } else {
920d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    return ThreadList::kInvalidThreadId;
92112c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes  }
92233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao}
92333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao
924c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu ChartierMonitorList::MonitorList()
925c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier    : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock"),
926c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier      monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
927c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
928c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
929c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesMonitorList::~MonitorList() {
93050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), monitor_list_lock_);
931c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  STLDeleteElements(&list_);
932c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
933c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
934c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::DisallowNewMonitors() {
93550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), monitor_list_lock_);
936c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  allow_new_monitors_ = false;
937c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier}
938c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier
939c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::AllowNewMonitors() {
940c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  Thread* self = Thread::Current();
941c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  MutexLock mu(self, monitor_list_lock_);
942c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  allow_new_monitors_ = true;
943c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  monitor_add_condition_.Broadcast(self);
944c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier}
945c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier
946c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::Add(Monitor* m) {
947c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  Thread* self = Thread::Current();
948c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  MutexLock mu(self, monitor_list_lock_);
949c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  while (UNLIKELY(!allow_new_monitors_)) {
950c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier    monitor_add_condition_.WaitHoldingLocks(self);
951c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier  }
952c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  list_.push_front(m);
953c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
954c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
9556aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartiervoid MonitorList::SweepMonitorList(RootVisitor visitor, void* arg) {
95650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), monitor_list_lock_);
95702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier  for (auto it = list_.begin(); it != list_.end(); ) {
958c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes    Monitor* m = *it;
9596aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier    mirror::Object* obj = m->GetObject();
9606aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier    mirror::Object* new_obj = visitor(obj, arg);
9616aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier    if (new_obj == nullptr) {
9626aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier      VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
9636aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier                    << m->GetObject();
964c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes      delete m;
965c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes      it = list_.erase(it);
966c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes    } else {
9676aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier      m->SetObject(new_obj);
968c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes      ++it;
969c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes    }
970c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes  }
971c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes}
972c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes
973d9c4fc94fa618617f94e1de9af5f034549100753Ian RogersMonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(NULL), entry_count_(0) {
974d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  DCHECK(obj != NULL);
975d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers
976d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  LockWord lock_word = obj->GetLockWord();
977d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  switch (lock_word.GetState()) {
978d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kUnlocked:
979d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
980d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kThinLocked:
981d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
982d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      entry_count_ = 1 + lock_word.ThinLockCount();
983d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      // Thin locks have no waiters.
984d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
985d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    case LockWord::kFatLocked: {
986d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      Monitor* mon = lock_word.FatLockMonitor();
987d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      owner_ = mon->owner_;
988d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      entry_count_ = 1 + mon->lock_count_;
989d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      for (Thread* waiter = mon->wait_set_; waiter != NULL; waiter = waiter->wait_next_) {
990d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        waiters_.push_back(waiter);
991d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      }
992d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers      break;
993f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    }
994f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
995f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
996f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
9975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes}  // namespace art
998