monitor.cc revision b2771b41a956b50266d4d83fbb067f99faf7b7dc
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 21e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h" 2276b6167407c2b6f5d40ad895b2793a6b037f54b2Elliott Hughes#include "base/mutex.h" 231aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h" 2432ce2adefb8a3d0eda59a29f5e87c1eb43eef796Mathieu Chartier#include "base/systrace.h" 2580afd02024d20e60b197d3adfbb43cc303cf29e0Vladimir Marko#include "base/time_utils.h" 2633dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao#include "class_linker.h" 274f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h" 280f7c93322e50ff53eeba6b9ae13cf73eb0617587Sebastien Hertz#include "dex_instruction-inl.h" 29d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers#include "lock_word-inl.h" 304f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "mirror/class-inl.h" 3105f3057d6a4d23d712092ccd36a531590bff323bIan Rogers#include "mirror/object-inl.h" 322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h" 3300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "thread.h" 358e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "thread_list.h" 3608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "verifier/method_verifier.h" 37044288fb8f69e154862763a6b3bfea2bac1b397eElliott Hughes#include "well_known_classes.h" 385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesnamespace art { 405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 41b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartierstatic constexpr uint64_t kLongWaitMs = 100; 42b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier 435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 44d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Every Object has a monitor associated with it, but not every Object is actually locked. Even 45d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * the ones that are locked do not need a full-fledged monitor until a) there is actual contention 46d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * or b) wait() is called on the Object. 475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 48d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s 49d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * "Thin locks: featherweight synchronization for Java" (ACM 1998). Things are even easier for us, 50d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * though, because we have a full 32 bits to work with. 515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 52d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * The two states of an Object's lock are referred to as "thin" and "fat". A lock may transition 53d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once 54d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * a lock has been inflated it remains in the "fat" state indefinitely. 555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 56d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * The lock value itself is stored in mirror::Object::monitor_ and the representation is described 57d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * in the LockWord value type. 5854e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * 595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Monitors provide: 605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - mutually exclusive access to resources 615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - a way for multiple threads to wait for notification 625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * In effect, they fill the role of both mutexes and condition variables. 645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 65d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * Only one thread can own the monitor at any time. There may be several threads waiting on it 66d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * (the wait call unlocks it). One or more waiting threads may be getting interrupted or notified 67d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers * at any given time. 685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6954e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 70fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesuint32_t Monitor::lock_profiling_threshold_ = 0; 7132d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes 72b2771b41a956b50266d4d83fbb067f99faf7b7dcCalin Juravlevoid Monitor::Init(uint32_t lock_profiling_threshold) { 73fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes lock_profiling_threshold_ = lock_profiling_threshold; 7432d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes} 7532d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes 76ef7d42fca18c16fbaf103822ad16f23246e2905dIan RogersMonitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) 7700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers : monitor_lock_("a monitor lock", kMonitorLock), 78d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_contenders_("monitor contenders", monitor_lock_), 7946bc778f1feed02b20d25e3d03470c93ca2c0506Mathieu Chartier num_waiters_(0), 8000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers owner_(owner), 815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_(0), 8294f7b49578b6aaa80de8ffed230648d601393905Hiroshi Yamauchi obj_(GcRoot<mirror::Object>(obj)), 832cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier wait_set_(nullptr), 84ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier hash_code_(hash_code), 852cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier locking_method_(nullptr), 86ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers locking_dex_pc_(0), 8774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe monitor_id_(MonitorPool::ComputeMonitorId(this, self)) { 8874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe#ifdef __LP64__ 8974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe DCHECK(false) << "Should not be reached in 64b"; 9074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe next_free_ = nullptr; 9174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe#endif 9274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race 9374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe // with the owner unlocking the thin-lock. 9474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe CHECK(owner == nullptr || owner == self || owner->IsSuspended()); 9574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe // The identity hash code is set for the life time of the monitor. 9674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe} 9774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe 9874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas GampeMonitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code, 9974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe MonitorId id) 10074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe : monitor_lock_("a monitor lock", kMonitorLock), 10174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe monitor_contenders_("monitor contenders", monitor_lock_), 10274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe num_waiters_(0), 10374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe owner_(owner), 10474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe lock_count_(0), 10594f7b49578b6aaa80de8ffed230648d601393905Hiroshi Yamauchi obj_(GcRoot<mirror::Object>(obj)), 1062cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier wait_set_(nullptr), 10774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe hash_code_(hash_code), 1082cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier locking_method_(nullptr), 10974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe locking_dex_pc_(0), 11074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe monitor_id_(id) { 11174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe#ifdef __LP64__ 11274240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe next_free_ = nullptr; 11374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe#endif 114d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race 115d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // with the owner unlocking the thin-lock. 116ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers CHECK(owner == nullptr || owner == self || owner->IsSuspended()); 117ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier // The identity hash code is set for the life time of the monitor. 118d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers} 119d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers 1204e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartierint32_t Monitor::GetHashCode() { 1214e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier while (!HasHashCode()) { 1223e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) { 1234e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier break; 1244e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier } 1254e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier } 1264e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier DCHECK(HasHashCode()); 1273e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers return hash_code_.LoadRelaxed(); 1284e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier} 1294e6a31eb97f22f4480827474b30b9e64f396eaceMathieu Chartier 130d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::Install(Thread* self) { 131d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers MutexLock mu(self, monitor_lock_); // Uncontended mutex acquisition as monitor isn't yet public. 132ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended()); 13300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Propagate the lock state. 1344cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi LockWord lw(GetObject()->GetLockWord(false)); 135ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier switch (lw.GetState()) { 136ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier case LockWord::kThinLocked: { 137ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner()); 138ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier lock_count_ = lw.ThinLockCount(); 139ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier break; 140ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier } 141ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier case LockWord::kHashCode: { 1423e5cf305db800b2989ad57b7cde8fb3cc9fa1b9eIan Rogers CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode())); 143ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier break; 144ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier } 145ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier case LockWord::kFatLocked: { 146ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier // The owner_ is suspended but another thread beat us to install a monitor. 147ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier return false; 148ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier } 149ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier case LockWord::kUnlocked: { 150ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier LOG(FATAL) << "Inflating unlocked lock word"; 151ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier break; 152ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier } 153590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier default: { 154590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier LOG(FATAL) << "Invalid monitor state " << lw.GetState(); 155590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier return false; 156590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 157d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 158e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi LockWord fat(this, lw.ReadBarrierState()); 159d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Publish the updated lock word, which may race with other threads. 160228602f562f1d130d06e60a98752d99c2d467d6aIan Rogers bool success = GetObject()->CasLockWordWeakSequentiallyConsistent(lw, fat); 16100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Lock profiling. 1629728f91a63016136261231ff5213bde703bd27b6Mathieu Chartier if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) { 1636ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on 1646ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe // abort. 1656ec8ebd178ed39aa09e4c7fad194900114c4121aAndreas Gampe locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false); 16600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 167d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return success; 1685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1705f79133a435ebcb20000370d56046fe01201dd80Elliott HughesMonitor::~Monitor() { 171590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // Deflated monitors have a null object. 1725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::AppendToWaitSet(Thread* thread) { 1755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(owner_ == Thread::Current()); 1762cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(thread != nullptr); 177dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext(); 1782cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (wait_set_ == nullptr) { 1795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread; 1805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // push_back. 1845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* t = wait_set_; 185dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers while (t->GetWaitNext() != nullptr) { 186dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers t = t->GetWaitNext(); 1875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 188dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers t->SetWaitNext(thread); 1895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::RemoveFromWaitSet(Thread *thread) { 1925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(owner_ == Thread::Current()); 1932cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(thread != nullptr); 1942cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (wait_set_ == nullptr) { 1955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == thread) { 198dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers wait_set_ = thread->GetWaitNext(); 199dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers thread->SetWaitNext(nullptr); 2005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 2015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 2025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* t = wait_set_; 2042cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier while (t->GetWaitNext() != nullptr) { 205dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (t->GetWaitNext() == thread) { 206dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers t->SetWaitNext(thread->GetWaitNext()); 207dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers thread->SetWaitNext(nullptr); 2085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 2095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 210dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers t = t->GetWaitNext(); 2115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 2125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 2135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2146aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartiervoid Monitor::SetObject(mirror::Object* object) { 21594f7b49578b6aaa80de8ffed230648d601393905Hiroshi Yamauchi obj_ = GcRoot<mirror::Object>(object); 2166aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier} 2176aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier 2185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Lock(Thread* self) { 219d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers MutexLock mu(self, monitor_lock_); 220d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers while (true) { 221440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier if (owner_ == nullptr) { // Unowned. 222d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers owner_ = self; 223d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers CHECK_EQ(lock_count_, 0); 224d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // When debugging, save the current monitor holder for future 225d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // acquisition failures to use in sampled logging. 226d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (lock_profiling_threshold_ != 0) { 227d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers locking_method_ = self->GetCurrentMethod(&locking_dex_pc_); 228fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 229d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return; 230d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } else if (owner_ == self) { // Recursive. 231d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers lock_count_++; 232d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return; 233fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 234d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Contended. 235d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers const bool log_contention = (lock_profiling_threshold_ != 0); 236b894a19dfd668b6779de939cf5265b7e409d8809Xin Guan uint64_t wait_start_ms = log_contention ? MilliTime() : 0; 237e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier ArtMethod* owners_method = locking_method_; 238d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers uint32_t owners_dex_pc = locking_dex_pc_; 239440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier // Do this before releasing the lock so that we don't get deflated. 240b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier size_t num_waiters = num_waiters_; 241440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier ++num_waiters_; 242d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_lock_.Unlock(self); // Let go of locks in order. 243a6e7f0872c42009ecbee82d7fbe452deef9ae65bMathieu Chartier self->SetMonitorEnterObject(GetObject()); 244d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers { 245d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers ScopedThreadStateChange tsc(self, kBlocked); // Change to blocked and give up mutator_lock_. 2462cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // Reacquire monitor_lock_ without mutator_lock_ for Wait. 2472cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier MutexLock mu2(self, monitor_lock_); 248f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier if (owner_ != nullptr) { // Did the owner_ give the lock up? 249f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier if (ATRACE_ENABLED()) { 250f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier std::string name; 251f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier owner_->GetThreadName(name); 252f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier ATRACE_BEGIN(("Contended on monitor with owner " + name).c_str()); 253f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier } 254d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_contenders_.Wait(self); // Still contended so wait. 255d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Woken from contention. 256d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (log_contention) { 257d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers uint64_t wait_ms = MilliTime() - wait_start_ms; 258d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers uint32_t sample_percent; 259d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (wait_ms >= lock_profiling_threshold_) { 260d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers sample_percent = 100; 261d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } else { 262d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers sample_percent = 100 * wait_ms / lock_profiling_threshold_; 263d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 264d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) { 265d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers const char* owners_filename; 266eaa4609574267771f2080cbaa3dbe26da709b6f6Brian Carlstrom int32_t owners_line_number; 267d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number); 268b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier if (wait_ms > kLongWaitMs && owners_method != nullptr) { 269b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier LOG(WARNING) << "Long monitor contention event with owner method=" 270b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier << PrettyMethod(owners_method) << " from " << owners_filename << ":" 271b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier << owners_line_number << " waiters=" << num_waiters << " for " 272b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier << PrettyDuration(MsToNs(wait_ms)); 273b9001abff3a45f1ae90536da7dd1ec28a6ae0174Mathieu Chartier } 274d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers LogContentionEvent(self, wait_ms, sample_percent, owners_filename, owners_line_number); 275d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 276d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 277f0dc8b5519102b3d3e738aed846975ae4239421eMathieu Chartier ATRACE_END(); 278fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 2795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 280a6e7f0872c42009ecbee82d7fbe452deef9ae65bMathieu Chartier self->SetMonitorEnterObject(nullptr); 281d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_lock_.Lock(self); // Reacquire locks in order. 282440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier --num_waiters_; 283fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 2845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 2855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2866d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) 2876d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers __attribute__((format(printf, 1, 2))); 2886d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers 28900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) 29090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier SHARED_REQUIRES(Locks::mutator_lock_) { 2916d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_list args; 2926d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_start(args, fmt); 29362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers Thread* self = Thread::Current(); 2940aa50ce2fb75bfc2e815a0c33adf9b049561923bNicolas Geoffray self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args); 295d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) { 29664277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom std::ostringstream ss; 29762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers self->Dump(ss); 298d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers LOG(Runtime::Current()->IsStarted() ? INFO : ERROR) 29914691c5e786e8c2c5734f687e4c96217340771beNicolas Geoffray << self->GetException()->Dump() << "\n" << ss.str(); 30064277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom } 3016d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_end(args); 3026d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers} 3036d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers 304d423741f91526cada9d081c64ee295ec9863032dElliott Hughesstatic std::string ThreadToString(Thread* thread) { 3052cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (thread == nullptr) { 3062cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier return "nullptr"; 307d423741f91526cada9d081c64ee295ec9863032dElliott Hughes } 308d423741f91526cada9d081c64ee295ec9863032dElliott Hughes std::ostringstream oss; 309d423741f91526cada9d081c64ee295ec9863032dElliott Hughes // TODO: alternatively, we could just return the thread's name. 310d423741f91526cada9d081c64ee295ec9863032dElliott Hughes oss << *thread; 311d423741f91526cada9d081c64ee295ec9863032dElliott Hughes return oss.str(); 312d423741f91526cada9d081c64ee295ec9863032dElliott Hughes} 313d423741f91526cada9d081c64ee295ec9863032dElliott Hughes 3142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::FailedUnlock(mirror::Object* o, Thread* expected_owner, Thread* found_owner, 315ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes Monitor* monitor) { 3162cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier Thread* current_owner = nullptr; 317ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string current_owner_string; 318ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string expected_owner_string; 319ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string found_owner_string; 320ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes { 321ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // TODO: isn't this too late to prevent threads from disappearing? 322ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Acquire thread list lock so threads won't disappear from under us. 32350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); 324ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Re-read owner now that we hold lock. 3252cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr; 326ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Get short descriptions of the threads involved. 327ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string = ThreadToString(current_owner); 328ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string = ThreadToString(expected_owner); 329ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string = ThreadToString(found_owner); 330ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 3312cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (current_owner == nullptr) { 3322cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (found_owner == nullptr) { 3336d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'" 3346d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " on thread '%s'", 335ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 336ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3376d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3386d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: the original read found an owner but now there is none 3396d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 3406d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " (where now the monitor appears unowned) on thread '%s'", 341ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string.c_str(), 342ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 343ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3446d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3456d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3462cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (found_owner == nullptr) { 3476d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: originally there was no owner, there is now 3486d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 3496d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " (originally believed to be unowned) on thread '%s'", 350ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 351ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 352ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3536d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3546d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner != current_owner) { 3556d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: originally found and current owner have changed 3566d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now" 3576d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " owned by '%s') on object of type '%s' on thread '%s'", 358ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string.c_str(), 359ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 360ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 361ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3626d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3636d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 3646d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " on thread '%s", 365ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 366ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 367ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3686d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3696d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3706d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 3725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 373d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::Unlock(Thread* self) { 3742cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(self != nullptr); 375d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers MutexLock mu(self, monitor_lock_); 3766d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers Thread* owner = owner_; 3776d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (owner == self) { 3785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // We own the monitor, so nobody else can be in here. 3795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock_count_ == 0) { 3802cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier owner_ = nullptr; 3812cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier locking_method_ = nullptr; 3820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_ = 0; 383d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Wake a contender. 384d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_contenders_.Signal(self); 3855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 3865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes --lock_count_; 3875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 3895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // We don't own this, so we're not allowed to unlock it. 3905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The JNI spec says that we should throw IllegalMonitorStateException 3915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // in this case. 3924cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi FailedUnlock(GetObject(), self, owner, this); 3935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 3945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return true; 3965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 3975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3984cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughesvoid Monitor::Wait(Thread* self, int64_t ms, int32_t ns, 3994cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes bool interruptShouldThrow, ThreadState why) { 4002cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(self != nullptr); 4014cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping); 4025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 403d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_lock_.Lock(self); 404d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers 4055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 4065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 407d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_lock_.Unlock(self); 4081af6a1fa35ff7dc0a5c653f19dbc8a91c914aa42Elena Sayapina ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); 4095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 4105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4114cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes 412df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes // We need to turn a zero-length timed wait into a regular wait because 413df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait(). 414df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes if (why == kTimedWaiting && (ms == 0 && ns == 0)) { 415df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes why = kWaiting; 416df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes } 417df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes 4185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Enforce the timeout range. 4195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (ms < 0 || ns < 0 || ns > 999999) { 4201af6a1fa35ff7dc0a5c653f19dbc8a91c914aa42Elena Sayapina monitor_lock_.Unlock(self); 4210aa50ce2fb75bfc2e815a0c33adf9b049561923bNicolas Geoffray self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", 422ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns); 4235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 4245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Add ourselves to the set of threads waiting on this monitor, and 4285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * release our hold. We need to let it go even if we're a few levels 4295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * deep in a recursive lock, and we need to restore that later. 4305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 4315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We append to the wait set ahead of clearing the count and owner 4325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * fields so the subroutine can check that the calling thread owns 4335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * the monitor. Aside from that, the order of member updates is 4345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * not order sensitive as we hold the pthread mutex. 4355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes AppendToWaitSet(self); 437440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier ++num_waiters_; 4380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers int prev_lock_count = lock_count_; 4395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_ = 0; 4402cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier owner_ = nullptr; 441e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier ArtMethod* saved_method = locking_method_; 4422cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier locking_method_ = nullptr; 4430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uintptr_t saved_dex_pc = locking_dex_pc_; 4440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_ = 0; 4455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 446b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes bool was_interrupted = false; 44700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 448f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier // Update thread state. If the GC wakes up, it'll ignore us, knowing 449f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier // that we won't touch any references in this state, and we'll check 450f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier // our suspend mode before we transition out. 451f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier ScopedThreadSuspension sts(self, why); 452f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier 45300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock. 454dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers MutexLock mu(self, *self->GetWaitMutex()); 4555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 45600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is 4572cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // non-null a notifying or interrupting thread must signal the thread's wait_cond_ to wake it 45800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // up. 459dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DCHECK(self->GetWaitMonitor() == nullptr); 460dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->SetWaitMonitor(this); 4615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 46200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release the monitor lock. 463d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_contenders_.Signal(self); 464d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_lock_.Unlock(self); 46500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 466b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // Handle the case where the thread was interrupted before we called wait(). 467dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (self->IsInterruptedLocked()) { 468b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes was_interrupted = true; 46900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } else { 47000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Wait for a notification or a timeout to occur. 4714cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes if (why == kWaiting) { 472dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->GetWaitConditionVariable()->Wait(self); 47300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } else { 4744cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes DCHECK(why == kTimedWaiting || why == kSleeping) << why; 475dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->GetWaitConditionVariable()->TimedWait(self, ms, ns); 47600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 477328c5dc0d178632125f5a05002051a6adc04766aHans Boehm was_interrupted = self->IsInterruptedLocked(); 47800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 4795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 481b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes { 482b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // We reset the thread's wait_monitor_ field after transitioning back to runnable so 483b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging 484b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads 485b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // are waiting on "null".) 486dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers MutexLock mu(self, *self->GetWaitMutex()); 487dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers DCHECK(self->GetWaitMonitor() != nullptr); 488dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->SetWaitMonitor(nullptr); 489b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes } 490b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes 491daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier // Allocate the interrupted exception not holding the monitor lock since it may cause a GC. 492daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier // If the GC requires acquiring the monitor for enqueuing cleared references, this would 493daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier // cause a deadlock if the monitor is held. 494daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier if (was_interrupted && interruptShouldThrow) { 495daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier /* 496daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier * We were interrupted while waiting, or somebody interrupted an 497daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier * un-interruptible thread earlier and we're bailing out immediately. 498daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier * 499daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier * The doc sayeth: "The interrupted status of the current thread is 500daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier * cleared when this exception is thrown." 501daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier */ 502daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier { 503daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier MutexLock mu(self, *self->GetWaitMutex()); 504daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier self->SetInterruptedLocked(false); 505daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier } 506daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr); 507daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier } 508daed5d81e2fdb9d1e03ee6c34567347b92dcfb22Mathieu Chartier 509d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Re-acquire the monitor and lock. 51000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Lock(self); 511d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers monitor_lock_.Lock(self); 512dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->GetWaitMutex()->AssertNotHeld(self); 5135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 5155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We remove our thread from wait set after restoring the count 5165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * and owner fields so the subroutine can check that the calling 5175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * thread owns the monitor. Aside from that, the order of member 5185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * updates is not order sensitive as we hold the pthread mutex. 5195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 5205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = self; 5210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers lock_count_ = prev_lock_count; 5220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_method_ = saved_method; 5230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_ = saved_dex_pc; 524440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier --num_waiters_; 5255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes RemoveFromWaitSet(self); 5265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5271af6a1fa35ff7dc0a5c653f19dbc8a91c914aa42Elena Sayapina monitor_lock_.Unlock(self); 5285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Notify(Thread* self) { 5312cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(self != nullptr); 532d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers MutexLock mu(self, monitor_lock_); 5335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 5345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 5356d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); 5365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Signal the first waiting thread in the wait set. 5392cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier while (wait_set_ != nullptr) { 5405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* thread = wait_set_; 541dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers wait_set_ = thread->GetWaitNext(); 542dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers thread->SetWaitNext(nullptr); 5435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Check to see if the thread is still waiting. 545277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe MutexLock wait_mu(self, *thread->GetWaitMutex()); 546dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers if (thread->GetWaitMonitor() != nullptr) { 547dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers thread->GetWaitConditionVariable()->Signal(self); 5485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::NotifyAll(Thread* self) { 5542cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(self != nullptr); 555d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers MutexLock mu(self, monitor_lock_); 5565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 5575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 5586d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); 5595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Signal all threads in the wait set. 5622cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier while (wait_set_ != nullptr) { 5635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* thread = wait_set_; 564dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers wait_set_ = thread->GetWaitNext(); 565dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers thread->SetWaitNext(nullptr); 5665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->Notify(); 5675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 570590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartierbool Monitor::Deflate(Thread* self, mirror::Object* obj) { 571590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier DCHECK(obj != nullptr); 5724d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier // Don't need volatile since we only deflate with mutators suspended. 5734d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier LockWord lw(obj->GetLockWord(false)); 574590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // If the lock isn't an inflated monitor, then we don't need to deflate anything. 575590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier if (lw.GetState() == LockWord::kFatLocked) { 576590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier Monitor* monitor = lw.FatLockMonitor(); 577440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier DCHECK(monitor != nullptr); 578590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier MutexLock mu(self, monitor->monitor_lock_); 579440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier // Can't deflate if we have anybody waiting on the CV. 580440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier if (monitor->num_waiters_ > 0) { 581440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier return false; 582440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier } 583590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier Thread* owner = monitor->owner_; 584590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier if (owner != nullptr) { 585590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // Can't deflate if we are locked and have a hash code. 586590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier if (monitor->HasHashCode()) { 587590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier return false; 588590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 589590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // Can't deflate if our lock count is too high. 590590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier if (monitor->lock_count_ > LockWord::kThinLockMaxCount) { 591590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier return false; 592590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 593590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // Deflate to a thin lock. 594e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_, 595e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi lw.ReadBarrierState()); 596e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // Assume no concurrent read barrier state changes as mutators are suspended. 597e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi obj->SetLockWord(new_lw, false); 5984d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / " 5994d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier << monitor->lock_count_; 600590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } else if (monitor->HasHashCode()) { 601e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.ReadBarrierState()); 602e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // Assume no concurrent read barrier state changes as mutators are suspended. 603e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi obj->SetLockWord(new_lw, false); 604440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode(); 605590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } else { 606590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // No lock and no hash, just put an empty lock word inside the object. 607e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi LockWord new_lw = LockWord::FromDefault(lw.ReadBarrierState()); 608e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // Assume no concurrent read barrier state changes as mutators are suspended. 609e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi obj->SetLockWord(new_lw, false); 610440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier VLOG(monitor) << "Deflated" << obj << " to empty lock word"; 611590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 6122cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier // The monitor is deflated, mark the object as null so that we know to delete it during the 613590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // next GC. 61494f7b49578b6aaa80de8ffed230648d601393905Hiroshi Yamauchi monitor->obj_ = GcRoot<mirror::Object>(nullptr); 615590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 616590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier return true; 617590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier} 618590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier 619ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartiervoid Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) { 62074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe DCHECK(self != nullptr); 62174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe DCHECK(obj != nullptr); 6225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Allocate and acquire a new monitor. 62374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code); 62474240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe DCHECK(m != nullptr); 625d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (m->Install(self)) { 62686ab7912423f63541073af5c487b509e9b2b5420Haifeng Li if (owner != nullptr) { 62786ab7912423f63541073af5c487b509e9b2b5420Haifeng Li VLOG(monitor) << "monitor: thread" << owner->GetThreadId() 62874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe << " created monitor " << m << " for object " << obj; 62986ab7912423f63541073af5c487b509e9b2b5420Haifeng Li } else { 63086ab7912423f63541073af5c487b509e9b2b5420Haifeng Li VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code 63174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe << " created monitor " << m << " for object " << obj; 63286ab7912423f63541073af5c487b509e9b2b5420Haifeng Li } 63374240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe Runtime::Current()->GetMonitorList()->Add(m); 6344d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked); 63574240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe } else { 63674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe MonitorPool::ReleaseMonitor(self, m); 637ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier } 638ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier} 639ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier 6400cd81352a7c06e381951cea1b104fd73516f4341Mathieu Chartiervoid Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word, 641ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier uint32_t hash_code) { 642ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked); 643ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier uint32_t owner_thread_id = lock_word.ThinLockOwner(); 644ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier if (owner_thread_id == self->GetThreadId()) { 645ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier // We own the monitor, we can easily inflate it. 646eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Inflate(self, self, obj.Get(), hash_code); 647ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier } else { 648ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier ThreadList* thread_list = Runtime::Current()->GetThreadList(); 649ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier // Suspend the owner, inflate. First change to blocked and give up mutator_lock_. 650eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier self->SetMonitorEnterObject(obj.Get()); 651a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier bool timed_out; 652f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier Thread* owner; 653f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier { 654f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier ScopedThreadSuspension sts(self, kBlocked); 655f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out); 656f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier } 657a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier if (owner != nullptr) { 658a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier // We succeeded in suspending the thread, check the lock's status didn't change. 659a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier lock_word = obj->GetLockWord(true); 660a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier if (lock_word.GetState() == LockWord::kThinLocked && 661a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier lock_word.ThinLockOwner() == owner_thread_id) { 662a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier // Go ahead and inflate the lock. 663a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier Inflate(self, owner, obj.Get(), hash_code); 664ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier } 665a1ee14fc66a3c99bb7d5744ace881ec93c46f59dMathieu Chartier thread_list->Resume(owner, false); 666ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier } 667dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers self->SetMonitorEnterObject(nullptr); 668d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 6695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 6705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 671719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Fool annotalysis into thinking that the lock on obj is acquired. 672719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic mirror::Object* FakeLock(mirror::Object* obj) 673719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { 674719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers return obj; 675719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers} 676719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 677719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers// Fool annotalysis into thinking that the lock on obj is release. 678719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic mirror::Object* FakeUnlock(mirror::Object* obj) 679719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { 680719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers return obj; 681719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers} 682719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers 683e7e8a5fea2d852cccc840fa046151a16627f26cdMathieu Chartiermirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) { 6842cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(self != nullptr); 6852cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(obj != nullptr); 6862d096c94fbd3fd2470b8ac1a0da6f577b3f69f42Mathieu Chartier self->AssertThreadSuspensionIsAllowable(); 687719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers obj = FakeLock(obj); 688d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers uint32_t thread_id = self->GetThreadId(); 689d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers size_t contention_count = 0; 690eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier StackHandleScope<1> hs(self); 691eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::Object> h_obj(hs.NewHandle(obj)); 692d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers while (true) { 693eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier LockWord lock_word = h_obj->GetLockWord(true); 694d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers switch (lock_word.GetState()) { 695d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kUnlocked: { 696e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.ReadBarrierState())); 697228602f562f1d130d06e60a98752d99c2d467d6aIan Rogers if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) { 6983035961cb41865b80b927546be0c708b6389cec6Hans Boehm // CasLockWord enforces more than the acquire ordering we need here. 699eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier return h_obj.Get(); // Success! 700d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 701d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers continue; // Go again. 7025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 703d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kThinLocked: { 704d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers uint32_t owner_thread_id = lock_word.ThinLockOwner(); 705d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (owner_thread_id == thread_id) { 706d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // We own the lock, increase the recursion count. 707d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers uint32_t new_count = lock_word.ThinLockCount() + 1; 708d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) { 709e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count, 710e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi lock_word.ReadBarrierState())); 711e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi if (!kUseReadBarrier) { 712e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi h_obj->SetLockWord(thin_locked, true); 713e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi return h_obj.Get(); // Success! 714e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } else { 715e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // Use CAS to preserve the read barrier state. 716e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) { 717e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi return h_obj.Get(); // Success! 718e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } 719e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } 720e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi continue; // Go again. 721d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } else { 722d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // We'd overflow the recursion count, so inflate the monitor. 723eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier InflateThinLocked(self, h_obj, lock_word, 0); 724d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 725d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } else { 726d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Contention. 727d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers contention_count++; 728ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier Runtime* runtime = Runtime::Current(); 729ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) { 730b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier // TODO: Consider switching the thread state to kBlocked when we are yielding. 731251755cd511463260e60be98bf138b6aa1c14bf3Mathieu Chartier // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the 732251755cd511463260e60be98bf138b6aa1c14bf3Mathieu Chartier // parameter you pass in. This can cause thread suspension to take excessively long 733b363f666883860d40823d5528df3c98c897f74f4Mathieu Chartier // and make long pauses. See b/16307460. 734251755cd511463260e60be98bf138b6aa1c14bf3Mathieu Chartier sched_yield(); 7355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 736d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers contention_count = 0; 737eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier InflateThinLocked(self, h_obj, lock_word, 0); 7385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 740d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers continue; // Start from the beginning. 741d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 742d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kFatLocked: { 743d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers Monitor* mon = lock_word.FatLockMonitor(); 744d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers mon->Lock(self); 745eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier return h_obj.Get(); // Success! 7465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 747719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers case LockWord::kHashCode: 748ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier // Inflate with the existing hashcode. 749eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode()); 750719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers continue; // Start from the beginning. 751590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier default: { 752590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); 753eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier return h_obj.Get(); 754590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 7555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 7585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7592dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersbool Monitor::MonitorExit(Thread* self, mirror::Object* obj) { 7602cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(self != nullptr); 7612cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier DCHECK(obj != nullptr); 7622d096c94fbd3fd2470b8ac1a0da6f577b3f69f42Mathieu Chartier self->AssertThreadSuspensionIsAllowable(); 763719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers obj = FakeUnlock(obj); 764eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier StackHandleScope<1> hs(self); 765eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier Handle<mirror::Object> h_obj(hs.NewHandle(obj)); 766e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi while (true) { 767e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi LockWord lock_word = obj->GetLockWord(true); 768e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi switch (lock_word.GetState()) { 769e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi case LockWord::kHashCode: 770e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // Fall-through. 771e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi case LockWord::kUnlocked: 772e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi FailedUnlock(h_obj.Get(), self, nullptr, nullptr); 773d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return false; // Failure. 774e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi case LockWord::kThinLocked: { 775e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi uint32_t thread_id = self->GetThreadId(); 776e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi uint32_t owner_thread_id = lock_word.ThinLockOwner(); 777e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi if (owner_thread_id != thread_id) { 778e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // TODO: there's a race here with the owner dying while we unlock. 779e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi Thread* owner = 780e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); 781e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi FailedUnlock(h_obj.Get(), self, owner, nullptr); 782e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi return false; // Failure. 783d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } else { 784e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // We own the lock, decrease the recursion count. 785e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi LockWord new_lw = LockWord::Default(); 786e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi if (lock_word.ThinLockCount() != 0) { 787e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi uint32_t new_count = lock_word.ThinLockCount() - 1; 788e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.ReadBarrierState()); 789e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } else { 790e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi new_lw = LockWord::FromDefault(lock_word.ReadBarrierState()); 791e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } 792e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi if (!kUseReadBarrier) { 793e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi DCHECK_EQ(new_lw.ReadBarrierState(), 0U); 794e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi h_obj->SetLockWord(new_lw, true); 795e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // Success! 796e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi return true; 797e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } else { 798e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // Use CAS to preserve the read barrier state. 799e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, new_lw)) { 800e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi // Success! 801e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi return true; 802e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } 803e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } 804e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi continue; // Go again. 805d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 8065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 807e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi case LockWord::kFatLocked: { 808e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi Monitor* mon = lock_word.FatLockMonitor(); 809e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi return mon->Unlock(self); 810e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } 811e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi default: { 812e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); 813e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi return false; 814e15ea086439b41a805d164d2beb07b4ba96aaa97Hiroshi Yamauchi } 815590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 8165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 8185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 8192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns, 8204cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes bool interruptShouldThrow, ThreadState why) { 8214d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier DCHECK(self != nullptr); 8224d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier DCHECK(obj != nullptr); 8234d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier LockWord lock_word = obj->GetLockWord(true); 82443c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers while (lock_word.GetState() != LockWord::kFatLocked) { 82543c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers switch (lock_word.GetState()) { 82643c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers case LockWord::kHashCode: 82743c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers // Fall-through. 82843c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers case LockWord::kUnlocked: 829d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); 830d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return; // Failure. 83143c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers case LockWord::kThinLocked: { 83243c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers uint32_t thread_id = self->GetThreadId(); 83343c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers uint32_t owner_thread_id = lock_word.ThinLockOwner(); 83443c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers if (owner_thread_id != thread_id) { 83543c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); 83643c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers return; // Failure. 83743c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers } else { 83843c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so 83943c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers // re-load. 84043c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers Inflate(self, self, obj, 0); 84143c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers lock_word = obj->GetLockWord(true); 84243c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers } 84343c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers break; 84443c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers } 84543c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through. 84643c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers default: { 84743c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); 84843c69cc4cea794cd4d89d9d0680b1e25c6d02accIan Rogers return; 849d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 850590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 8515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 852d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers Monitor* mon = lock_word.FatLockMonitor(); 853d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers mon->Wait(self, ms, ns, interruptShouldThrow, why); 8545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 8555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 85613c479e7e9b62115fe0409e5273c1e976a1c09f9Ian Rogersvoid Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) { 8574d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier DCHECK(self != nullptr); 8584d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier DCHECK(obj != nullptr); 8594d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier LockWord lock_word = obj->GetLockWord(true); 860d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers switch (lock_word.GetState()) { 861ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier case LockWord::kHashCode: 862ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier // Fall-through. 863d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kUnlocked: 8646d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); 865d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return; // Failure. 866d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kThinLocked: { 867d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers uint32_t thread_id = self->GetThreadId(); 868d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers uint32_t owner_thread_id = lock_word.ThinLockOwner(); 869d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (owner_thread_id != thread_id) { 870d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); 871d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return; // Failure. 872d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } else { 873d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // We own the lock but there's no Monitor and therefore no waiters. 874d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return; // Success. 875d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 876d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 877d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kFatLocked: { 878d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers Monitor* mon = lock_word.FatLockMonitor(); 879d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (notify_all) { 880d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers mon->NotifyAll(self); 881d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } else { 882d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers mon->Notify(self); 883d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 884d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return; // Success. 8855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 886590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier default: { 887590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); 888590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier return; 889590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 8905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 8925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 893d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersuint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) { 8944d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier DCHECK(obj != nullptr); 8954d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier LockWord lock_word = obj->GetLockWord(true); 896d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers switch (lock_word.GetState()) { 897ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier case LockWord::kHashCode: 898ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier // Fall-through. 899d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kUnlocked: 900d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return ThreadList::kInvalidThreadId; 901d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kThinLocked: 902d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return lock_word.ThinLockOwner(); 903d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kFatLocked: { 904d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers Monitor* mon = lock_word.FatLockMonitor(); 905d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return mon->GetOwnerThreadId(); 9065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 907590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier default: { 908d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers LOG(FATAL) << "Unreachable"; 9092c4257be8191c5eefde744e8965fcefc80a0a97dIan Rogers UNREACHABLE(); 910590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier } 9115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 9125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 9135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 9148e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesvoid Monitor::DescribeWait(std::ostream& os, const Thread* thread) { 915d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers // Determine the wait message and object we're waiting or blocked upon. 916d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers mirror::Object* pretty_object = nullptr; 917d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers const char* wait_message = nullptr; 918d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers uint32_t lock_owner = ThreadList::kInvalidThreadId; 919d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers ThreadState state = thread->GetState(); 920b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes if (state == kWaiting || state == kTimedWaiting || state == kSleeping) { 921d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on "; 922d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers Thread* self = Thread::Current(); 923d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers MutexLock mu(self, *thread->GetWaitMutex()); 924d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers Monitor* monitor = thread->GetWaitMonitor(); 925d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers if (monitor != nullptr) { 9264cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi pretty_object = monitor->GetObject(); 9278e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 92834e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes } else if (state == kBlocked) { 929d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers wait_message = " - waiting to lock "; 930d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers pretty_object = thread->GetMonitorEnterObject(); 931d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers if (pretty_object != nullptr) { 932d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers lock_owner = pretty_object->GetLockOwnerThreadId(); 9338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 9348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 9358e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 936d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers if (wait_message != nullptr) { 937d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers if (pretty_object == nullptr) { 938d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << wait_message << "an unknown object"; 939d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers } else { 9404d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) && 941d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) { 942d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers // Getting the identity hashcode here would result in lock inflation and suspension of the 943d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers // current thread, which isn't safe if this is the only runnable thread. 944d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)", 945d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers reinterpret_cast<intptr_t>(pretty_object), 946d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers PrettyTypeOf(pretty_object).c_str()); 947d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers } else { 948d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>) 9494936159997132d7706d9700c646f35ef0283df4bMathieu Chartier // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread 9504936159997132d7706d9700c646f35ef0283df4bMathieu Chartier // suspension and move pretty_object. 9514936159997132d7706d9700c646f35ef0283df4bMathieu Chartier const std::string pretty_type(PrettyTypeOf(pretty_object)); 952d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(), 9534936159997132d7706d9700c646f35ef0283df4bMathieu Chartier pretty_type.c_str()); 954d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers } 955d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers } 956d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5 957d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers if (lock_owner != ThreadList::kInvalidThreadId) { 958d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << " held by thread " << lock_owner; 959d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers } 960d803bc7ce255be6c16eaf6a8a58a742515e9da9fIan Rogers os << "\n"; 9618e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 9628e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 9638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 9642dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Object* Monitor::GetContendedMonitor(Thread* thread) { 965f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre 966f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes // definition of contended that includes a monitor a thread is trying to enter... 967dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers mirror::Object* result = thread->GetMonitorEnterObject(); 9682cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (result == nullptr) { 969d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // ...but also a monitor that the thread is waiting on. 970dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers MutexLock mu(Thread::Current(), *thread->GetWaitMutex()); 971dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers Monitor* monitor = thread->GetWaitMonitor(); 9722cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (monitor != nullptr) { 973d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers result = monitor->GetObject(); 974f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes } 975f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes } 976d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return result; 977f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes} 978f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes 9792dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*), 980760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe void* callback_context, bool abort_on_failure) { 981e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier ArtMethod* m = stack_visitor->GetMethod(); 9822cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier CHECK(m != nullptr); 98308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 98408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // Native methods are an easy special case. 98508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. 98608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (m->IsNative()) { 98708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (m->IsSynchronized()) { 988e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier mirror::Object* jni_this = 989e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0); 9904993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes callback(jni_this, callback_context); 99108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 99208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes return; 99308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 99408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 99561f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao // Proxy methods should not be synchronized. 99661f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao if (m->IsProxyMethod()) { 99761f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao CHECK(!m->IsSynchronized()); 99861f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao return; 99961f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao } 100061f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao 100108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // Is there any reason to believe there's any synchronization in this method? 1002bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier const DexFile::CodeItem* code_item = m->GetCodeItem(); 10032cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier CHECK(code_item != nullptr) << PrettyMethod(m); 100408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (code_item->tries_size_ == 0) { 10057934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom return; // No "tries" implies no synchronization, so no held locks to report. 100608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 100708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 1008760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot 1009760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an 1010760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe // inconsistent stack anyways. 1011760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure); 1012760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) { 1013760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe LOG(ERROR) << "Could not find dex_pc for " << PrettyMethod(m); 1014760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe return; 1015760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe } 1016760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe 101780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to 101880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // the locks held in this stack frame. 101980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes std::vector<uint32_t> monitor_enter_dex_pcs; 1020760172c3ccd6e75f6f1a89d8006934e8ffb1303eAndreas Gampe verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs); 1021e6a8eec3a5db28de7d5db6d78e38033b80740e49Mathieu Chartier for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) { 102280537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // The verifier works in terms of the dex pcs of the monitor-enter instructions. 102380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // We want the registers used by those instructions (so we can read the values out of them). 10240f7c93322e50ff53eeba6b9ae13cf73eb0617587Sebastien Hertz const Instruction* monitor_enter_instruction = 10250f7c93322e50ff53eeba6b9ae13cf73eb0617587Sebastien Hertz Instruction::At(&code_item->insns_[monitor_dex_pc]); 102608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 102780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // Quick sanity check. 10280f7c93322e50ff53eeba6b9ae13cf73eb0617587Sebastien Hertz CHECK_EQ(monitor_enter_instruction->Opcode(), Instruction::MONITOR_ENTER) 10290f7c93322e50ff53eeba6b9ae13cf73eb0617587Sebastien Hertz << "expected monitor-enter @" << monitor_dex_pc << "; was " 10300f7c93322e50ff53eeba6b9ae13cf73eb0617587Sebastien Hertz << reinterpret_cast<const void*>(monitor_enter_instruction); 103180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes 10320f7c93322e50ff53eeba6b9ae13cf73eb0617587Sebastien Hertz uint16_t monitor_register = monitor_enter_instruction->VRegA(); 103315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray uint32_t value; 103415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray bool success = stack_visitor->GetVReg(m, monitor_register, kReferenceVReg, &value); 103515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray CHECK(success) << "Failed to read v" << monitor_register << " of kind " 103615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray << kReferenceVReg << " in method " << PrettyMethod(m); 103715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray mirror::Object* o = reinterpret_cast<mirror::Object*>(value); 10384993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes callback(o, callback_context); 103908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 104008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes} 104108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 1042d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersbool Monitor::IsValidLockWord(LockWord lock_word) { 1043d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers switch (lock_word.GetState()) { 1044d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kUnlocked: 1045d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Nothing to check. 1046d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return true; 1047d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kThinLocked: 1048d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Basic sanity check of owner. 1049d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId; 1050d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kFatLocked: { 1051d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Check the monitor appears in the monitor list. 1052d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers Monitor* mon = lock_word.FatLockMonitor(); 1053d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers MonitorList* list = Runtime::Current()->GetMonitorList(); 1054d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers MutexLock mu(Thread::Current(), list->monitor_list_lock_); 1055d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers for (Monitor* list_mon : list->list_) { 1056d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers if (mon == list_mon) { 1057d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return true; // Found our monitor. 1058d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 10597dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers } 1060d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return false; // Fail - unowned monitor in an object. 10617dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers } 1062ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier case LockWord::kHashCode: 1063ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier return true; 1064d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers default: 1065d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers LOG(FATAL) << "Unreachable"; 10662c4257be8191c5eefde744e8965fcefc80a0a97dIan Rogers UNREACHABLE(); 10677dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers } 10687dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers} 10697dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers 107090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartierbool Monitor::IsLocked() SHARED_REQUIRES(Locks::mutator_lock_) { 1071ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier MutexLock mu(Thread::Current(), monitor_lock_); 1072ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier return owner_ != nullptr; 1073ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier} 1074ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier 1075e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartiervoid Monitor::TranslateLocation(ArtMethod* method, uint32_t dex_pc, 1076eaa4609574267771f2080cbaa3dbe26da709b6f6Brian Carlstrom const char** source_file, int32_t* line_number) const { 107733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao // If method is null, location is unknown 10782cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (method == nullptr) { 1079d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers *source_file = ""; 1080d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers *line_number = 0; 108133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao return; 108233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao } 1083bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier *source_file = method->GetDeclaringClassSourceFile(); 10842cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (*source_file == nullptr) { 1085d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers *source_file = ""; 1086d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 1087bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier *line_number = method->GetLineNumFromDexPC(dex_pc); 1088d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers} 1089d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers 1090d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogersuint32_t Monitor::GetOwnerThreadId() { 1091d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers MutexLock mu(Thread::Current(), monitor_lock_); 1092d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers Thread* owner = owner_; 10932cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (owner != nullptr) { 1094d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return owner->GetThreadId(); 1095d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } else { 1096d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers return ThreadList::kInvalidThreadId; 109712c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes } 109833dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao} 109933dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 1100c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu ChartierMonitorList::MonitorList() 1101440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock), 1102c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) { 1103c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 1104c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 1105c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesMonitorList::~MonitorList() { 110674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe Thread* self = Thread::Current(); 110774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe MutexLock mu(self, monitor_list_lock_); 110874240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe // Release all monitors to the pool. 110974240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe // TODO: Is it an invariant that *all* open monitors are in the list? Then we could 111074240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe // clear faster in the pool. 111174240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe MonitorPool::ReleaseMonitors(self, &list_); 1112c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 1113c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 1114c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::DisallowNewMonitors() { 1115fdbd13c7af91a042eda753e436eeebf0e1937250Hiroshi Yamauchi CHECK(!kUseReadBarrier); 111650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), monitor_list_lock_); 1117c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier allow_new_monitors_ = false; 1118c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier} 1119c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier 1120c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::AllowNewMonitors() { 1121fdbd13c7af91a042eda753e436eeebf0e1937250Hiroshi Yamauchi CHECK(!kUseReadBarrier); 1122c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier Thread* self = Thread::Current(); 1123c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier MutexLock mu(self, monitor_list_lock_); 1124c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier allow_new_monitors_ = true; 1125c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier monitor_add_condition_.Broadcast(self); 1126c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier} 1127c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier 11280b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchivoid MonitorList::BroadcastForNewMonitors() { 11290b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi CHECK(kUseReadBarrier); 11300b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi Thread* self = Thread::Current(); 11310b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi MutexLock mu(self, monitor_list_lock_); 11320b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi monitor_add_condition_.Broadcast(self); 11330b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi} 11340b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi 1135c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::Add(Monitor* m) { 1136c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier Thread* self = Thread::Current(); 1137c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier MutexLock mu(self, monitor_list_lock_); 11380b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi while (UNLIKELY((!kUseReadBarrier && !allow_new_monitors_) || 11390b71357fb52be9bb06d35396a3042b4381b01041Hiroshi Yamauchi (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) { 1140c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier monitor_add_condition_.WaitHoldingLocks(self); 1141c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier } 1142c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes list_.push_front(m); 1143c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 1144c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 114597509954404d031594b2ecbda607314d169d512eMathieu Chartiervoid MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) { 114674240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe Thread* self = Thread::Current(); 114774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe MutexLock mu(self, monitor_list_lock_); 114802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier for (auto it = list_.begin(); it != list_.end(); ) { 1149c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes Monitor* m = *it; 11504cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi // Disable the read barrier in GetObject() as this is called by GC. 11514cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi mirror::Object* obj = m->GetObject<kWithoutReadBarrier>(); 1152590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // The object of a monitor can be null if we have deflated it. 115397509954404d031594b2ecbda607314d169d512eMathieu Chartier mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr; 11546aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier if (new_obj == nullptr) { 11556aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " 11564cba0d979a11f955e6ec3c0f1bf61478af7aa810Hiroshi Yamauchi << obj; 115774240819ae09e29b2753ef38f4eb4be1c2762e2eAndreas Gampe MonitorPool::ReleaseMonitor(self, m); 1158c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes it = list_.erase(it); 1159c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } else { 11606aa3df965395566ed6a4fec4af37c2b7577992e9Mathieu Chartier m->SetObject(new_obj); 1161c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes ++it; 1162c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } 1163c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } 1164c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 1165c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 116697509954404d031594b2ecbda607314d169d512eMathieu Chartierclass MonitorDeflateVisitor : public IsMarkedVisitor { 116797509954404d031594b2ecbda607314d169d512eMathieu Chartier public: 116897509954404d031594b2ecbda607314d169d512eMathieu Chartier MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {} 116997509954404d031594b2ecbda607314d169d512eMathieu Chartier 117097509954404d031594b2ecbda607314d169d512eMathieu Chartier virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE 117190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier SHARED_REQUIRES(Locks::mutator_lock_) { 117297509954404d031594b2ecbda607314d169d512eMathieu Chartier if (Monitor::Deflate(self_, object)) { 117397509954404d031594b2ecbda607314d169d512eMathieu Chartier DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked); 117497509954404d031594b2ecbda607314d169d512eMathieu Chartier ++deflate_count_; 117597509954404d031594b2ecbda607314d169d512eMathieu Chartier // If we deflated, return null so that the monitor gets removed from the array. 117697509954404d031594b2ecbda607314d169d512eMathieu Chartier return nullptr; 117797509954404d031594b2ecbda607314d169d512eMathieu Chartier } 117897509954404d031594b2ecbda607314d169d512eMathieu Chartier return object; // Monitor was not deflated. 1179440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier } 118097509954404d031594b2ecbda607314d169d512eMathieu Chartier 118197509954404d031594b2ecbda607314d169d512eMathieu Chartier Thread* const self_; 118297509954404d031594b2ecbda607314d169d512eMathieu Chartier size_t deflate_count_; 118397509954404d031594b2ecbda607314d169d512eMathieu Chartier}; 1184440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier 118548ab687d1f864fec93c2682de6fdc44ab784e2f8Mathieu Chartiersize_t MonitorList::DeflateMonitors() { 118697509954404d031594b2ecbda607314d169d512eMathieu Chartier MonitorDeflateVisitor visitor; 118797509954404d031594b2ecbda607314d169d512eMathieu Chartier Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_); 118897509954404d031594b2ecbda607314d169d512eMathieu Chartier SweepMonitorList(&visitor); 118997509954404d031594b2ecbda607314d169d512eMathieu Chartier return visitor.deflate_count_; 1190440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier} 1191440e4ceb310349ee8eb569495bc04d3d7fbe71cbMathieu Chartier 11922cebb24bfc3247d3e9be138a3350106737455918Mathieu ChartierMonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) { 11934d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier DCHECK(obj != nullptr); 11944d7f61d44a732cfbc8573e5d93364983fd746888Mathieu Chartier LockWord lock_word = obj->GetLockWord(true); 1195d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers switch (lock_word.GetState()) { 1196d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kUnlocked: 1197ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier // Fall-through. 1198590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier case LockWord::kForwardingAddress: 1199590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier // Fall-through. 1200ad2541a59c00c2c69e8973088891a2b5257c9780Mathieu Chartier case LockWord::kHashCode: 1201d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers break; 1202d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kThinLocked: 1203d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); 1204d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers entry_count_ = 1 + lock_word.ThinLockCount(); 1205d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers // Thin locks have no waiters. 1206d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers break; 1207d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers case LockWord::kFatLocked: { 1208d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers Monitor* mon = lock_word.FatLockMonitor(); 1209d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers owner_ = mon->owner_; 1210d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers entry_count_ = 1 + mon->lock_count_; 12112cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) { 1212d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers waiters_.push_back(waiter); 1213d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers } 1214d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers break; 1215f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } 1216f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } 1217f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes} 1218f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes 12195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} // namespace art 1220