Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 17 | #include "monitor.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 21 | #include "art_method-inl.h" |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 22 | #include "base/mutex.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 23 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 24 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/time_utils.h" |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 26 | #include "class_linker.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 27 | #include "dex_file-inl.h" |
Sebastien Hertz | 0f7c933 | 2015-11-05 15:57:30 +0100 | [diff] [blame] | 28 | #include "dex_instruction-inl.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 29 | #include "lock_word-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | #include "mirror/class-inl.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 31 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "mirror/object_array-inl.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 33 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 34 | #include "thread.h" |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 35 | #include "thread_list.h" |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 36 | #include "verifier/method_verifier.h" |
Elliott Hughes | 044288f | 2012-06-25 14:46:39 -0700 | [diff] [blame] | 37 | #include "well_known_classes.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Mathieu Chartier | b9001ab | 2014-10-03 13:28:46 -0700 | [diff] [blame] | 41 | static constexpr uint64_t kLongWaitMs = 100; |
| 42 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 43 | /* |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 44 | * Every Object has a monitor associated with it, but not every Object is actually locked. Even |
| 45 | * the ones that are locked do not need a full-fledged monitor until a) there is actual contention |
| 46 | * or b) wait() is called on the Object. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 47 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 48 | * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s |
| 49 | * "Thin locks: featherweight synchronization for Java" (ACM 1998). Things are even easier for us, |
| 50 | * though, because we have a full 32 bits to work with. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 51 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 52 | * The two states of an Object's lock are referred to as "thin" and "fat". A lock may transition |
| 53 | * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once |
| 54 | * a lock has been inflated it remains in the "fat" state indefinitely. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 55 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 56 | * The lock value itself is stored in mirror::Object::monitor_ and the representation is described |
| 57 | * in the LockWord value type. |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 58 | * |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 59 | * Monitors provide: |
| 60 | * - mutually exclusive access to resources |
| 61 | * - a way for multiple threads to wait for notification |
| 62 | * |
| 63 | * In effect, they fill the role of both mutexes and condition variables. |
| 64 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 65 | * Only one thread can own the monitor at any time. There may be several threads waiting on it |
| 66 | * (the wait call unlocks it). One or more waiting threads may be getting interrupted or notified |
| 67 | * at any given time. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 68 | */ |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 69 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 70 | uint32_t Monitor::lock_profiling_threshold_ = 0; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 71 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 72 | void Monitor::Init(uint32_t lock_profiling_threshold) { |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 73 | lock_profiling_threshold_ = lock_profiling_threshold; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 76 | Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 77 | : monitor_lock_("a monitor lock", kMonitorLock), |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 78 | monitor_contenders_("monitor contenders", monitor_lock_), |
Mathieu Chartier | 46bc778 | 2013-11-12 17:03:02 -0800 | [diff] [blame] | 79 | num_waiters_(0), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 80 | owner_(owner), |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 81 | lock_count_(0), |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 82 | obj_(GcRoot<mirror::Object>(obj)), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 83 | wait_set_(nullptr), |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 84 | hash_code_(hash_code), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 85 | locking_method_(nullptr), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 86 | locking_dex_pc_(0), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 87 | monitor_id_(MonitorPool::ComputeMonitorId(this, self)) { |
| 88 | #ifdef __LP64__ |
| 89 | DCHECK(false) << "Should not be reached in 64b"; |
| 90 | next_free_ = nullptr; |
| 91 | #endif |
| 92 | // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race |
| 93 | // with the owner unlocking the thin-lock. |
| 94 | CHECK(owner == nullptr || owner == self || owner->IsSuspended()); |
| 95 | // The identity hash code is set for the life time of the monitor. |
| 96 | } |
| 97 | |
| 98 | Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code, |
| 99 | MonitorId id) |
| 100 | : monitor_lock_("a monitor lock", kMonitorLock), |
| 101 | monitor_contenders_("monitor contenders", monitor_lock_), |
| 102 | num_waiters_(0), |
| 103 | owner_(owner), |
| 104 | lock_count_(0), |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 105 | obj_(GcRoot<mirror::Object>(obj)), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 106 | wait_set_(nullptr), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 107 | hash_code_(hash_code), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 108 | locking_method_(nullptr), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 109 | locking_dex_pc_(0), |
| 110 | monitor_id_(id) { |
| 111 | #ifdef __LP64__ |
| 112 | next_free_ = nullptr; |
| 113 | #endif |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 114 | // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race |
| 115 | // with the owner unlocking the thin-lock. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 116 | CHECK(owner == nullptr || owner == self || owner->IsSuspended()); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 117 | // The identity hash code is set for the life time of the monitor. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 120 | int32_t Monitor::GetHashCode() { |
| 121 | while (!HasHashCode()) { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 122 | if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) { |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 123 | break; |
| 124 | } |
| 125 | } |
| 126 | DCHECK(HasHashCode()); |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 127 | return hash_code_.LoadRelaxed(); |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 130 | bool Monitor::Install(Thread* self) { |
| 131 | MutexLock mu(self, monitor_lock_); // Uncontended mutex acquisition as monitor isn't yet public. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 132 | CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 133 | // Propagate the lock state. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 134 | LockWord lw(GetObject()->GetLockWord(false)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 135 | switch (lw.GetState()) { |
| 136 | case LockWord::kThinLocked: { |
| 137 | CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner()); |
| 138 | lock_count_ = lw.ThinLockCount(); |
| 139 | break; |
| 140 | } |
| 141 | case LockWord::kHashCode: { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 142 | CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode())); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | case LockWord::kFatLocked: { |
| 146 | // The owner_ is suspended but another thread beat us to install a monitor. |
| 147 | return false; |
| 148 | } |
| 149 | case LockWord::kUnlocked: { |
| 150 | LOG(FATAL) << "Inflating unlocked lock word"; |
| 151 | break; |
| 152 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 153 | default: { |
| 154 | LOG(FATAL) << "Invalid monitor state " << lw.GetState(); |
| 155 | return false; |
| 156 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 157 | } |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 158 | LockWord fat(this, lw.ReadBarrierState()); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 159 | // Publish the updated lock word, which may race with other threads. |
Ian Rogers | 228602f | 2014-07-10 02:07:54 -0700 | [diff] [blame] | 160 | bool success = GetObject()->CasLockWordWeakSequentiallyConsistent(lw, fat); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 161 | // Lock profiling. |
Mathieu Chartier | 9728f91 | 2013-10-30 09:45:13 -0700 | [diff] [blame] | 162 | if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) { |
Andreas Gampe | 6ec8ebd | 2014-07-25 13:36:56 -0700 | [diff] [blame] | 163 | // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on |
| 164 | // abort. |
| 165 | locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 166 | } |
| 167 | return success; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | Monitor::~Monitor() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 171 | // Deflated monitors have a null object. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 174 | void Monitor::AppendToWaitSet(Thread* thread) { |
| 175 | DCHECK(owner_ == Thread::Current()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 176 | DCHECK(thread != nullptr); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 177 | DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 178 | if (wait_set_ == nullptr) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 179 | wait_set_ = thread; |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | // push_back. |
| 184 | Thread* t = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 185 | while (t->GetWaitNext() != nullptr) { |
| 186 | t = t->GetWaitNext(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 187 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 188 | t->SetWaitNext(thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 191 | void Monitor::RemoveFromWaitSet(Thread *thread) { |
| 192 | DCHECK(owner_ == Thread::Current()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 193 | DCHECK(thread != nullptr); |
| 194 | if (wait_set_ == nullptr) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | if (wait_set_ == thread) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 198 | wait_set_ = thread->GetWaitNext(); |
| 199 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
| 203 | Thread* t = wait_set_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 204 | while (t->GetWaitNext() != nullptr) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 205 | if (t->GetWaitNext() == thread) { |
| 206 | t->SetWaitNext(thread->GetWaitNext()); |
| 207 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 208 | return; |
| 209 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 210 | t = t->GetWaitNext(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 214 | void Monitor::SetObject(mirror::Object* object) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 215 | obj_ = GcRoot<mirror::Object>(object); |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame^] | 218 | std::string Monitor::PrettyContentionInfo(const std::string& owner_name, |
| 219 | pid_t owner_tid, |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 220 | ArtMethod* owners_method, |
| 221 | uint32_t owners_dex_pc, |
| 222 | size_t num_waiters) { |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 223 | const char* owners_filename; |
Goran Jakovljevic | 49c882b | 2016-04-19 10:27:21 +0200 | [diff] [blame] | 224 | int32_t owners_line_number = 0; |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 225 | if (owners_method != nullptr) { |
| 226 | TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number); |
| 227 | } |
| 228 | std::ostringstream oss; |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame^] | 229 | oss << "monitor contention with owner " << owner_name << " (" << owner_tid << ")"; |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 230 | if (owners_method != nullptr) { |
| 231 | oss << " owner method=" << PrettyMethod(owners_method); |
| 232 | oss << " from " << owners_filename << ":" << owners_line_number; |
| 233 | } |
| 234 | oss << " waiters=" << num_waiters; |
| 235 | return oss.str(); |
| 236 | } |
| 237 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 238 | void Monitor::Lock(Thread* self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 239 | MutexLock mu(self, monitor_lock_); |
| 240 | while (true) { |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 241 | if (owner_ == nullptr) { // Unowned. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 242 | owner_ = self; |
| 243 | CHECK_EQ(lock_count_, 0); |
| 244 | // When debugging, save the current monitor holder for future |
| 245 | // acquisition failures to use in sampled logging. |
| 246 | if (lock_profiling_threshold_ != 0) { |
| 247 | locking_method_ = self->GetCurrentMethod(&locking_dex_pc_); |
| 248 | } |
| 249 | return; |
| 250 | } else if (owner_ == self) { // Recursive. |
| 251 | lock_count_++; |
| 252 | return; |
| 253 | } |
| 254 | // Contended. |
| 255 | const bool log_contention = (lock_profiling_threshold_ != 0); |
Xin Guan | b894a19 | 2014-08-22 11:55:37 -0500 | [diff] [blame] | 256 | uint64_t wait_start_ms = log_contention ? MilliTime() : 0; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 257 | ArtMethod* owners_method = locking_method_; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 258 | uint32_t owners_dex_pc = locking_dex_pc_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 259 | // Do this before releasing the lock so that we don't get deflated. |
Mathieu Chartier | b9001ab | 2014-10-03 13:28:46 -0700 | [diff] [blame] | 260 | size_t num_waiters = num_waiters_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 261 | ++num_waiters_; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 262 | monitor_lock_.Unlock(self); // Let go of locks in order. |
Mathieu Chartier | a6e7f08 | 2014-05-22 14:43:37 -0700 | [diff] [blame] | 263 | self->SetMonitorEnterObject(GetObject()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 264 | { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 265 | uint32_t original_owner_thread_id = 0u; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 266 | ScopedThreadStateChange tsc(self, kBlocked); // Change to blocked and give up mutator_lock_. |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 267 | { |
| 268 | // Reacquire monitor_lock_ without mutator_lock_ for Wait. |
| 269 | MutexLock mu2(self, monitor_lock_); |
| 270 | if (owner_ != nullptr) { // Did the owner_ give the lock up? |
| 271 | original_owner_thread_id = owner_->GetThreadId(); |
| 272 | if (ATRACE_ENABLED()) { |
| 273 | std::ostringstream oss; |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame^] | 274 | std::string name; |
| 275 | owner_->GetThreadName(name); |
| 276 | oss << PrettyContentionInfo(name, |
| 277 | owner_->GetTid(), |
| 278 | owners_method, |
| 279 | owners_dex_pc, |
| 280 | num_waiters); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 281 | // Add info for contending thread. |
| 282 | uint32_t pc; |
| 283 | ArtMethod* m = self->GetCurrentMethod(&pc); |
| 284 | const char* filename; |
| 285 | int32_t line_number; |
| 286 | TranslateLocation(m, pc, &filename, &line_number); |
| 287 | oss << " blocking from " << (filename != nullptr ? filename : "null") |
| 288 | << ":" << line_number; |
| 289 | ATRACE_BEGIN(oss.str().c_str()); |
| 290 | } |
| 291 | monitor_contenders_.Wait(self); // Still contended so wait. |
Mathieu Chartier | f0dc8b5 | 2014-12-17 10:13:30 -0800 | [diff] [blame] | 292 | } |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 293 | } |
| 294 | if (original_owner_thread_id != 0u) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 295 | // Woken from contention. |
| 296 | if (log_contention) { |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame^] | 297 | uint32_t original_owner_tid = 0; |
| 298 | std::string original_owner_name; |
| 299 | { |
| 300 | MutexLock mu2(Thread::Current(), *Locks::thread_list_lock_); |
| 301 | // Re-find the owner in case the thread got killed. |
| 302 | Thread* original_owner = Runtime::Current()->GetThreadList()->FindThreadByThreadId( |
| 303 | original_owner_thread_id); |
| 304 | // Do not do any work that requires the mutator lock. |
| 305 | if (original_owner != nullptr) { |
| 306 | original_owner_tid = original_owner->GetTid(); |
| 307 | original_owner->GetThreadName(original_owner_name); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (original_owner_tid != 0u) { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 312 | uint64_t wait_ms = MilliTime() - wait_start_ms; |
| 313 | uint32_t sample_percent; |
| 314 | if (wait_ms >= lock_profiling_threshold_) { |
| 315 | sample_percent = 100; |
| 316 | } else { |
| 317 | sample_percent = 100 * wait_ms / lock_profiling_threshold_; |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 318 | } |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 319 | if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) { |
| 320 | if (wait_ms > kLongWaitMs && owners_method != nullptr) { |
| 321 | // TODO: We should maybe check that original_owner is still a live thread. |
| 322 | LOG(WARNING) << "Long " |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame^] | 323 | << PrettyContentionInfo(original_owner_name, |
| 324 | original_owner_tid, |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 325 | owners_method, |
| 326 | owners_dex_pc, |
| 327 | num_waiters) |
| 328 | << " for " << PrettyDuration(MsToNs(wait_ms)); |
| 329 | } |
| 330 | const char* owners_filename; |
| 331 | int32_t owners_line_number; |
| 332 | TranslateLocation(owners_method, |
| 333 | owners_dex_pc, |
| 334 | &owners_filename, |
| 335 | &owners_line_number); |
| 336 | LogContentionEvent(self, |
| 337 | wait_ms, |
| 338 | sample_percent, |
| 339 | owners_filename, |
| 340 | owners_line_number); |
| 341 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
Mathieu Chartier | f0dc8b5 | 2014-12-17 10:13:30 -0800 | [diff] [blame] | 344 | ATRACE_END(); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 345 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 346 | } |
Mathieu Chartier | a6e7f08 | 2014-05-22 14:43:37 -0700 | [diff] [blame] | 347 | self->SetMonitorEnterObject(nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 348 | monitor_lock_.Lock(self); // Reacquire locks in order. |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 349 | --num_waiters_; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 350 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 353 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
| 354 | __attribute__((format(printf, 1, 2))); |
| 355 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 356 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 357 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 358 | va_list args; |
| 359 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 360 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 361 | self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 362 | if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) { |
Brian Carlstrom | 64277f3 | 2012-03-26 23:53:34 -0700 | [diff] [blame] | 363 | std::ostringstream ss; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 364 | self->Dump(ss); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 365 | LOG(Runtime::Current()->IsStarted() ? INFO : ERROR) |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 366 | << self->GetException()->Dump() << "\n" << ss.str(); |
Brian Carlstrom | 64277f3 | 2012-03-26 23:53:34 -0700 | [diff] [blame] | 367 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 368 | va_end(args); |
| 369 | } |
| 370 | |
Elliott Hughes | d423741 | 2012-02-21 11:24:45 -0800 | [diff] [blame] | 371 | static std::string ThreadToString(Thread* thread) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 372 | if (thread == nullptr) { |
| 373 | return "nullptr"; |
Elliott Hughes | d423741 | 2012-02-21 11:24:45 -0800 | [diff] [blame] | 374 | } |
| 375 | std::ostringstream oss; |
| 376 | // TODO: alternatively, we could just return the thread's name. |
| 377 | oss << *thread; |
| 378 | return oss.str(); |
| 379 | } |
| 380 | |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 381 | void Monitor::FailedUnlock(mirror::Object* o, |
| 382 | uint32_t expected_owner_thread_id, |
| 383 | uint32_t found_owner_thread_id, |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 384 | Monitor* monitor) { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 385 | // Acquire thread list lock so threads won't disappear from under us. |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 386 | std::string current_owner_string; |
| 387 | std::string expected_owner_string; |
| 388 | std::string found_owner_string; |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 389 | uint32_t current_owner_thread_id = 0u; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 390 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 391 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 392 | ThreadList* const thread_list = Runtime::Current()->GetThreadList(); |
| 393 | Thread* expected_owner = thread_list->FindThreadByThreadId(expected_owner_thread_id); |
| 394 | Thread* found_owner = thread_list->FindThreadByThreadId(found_owner_thread_id); |
| 395 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 396 | // Re-read owner now that we hold lock. |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 397 | Thread* current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr; |
| 398 | if (current_owner != nullptr) { |
| 399 | current_owner_thread_id = current_owner->GetThreadId(); |
| 400 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 401 | // Get short descriptions of the threads involved. |
| 402 | current_owner_string = ThreadToString(current_owner); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 403 | expected_owner_string = expected_owner != nullptr ? ThreadToString(expected_owner) : "unnamed"; |
| 404 | found_owner_string = found_owner != nullptr ? ThreadToString(found_owner) : "unnamed"; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 405 | } |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 406 | |
| 407 | if (current_owner_thread_id == 0u) { |
| 408 | if (found_owner_thread_id == 0u) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 409 | ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'" |
| 410 | " on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 411 | PrettyTypeOf(o).c_str(), |
| 412 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 413 | } else { |
| 414 | // Race: the original read found an owner but now there is none |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 415 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 416 | " (where now the monitor appears unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 417 | found_owner_string.c_str(), |
| 418 | PrettyTypeOf(o).c_str(), |
| 419 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 420 | } |
| 421 | } else { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 422 | if (found_owner_thread_id == 0u) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 423 | // Race: originally there was no owner, there is now |
| 424 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 425 | " (originally believed to be unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 426 | current_owner_string.c_str(), |
| 427 | PrettyTypeOf(o).c_str(), |
| 428 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 429 | } else { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 430 | if (found_owner_thread_id != current_owner_thread_id) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 431 | // Race: originally found and current owner have changed |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 432 | ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now" |
| 433 | " owned by '%s') on object of type '%s' on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 434 | found_owner_string.c_str(), |
| 435 | current_owner_string.c_str(), |
| 436 | PrettyTypeOf(o).c_str(), |
| 437 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 438 | } else { |
| 439 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 440 | " on thread '%s", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 441 | current_owner_string.c_str(), |
| 442 | PrettyTypeOf(o).c_str(), |
| 443 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 449 | bool Monitor::Unlock(Thread* self) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 450 | DCHECK(self != nullptr); |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame^] | 451 | uint32_t owner_thread_id = 0u; |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 452 | { |
| 453 | MutexLock mu(self, monitor_lock_); |
| 454 | Thread* owner = owner_; |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame^] | 455 | if (owner != nullptr) { |
| 456 | owner_thread_id = owner->GetThreadId(); |
| 457 | } |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 458 | if (owner == self) { |
| 459 | // We own the monitor, so nobody else can be in here. |
| 460 | if (lock_count_ == 0) { |
| 461 | owner_ = nullptr; |
| 462 | locking_method_ = nullptr; |
| 463 | locking_dex_pc_ = 0; |
| 464 | // Wake a contender. |
| 465 | monitor_contenders_.Signal(self); |
| 466 | } else { |
| 467 | --lock_count_; |
| 468 | } |
| 469 | return true; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 470 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 471 | } |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 472 | // We don't own this, so we're not allowed to unlock it. |
| 473 | // The JNI spec says that we should throw IllegalMonitorStateException in this case. |
| 474 | FailedUnlock(GetObject(), self->GetThreadId(), owner_thread_id, this); |
| 475 | return false; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 478 | void Monitor::Wait(Thread* self, int64_t ms, int32_t ns, |
| 479 | bool interruptShouldThrow, ThreadState why) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 480 | DCHECK(self != nullptr); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 481 | DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 482 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 483 | monitor_lock_.Lock(self); |
| 484 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 485 | // Make sure that we hold the lock. |
| 486 | if (owner_ != self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 487 | monitor_lock_.Unlock(self); |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 488 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 489 | return; |
| 490 | } |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 491 | |
Elliott Hughes | df42c48 | 2013-01-09 12:49:02 -0800 | [diff] [blame] | 492 | // We need to turn a zero-length timed wait into a regular wait because |
| 493 | // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait(). |
| 494 | if (why == kTimedWaiting && (ms == 0 && ns == 0)) { |
| 495 | why = kWaiting; |
| 496 | } |
| 497 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 498 | // Enforce the timeout range. |
| 499 | if (ms < 0 || ns < 0 || ns > 999999) { |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 500 | monitor_lock_.Unlock(self); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 501 | self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 502 | "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 506 | /* |
| 507 | * Add ourselves to the set of threads waiting on this monitor, and |
| 508 | * release our hold. We need to let it go even if we're a few levels |
| 509 | * deep in a recursive lock, and we need to restore that later. |
| 510 | * |
| 511 | * We append to the wait set ahead of clearing the count and owner |
| 512 | * fields so the subroutine can check that the calling thread owns |
| 513 | * the monitor. Aside from that, the order of member updates is |
| 514 | * not order sensitive as we hold the pthread mutex. |
| 515 | */ |
| 516 | AppendToWaitSet(self); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 517 | ++num_waiters_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 518 | int prev_lock_count = lock_count_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 519 | lock_count_ = 0; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 520 | owner_ = nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 521 | ArtMethod* saved_method = locking_method_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 522 | locking_method_ = nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 523 | uintptr_t saved_dex_pc = locking_dex_pc_; |
| 524 | locking_dex_pc_ = 0; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 525 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 526 | bool was_interrupted = false; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 527 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 528 | // Update thread state. If the GC wakes up, it'll ignore us, knowing |
| 529 | // that we won't touch any references in this state, and we'll check |
| 530 | // our suspend mode before we transition out. |
| 531 | ScopedThreadSuspension sts(self, why); |
| 532 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 533 | // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 534 | MutexLock mu(self, *self->GetWaitMutex()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 535 | |
| 536 | // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 537 | // non-null a notifying or interrupting thread must signal the thread's wait_cond_ to wake it |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 538 | // up. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 539 | DCHECK(self->GetWaitMonitor() == nullptr); |
| 540 | self->SetWaitMonitor(this); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 541 | |
| 542 | // Release the monitor lock. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 543 | monitor_contenders_.Signal(self); |
| 544 | monitor_lock_.Unlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 545 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 546 | // Handle the case where the thread was interrupted before we called wait(). |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 547 | if (self->IsInterruptedLocked()) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 548 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 549 | } else { |
| 550 | // Wait for a notification or a timeout to occur. |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 551 | if (why == kWaiting) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 552 | self->GetWaitConditionVariable()->Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 553 | } else { |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 554 | DCHECK(why == kTimedWaiting || why == kSleeping) << why; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 555 | self->GetWaitConditionVariable()->TimedWait(self, ms, ns); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 556 | } |
Hans Boehm | 328c5dc | 2015-11-11 16:13:57 -0800 | [diff] [blame] | 557 | was_interrupted = self->IsInterruptedLocked(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 558 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 561 | { |
| 562 | // We reset the thread's wait_monitor_ field after transitioning back to runnable so |
| 563 | // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging |
| 564 | // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads |
| 565 | // are waiting on "null".) |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 566 | MutexLock mu(self, *self->GetWaitMutex()); |
| 567 | DCHECK(self->GetWaitMonitor() != nullptr); |
| 568 | self->SetWaitMonitor(nullptr); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Mathieu Chartier | daed5d8 | 2016-03-10 10:49:35 -0800 | [diff] [blame] | 571 | // Allocate the interrupted exception not holding the monitor lock since it may cause a GC. |
| 572 | // If the GC requires acquiring the monitor for enqueuing cleared references, this would |
| 573 | // cause a deadlock if the monitor is held. |
| 574 | if (was_interrupted && interruptShouldThrow) { |
| 575 | /* |
| 576 | * We were interrupted while waiting, or somebody interrupted an |
| 577 | * un-interruptible thread earlier and we're bailing out immediately. |
| 578 | * |
| 579 | * The doc sayeth: "The interrupted status of the current thread is |
| 580 | * cleared when this exception is thrown." |
| 581 | */ |
| 582 | { |
| 583 | MutexLock mu(self, *self->GetWaitMutex()); |
| 584 | self->SetInterruptedLocked(false); |
| 585 | } |
| 586 | self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr); |
| 587 | } |
| 588 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 589 | // Re-acquire the monitor and lock. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 590 | Lock(self); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 591 | monitor_lock_.Lock(self); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 592 | self->GetWaitMutex()->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 593 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 594 | /* |
| 595 | * We remove our thread from wait set after restoring the count |
| 596 | * and owner fields so the subroutine can check that the calling |
| 597 | * thread owns the monitor. Aside from that, the order of member |
| 598 | * updates is not order sensitive as we hold the pthread mutex. |
| 599 | */ |
| 600 | owner_ = self; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 601 | lock_count_ = prev_lock_count; |
| 602 | locking_method_ = saved_method; |
| 603 | locking_dex_pc_ = saved_dex_pc; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 604 | --num_waiters_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 605 | RemoveFromWaitSet(self); |
| 606 | |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 607 | monitor_lock_.Unlock(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | void Monitor::Notify(Thread* self) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 611 | DCHECK(self != nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 612 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 613 | // Make sure that we hold the lock. |
| 614 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 615 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 616 | return; |
| 617 | } |
| 618 | // Signal the first waiting thread in the wait set. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 619 | while (wait_set_ != nullptr) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 620 | Thread* thread = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 621 | wait_set_ = thread->GetWaitNext(); |
| 622 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 623 | |
| 624 | // Check to see if the thread is still waiting. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 625 | MutexLock wait_mu(self, *thread->GetWaitMutex()); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 626 | if (thread->GetWaitMonitor() != nullptr) { |
| 627 | thread->GetWaitConditionVariable()->Signal(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | void Monitor::NotifyAll(Thread* self) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 634 | DCHECK(self != nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 635 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 636 | // Make sure that we hold the lock. |
| 637 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 638 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | // Signal all threads in the wait set. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 642 | while (wait_set_ != nullptr) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 643 | Thread* thread = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 644 | wait_set_ = thread->GetWaitNext(); |
| 645 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 646 | thread->Notify(); |
| 647 | } |
| 648 | } |
| 649 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 650 | bool Monitor::Deflate(Thread* self, mirror::Object* obj) { |
| 651 | DCHECK(obj != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 652 | // Don't need volatile since we only deflate with mutators suspended. |
| 653 | LockWord lw(obj->GetLockWord(false)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 654 | // If the lock isn't an inflated monitor, then we don't need to deflate anything. |
| 655 | if (lw.GetState() == LockWord::kFatLocked) { |
| 656 | Monitor* monitor = lw.FatLockMonitor(); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 657 | DCHECK(monitor != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 658 | MutexLock mu(self, monitor->monitor_lock_); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 659 | // Can't deflate if we have anybody waiting on the CV. |
| 660 | if (monitor->num_waiters_ > 0) { |
| 661 | return false; |
| 662 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 663 | Thread* owner = monitor->owner_; |
| 664 | if (owner != nullptr) { |
| 665 | // Can't deflate if we are locked and have a hash code. |
| 666 | if (monitor->HasHashCode()) { |
| 667 | return false; |
| 668 | } |
| 669 | // Can't deflate if our lock count is too high. |
| 670 | if (monitor->lock_count_ > LockWord::kThinLockMaxCount) { |
| 671 | return false; |
| 672 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 673 | // Deflate to a thin lock. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 674 | LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_, |
| 675 | lw.ReadBarrierState()); |
| 676 | // Assume no concurrent read barrier state changes as mutators are suspended. |
| 677 | obj->SetLockWord(new_lw, false); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 678 | VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / " |
| 679 | << monitor->lock_count_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 680 | } else if (monitor->HasHashCode()) { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 681 | LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.ReadBarrierState()); |
| 682 | // Assume no concurrent read barrier state changes as mutators are suspended. |
| 683 | obj->SetLockWord(new_lw, false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 684 | VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 685 | } else { |
| 686 | // No lock and no hash, just put an empty lock word inside the object. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 687 | LockWord new_lw = LockWord::FromDefault(lw.ReadBarrierState()); |
| 688 | // Assume no concurrent read barrier state changes as mutators are suspended. |
| 689 | obj->SetLockWord(new_lw, false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 690 | VLOG(monitor) << "Deflated" << obj << " to empty lock word"; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 691 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 692 | // The monitor is deflated, mark the object as null so that we know to delete it during the |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 693 | // next GC. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 694 | monitor->obj_ = GcRoot<mirror::Object>(nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 695 | } |
| 696 | return true; |
| 697 | } |
| 698 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 699 | void Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 700 | DCHECK(self != nullptr); |
| 701 | DCHECK(obj != nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 702 | // Allocate and acquire a new monitor. |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 703 | Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code); |
| 704 | DCHECK(m != nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 705 | if (m->Install(self)) { |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 706 | if (owner != nullptr) { |
| 707 | VLOG(monitor) << "monitor: thread" << owner->GetThreadId() |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 708 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 709 | } else { |
| 710 | VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 711 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 712 | } |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 713 | Runtime::Current()->GetMonitorList()->Add(m); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 714 | CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 715 | } else { |
| 716 | MonitorPool::ReleaseMonitor(self, m); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 717 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 720 | void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 721 | uint32_t hash_code) { |
| 722 | DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked); |
| 723 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 724 | if (owner_thread_id == self->GetThreadId()) { |
| 725 | // We own the monitor, we can easily inflate it. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 726 | Inflate(self, self, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 727 | } else { |
| 728 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 729 | // Suspend the owner, inflate. First change to blocked and give up mutator_lock_. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 730 | self->SetMonitorEnterObject(obj.Get()); |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 731 | bool timed_out; |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 732 | Thread* owner; |
| 733 | { |
| 734 | ScopedThreadSuspension sts(self, kBlocked); |
| 735 | owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out); |
| 736 | } |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 737 | if (owner != nullptr) { |
| 738 | // We succeeded in suspending the thread, check the lock's status didn't change. |
| 739 | lock_word = obj->GetLockWord(true); |
| 740 | if (lock_word.GetState() == LockWord::kThinLocked && |
| 741 | lock_word.ThinLockOwner() == owner_thread_id) { |
| 742 | // Go ahead and inflate the lock. |
| 743 | Inflate(self, owner, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 744 | } |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 745 | thread_list->Resume(owner, false); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 746 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 747 | self->SetMonitorEnterObject(nullptr); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 748 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 751 | // Fool annotalysis into thinking that the lock on obj is acquired. |
| 752 | static mirror::Object* FakeLock(mirror::Object* obj) |
| 753 | EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 754 | return obj; |
| 755 | } |
| 756 | |
| 757 | // Fool annotalysis into thinking that the lock on obj is release. |
| 758 | static mirror::Object* FakeUnlock(mirror::Object* obj) |
| 759 | UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 760 | return obj; |
| 761 | } |
| 762 | |
Mathieu Chartier | e7e8a5f | 2014-02-14 16:59:41 -0800 | [diff] [blame] | 763 | mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 764 | DCHECK(self != nullptr); |
| 765 | DCHECK(obj != nullptr); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 766 | self->AssertThreadSuspensionIsAllowable(); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 767 | obj = FakeLock(obj); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 768 | uint32_t thread_id = self->GetThreadId(); |
| 769 | size_t contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 770 | StackHandleScope<1> hs(self); |
| 771 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 772 | while (true) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 773 | LockWord lock_word = h_obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 774 | switch (lock_word.GetState()) { |
| 775 | case LockWord::kUnlocked: { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 776 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.ReadBarrierState())); |
Ian Rogers | 228602f | 2014-07-10 02:07:54 -0700 | [diff] [blame] | 777 | if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 778 | // CasLockWord enforces more than the acquire ordering we need here. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 779 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 780 | } |
| 781 | continue; // Go again. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 782 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 783 | case LockWord::kThinLocked: { |
| 784 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 785 | if (owner_thread_id == thread_id) { |
| 786 | // We own the lock, increase the recursion count. |
| 787 | uint32_t new_count = lock_word.ThinLockCount() + 1; |
| 788 | if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 789 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count, |
| 790 | lock_word.ReadBarrierState())); |
| 791 | if (!kUseReadBarrier) { |
| 792 | h_obj->SetLockWord(thin_locked, true); |
| 793 | return h_obj.Get(); // Success! |
| 794 | } else { |
| 795 | // Use CAS to preserve the read barrier state. |
| 796 | if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) { |
| 797 | return h_obj.Get(); // Success! |
| 798 | } |
| 799 | } |
| 800 | continue; // Go again. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 801 | } else { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 802 | // We'd overflow the recursion count, so inflate the monitor. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 803 | InflateThinLocked(self, h_obj, lock_word, 0); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 804 | } |
| 805 | } else { |
| 806 | // Contention. |
| 807 | contention_count++; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 808 | Runtime* runtime = Runtime::Current(); |
| 809 | if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 810 | // TODO: Consider switching the thread state to kBlocked when we are yielding. |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 811 | // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the |
| 812 | // parameter you pass in. This can cause thread suspension to take excessively long |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 813 | // and make long pauses. See b/16307460. |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 814 | sched_yield(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 815 | } else { |
| 816 | contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 817 | InflateThinLocked(self, h_obj, lock_word, 0); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 818 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 819 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 820 | continue; // Start from the beginning. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 821 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 822 | case LockWord::kFatLocked: { |
| 823 | Monitor* mon = lock_word.FatLockMonitor(); |
| 824 | mon->Lock(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 825 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 826 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 827 | case LockWord::kHashCode: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 828 | // Inflate with the existing hashcode. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 829 | Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode()); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 830 | continue; // Start from the beginning. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 831 | default: { |
| 832 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 833 | return h_obj.Get(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 834 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 835 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 839 | bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 840 | DCHECK(self != nullptr); |
| 841 | DCHECK(obj != nullptr); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 842 | self->AssertThreadSuspensionIsAllowable(); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 843 | obj = FakeUnlock(obj); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 844 | StackHandleScope<1> hs(self); |
| 845 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 846 | while (true) { |
| 847 | LockWord lock_word = obj->GetLockWord(true); |
| 848 | switch (lock_word.GetState()) { |
| 849 | case LockWord::kHashCode: |
| 850 | // Fall-through. |
| 851 | case LockWord::kUnlocked: |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 852 | FailedUnlock(h_obj.Get(), self->GetThreadId(), 0u, nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 853 | return false; // Failure. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 854 | case LockWord::kThinLocked: { |
| 855 | uint32_t thread_id = self->GetThreadId(); |
| 856 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 857 | if (owner_thread_id != thread_id) { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 858 | FailedUnlock(h_obj.Get(), thread_id, owner_thread_id, nullptr); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 859 | return false; // Failure. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 860 | } else { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 861 | // We own the lock, decrease the recursion count. |
| 862 | LockWord new_lw = LockWord::Default(); |
| 863 | if (lock_word.ThinLockCount() != 0) { |
| 864 | uint32_t new_count = lock_word.ThinLockCount() - 1; |
| 865 | new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.ReadBarrierState()); |
| 866 | } else { |
| 867 | new_lw = LockWord::FromDefault(lock_word.ReadBarrierState()); |
| 868 | } |
| 869 | if (!kUseReadBarrier) { |
| 870 | DCHECK_EQ(new_lw.ReadBarrierState(), 0U); |
| 871 | h_obj->SetLockWord(new_lw, true); |
| 872 | // Success! |
| 873 | return true; |
| 874 | } else { |
| 875 | // Use CAS to preserve the read barrier state. |
| 876 | if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, new_lw)) { |
| 877 | // Success! |
| 878 | return true; |
| 879 | } |
| 880 | } |
| 881 | continue; // Go again. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 882 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 883 | } |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 884 | case LockWord::kFatLocked: { |
| 885 | Monitor* mon = lock_word.FatLockMonitor(); |
| 886 | return mon->Unlock(self); |
| 887 | } |
| 888 | default: { |
| 889 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 890 | return false; |
| 891 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 892 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 893 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 896 | void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns, |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 897 | bool interruptShouldThrow, ThreadState why) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 898 | DCHECK(self != nullptr); |
| 899 | DCHECK(obj != nullptr); |
| 900 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 901 | while (lock_word.GetState() != LockWord::kFatLocked) { |
| 902 | switch (lock_word.GetState()) { |
| 903 | case LockWord::kHashCode: |
| 904 | // Fall-through. |
| 905 | case LockWord::kUnlocked: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 906 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
| 907 | return; // Failure. |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 908 | case LockWord::kThinLocked: { |
| 909 | uint32_t thread_id = self->GetThreadId(); |
| 910 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 911 | if (owner_thread_id != thread_id) { |
| 912 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
| 913 | return; // Failure. |
| 914 | } else { |
| 915 | // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so |
| 916 | // re-load. |
| 917 | Inflate(self, self, obj, 0); |
| 918 | lock_word = obj->GetLockWord(true); |
| 919 | } |
| 920 | break; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 921 | } |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 922 | case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through. |
| 923 | default: { |
| 924 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 925 | return; |
| 926 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 927 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 928 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 929 | Monitor* mon = lock_word.FatLockMonitor(); |
| 930 | mon->Wait(self, ms, ns, interruptShouldThrow, why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Ian Rogers | 13c479e | 2013-10-11 07:59:01 -0700 | [diff] [blame] | 933 | void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 934 | DCHECK(self != nullptr); |
| 935 | DCHECK(obj != nullptr); |
| 936 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 937 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 938 | case LockWord::kHashCode: |
| 939 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 940 | case LockWord::kUnlocked: |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 941 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 942 | return; // Failure. |
| 943 | case LockWord::kThinLocked: { |
| 944 | uint32_t thread_id = self->GetThreadId(); |
| 945 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 946 | if (owner_thread_id != thread_id) { |
| 947 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
| 948 | return; // Failure. |
| 949 | } else { |
| 950 | // We own the lock but there's no Monitor and therefore no waiters. |
| 951 | return; // Success. |
| 952 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 953 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 954 | case LockWord::kFatLocked: { |
| 955 | Monitor* mon = lock_word.FatLockMonitor(); |
| 956 | if (notify_all) { |
| 957 | mon->NotifyAll(self); |
| 958 | } else { |
| 959 | mon->Notify(self); |
| 960 | } |
| 961 | return; // Success. |
| 962 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 963 | default: { |
| 964 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 965 | return; |
| 966 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 970 | uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 971 | DCHECK(obj != nullptr); |
| 972 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 973 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 974 | case LockWord::kHashCode: |
| 975 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 976 | case LockWord::kUnlocked: |
| 977 | return ThreadList::kInvalidThreadId; |
| 978 | case LockWord::kThinLocked: |
| 979 | return lock_word.ThinLockOwner(); |
| 980 | case LockWord::kFatLocked: { |
| 981 | Monitor* mon = lock_word.FatLockMonitor(); |
| 982 | return mon->GetOwnerThreadId(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 983 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 984 | default: { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 985 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 986 | UNREACHABLE(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 987 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 991 | void Monitor::DescribeWait(std::ostream& os, const Thread* thread) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 992 | // Determine the wait message and object we're waiting or blocked upon. |
| 993 | mirror::Object* pretty_object = nullptr; |
| 994 | const char* wait_message = nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 995 | uint32_t lock_owner = ThreadList::kInvalidThreadId; |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 996 | ThreadState state = thread->GetState(); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 997 | if (state == kWaiting || state == kTimedWaiting || state == kSleeping) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 998 | wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on "; |
| 999 | Thread* self = Thread::Current(); |
| 1000 | MutexLock mu(self, *thread->GetWaitMutex()); |
| 1001 | Monitor* monitor = thread->GetWaitMonitor(); |
| 1002 | if (monitor != nullptr) { |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1003 | pretty_object = monitor->GetObject(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1004 | } |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 1005 | } else if (state == kBlocked) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 1006 | wait_message = " - waiting to lock "; |
| 1007 | pretty_object = thread->GetMonitorEnterObject(); |
| 1008 | if (pretty_object != nullptr) { |
| 1009 | lock_owner = pretty_object->GetLockOwnerThreadId(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 1010 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 1011 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 1012 | |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 1013 | if (wait_message != nullptr) { |
| 1014 | if (pretty_object == nullptr) { |
| 1015 | os << wait_message << "an unknown object"; |
| 1016 | } else { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1017 | if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) && |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 1018 | Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) { |
| 1019 | // Getting the identity hashcode here would result in lock inflation and suspension of the |
| 1020 | // current thread, which isn't safe if this is the only runnable thread. |
| 1021 | os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)", |
| 1022 | reinterpret_cast<intptr_t>(pretty_object), |
| 1023 | PrettyTypeOf(pretty_object).c_str()); |
| 1024 | } else { |
| 1025 | // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>) |
Mathieu Chartier | 4936159 | 2015-01-22 16:36:10 -0800 | [diff] [blame] | 1026 | // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread |
| 1027 | // suspension and move pretty_object. |
| 1028 | const std::string pretty_type(PrettyTypeOf(pretty_object)); |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 1029 | os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(), |
Mathieu Chartier | 4936159 | 2015-01-22 16:36:10 -0800 | [diff] [blame] | 1030 | pretty_type.c_str()); |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5 |
| 1034 | if (lock_owner != ThreadList::kInvalidThreadId) { |
| 1035 | os << " held by thread " << lock_owner; |
| 1036 | } |
| 1037 | os << "\n"; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 1038 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1041 | mirror::Object* Monitor::GetContendedMonitor(Thread* thread) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 1042 | // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre |
| 1043 | // definition of contended that includes a monitor a thread is trying to enter... |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1044 | mirror::Object* result = thread->GetMonitorEnterObject(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1045 | if (result == nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1046 | // ...but also a monitor that the thread is waiting on. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1047 | MutexLock mu(Thread::Current(), *thread->GetWaitMutex()); |
| 1048 | Monitor* monitor = thread->GetWaitMonitor(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1049 | if (monitor != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1050 | result = monitor->GetObject(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 1051 | } |
| 1052 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1053 | return result; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1056 | void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*), |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1057 | void* callback_context, bool abort_on_failure) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1058 | ArtMethod* m = stack_visitor->GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1059 | CHECK(m != nullptr); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1060 | |
| 1061 | // Native methods are an easy special case. |
| 1062 | // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. |
| 1063 | if (m->IsNative()) { |
| 1064 | if (m->IsSynchronized()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1065 | mirror::Object* jni_this = |
| 1066 | stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 1067 | callback(jni_this, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1068 | } |
| 1069 | return; |
| 1070 | } |
| 1071 | |
jeffhao | 61f916c | 2012-10-25 17:48:51 -0700 | [diff] [blame] | 1072 | // Proxy methods should not be synchronized. |
| 1073 | if (m->IsProxyMethod()) { |
| 1074 | CHECK(!m->IsSynchronized()); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1078 | // Is there any reason to believe there's any synchronization in this method? |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1079 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1080 | CHECK(code_item != nullptr) << PrettyMethod(m); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1081 | if (code_item->tries_size_ == 0) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1082 | return; // No "tries" implies no synchronization, so no held locks to report. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1085 | // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot |
| 1086 | // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an |
| 1087 | // inconsistent stack anyways. |
| 1088 | uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure); |
| 1089 | if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) { |
| 1090 | LOG(ERROR) << "Could not find dex_pc for " << PrettyMethod(m); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1094 | // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to |
| 1095 | // the locks held in this stack frame. |
| 1096 | std::vector<uint32_t> monitor_enter_dex_pcs; |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1097 | verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs); |
Mathieu Chartier | e6a8eec | 2015-01-06 14:17:57 -0800 | [diff] [blame] | 1098 | for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) { |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1099 | // The verifier works in terms of the dex pcs of the monitor-enter instructions. |
| 1100 | // We want the registers used by those instructions (so we can read the values out of them). |
Sebastien Hertz | 0f7c933 | 2015-11-05 15:57:30 +0100 | [diff] [blame] | 1101 | const Instruction* monitor_enter_instruction = |
| 1102 | Instruction::At(&code_item->insns_[monitor_dex_pc]); |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1103 | |
| 1104 | // Quick sanity check. |
Sebastien Hertz | 0f7c933 | 2015-11-05 15:57:30 +0100 | [diff] [blame] | 1105 | CHECK_EQ(monitor_enter_instruction->Opcode(), Instruction::MONITOR_ENTER) |
| 1106 | << "expected monitor-enter @" << monitor_dex_pc << "; was " |
| 1107 | << reinterpret_cast<const void*>(monitor_enter_instruction); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1108 | |
Sebastien Hertz | 0f7c933 | 2015-11-05 15:57:30 +0100 | [diff] [blame] | 1109 | uint16_t monitor_register = monitor_enter_instruction->VRegA(); |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 1110 | uint32_t value; |
| 1111 | bool success = stack_visitor->GetVReg(m, monitor_register, kReferenceVReg, &value); |
| 1112 | CHECK(success) << "Failed to read v" << monitor_register << " of kind " |
| 1113 | << kReferenceVReg << " in method " << PrettyMethod(m); |
| 1114 | mirror::Object* o = reinterpret_cast<mirror::Object*>(value); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 1115 | callback(o, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1119 | bool Monitor::IsValidLockWord(LockWord lock_word) { |
| 1120 | switch (lock_word.GetState()) { |
| 1121 | case LockWord::kUnlocked: |
| 1122 | // Nothing to check. |
| 1123 | return true; |
| 1124 | case LockWord::kThinLocked: |
| 1125 | // Basic sanity check of owner. |
| 1126 | return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId; |
| 1127 | case LockWord::kFatLocked: { |
| 1128 | // Check the monitor appears in the monitor list. |
| 1129 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1130 | MonitorList* list = Runtime::Current()->GetMonitorList(); |
| 1131 | MutexLock mu(Thread::Current(), list->monitor_list_lock_); |
| 1132 | for (Monitor* list_mon : list->list_) { |
| 1133 | if (mon == list_mon) { |
| 1134 | return true; // Found our monitor. |
| 1135 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1136 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1137 | return false; // Fail - unowned monitor in an object. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1138 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1139 | case LockWord::kHashCode: |
| 1140 | return true; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1141 | default: |
| 1142 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1143 | UNREACHABLE(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1147 | bool Monitor::IsLocked() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1148 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1149 | return owner_ != nullptr; |
| 1150 | } |
| 1151 | |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 1152 | void Monitor::TranslateLocation(ArtMethod* method, |
| 1153 | uint32_t dex_pc, |
| 1154 | const char** source_file, |
| 1155 | int32_t* line_number) { |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1156 | // If method is null, location is unknown |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1157 | if (method == nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1158 | *source_file = ""; |
| 1159 | *line_number = 0; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1160 | return; |
| 1161 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1162 | *source_file = method->GetDeclaringClassSourceFile(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1163 | if (*source_file == nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1164 | *source_file = ""; |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 1165 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1166 | *line_number = method->GetLineNumFromDexPC(dex_pc); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | uint32_t Monitor::GetOwnerThreadId() { |
| 1170 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1171 | Thread* owner = owner_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1172 | if (owner != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1173 | return owner->GetThreadId(); |
| 1174 | } else { |
| 1175 | return ThreadList::kInvalidThreadId; |
| 1176 | } |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1179 | MonitorList::MonitorList() |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1180 | : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock), |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1181 | monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | MonitorList::~MonitorList() { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1185 | Thread* self = Thread::Current(); |
| 1186 | MutexLock mu(self, monitor_list_lock_); |
| 1187 | // Release all monitors to the pool. |
| 1188 | // TODO: Is it an invariant that *all* open monitors are in the list? Then we could |
| 1189 | // clear faster in the pool. |
| 1190 | MonitorPool::ReleaseMonitors(self, &list_); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1193 | void MonitorList::DisallowNewMonitors() { |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 1194 | CHECK(!kUseReadBarrier); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1195 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1196 | allow_new_monitors_ = false; |
| 1197 | } |
| 1198 | |
| 1199 | void MonitorList::AllowNewMonitors() { |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 1200 | CHECK(!kUseReadBarrier); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1201 | Thread* self = Thread::Current(); |
| 1202 | MutexLock mu(self, monitor_list_lock_); |
| 1203 | allow_new_monitors_ = true; |
| 1204 | monitor_add_condition_.Broadcast(self); |
| 1205 | } |
| 1206 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1207 | void MonitorList::BroadcastForNewMonitors() { |
| 1208 | CHECK(kUseReadBarrier); |
| 1209 | Thread* self = Thread::Current(); |
| 1210 | MutexLock mu(self, monitor_list_lock_); |
| 1211 | monitor_add_condition_.Broadcast(self); |
| 1212 | } |
| 1213 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1214 | void MonitorList::Add(Monitor* m) { |
| 1215 | Thread* self = Thread::Current(); |
| 1216 | MutexLock mu(self, monitor_list_lock_); |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1217 | while (UNLIKELY((!kUseReadBarrier && !allow_new_monitors_) || |
| 1218 | (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) { |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1219 | monitor_add_condition_.WaitHoldingLocks(self); |
| 1220 | } |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1221 | list_.push_front(m); |
| 1222 | } |
| 1223 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1224 | void MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1225 | Thread* self = Thread::Current(); |
| 1226 | MutexLock mu(self, monitor_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1227 | for (auto it = list_.begin(); it != list_.end(); ) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1228 | Monitor* m = *it; |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1229 | // Disable the read barrier in GetObject() as this is called by GC. |
| 1230 | mirror::Object* obj = m->GetObject<kWithoutReadBarrier>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1231 | // The object of a monitor can be null if we have deflated it. |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1232 | mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr; |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1233 | if (new_obj == nullptr) { |
| 1234 | VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1235 | << obj; |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1236 | MonitorPool::ReleaseMonitor(self, m); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1237 | it = list_.erase(it); |
| 1238 | } else { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1239 | m->SetObject(new_obj); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1240 | ++it; |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1245 | class MonitorDeflateVisitor : public IsMarkedVisitor { |
| 1246 | public: |
| 1247 | MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {} |
| 1248 | |
| 1249 | virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1250 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1251 | if (Monitor::Deflate(self_, object)) { |
| 1252 | DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked); |
| 1253 | ++deflate_count_; |
| 1254 | // If we deflated, return null so that the monitor gets removed from the array. |
| 1255 | return nullptr; |
| 1256 | } |
| 1257 | return object; // Monitor was not deflated. |
| 1258 | } |
| 1259 | |
| 1260 | Thread* const self_; |
| 1261 | size_t deflate_count_; |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1262 | }; |
| 1263 | |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1264 | size_t MonitorList::DeflateMonitors() { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1265 | MonitorDeflateVisitor visitor; |
| 1266 | Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_); |
| 1267 | SweepMonitorList(&visitor); |
| 1268 | return visitor.deflate_count_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1271 | MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1272 | DCHECK(obj != nullptr); |
| 1273 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1274 | switch (lock_word.GetState()) { |
| 1275 | case LockWord::kUnlocked: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1276 | // Fall-through. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1277 | case LockWord::kForwardingAddress: |
| 1278 | // Fall-through. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1279 | case LockWord::kHashCode: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1280 | break; |
| 1281 | case LockWord::kThinLocked: |
| 1282 | owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
| 1283 | entry_count_ = 1 + lock_word.ThinLockCount(); |
| 1284 | // Thin locks have no waiters. |
| 1285 | break; |
| 1286 | case LockWord::kFatLocked: { |
| 1287 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1288 | owner_ = mon->owner_; |
| 1289 | entry_count_ = 1 + mon->lock_count_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1290 | for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1291 | waiters_.push_back(waiter); |
| 1292 | } |
| 1293 | break; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1298 | } // namespace art |