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 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 17 | #include "monitor-inl.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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 24 | #include "base/logging.h" // For VLOG. |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 25 | #include "base/mutex.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 26 | #include "base/quasi_atomic.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 27 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 28 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 29 | #include "base/time_utils.h" |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 30 | #include "class_linker.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 31 | #include "dex/dex_file-inl.h" |
| 32 | #include "dex/dex_file_types.h" |
| 33 | #include "dex/dex_instruction-inl.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 34 | #include "lock_word-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 35 | #include "mirror/class-inl.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 36 | #include "mirror/object-inl.h" |
Andreas Gampe | 5d08fcc | 2017-06-05 17:56:46 -0700 | [diff] [blame] | 37 | #include "object_callbacks.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 38 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 39 | #include "stack.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 40 | #include "thread.h" |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 41 | #include "thread_list.h" |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 42 | #include "verifier/method_verifier.h" |
Elliott Hughes | 044288f | 2012-06-25 14:46:39 -0700 | [diff] [blame] | 43 | #include "well_known_classes.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 44 | |
| 45 | namespace art { |
| 46 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 47 | using android::base::StringPrintf; |
| 48 | |
Andreas Gampe | 5d68914 | 2017-10-19 13:03:29 -0700 | [diff] [blame] | 49 | static constexpr uint64_t kDebugThresholdFudgeFactor = kIsDebugBuild ? 10 : 1; |
| 50 | static constexpr uint64_t kLongWaitMs = 100 * kDebugThresholdFudgeFactor; |
Mathieu Chartier | b9001ab | 2014-10-03 13:28:46 -0700 | [diff] [blame] | 51 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 52 | /* |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 53 | * Every Object has a monitor associated with it, but not every Object is actually locked. Even |
| 54 | * the ones that are locked do not need a full-fledged monitor until a) there is actual contention |
| 55 | * or b) wait() is called on the Object. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 56 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 57 | * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s |
| 58 | * "Thin locks: featherweight synchronization for Java" (ACM 1998). Things are even easier for us, |
| 59 | * though, because we have a full 32 bits to work with. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 60 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 61 | * The two states of an Object's lock are referred to as "thin" and "fat". A lock may transition |
Daniel Colascione | c3d5b84 | 2018-04-15 10:52:18 -0700 | [diff] [blame] | 62 | * from the "thin" state to the "fat" state and this transition is referred to as inflation. We |
| 63 | * deflate locks from time to time as part of heap trimming. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 64 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 65 | * The lock value itself is stored in mirror::Object::monitor_ and the representation is described |
| 66 | * in the LockWord value type. |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 67 | * |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 68 | * Monitors provide: |
| 69 | * - mutually exclusive access to resources |
| 70 | * - a way for multiple threads to wait for notification |
| 71 | * |
| 72 | * In effect, they fill the role of both mutexes and condition variables. |
| 73 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 74 | * Only one thread can own the monitor at any time. There may be several threads waiting on it |
| 75 | * (the wait call unlocks it). One or more waiting threads may be getting interrupted or notified |
| 76 | * at any given time. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 77 | */ |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 78 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 79 | uint32_t Monitor::lock_profiling_threshold_ = 0; |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 80 | uint32_t Monitor::stack_dump_lock_profiling_threshold_ = 0; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 81 | |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 82 | void Monitor::Init(uint32_t lock_profiling_threshold, |
| 83 | uint32_t stack_dump_lock_profiling_threshold) { |
Andreas Gampe | 5d68914 | 2017-10-19 13:03:29 -0700 | [diff] [blame] | 84 | // It isn't great to always include the debug build fudge factor for command- |
| 85 | // line driven arguments, but it's easier to adjust here than in the build. |
| 86 | lock_profiling_threshold_ = |
| 87 | lock_profiling_threshold * kDebugThresholdFudgeFactor; |
| 88 | stack_dump_lock_profiling_threshold_ = |
| 89 | stack_dump_lock_profiling_threshold * kDebugThresholdFudgeFactor; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 92 | Monitor::Monitor(Thread* self, Thread* owner, ObjPtr<mirror::Object> obj, int32_t hash_code) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 93 | : monitor_lock_("a monitor lock", kMonitorLock), |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 94 | monitor_contenders_("monitor contenders", monitor_lock_), |
Mathieu Chartier | 46bc778 | 2013-11-12 17:03:02 -0800 | [diff] [blame] | 95 | num_waiters_(0), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 96 | owner_(owner), |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 97 | lock_count_(0), |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 98 | obj_(GcRoot<mirror::Object>(obj)), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 99 | wait_set_(nullptr), |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 100 | wake_set_(nullptr), |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 101 | hash_code_(hash_code), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 102 | locking_method_(nullptr), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 103 | locking_dex_pc_(0), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 104 | monitor_id_(MonitorPool::ComputeMonitorId(this, self)) { |
| 105 | #ifdef __LP64__ |
| 106 | DCHECK(false) << "Should not be reached in 64b"; |
| 107 | next_free_ = nullptr; |
| 108 | #endif |
| 109 | // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race |
| 110 | // with the owner unlocking the thin-lock. |
| 111 | CHECK(owner == nullptr || owner == self || owner->IsSuspended()); |
| 112 | // The identity hash code is set for the life time of the monitor. |
| 113 | } |
| 114 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 115 | Monitor::Monitor(Thread* self, |
| 116 | Thread* owner, |
| 117 | ObjPtr<mirror::Object> obj, |
| 118 | int32_t hash_code, |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 119 | MonitorId id) |
| 120 | : monitor_lock_("a monitor lock", kMonitorLock), |
| 121 | monitor_contenders_("monitor contenders", monitor_lock_), |
| 122 | num_waiters_(0), |
| 123 | owner_(owner), |
| 124 | lock_count_(0), |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 125 | obj_(GcRoot<mirror::Object>(obj)), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 126 | wait_set_(nullptr), |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 127 | wake_set_(nullptr), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 128 | hash_code_(hash_code), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 129 | locking_method_(nullptr), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 130 | locking_dex_pc_(0), |
| 131 | monitor_id_(id) { |
| 132 | #ifdef __LP64__ |
| 133 | next_free_ = nullptr; |
| 134 | #endif |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 135 | // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race |
| 136 | // with the owner unlocking the thin-lock. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 137 | CHECK(owner == nullptr || owner == self || owner->IsSuspended()); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 138 | // 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] | 139 | } |
| 140 | |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 141 | int32_t Monitor::GetHashCode() { |
Mathieu Chartier | 8bb3c68 | 2018-06-18 12:53:10 -0700 | [diff] [blame] | 142 | int32_t hc = hash_code_.load(std::memory_order_relaxed); |
| 143 | if (!HasHashCode()) { |
| 144 | // Use a strong CAS to prevent spurious failures since these can make the boot image |
| 145 | // non-deterministic. |
| 146 | hash_code_.CompareAndSetStrongRelaxed(0, mirror::Object::GenerateIdentityHashCode()); |
| 147 | hc = hash_code_.load(std::memory_order_relaxed); |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 148 | } |
| 149 | DCHECK(HasHashCode()); |
Mathieu Chartier | 8bb3c68 | 2018-06-18 12:53:10 -0700 | [diff] [blame] | 150 | return hc; |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 153 | bool Monitor::Install(Thread* self) { |
| 154 | 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] | 155 | CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 156 | // Propagate the lock state. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 157 | LockWord lw(GetObject()->GetLockWord(false)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 158 | switch (lw.GetState()) { |
| 159 | case LockWord::kThinLocked: { |
| 160 | CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner()); |
| 161 | lock_count_ = lw.ThinLockCount(); |
| 162 | break; |
| 163 | } |
| 164 | case LockWord::kHashCode: { |
Orion Hodson | 88591fe | 2018-03-06 13:35:43 +0000 | [diff] [blame] | 165 | CHECK_EQ(hash_code_.load(std::memory_order_relaxed), static_cast<int32_t>(lw.GetHashCode())); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | case LockWord::kFatLocked: { |
| 169 | // The owner_ is suspended but another thread beat us to install a monitor. |
| 170 | return false; |
| 171 | } |
| 172 | case LockWord::kUnlocked: { |
| 173 | LOG(FATAL) << "Inflating unlocked lock word"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 174 | UNREACHABLE(); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 175 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 176 | default: { |
| 177 | LOG(FATAL) << "Invalid monitor state " << lw.GetState(); |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 178 | UNREACHABLE(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 179 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 180 | } |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 181 | LockWord fat(this, lw.GCState()); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 182 | // Publish the updated lock word, which may race with other threads. |
Mathieu Chartier | 42c2e50 | 2018-06-19 12:30:56 -0700 | [diff] [blame] | 183 | bool success = GetObject()->CasLockWord(lw, fat, CASMode::kWeak, std::memory_order_release); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 184 | // Lock profiling. |
Mathieu Chartier | 9728f91 | 2013-10-30 09:45:13 -0700 | [diff] [blame] | 185 | if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) { |
Andreas Gampe | 6ec8ebd | 2014-07-25 13:36:56 -0700 | [diff] [blame] | 186 | // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on |
| 187 | // abort. |
| 188 | locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false); |
Andreas Gampe | 5a38727 | 2017-11-06 19:47:16 -0800 | [diff] [blame] | 189 | if (locking_method_ != nullptr && UNLIKELY(locking_method_->IsProxyMethod())) { |
| 190 | // Grab another frame. Proxy methods are not helpful for lock profiling. This should be rare |
| 191 | // enough that it's OK to walk the stack twice. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 192 | struct NextMethodVisitor final : public StackVisitor { |
Andreas Gampe | 5a38727 | 2017-11-06 19:47:16 -0800 | [diff] [blame] | 193 | explicit NextMethodVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) |
| 194 | : StackVisitor(thread, |
| 195 | nullptr, |
| 196 | StackVisitor::StackWalkKind::kIncludeInlinedFrames, |
| 197 | false), |
| 198 | count_(0), |
| 199 | method_(nullptr), |
| 200 | dex_pc_(0) {} |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 201 | bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 5a38727 | 2017-11-06 19:47:16 -0800 | [diff] [blame] | 202 | ArtMethod* m = GetMethod(); |
| 203 | if (m->IsRuntimeMethod()) { |
| 204 | // Continue if this is a runtime method. |
| 205 | return true; |
| 206 | } |
| 207 | count_++; |
| 208 | if (count_ == 2u) { |
| 209 | method_ = m; |
| 210 | dex_pc_ = GetDexPc(false); |
| 211 | return false; |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | size_t count_; |
| 216 | ArtMethod* method_; |
| 217 | uint32_t dex_pc_; |
| 218 | }; |
| 219 | NextMethodVisitor nmv(owner_); |
| 220 | nmv.WalkStack(); |
| 221 | locking_method_ = nmv.method_; |
| 222 | locking_dex_pc_ = nmv.dex_pc_; |
| 223 | } |
| 224 | DCHECK(locking_method_ == nullptr || !locking_method_->IsProxyMethod()); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 225 | } |
| 226 | return success; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | Monitor::~Monitor() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 230 | // Deflated monitors have a null object. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 233 | void Monitor::AppendToWaitSet(Thread* thread) { |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 234 | // Not checking that the owner is equal to this thread, since we've released |
| 235 | // the monitor by the time this method is called. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 236 | DCHECK(thread != nullptr); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 237 | DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 238 | if (wait_set_ == nullptr) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 239 | wait_set_ = thread; |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | // push_back. |
| 244 | Thread* t = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 245 | while (t->GetWaitNext() != nullptr) { |
| 246 | t = t->GetWaitNext(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 247 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 248 | t->SetWaitNext(thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 251 | void Monitor::RemoveFromWaitSet(Thread *thread) { |
| 252 | DCHECK(owner_ == Thread::Current()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 253 | DCHECK(thread != nullptr); |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 254 | auto remove = [&](Thread*& set){ |
| 255 | if (set != nullptr) { |
| 256 | if (set == thread) { |
| 257 | set = thread->GetWaitNext(); |
| 258 | thread->SetWaitNext(nullptr); |
| 259 | return true; |
| 260 | } |
| 261 | Thread* t = set; |
| 262 | while (t->GetWaitNext() != nullptr) { |
| 263 | if (t->GetWaitNext() == thread) { |
| 264 | t->SetWaitNext(thread->GetWaitNext()); |
| 265 | thread->SetWaitNext(nullptr); |
| 266 | return true; |
| 267 | } |
| 268 | t = t->GetWaitNext(); |
| 269 | } |
Roland Levillain | 9cec965 | 2018-11-06 10:50:20 +0000 | [diff] [blame] | 270 | } |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 271 | return false; |
| 272 | }; |
| 273 | if (remove(wait_set_)) { |
| 274 | return; |
Roland Levillain | 9cec965 | 2018-11-06 10:50:20 +0000 | [diff] [blame] | 275 | } |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 276 | remove(wake_set_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 279 | void Monitor::SetObject(ObjPtr<mirror::Object> object) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 280 | obj_ = GcRoot<mirror::Object>(object); |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 283 | // This function is inlined and just helps to not have the VLOG and ATRACE check at all the |
| 284 | // potential tracing points. |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 285 | void Monitor::AtraceMonitorLock(Thread* self, ObjPtr<mirror::Object> obj, bool is_wait) { |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 286 | if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging) && ATraceEnabled())) { |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 287 | AtraceMonitorLockImpl(self, obj, is_wait); |
| 288 | } |
| 289 | } |
| 290 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 291 | void Monitor::AtraceMonitorLockImpl(Thread* self, ObjPtr<mirror::Object> obj, bool is_wait) { |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 292 | // Wait() requires a deeper call stack to be useful. Otherwise you'll see "Waiting at |
| 293 | // Object.java". Assume that we'll wait a nontrivial amount, so it's OK to do a longer |
| 294 | // stack walk than if !is_wait. |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 295 | const size_t wanted_frame_number = is_wait ? 1U : 0U; |
| 296 | |
| 297 | ArtMethod* method = nullptr; |
| 298 | uint32_t dex_pc = 0u; |
| 299 | |
| 300 | size_t current_frame_number = 0u; |
| 301 | StackVisitor::WalkStack( |
| 302 | // Note: Adapted from CurrentMethodVisitor in thread.cc. We must not resolve here. |
| 303 | [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 304 | ArtMethod* m = stack_visitor->GetMethod(); |
| 305 | if (m == nullptr || m->IsRuntimeMethod()) { |
| 306 | // Runtime method, upcall, or resolution issue. Skip. |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | // Is this the requested frame? |
| 311 | if (current_frame_number == wanted_frame_number) { |
| 312 | method = m; |
| 313 | dex_pc = stack_visitor->GetDexPc(false /* abort_on_error*/); |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | // Look for more. |
| 318 | current_frame_number++; |
| 319 | return true; |
| 320 | }, |
| 321 | self, |
| 322 | /* context= */ nullptr, |
| 323 | art::StackVisitor::StackWalkKind::kIncludeInlinedFrames); |
| 324 | |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 325 | const char* prefix = is_wait ? "Waiting on " : "Locking "; |
| 326 | |
| 327 | const char* filename; |
| 328 | int32_t line_number; |
Andreas Gampe | c7d878d | 2018-11-19 18:42:06 +0000 | [diff] [blame] | 329 | TranslateLocation(method, dex_pc, &filename, &line_number); |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 330 | |
| 331 | // It would be nice to have a stable "ID" for the object here. However, the only stable thing |
| 332 | // would be the identity hashcode. But we cannot use IdentityHashcode here: For one, there are |
| 333 | // times when it is unsafe to make that call (see stack dumping for an explanation). More |
| 334 | // importantly, we would have to give up on thin-locking when adding systrace locks, as the |
| 335 | // identity hashcode is stored in the lockword normally (so can't be used with thin-locks). |
| 336 | // |
| 337 | // Because of thin-locks we also cannot use the monitor id (as there is no monitor). Monitor ids |
| 338 | // also do not have to be stable, as the monitor may be deflated. |
| 339 | std::string tmp = StringPrintf("%s %d at %s:%d", |
| 340 | prefix, |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 341 | (obj == nullptr ? -1 : static_cast<int32_t>(reinterpret_cast<uintptr_t>(obj.Ptr()))), |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 342 | (filename != nullptr ? filename : "null"), |
| 343 | line_number); |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 344 | ATraceBegin(tmp.c_str()); |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void Monitor::AtraceMonitorUnlock() { |
| 348 | if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging))) { |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 349 | ATraceEnd(); |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame] | 353 | std::string Monitor::PrettyContentionInfo(const std::string& owner_name, |
| 354 | pid_t owner_tid, |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 355 | ArtMethod* owners_method, |
| 356 | uint32_t owners_dex_pc, |
| 357 | size_t num_waiters) { |
Hiroshi Yamauchi | 71cd68d | 2017-01-25 18:28:12 -0800 | [diff] [blame] | 358 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 359 | const char* owners_filename; |
Goran Jakovljevic | 49c882b | 2016-04-19 10:27:21 +0200 | [diff] [blame] | 360 | int32_t owners_line_number = 0; |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 361 | if (owners_method != nullptr) { |
| 362 | TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number); |
| 363 | } |
| 364 | std::ostringstream oss; |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame] | 365 | oss << "monitor contention with owner " << owner_name << " (" << owner_tid << ")"; |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 366 | if (owners_method != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 367 | oss << " at " << owners_method->PrettyMethod(); |
Mathieu Chartier | 36891fe | 2016-04-28 17:21:08 -0700 | [diff] [blame] | 368 | oss << "(" << owners_filename << ":" << owners_line_number << ")"; |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 369 | } |
| 370 | oss << " waiters=" << num_waiters; |
| 371 | return oss.str(); |
| 372 | } |
| 373 | |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 374 | bool Monitor::TryLockLocked(Thread* self) { |
| 375 | if (owner_ == nullptr) { // Unowned. |
| 376 | owner_ = self; |
| 377 | CHECK_EQ(lock_count_, 0); |
| 378 | // When debugging, save the current monitor holder for future |
| 379 | // acquisition failures to use in sampled logging. |
| 380 | if (lock_profiling_threshold_ != 0) { |
| 381 | locking_method_ = self->GetCurrentMethod(&locking_dex_pc_); |
Andreas Gampe | 5a38727 | 2017-11-06 19:47:16 -0800 | [diff] [blame] | 382 | // We don't expect a proxy method here. |
| 383 | DCHECK(locking_method_ == nullptr || !locking_method_->IsProxyMethod()); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 384 | } |
| 385 | } else if (owner_ == self) { // Recursive. |
| 386 | lock_count_++; |
| 387 | } else { |
| 388 | return false; |
| 389 | } |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 390 | AtraceMonitorLock(self, GetObject(), /* is_wait= */ false); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 391 | return true; |
| 392 | } |
| 393 | |
| 394 | bool Monitor::TryLock(Thread* self) { |
| 395 | MutexLock mu(self, monitor_lock_); |
| 396 | return TryLockLocked(self); |
| 397 | } |
| 398 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 399 | // Asserts that a mutex isn't held when the class comes into and out of scope. |
| 400 | class ScopedAssertNotHeld { |
| 401 | public: |
| 402 | ScopedAssertNotHeld(Thread* self, Mutex& mu) : self_(self), mu_(mu) { |
| 403 | mu_.AssertNotHeld(self_); |
| 404 | } |
| 405 | |
| 406 | ~ScopedAssertNotHeld() { |
| 407 | mu_.AssertNotHeld(self_); |
| 408 | } |
| 409 | |
| 410 | private: |
| 411 | Thread* const self_; |
| 412 | Mutex& mu_; |
| 413 | DISALLOW_COPY_AND_ASSIGN(ScopedAssertNotHeld); |
| 414 | }; |
| 415 | |
| 416 | template <LockReason reason> |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 417 | void Monitor::Lock(Thread* self) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 418 | ScopedAssertNotHeld sanh(self, monitor_lock_); |
| 419 | bool called_monitors_callback = false; |
| 420 | monitor_lock_.Lock(self); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 421 | while (true) { |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 422 | if (TryLockLocked(self)) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 423 | break; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 424 | } |
| 425 | // Contended. |
| 426 | const bool log_contention = (lock_profiling_threshold_ != 0); |
Xin Guan | b894a19 | 2014-08-22 11:55:37 -0500 | [diff] [blame] | 427 | uint64_t wait_start_ms = log_contention ? MilliTime() : 0; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 428 | ArtMethod* owners_method = locking_method_; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 429 | uint32_t owners_dex_pc = locking_dex_pc_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 430 | // 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] | 431 | size_t num_waiters = num_waiters_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 432 | ++num_waiters_; |
Andreas Gampe | 2702d56 | 2017-02-06 09:48:00 -0800 | [diff] [blame] | 433 | |
| 434 | // If systrace logging is enabled, first look at the lock owner. Acquiring the monitor's |
| 435 | // lock and then re-acquiring the mutator lock can deadlock. |
| 436 | bool started_trace = false; |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 437 | if (ATraceEnabled()) { |
Andreas Gampe | 2702d56 | 2017-02-06 09:48:00 -0800 | [diff] [blame] | 438 | if (owner_ != nullptr) { // Did the owner_ give the lock up? |
| 439 | std::ostringstream oss; |
| 440 | std::string name; |
| 441 | owner_->GetThreadName(name); |
| 442 | oss << PrettyContentionInfo(name, |
| 443 | owner_->GetTid(), |
| 444 | owners_method, |
| 445 | owners_dex_pc, |
| 446 | num_waiters); |
| 447 | // Add info for contending thread. |
| 448 | uint32_t pc; |
| 449 | ArtMethod* m = self->GetCurrentMethod(&pc); |
| 450 | const char* filename; |
| 451 | int32_t line_number; |
| 452 | TranslateLocation(m, pc, &filename, &line_number); |
| 453 | oss << " blocking from " |
| 454 | << ArtMethod::PrettyMethod(m) << "(" << (filename != nullptr ? filename : "null") |
| 455 | << ":" << line_number << ")"; |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 456 | ATraceBegin(oss.str().c_str()); |
Andreas Gampe | 2702d56 | 2017-02-06 09:48:00 -0800 | [diff] [blame] | 457 | started_trace = true; |
| 458 | } |
| 459 | } |
| 460 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 461 | monitor_lock_.Unlock(self); // Let go of locks in order. |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 462 | // Call the contended locking cb once and only once. Also only call it if we are locking for |
| 463 | // the first time, not during a Wait wakeup. |
| 464 | if (reason == LockReason::kForLock && !called_monitors_callback) { |
| 465 | called_monitors_callback = true; |
| 466 | Runtime::Current()->GetRuntimeCallbacks()->MonitorContendedLocking(this); |
| 467 | } |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 468 | self->SetMonitorEnterObject(GetObject().Ptr()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 469 | { |
Hiroshi Yamauchi | 71cd68d | 2017-01-25 18:28:12 -0800 | [diff] [blame] | 470 | ScopedThreadSuspension tsc(self, kBlocked); // Change to blocked and give up mutator_lock_. |
Andreas Gampe | 2702d56 | 2017-02-06 09:48:00 -0800 | [diff] [blame] | 471 | uint32_t original_owner_thread_id = 0u; |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 472 | { |
| 473 | // Reacquire monitor_lock_ without mutator_lock_ for Wait. |
| 474 | MutexLock mu2(self, monitor_lock_); |
| 475 | if (owner_ != nullptr) { // Did the owner_ give the lock up? |
| 476 | original_owner_thread_id = owner_->GetThreadId(); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 477 | monitor_contenders_.Wait(self); // Still contended so wait. |
Mathieu Chartier | f0dc8b5 | 2014-12-17 10:13:30 -0800 | [diff] [blame] | 478 | } |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 479 | } |
| 480 | if (original_owner_thread_id != 0u) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 481 | // Woken from contention. |
| 482 | if (log_contention) { |
Andreas Gampe | 111b109 | 2017-06-22 20:28:23 -0700 | [diff] [blame] | 483 | uint64_t wait_ms = MilliTime() - wait_start_ms; |
| 484 | uint32_t sample_percent; |
| 485 | if (wait_ms >= lock_profiling_threshold_) { |
| 486 | sample_percent = 100; |
| 487 | } else { |
| 488 | sample_percent = 100 * wait_ms / lock_profiling_threshold_; |
| 489 | } |
| 490 | if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) { |
| 491 | // Reacquire mutator_lock_ for logging. |
| 492 | ScopedObjectAccess soa(self); |
Andreas Gampe | 111b109 | 2017-06-22 20:28:23 -0700 | [diff] [blame] | 493 | |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 494 | bool owner_alive = false; |
| 495 | pid_t original_owner_tid = 0; |
| 496 | std::string original_owner_name; |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame] | 497 | |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 498 | const bool should_dump_stacks = stack_dump_lock_profiling_threshold_ > 0 && |
| 499 | wait_ms > stack_dump_lock_profiling_threshold_; |
| 500 | std::string owner_stack_dump; |
Andreas Gampe | 111b109 | 2017-06-22 20:28:23 -0700 | [diff] [blame] | 501 | |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 502 | // Acquire thread-list lock to find thread and keep it from dying until we've got all |
| 503 | // the info we need. |
| 504 | { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 505 | Locks::thread_list_lock_->ExclusiveLock(Thread::Current()); |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 506 | |
| 507 | // Re-find the owner in case the thread got killed. |
| 508 | Thread* original_owner = Runtime::Current()->GetThreadList()->FindThreadByThreadId( |
| 509 | original_owner_thread_id); |
| 510 | |
| 511 | if (original_owner != nullptr) { |
| 512 | owner_alive = true; |
| 513 | original_owner_tid = original_owner->GetTid(); |
| 514 | original_owner->GetThreadName(original_owner_name); |
| 515 | |
| 516 | if (should_dump_stacks) { |
| 517 | // Very long contention. Dump stacks. |
| 518 | struct CollectStackTrace : public Closure { |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 519 | void Run(art::Thread* thread) override |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 520 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 521 | thread->DumpJavaStack(oss); |
| 522 | } |
| 523 | |
| 524 | std::ostringstream oss; |
| 525 | }; |
| 526 | CollectStackTrace owner_trace; |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 527 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its |
| 528 | // execution. |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 529 | original_owner->RequestSynchronousCheckpoint(&owner_trace); |
| 530 | owner_stack_dump = owner_trace.oss.str(); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 531 | } else { |
| 532 | Locks::thread_list_lock_->ExclusiveUnlock(Thread::Current()); |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 533 | } |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 534 | } else { |
| 535 | Locks::thread_list_lock_->ExclusiveUnlock(Thread::Current()); |
Andreas Gampe | d0210e5 | 2017-06-23 13:38:09 -0700 | [diff] [blame] | 536 | } |
| 537 | // This is all the data we need. Now drop the thread-list lock, it's OK for the |
| 538 | // owner to go away now. |
| 539 | } |
| 540 | |
| 541 | // If we found the owner (and thus have owner data), go and log now. |
| 542 | if (owner_alive) { |
| 543 | // Give the detailed traces for really long contention. |
| 544 | if (should_dump_stacks) { |
| 545 | // This must be here (and not above) because we cannot hold the thread-list lock |
| 546 | // while running the checkpoint. |
| 547 | std::ostringstream self_trace_oss; |
| 548 | self->DumpJavaStack(self_trace_oss); |
| 549 | |
| 550 | uint32_t pc; |
| 551 | ArtMethod* m = self->GetCurrentMethod(&pc); |
| 552 | |
| 553 | LOG(WARNING) << "Long " |
| 554 | << PrettyContentionInfo(original_owner_name, |
| 555 | original_owner_tid, |
| 556 | owners_method, |
| 557 | owners_dex_pc, |
| 558 | num_waiters) |
| 559 | << " in " << ArtMethod::PrettyMethod(m) << " for " |
| 560 | << PrettyDuration(MsToNs(wait_ms)) << "\n" |
| 561 | << "Current owner stack:\n" << owner_stack_dump |
| 562 | << "Contender stack:\n" << self_trace_oss.str(); |
| 563 | } else if (wait_ms > kLongWaitMs && owners_method != nullptr) { |
Mathieu Chartier | 36891fe | 2016-04-28 17:21:08 -0700 | [diff] [blame] | 564 | uint32_t pc; |
| 565 | ArtMethod* m = self->GetCurrentMethod(&pc); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 566 | // TODO: We should maybe check that original_owner is still a live thread. |
| 567 | LOG(WARNING) << "Long " |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame] | 568 | << PrettyContentionInfo(original_owner_name, |
| 569 | original_owner_tid, |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 570 | owners_method, |
| 571 | owners_dex_pc, |
| 572 | num_waiters) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 573 | << " in " << ArtMethod::PrettyMethod(m) << " for " |
| 574 | << PrettyDuration(MsToNs(wait_ms)); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 575 | } |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 576 | LogContentionEvent(self, |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 577 | wait_ms, |
| 578 | sample_percent, |
| 579 | owners_method, |
| 580 | owners_dex_pc); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 581 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 582 | } |
| 583 | } |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 584 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 585 | } |
Andreas Gampe | 2702d56 | 2017-02-06 09:48:00 -0800 | [diff] [blame] | 586 | if (started_trace) { |
Orion Hodson | 119733d | 2019-01-30 15:14:41 +0000 | [diff] [blame] | 587 | ATraceEnd(); |
Andreas Gampe | 2702d56 | 2017-02-06 09:48:00 -0800 | [diff] [blame] | 588 | } |
Mathieu Chartier | a6e7f08 | 2014-05-22 14:43:37 -0700 | [diff] [blame] | 589 | self->SetMonitorEnterObject(nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 590 | monitor_lock_.Lock(self); // Reacquire locks in order. |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 591 | --num_waiters_; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 592 | } |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 593 | monitor_lock_.Unlock(self); |
| 594 | // We need to pair this with a single contended locking call. NB we match the RI behavior and call |
| 595 | // this even if MonitorEnter failed. |
| 596 | if (called_monitors_callback) { |
| 597 | CHECK(reason == LockReason::kForLock); |
| 598 | Runtime::Current()->GetRuntimeCallbacks()->MonitorContendedLocked(this); |
| 599 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 602 | template void Monitor::Lock<LockReason::kForLock>(Thread* self); |
| 603 | template void Monitor::Lock<LockReason::kForWait>(Thread* self); |
| 604 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 605 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
| 606 | __attribute__((format(printf, 1, 2))); |
| 607 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 608 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 609 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 610 | va_list args; |
| 611 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 612 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 613 | self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 614 | if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) { |
Brian Carlstrom | 64277f3 | 2012-03-26 23:53:34 -0700 | [diff] [blame] | 615 | std::ostringstream ss; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 616 | self->Dump(ss); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 617 | LOG(Runtime::Current()->IsStarted() ? ::android::base::INFO : ::android::base::ERROR) |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 618 | << self->GetException()->Dump() << "\n" << ss.str(); |
Brian Carlstrom | 64277f3 | 2012-03-26 23:53:34 -0700 | [diff] [blame] | 619 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 620 | va_end(args); |
| 621 | } |
| 622 | |
Elliott Hughes | d423741 | 2012-02-21 11:24:45 -0800 | [diff] [blame] | 623 | static std::string ThreadToString(Thread* thread) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 624 | if (thread == nullptr) { |
| 625 | return "nullptr"; |
Elliott Hughes | d423741 | 2012-02-21 11:24:45 -0800 | [diff] [blame] | 626 | } |
| 627 | std::ostringstream oss; |
| 628 | // TODO: alternatively, we could just return the thread's name. |
| 629 | oss << *thread; |
| 630 | return oss.str(); |
| 631 | } |
| 632 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 633 | void Monitor::FailedUnlock(ObjPtr<mirror::Object> o, |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 634 | uint32_t expected_owner_thread_id, |
| 635 | uint32_t found_owner_thread_id, |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 636 | Monitor* monitor) { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 637 | // Acquire thread list lock so threads won't disappear from under us. |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 638 | std::string current_owner_string; |
| 639 | std::string expected_owner_string; |
| 640 | std::string found_owner_string; |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 641 | uint32_t current_owner_thread_id = 0u; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 642 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 643 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 644 | ThreadList* const thread_list = Runtime::Current()->GetThreadList(); |
| 645 | Thread* expected_owner = thread_list->FindThreadByThreadId(expected_owner_thread_id); |
| 646 | Thread* found_owner = thread_list->FindThreadByThreadId(found_owner_thread_id); |
| 647 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 648 | // Re-read owner now that we hold lock. |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 649 | Thread* current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr; |
| 650 | if (current_owner != nullptr) { |
| 651 | current_owner_thread_id = current_owner->GetThreadId(); |
| 652 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 653 | // Get short descriptions of the threads involved. |
| 654 | current_owner_string = ThreadToString(current_owner); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 655 | expected_owner_string = expected_owner != nullptr ? ThreadToString(expected_owner) : "unnamed"; |
| 656 | found_owner_string = found_owner != nullptr ? ThreadToString(found_owner) : "unnamed"; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 657 | } |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 658 | |
| 659 | if (current_owner_thread_id == 0u) { |
| 660 | if (found_owner_thread_id == 0u) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 661 | ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'" |
| 662 | " on thread '%s'", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 663 | mirror::Object::PrettyTypeOf(o).c_str(), |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 664 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 665 | } else { |
| 666 | // Race: the original read found an owner but now there is none |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 667 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 668 | " (where now the monitor appears unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 669 | found_owner_string.c_str(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 670 | mirror::Object::PrettyTypeOf(o).c_str(), |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 671 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 672 | } |
| 673 | } else { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 674 | if (found_owner_thread_id == 0u) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 675 | // Race: originally there was no owner, there is now |
| 676 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 677 | " (originally believed to be unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 678 | current_owner_string.c_str(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 679 | mirror::Object::PrettyTypeOf(o).c_str(), |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 680 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 681 | } else { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 682 | if (found_owner_thread_id != current_owner_thread_id) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 683 | // Race: originally found and current owner have changed |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 684 | ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now" |
| 685 | " owned by '%s') on object of type '%s' on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 686 | found_owner_string.c_str(), |
| 687 | current_owner_string.c_str(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 688 | mirror::Object::PrettyTypeOf(o).c_str(), |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 689 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 690 | } else { |
| 691 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 692 | " on thread '%s", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 693 | current_owner_string.c_str(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 694 | mirror::Object::PrettyTypeOf(o).c_str(), |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 695 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 701 | bool Monitor::Unlock(Thread* self) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 702 | DCHECK(self != nullptr); |
Mathieu Chartier | 0ffdc9c | 2016-04-19 13:46:03 -0700 | [diff] [blame] | 703 | uint32_t owner_thread_id = 0u; |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 704 | DCHECK(!monitor_lock_.IsExclusiveHeld(self)); |
| 705 | monitor_lock_.Lock(self); |
| 706 | Thread* owner = owner_; |
| 707 | if (owner != nullptr) { |
| 708 | owner_thread_id = owner->GetThreadId(); |
| 709 | } |
| 710 | if (owner == self) { |
| 711 | // We own the monitor, so nobody else can be in here. |
| 712 | AtraceMonitorUnlock(); |
| 713 | if (lock_count_ == 0) { |
| 714 | owner_ = nullptr; |
| 715 | locking_method_ = nullptr; |
| 716 | locking_dex_pc_ = 0; |
| 717 | SignalContendersAndReleaseMonitorLock(self); |
| 718 | return true; |
| 719 | } else { |
| 720 | --lock_count_; |
| 721 | monitor_lock_.Unlock(self); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 722 | return true; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 723 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 724 | } |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 725 | // We don't own this, so we're not allowed to unlock it. |
| 726 | // The JNI spec says that we should throw IllegalMonitorStateException in this case. |
| 727 | FailedUnlock(GetObject(), self->GetThreadId(), owner_thread_id, this); |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 728 | monitor_lock_.Unlock(self); |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 729 | return false; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 732 | void Monitor::SignalContendersAndReleaseMonitorLock(Thread* self) { |
| 733 | // We want to signal one thread to wake up, to acquire the monitor that |
| 734 | // we are releasing. This could either be a Thread waiting on its own |
| 735 | // ConditionVariable, or a thread waiting on monitor_contenders_. |
| 736 | while (wake_set_ != nullptr) { |
| 737 | // No risk of waking ourselves here; since monitor_lock_ is not released until we're ready to |
| 738 | // return, notify can't move the current thread from wait_set_ to wake_set_ until this |
| 739 | // method is done checking wake_set_. |
| 740 | Thread* thread = wake_set_; |
| 741 | wake_set_ = thread->GetWaitNext(); |
| 742 | thread->SetWaitNext(nullptr); |
| 743 | |
| 744 | // Check to see if the thread is still waiting. |
| 745 | { |
| 746 | // In the case of wait(), we'll be acquiring another thread's GetWaitMutex with |
| 747 | // self's GetWaitMutex held. This does not risk deadlock, because we only acquire this lock |
| 748 | // for threads in the wake_set_. A thread can only enter wake_set_ from Notify or NotifyAll, |
| 749 | // and those hold monitor_lock_. Thus, the threads whose wait mutexes we acquire here must |
| 750 | // have already been released from wait(), since we have not released monitor_lock_ until |
| 751 | // after we've chosen our thread to wake, so there is no risk of the following lock ordering |
| 752 | // leading to deadlock: |
| 753 | // Thread 1 waits |
| 754 | // Thread 2 waits |
| 755 | // Thread 3 moves threads 1 and 2 from wait_set_ to wake_set_ |
| 756 | // Thread 1 enters this block, and attempts to acquire Thread 2's GetWaitMutex to wake it |
| 757 | // Thread 2 enters this block, and attempts to acquire Thread 1's GetWaitMutex to wake it |
| 758 | // |
| 759 | // Since monitor_lock_ is not released until the thread-to-be-woken-up's GetWaitMutex is |
| 760 | // acquired, two threads cannot attempt to acquire each other's GetWaitMutex while holding |
| 761 | // their own and cause deadlock. |
| 762 | MutexLock wait_mu(self, *thread->GetWaitMutex()); |
| 763 | if (thread->GetWaitMonitor() != nullptr) { |
| 764 | // Release the lock, so that a potentially awakened thread will not |
| 765 | // immediately contend on it. The lock ordering here is: |
| 766 | // monitor_lock_, self->GetWaitMutex, thread->GetWaitMutex |
| 767 | monitor_lock_.Unlock(self); |
| 768 | thread->GetWaitConditionVariable()->Signal(self); |
| 769 | return; |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | // If we didn't wake any threads that were originally waiting on us, |
| 774 | // wake a contender. |
| 775 | monitor_contenders_.Signal(self); |
| 776 | monitor_lock_.Unlock(self); |
| 777 | } |
| 778 | |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 779 | void Monitor::Wait(Thread* self, int64_t ms, int32_t ns, |
| 780 | bool interruptShouldThrow, ThreadState why) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 781 | DCHECK(self != nullptr); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 782 | DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 783 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 784 | monitor_lock_.Lock(self); |
| 785 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 786 | // Make sure that we hold the lock. |
| 787 | if (owner_ != self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 788 | monitor_lock_.Unlock(self); |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 789 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 790 | return; |
| 791 | } |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 792 | |
Elliott Hughes | df42c48 | 2013-01-09 12:49:02 -0800 | [diff] [blame] | 793 | // We need to turn a zero-length timed wait into a regular wait because |
| 794 | // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait(). |
| 795 | if (why == kTimedWaiting && (ms == 0 && ns == 0)) { |
| 796 | why = kWaiting; |
| 797 | } |
| 798 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 799 | // Enforce the timeout range. |
| 800 | if (ms < 0 || ns < 0 || ns > 999999) { |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 801 | monitor_lock_.Unlock(self); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 802 | self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 803 | "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 804 | return; |
| 805 | } |
| 806 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 807 | /* |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 808 | * Release our hold - we need to let it go even if we're a few levels |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 809 | * deep in a recursive lock, and we need to restore that later. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 810 | */ |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 811 | int prev_lock_count = lock_count_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 812 | lock_count_ = 0; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 813 | owner_ = nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 814 | ArtMethod* saved_method = locking_method_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 815 | locking_method_ = nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 816 | uintptr_t saved_dex_pc = locking_dex_pc_; |
| 817 | locking_dex_pc_ = 0; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 818 | |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 819 | AtraceMonitorUnlock(); // For the implict Unlock() just above. This will only end the deepest |
| 820 | // nesting, but that is enough for the visualization, and corresponds to |
| 821 | // the single Lock() we do afterwards. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 822 | AtraceMonitorLock(self, GetObject(), /* is_wait= */ true); |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 823 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 824 | bool was_interrupted = false; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 825 | bool timed_out = false; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 826 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 827 | // Update thread state. If the GC wakes up, it'll ignore us, knowing |
| 828 | // that we won't touch any references in this state, and we'll check |
| 829 | // our suspend mode before we transition out. |
| 830 | ScopedThreadSuspension sts(self, why); |
| 831 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 832 | // 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] | 833 | MutexLock mu(self, *self->GetWaitMutex()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 834 | |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 835 | /* |
| 836 | * Add ourselves to the set of threads waiting on this monitor. |
| 837 | * It's important that we are only added to the wait set after |
| 838 | * acquiring our GetWaitMutex, so that calls to Notify() that occur after we |
| 839 | * have released monitor_lock_ will not move us from wait_set_ to wake_set_ |
| 840 | * until we've signalled contenders on this monitor. |
| 841 | */ |
| 842 | AppendToWaitSet(self); |
| 843 | ++num_waiters_; |
| 844 | |
| 845 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 846 | // 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] | 847 | // 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] | 848 | // up. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 849 | DCHECK(self->GetWaitMonitor() == nullptr); |
| 850 | self->SetWaitMonitor(this); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 851 | |
| 852 | // Release the monitor lock. |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 853 | SignalContendersAndReleaseMonitorLock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 854 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 855 | // Handle the case where the thread was interrupted before we called wait(). |
Nicolas Geoffray | 365719c | 2017-03-08 13:11:50 +0000 | [diff] [blame] | 856 | if (self->IsInterrupted()) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 857 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 858 | } else { |
| 859 | // Wait for a notification or a timeout to occur. |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 860 | if (why == kWaiting) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 861 | self->GetWaitConditionVariable()->Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 862 | } else { |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 863 | DCHECK(why == kTimedWaiting || why == kSleeping) << why; |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 864 | timed_out = self->GetWaitConditionVariable()->TimedWait(self, ms, ns); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 865 | } |
Nicolas Geoffray | 365719c | 2017-03-08 13:11:50 +0000 | [diff] [blame] | 866 | was_interrupted = self->IsInterrupted(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 867 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 870 | { |
| 871 | // We reset the thread's wait_monitor_ field after transitioning back to runnable so |
| 872 | // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging |
| 873 | // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads |
| 874 | // are waiting on "null".) |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 875 | MutexLock mu(self, *self->GetWaitMutex()); |
| 876 | DCHECK(self->GetWaitMonitor() != nullptr); |
| 877 | self->SetWaitMonitor(nullptr); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 878 | } |
| 879 | |
Mathieu Chartier | daed5d8 | 2016-03-10 10:49:35 -0800 | [diff] [blame] | 880 | // Allocate the interrupted exception not holding the monitor lock since it may cause a GC. |
| 881 | // If the GC requires acquiring the monitor for enqueuing cleared references, this would |
| 882 | // cause a deadlock if the monitor is held. |
| 883 | if (was_interrupted && interruptShouldThrow) { |
| 884 | /* |
| 885 | * We were interrupted while waiting, or somebody interrupted an |
| 886 | * un-interruptible thread earlier and we're bailing out immediately. |
| 887 | * |
| 888 | * The doc sayeth: "The interrupted status of the current thread is |
| 889 | * cleared when this exception is thrown." |
| 890 | */ |
Nicolas Geoffray | 365719c | 2017-03-08 13:11:50 +0000 | [diff] [blame] | 891 | self->SetInterrupted(false); |
Mathieu Chartier | daed5d8 | 2016-03-10 10:49:35 -0800 | [diff] [blame] | 892 | self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr); |
| 893 | } |
| 894 | |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 895 | AtraceMonitorUnlock(); // End Wait(). |
| 896 | |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 897 | // We just slept, tell the runtime callbacks about this. |
| 898 | Runtime::Current()->GetRuntimeCallbacks()->MonitorWaitFinished(this, timed_out); |
| 899 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 900 | // Re-acquire the monitor and lock. |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 901 | Lock<LockReason::kForWait>(self); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 902 | monitor_lock_.Lock(self); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 903 | self->GetWaitMutex()->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 904 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 905 | /* |
| 906 | * We remove our thread from wait set after restoring the count |
| 907 | * and owner fields so the subroutine can check that the calling |
| 908 | * thread owns the monitor. Aside from that, the order of member |
| 909 | * updates is not order sensitive as we hold the pthread mutex. |
| 910 | */ |
| 911 | owner_ = self; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 912 | lock_count_ = prev_lock_count; |
| 913 | locking_method_ = saved_method; |
| 914 | locking_dex_pc_ = saved_dex_pc; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 915 | --num_waiters_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 916 | RemoveFromWaitSet(self); |
| 917 | |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 918 | monitor_lock_.Unlock(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | void Monitor::Notify(Thread* self) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 922 | DCHECK(self != nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 923 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 924 | // Make sure that we hold the lock. |
| 925 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 926 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 927 | return; |
| 928 | } |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 929 | // Move one thread from waiters to wake set |
| 930 | Thread* to_move = wait_set_; |
| 931 | if (to_move != nullptr) { |
| 932 | wait_set_ = to_move->GetWaitNext(); |
| 933 | to_move->SetWaitNext(wake_set_); |
| 934 | wake_set_ = to_move; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
| 938 | void Monitor::NotifyAll(Thread* self) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 939 | DCHECK(self != nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 940 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 941 | // Make sure that we hold the lock. |
| 942 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 943 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 944 | return; |
| 945 | } |
Charles Munger | c665d63 | 2018-11-06 16:20:13 +0000 | [diff] [blame] | 946 | |
| 947 | // Move all threads from waiters to wake set |
| 948 | Thread* to_move = wait_set_; |
| 949 | if (to_move != nullptr) { |
| 950 | wait_set_ = nullptr; |
| 951 | Thread* move_to = wake_set_; |
| 952 | if (move_to == nullptr) { |
| 953 | wake_set_ = to_move; |
| 954 | return; |
| 955 | } |
| 956 | while (move_to->GetWaitNext() != nullptr) { |
| 957 | move_to = move_to->GetWaitNext(); |
| 958 | } |
| 959 | move_to->SetWaitNext(to_move); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 963 | bool Monitor::Deflate(Thread* self, ObjPtr<mirror::Object> obj) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 964 | DCHECK(obj != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 965 | // Don't need volatile since we only deflate with mutators suspended. |
| 966 | LockWord lw(obj->GetLockWord(false)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 967 | // If the lock isn't an inflated monitor, then we don't need to deflate anything. |
| 968 | if (lw.GetState() == LockWord::kFatLocked) { |
| 969 | Monitor* monitor = lw.FatLockMonitor(); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 970 | DCHECK(monitor != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 971 | MutexLock mu(self, monitor->monitor_lock_); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 972 | // Can't deflate if we have anybody waiting on the CV. |
| 973 | if (monitor->num_waiters_ > 0) { |
| 974 | return false; |
| 975 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 976 | Thread* owner = monitor->owner_; |
| 977 | if (owner != nullptr) { |
| 978 | // Can't deflate if we are locked and have a hash code. |
| 979 | if (monitor->HasHashCode()) { |
| 980 | return false; |
| 981 | } |
| 982 | // Can't deflate if our lock count is too high. |
Mathieu Chartier | 1cf194f | 2016-11-01 20:13:24 -0700 | [diff] [blame] | 983 | if (static_cast<uint32_t>(monitor->lock_count_) > LockWord::kThinLockMaxCount) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 984 | return false; |
| 985 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 986 | // Deflate to a thin lock. |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 987 | LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(), |
| 988 | monitor->lock_count_, |
| 989 | lw.GCState()); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 990 | // Assume no concurrent read barrier state changes as mutators are suspended. |
| 991 | obj->SetLockWord(new_lw, false); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 992 | VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / " |
| 993 | << monitor->lock_count_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 994 | } else if (monitor->HasHashCode()) { |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 995 | LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.GCState()); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 996 | // Assume no concurrent read barrier state changes as mutators are suspended. |
| 997 | obj->SetLockWord(new_lw, false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 998 | VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 999 | } else { |
| 1000 | // No lock and no hash, just put an empty lock word inside the object. |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 1001 | LockWord new_lw = LockWord::FromDefault(lw.GCState()); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1002 | // Assume no concurrent read barrier state changes as mutators are suspended. |
| 1003 | obj->SetLockWord(new_lw, false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1004 | VLOG(monitor) << "Deflated" << obj << " to empty lock word"; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1005 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1006 | // 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] | 1007 | // next GC. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1008 | monitor->obj_ = GcRoot<mirror::Object>(nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1009 | } |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1013 | void Monitor::Inflate(Thread* self, Thread* owner, ObjPtr<mirror::Object> obj, int32_t hash_code) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1014 | DCHECK(self != nullptr); |
| 1015 | DCHECK(obj != nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1016 | // Allocate and acquire a new monitor. |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1017 | Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code); |
| 1018 | DCHECK(m != nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1019 | if (m->Install(self)) { |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 1020 | if (owner != nullptr) { |
| 1021 | VLOG(monitor) << "monitor: thread" << owner->GetThreadId() |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1022 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 1023 | } else { |
| 1024 | VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1025 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 1026 | } |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1027 | Runtime::Current()->GetMonitorList()->Add(m); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1028 | CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1029 | } else { |
| 1030 | MonitorPool::ReleaseMonitor(self, m); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1031 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1034 | void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1035 | uint32_t hash_code) { |
| 1036 | DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked); |
| 1037 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 1038 | if (owner_thread_id == self->GetThreadId()) { |
| 1039 | // We own the monitor, we can easily inflate it. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1040 | Inflate(self, self, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1041 | } else { |
| 1042 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1043 | // 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] | 1044 | self->SetMonitorEnterObject(obj.Get()); |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 1045 | bool timed_out; |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 1046 | Thread* owner; |
| 1047 | { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1048 | ScopedThreadSuspension sts(self, kWaitingForLockInflation); |
Alex Light | 46f9340 | 2017-06-29 11:59:50 -0700 | [diff] [blame] | 1049 | owner = thread_list->SuspendThreadByThreadId(owner_thread_id, |
| 1050 | SuspendReason::kInternal, |
| 1051 | &timed_out); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 1052 | } |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 1053 | if (owner != nullptr) { |
| 1054 | // We succeeded in suspending the thread, check the lock's status didn't change. |
| 1055 | lock_word = obj->GetLockWord(true); |
| 1056 | if (lock_word.GetState() == LockWord::kThinLocked && |
| 1057 | lock_word.ThinLockOwner() == owner_thread_id) { |
| 1058 | // Go ahead and inflate the lock. |
| 1059 | Inflate(self, owner, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1060 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 1061 | bool resumed = thread_list->Resume(owner, SuspendReason::kInternal); |
| 1062 | DCHECK(resumed); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1063 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1064 | self->SetMonitorEnterObject(nullptr); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1065 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 1068 | // Fool annotalysis into thinking that the lock on obj is acquired. |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1069 | static ObjPtr<mirror::Object> FakeLock(ObjPtr<mirror::Object> obj) |
| 1070 | EXCLUSIVE_LOCK_FUNCTION(obj.Ptr()) NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 1071 | return obj; |
| 1072 | } |
| 1073 | |
| 1074 | // Fool annotalysis into thinking that the lock on obj is release. |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1075 | static ObjPtr<mirror::Object> FakeUnlock(ObjPtr<mirror::Object> obj) |
| 1076 | UNLOCK_FUNCTION(obj.Ptr()) NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 1077 | return obj; |
| 1078 | } |
| 1079 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1080 | ObjPtr<mirror::Object> Monitor::MonitorEnter(Thread* self, |
| 1081 | ObjPtr<mirror::Object> obj, |
| 1082 | bool trylock) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1083 | DCHECK(self != nullptr); |
| 1084 | DCHECK(obj != nullptr); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 1085 | self->AssertThreadSuspensionIsAllowable(); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 1086 | obj = FakeLock(obj); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1087 | uint32_t thread_id = self->GetThreadId(); |
| 1088 | size_t contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1089 | StackHandleScope<1> hs(self); |
| 1090 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1091 | while (true) { |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1092 | // We initially read the lockword with ordinary Java/relaxed semantics. When stronger |
| 1093 | // semantics are needed, we address it below. Since GetLockWord bottoms out to a relaxed load, |
| 1094 | // we can fix it later, in an infrequently executed case, with a fence. |
| 1095 | LockWord lock_word = h_obj->GetLockWord(false); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1096 | switch (lock_word.GetState()) { |
| 1097 | case LockWord::kUnlocked: { |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1098 | // No ordering required for preceding lockword read, since we retest. |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 1099 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.GCState())); |
Mathieu Chartier | 42c2e50 | 2018-06-19 12:30:56 -0700 | [diff] [blame] | 1100 | if (h_obj->CasLockWord(lock_word, thin_locked, CASMode::kWeak, std::memory_order_acquire)) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1101 | AtraceMonitorLock(self, h_obj.Get(), /* is_wait= */ false); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1102 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1103 | } |
| 1104 | continue; // Go again. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1105 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1106 | case LockWord::kThinLocked: { |
| 1107 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 1108 | if (owner_thread_id == thread_id) { |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1109 | // No ordering required for initial lockword read. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1110 | // We own the lock, increase the recursion count. |
| 1111 | uint32_t new_count = lock_word.ThinLockCount() + 1; |
| 1112 | if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) { |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 1113 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, |
| 1114 | new_count, |
| 1115 | lock_word.GCState())); |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1116 | // Only this thread pays attention to the count. Thus there is no need for stronger |
| 1117 | // than relaxed memory ordering. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1118 | if (!kUseReadBarrier) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1119 | h_obj->SetLockWord(thin_locked, /* as_volatile= */ false); |
| 1120 | AtraceMonitorLock(self, h_obj.Get(), /* is_wait= */ false); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1121 | return h_obj.Get(); // Success! |
| 1122 | } else { |
| 1123 | // Use CAS to preserve the read barrier state. |
Mathieu Chartier | 42c2e50 | 2018-06-19 12:30:56 -0700 | [diff] [blame] | 1124 | if (h_obj->CasLockWord(lock_word, |
| 1125 | thin_locked, |
| 1126 | CASMode::kWeak, |
| 1127 | std::memory_order_relaxed)) { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1128 | AtraceMonitorLock(self, h_obj.Get(), /* is_wait= */ false); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1129 | return h_obj.Get(); // Success! |
| 1130 | } |
| 1131 | } |
| 1132 | continue; // Go again. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1133 | } else { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1134 | // We'd overflow the recursion count, so inflate the monitor. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1135 | InflateThinLocked(self, h_obj, lock_word, 0); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } else { |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 1138 | if (trylock) { |
| 1139 | return nullptr; |
| 1140 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1141 | // Contention. |
| 1142 | contention_count++; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1143 | Runtime* runtime = Runtime::Current(); |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1144 | if (contention_count <= runtime->GetMaxSpinsBeforeThinLockInflation()) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1145 | // TODO: Consider switching the thread state to kWaitingForLockInflation when we are |
| 1146 | // yielding. Use sched_yield instead of NanoSleep since NanoSleep can wait much longer |
| 1147 | // than the parameter you pass in. This can cause thread suspension to take excessively |
| 1148 | // long and make long pauses. See b/16307460. |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1149 | // TODO: We should literally spin first, without sched_yield. Sched_yield either does |
| 1150 | // nothing (at significant expense), or guarantees that we wait at least microseconds. |
| 1151 | // If the owner is running, I would expect the median lock hold time to be hundreds |
| 1152 | // of nanoseconds or less. |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 1153 | sched_yield(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1154 | } else { |
| 1155 | contention_count = 0; |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1156 | // No ordering required for initial lockword read. Install rereads it anyway. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1157 | InflateThinLocked(self, h_obj, lock_word, 0); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1158 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1159 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1160 | continue; // Start from the beginning. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1161 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1162 | case LockWord::kFatLocked: { |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1163 | // We should have done an acquire read of the lockword initially, to ensure |
| 1164 | // visibility of the monitor data structure. Use an explicit fence instead. |
Orion Hodson | 27b9676 | 2018-03-13 16:06:57 +0000 | [diff] [blame] | 1165 | std::atomic_thread_fence(std::memory_order_acquire); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1166 | Monitor* mon = lock_word.FatLockMonitor(); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 1167 | if (trylock) { |
| 1168 | return mon->TryLock(self) ? h_obj.Get() : nullptr; |
| 1169 | } else { |
| 1170 | mon->Lock(self); |
| 1171 | return h_obj.Get(); // Success! |
| 1172 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1173 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 1174 | case LockWord::kHashCode: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1175 | // Inflate with the existing hashcode. |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1176 | // Again no ordering required for initial lockword read, since we don't rely |
| 1177 | // on the visibility of any prior computation. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1178 | Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode()); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 1179 | continue; // Start from the beginning. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1180 | default: { |
| 1181 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Andreas Gampe | c7ed09b | 2016-04-25 20:08:55 -0700 | [diff] [blame] | 1182 | UNREACHABLE(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1183 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1184 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1188 | bool Monitor::MonitorExit(Thread* self, ObjPtr<mirror::Object> obj) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1189 | DCHECK(self != nullptr); |
| 1190 | DCHECK(obj != nullptr); |
Mathieu Chartier | 2d096c9 | 2015-10-12 16:18:20 -0700 | [diff] [blame] | 1191 | self->AssertThreadSuspensionIsAllowable(); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 1192 | obj = FakeUnlock(obj); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1193 | StackHandleScope<1> hs(self); |
| 1194 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1195 | while (true) { |
| 1196 | LockWord lock_word = obj->GetLockWord(true); |
| 1197 | switch (lock_word.GetState()) { |
| 1198 | case LockWord::kHashCode: |
| 1199 | // Fall-through. |
| 1200 | case LockWord::kUnlocked: |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 1201 | FailedUnlock(h_obj.Get(), self->GetThreadId(), 0u, nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1202 | return false; // Failure. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1203 | case LockWord::kThinLocked: { |
| 1204 | uint32_t thread_id = self->GetThreadId(); |
| 1205 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 1206 | if (owner_thread_id != thread_id) { |
Mathieu Chartier | 61b3cd4 | 2016-04-18 11:43:29 -0700 | [diff] [blame] | 1207 | FailedUnlock(h_obj.Get(), thread_id, owner_thread_id, nullptr); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1208 | return false; // Failure. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1209 | } else { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1210 | // We own the lock, decrease the recursion count. |
| 1211 | LockWord new_lw = LockWord::Default(); |
| 1212 | if (lock_word.ThinLockCount() != 0) { |
| 1213 | uint32_t new_count = lock_word.ThinLockCount() - 1; |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 1214 | new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.GCState()); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1215 | } else { |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 1216 | new_lw = LockWord::FromDefault(lock_word.GCState()); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1217 | } |
| 1218 | if (!kUseReadBarrier) { |
| 1219 | DCHECK_EQ(new_lw.ReadBarrierState(), 0U); |
Hans Boehm | b3da36c | 2016-12-15 13:12:59 -0800 | [diff] [blame] | 1220 | // TODO: This really only needs memory_order_release, but we currently have |
| 1221 | // no way to specify that. In fact there seem to be no legitimate uses of SetLockWord |
| 1222 | // with a final argument of true. This slows down x86 and ARMv7, but probably not v8. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1223 | h_obj->SetLockWord(new_lw, true); |
Andreas Gampe | 6e759ad | 2016-05-17 10:13:10 -0700 | [diff] [blame] | 1224 | AtraceMonitorUnlock(); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1225 | // Success! |
| 1226 | return true; |
| 1227 | } else { |
| 1228 | // Use CAS to preserve the read barrier state. |
Mathieu Chartier | 42c2e50 | 2018-06-19 12:30:56 -0700 | [diff] [blame] | 1229 | if (h_obj->CasLockWord(lock_word, new_lw, CASMode::kWeak, std::memory_order_release)) { |
Andreas Gampe | 6e759ad | 2016-05-17 10:13:10 -0700 | [diff] [blame] | 1230 | AtraceMonitorUnlock(); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1231 | // Success! |
| 1232 | return true; |
| 1233 | } |
| 1234 | } |
| 1235 | continue; // Go again. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1236 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1237 | } |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1238 | case LockWord::kFatLocked: { |
| 1239 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1240 | return mon->Unlock(self); |
| 1241 | } |
| 1242 | default: { |
| 1243 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1244 | UNREACHABLE(); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1245 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1246 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1247 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1250 | void Monitor::Wait(Thread* self, |
| 1251 | ObjPtr<mirror::Object> obj, |
| 1252 | int64_t ms, |
| 1253 | int32_t ns, |
| 1254 | bool interruptShouldThrow, |
| 1255 | ThreadState why) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1256 | DCHECK(self != nullptr); |
| 1257 | DCHECK(obj != nullptr); |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1258 | StackHandleScope<1> hs(self); |
| 1259 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
| 1260 | |
| 1261 | Runtime::Current()->GetRuntimeCallbacks()->ObjectWaitStart(h_obj, ms); |
Alex Light | 848574c | 2017-09-25 16:59:39 -0700 | [diff] [blame] | 1262 | if (UNLIKELY(self->ObserveAsyncException() || self->IsExceptionPending())) { |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1263 | // See b/65558434 for information on handling of exceptions here. |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | LockWord lock_word = h_obj->GetLockWord(true); |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 1268 | while (lock_word.GetState() != LockWord::kFatLocked) { |
| 1269 | switch (lock_word.GetState()) { |
| 1270 | case LockWord::kHashCode: |
| 1271 | // Fall-through. |
| 1272 | case LockWord::kUnlocked: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1273 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
| 1274 | return; // Failure. |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 1275 | case LockWord::kThinLocked: { |
| 1276 | uint32_t thread_id = self->GetThreadId(); |
| 1277 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 1278 | if (owner_thread_id != thread_id) { |
| 1279 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
| 1280 | return; // Failure. |
| 1281 | } else { |
| 1282 | // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so |
| 1283 | // re-load. |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 1284 | Inflate(self, self, h_obj.Get(), 0); |
| 1285 | lock_word = h_obj->GetLockWord(true); |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 1286 | } |
| 1287 | break; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1288 | } |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 1289 | case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through. |
| 1290 | default: { |
| 1291 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1292 | UNREACHABLE(); |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 1293 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1294 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1295 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1296 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1297 | mon->Wait(self, ms, ns, interruptShouldThrow, why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1300 | void Monitor::DoNotify(Thread* self, ObjPtr<mirror::Object> obj, bool notify_all) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1301 | DCHECK(self != nullptr); |
| 1302 | DCHECK(obj != nullptr); |
| 1303 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1304 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1305 | case LockWord::kHashCode: |
| 1306 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1307 | case LockWord::kUnlocked: |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 1308 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1309 | return; // Failure. |
| 1310 | case LockWord::kThinLocked: { |
| 1311 | uint32_t thread_id = self->GetThreadId(); |
| 1312 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 1313 | if (owner_thread_id != thread_id) { |
| 1314 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
| 1315 | return; // Failure. |
| 1316 | } else { |
| 1317 | // We own the lock but there's no Monitor and therefore no waiters. |
| 1318 | return; // Success. |
| 1319 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1320 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1321 | case LockWord::kFatLocked: { |
| 1322 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1323 | if (notify_all) { |
| 1324 | mon->NotifyAll(self); |
| 1325 | } else { |
| 1326 | mon->Notify(self); |
| 1327 | } |
| 1328 | return; // Success. |
| 1329 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1330 | default: { |
| 1331 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1332 | UNREACHABLE(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1333 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1337 | uint32_t Monitor::GetLockOwnerThreadId(ObjPtr<mirror::Object> obj) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1338 | DCHECK(obj != nullptr); |
| 1339 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1340 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1341 | case LockWord::kHashCode: |
| 1342 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1343 | case LockWord::kUnlocked: |
| 1344 | return ThreadList::kInvalidThreadId; |
| 1345 | case LockWord::kThinLocked: |
| 1346 | return lock_word.ThinLockOwner(); |
| 1347 | case LockWord::kFatLocked: { |
| 1348 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1349 | return mon->GetOwnerThreadId(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1350 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1351 | default: { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1352 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1353 | UNREACHABLE(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1354 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | |
Andreas Gampe | f3ebcce | 2017-12-11 20:40:23 -0800 | [diff] [blame] | 1358 | ThreadState Monitor::FetchState(const Thread* thread, |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1359 | /* out */ ObjPtr<mirror::Object>* monitor_object, |
Andreas Gampe | f3ebcce | 2017-12-11 20:40:23 -0800 | [diff] [blame] | 1360 | /* out */ uint32_t* lock_owner_tid) { |
| 1361 | DCHECK(monitor_object != nullptr); |
| 1362 | DCHECK(lock_owner_tid != nullptr); |
| 1363 | |
| 1364 | *monitor_object = nullptr; |
| 1365 | *lock_owner_tid = ThreadList::kInvalidThreadId; |
| 1366 | |
| 1367 | ThreadState state = thread->GetState(); |
| 1368 | |
| 1369 | switch (state) { |
| 1370 | case kWaiting: |
| 1371 | case kTimedWaiting: |
| 1372 | case kSleeping: |
| 1373 | { |
| 1374 | Thread* self = Thread::Current(); |
| 1375 | MutexLock mu(self, *thread->GetWaitMutex()); |
| 1376 | Monitor* monitor = thread->GetWaitMonitor(); |
| 1377 | if (monitor != nullptr) { |
| 1378 | *monitor_object = monitor->GetObject(); |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 1379 | } |
| 1380 | } |
Andreas Gampe | f3ebcce | 2017-12-11 20:40:23 -0800 | [diff] [blame] | 1381 | break; |
| 1382 | |
| 1383 | case kBlocked: |
| 1384 | case kWaitingForLockInflation: |
| 1385 | { |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1386 | ObjPtr<mirror::Object> lock_object = thread->GetMonitorEnterObject(); |
Andreas Gampe | f3ebcce | 2017-12-11 20:40:23 -0800 | [diff] [blame] | 1387 | if (lock_object != nullptr) { |
| 1388 | if (kUseReadBarrier && Thread::Current()->GetIsGcMarking()) { |
| 1389 | // We may call Thread::Dump() in the middle of the CC thread flip and this thread's stack |
| 1390 | // may have not been flipped yet and "pretty_object" may be a from-space (stale) ref, in |
| 1391 | // which case the GetLockOwnerThreadId() call below will crash. So explicitly mark/forward |
| 1392 | // it here. |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1393 | lock_object = ReadBarrier::Mark(lock_object.Ptr()); |
Andreas Gampe | f3ebcce | 2017-12-11 20:40:23 -0800 | [diff] [blame] | 1394 | } |
| 1395 | *monitor_object = lock_object; |
| 1396 | *lock_owner_tid = lock_object->GetLockOwnerThreadId(); |
| 1397 | } |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 1398 | } |
Andreas Gampe | f3ebcce | 2017-12-11 20:40:23 -0800 | [diff] [blame] | 1399 | break; |
| 1400 | |
| 1401 | default: |
| 1402 | break; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 1403 | } |
Andreas Gampe | f3ebcce | 2017-12-11 20:40:23 -0800 | [diff] [blame] | 1404 | |
| 1405 | return state; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1408 | ObjPtr<mirror::Object> Monitor::GetContendedMonitor(Thread* thread) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 1409 | // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre |
| 1410 | // definition of contended that includes a monitor a thread is trying to enter... |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1411 | ObjPtr<mirror::Object> result = thread->GetMonitorEnterObject(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1412 | if (result == nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1413 | // ...but also a monitor that the thread is waiting on. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1414 | MutexLock mu(Thread::Current(), *thread->GetWaitMutex()); |
| 1415 | Monitor* monitor = thread->GetWaitMonitor(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1416 | if (monitor != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1417 | result = monitor->GetObject(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 1418 | } |
| 1419 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1420 | return result; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 1421 | } |
| 1422 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1423 | void Monitor::VisitLocks(StackVisitor* stack_visitor, |
| 1424 | void (*callback)(ObjPtr<mirror::Object>, void*), |
| 1425 | void* callback_context, |
| 1426 | bool abort_on_failure) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1427 | ArtMethod* m = stack_visitor->GetMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1428 | CHECK(m != nullptr); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1429 | |
| 1430 | // Native methods are an easy special case. |
| 1431 | // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. |
| 1432 | if (m->IsNative()) { |
| 1433 | if (m->IsSynchronized()) { |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1434 | ObjPtr<mirror::Object> jni_this = |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1435 | stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 1436 | callback(jni_this, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1437 | } |
| 1438 | return; |
| 1439 | } |
| 1440 | |
jeffhao | 61f916c | 2012-10-25 17:48:51 -0700 | [diff] [blame] | 1441 | // Proxy methods should not be synchronized. |
| 1442 | if (m->IsProxyMethod()) { |
| 1443 | CHECK(!m->IsSynchronized()); |
| 1444 | return; |
| 1445 | } |
| 1446 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1447 | // Is there any reason to believe there's any synchronization in this method? |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1448 | CHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod(); |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1449 | CodeItemDataAccessor accessor(m->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1450 | if (accessor.TriesSize() == 0) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1451 | return; // No "tries" implies no synchronization, so no held locks to report. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1454 | // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot |
| 1455 | // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an |
| 1456 | // inconsistent stack anyways. |
| 1457 | uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 1458 | if (!abort_on_failure && dex_pc == dex::kDexNoIndex) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1459 | LOG(ERROR) << "Could not find dex_pc for " << m->PrettyMethod(); |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1463 | // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to |
| 1464 | // the locks held in this stack frame. |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 1465 | std::vector<verifier::MethodVerifier::DexLockInfo> monitor_enter_dex_pcs; |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 1466 | verifier::MethodVerifier::FindLocksAtDexPc(m, |
| 1467 | dex_pc, |
| 1468 | &monitor_enter_dex_pcs, |
| 1469 | Runtime::Current()->GetTargetSdkVersion()); |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 1470 | for (verifier::MethodVerifier::DexLockInfo& dex_lock_info : monitor_enter_dex_pcs) { |
| 1471 | // As a debug check, check that dex PC corresponds to a monitor-enter. |
| 1472 | if (kIsDebugBuild) { |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1473 | const Instruction& monitor_enter_instruction = accessor.InstructionAt(dex_lock_info.dex_pc); |
| 1474 | CHECK_EQ(monitor_enter_instruction.Opcode(), Instruction::MONITOR_ENTER) |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 1475 | << "expected monitor-enter @" << dex_lock_info.dex_pc << "; was " |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1476 | << reinterpret_cast<const void*>(&monitor_enter_instruction); |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 1477 | } |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1478 | |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 1479 | // Iterate through the set of dex registers, as the compiler may not have held all of them |
| 1480 | // live. |
| 1481 | bool success = false; |
| 1482 | for (uint32_t dex_reg : dex_lock_info.dex_registers) { |
| 1483 | uint32_t value; |
Artem Serov | 2808be8 | 2018-12-20 19:15:11 +0000 | [diff] [blame^] | 1484 | |
| 1485 | // For optimized code we expect the DexRegisterMap to be present - monitor information |
| 1486 | // not be optimized out. |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 1487 | success = stack_visitor->GetVReg(m, dex_reg, kReferenceVReg, &value); |
| 1488 | if (success) { |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1489 | ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(value); |
Andreas Gampe | aaf0d38 | 2017-11-27 14:10:21 -0800 | [diff] [blame] | 1490 | callback(o, callback_context); |
| 1491 | break; |
| 1492 | } |
| 1493 | } |
| 1494 | DCHECK(success) << "Failed to find/read reference for monitor-enter at dex pc " |
| 1495 | << dex_lock_info.dex_pc |
| 1496 | << " in method " |
| 1497 | << m->PrettyMethod(); |
| 1498 | if (!success) { |
| 1499 | LOG(WARNING) << "Had a lock reported for dex pc " << dex_lock_info.dex_pc |
| 1500 | << " but was not able to fetch a corresponding object!"; |
| 1501 | } |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1505 | bool Monitor::IsValidLockWord(LockWord lock_word) { |
| 1506 | switch (lock_word.GetState()) { |
| 1507 | case LockWord::kUnlocked: |
| 1508 | // Nothing to check. |
| 1509 | return true; |
| 1510 | case LockWord::kThinLocked: |
| 1511 | // Basic sanity check of owner. |
| 1512 | return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId; |
| 1513 | case LockWord::kFatLocked: { |
| 1514 | // Check the monitor appears in the monitor list. |
| 1515 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1516 | MonitorList* list = Runtime::Current()->GetMonitorList(); |
| 1517 | MutexLock mu(Thread::Current(), list->monitor_list_lock_); |
| 1518 | for (Monitor* list_mon : list->list_) { |
| 1519 | if (mon == list_mon) { |
| 1520 | return true; // Found our monitor. |
| 1521 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1522 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1523 | return false; // Fail - unowned monitor in an object. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1524 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1525 | case LockWord::kHashCode: |
| 1526 | return true; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1527 | default: |
| 1528 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1529 | UNREACHABLE(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1530 | } |
| 1531 | } |
| 1532 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1533 | bool Monitor::IsLocked() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1534 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1535 | return owner_ != nullptr; |
| 1536 | } |
| 1537 | |
Mathieu Chartier | 74b3c8f | 2016-04-15 19:11:45 -0700 | [diff] [blame] | 1538 | void Monitor::TranslateLocation(ArtMethod* method, |
| 1539 | uint32_t dex_pc, |
| 1540 | const char** source_file, |
| 1541 | int32_t* line_number) { |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1542 | // If method is null, location is unknown |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1543 | if (method == nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1544 | *source_file = ""; |
| 1545 | *line_number = 0; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1546 | return; |
| 1547 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1548 | *source_file = method->GetDeclaringClassSourceFile(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1549 | if (*source_file == nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1550 | *source_file = ""; |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 1551 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1552 | *line_number = method->GetLineNumFromDexPC(dex_pc); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | uint32_t Monitor::GetOwnerThreadId() { |
| 1556 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1557 | Thread* owner = owner_; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1558 | if (owner != nullptr) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1559 | return owner->GetThreadId(); |
| 1560 | } else { |
| 1561 | return ThreadList::kInvalidThreadId; |
| 1562 | } |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1563 | } |
| 1564 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1565 | MonitorList::MonitorList() |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1566 | : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock), |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1567 | monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | MonitorList::~MonitorList() { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1571 | Thread* self = Thread::Current(); |
| 1572 | MutexLock mu(self, monitor_list_lock_); |
| 1573 | // Release all monitors to the pool. |
| 1574 | // TODO: Is it an invariant that *all* open monitors are in the list? Then we could |
| 1575 | // clear faster in the pool. |
| 1576 | MonitorPool::ReleaseMonitors(self, &list_); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1579 | void MonitorList::DisallowNewMonitors() { |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 1580 | CHECK(!kUseReadBarrier); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1581 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1582 | allow_new_monitors_ = false; |
| 1583 | } |
| 1584 | |
| 1585 | void MonitorList::AllowNewMonitors() { |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 1586 | CHECK(!kUseReadBarrier); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1587 | Thread* self = Thread::Current(); |
| 1588 | MutexLock mu(self, monitor_list_lock_); |
| 1589 | allow_new_monitors_ = true; |
| 1590 | monitor_add_condition_.Broadcast(self); |
| 1591 | } |
| 1592 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1593 | void MonitorList::BroadcastForNewMonitors() { |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 1594 | Thread* self = Thread::Current(); |
| 1595 | MutexLock mu(self, monitor_list_lock_); |
| 1596 | monitor_add_condition_.Broadcast(self); |
| 1597 | } |
| 1598 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1599 | void MonitorList::Add(Monitor* m) { |
| 1600 | Thread* self = Thread::Current(); |
| 1601 | MutexLock mu(self, monitor_list_lock_); |
Hiroshi Yamauchi | f1c6f87 | 2017-01-06 12:23:47 -0800 | [diff] [blame] | 1602 | // CMS needs this to block for concurrent reference processing because an object allocated during |
| 1603 | // the GC won't be marked and concurrent reference processing would incorrectly clear the JNI weak |
| 1604 | // ref. But CC (kUseReadBarrier == true) doesn't because of the to-space invariant. |
| 1605 | while (!kUseReadBarrier && UNLIKELY(!allow_new_monitors_)) { |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 1606 | // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the |
| 1607 | // presence of threads blocking for weak ref access. |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 1608 | self->CheckEmptyCheckpointFromWeakRefAccess(&monitor_list_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1609 | monitor_add_condition_.WaitHoldingLocks(self); |
| 1610 | } |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1611 | list_.push_front(m); |
| 1612 | } |
| 1613 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1614 | void MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1615 | Thread* self = Thread::Current(); |
| 1616 | MutexLock mu(self, monitor_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1617 | for (auto it = list_.begin(); it != list_.end(); ) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1618 | Monitor* m = *it; |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1619 | // Disable the read barrier in GetObject() as this is called by GC. |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1620 | ObjPtr<mirror::Object> obj = m->GetObject<kWithoutReadBarrier>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1621 | // The object of a monitor can be null if we have deflated it. |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1622 | ObjPtr<mirror::Object> new_obj = obj != nullptr ? visitor->IsMarked(obj.Ptr()) : nullptr; |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1623 | if (new_obj == nullptr) { |
| 1624 | VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1625 | << obj; |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1626 | MonitorPool::ReleaseMonitor(self, m); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1627 | it = list_.erase(it); |
| 1628 | } else { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1629 | m->SetObject(new_obj); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1630 | ++it; |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | |
Hans Boehm | 6fe97e0 | 2016-05-04 18:35:57 -0700 | [diff] [blame] | 1635 | size_t MonitorList::Size() { |
| 1636 | Thread* self = Thread::Current(); |
| 1637 | MutexLock mu(self, monitor_list_lock_); |
| 1638 | return list_.size(); |
| 1639 | } |
| 1640 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1641 | class MonitorDeflateVisitor : public IsMarkedVisitor { |
| 1642 | public: |
| 1643 | MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {} |
| 1644 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 1645 | mirror::Object* IsMarked(mirror::Object* object) override |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1646 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1647 | if (Monitor::Deflate(self_, object)) { |
| 1648 | DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked); |
| 1649 | ++deflate_count_; |
| 1650 | // If we deflated, return null so that the monitor gets removed from the array. |
| 1651 | return nullptr; |
| 1652 | } |
| 1653 | return object; // Monitor was not deflated. |
| 1654 | } |
| 1655 | |
| 1656 | Thread* const self_; |
| 1657 | size_t deflate_count_; |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1658 | }; |
| 1659 | |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1660 | size_t MonitorList::DeflateMonitors() { |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 1661 | MonitorDeflateVisitor visitor; |
| 1662 | Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_); |
| 1663 | SweepMonitorList(&visitor); |
| 1664 | return visitor.deflate_count_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Vladimir Marko | f52d92f | 2019-03-29 12:33:02 +0000 | [diff] [blame] | 1667 | MonitorInfo::MonitorInfo(ObjPtr<mirror::Object> obj) : owner_(nullptr), entry_count_(0) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1668 | DCHECK(obj != nullptr); |
| 1669 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1670 | switch (lock_word.GetState()) { |
| 1671 | case LockWord::kUnlocked: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1672 | // Fall-through. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1673 | case LockWord::kForwardingAddress: |
| 1674 | // Fall-through. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1675 | case LockWord::kHashCode: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1676 | break; |
| 1677 | case LockWord::kThinLocked: |
| 1678 | owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
Alex Light | ce56864 | 2017-09-05 16:54:25 -0700 | [diff] [blame] | 1679 | DCHECK(owner_ != nullptr) << "Thin-locked without owner!"; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1680 | entry_count_ = 1 + lock_word.ThinLockCount(); |
| 1681 | // Thin locks have no waiters. |
| 1682 | break; |
| 1683 | case LockWord::kFatLocked: { |
| 1684 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1685 | owner_ = mon->owner_; |
Alex Light | ce56864 | 2017-09-05 16:54:25 -0700 | [diff] [blame] | 1686 | // Here it is okay for the owner to be null since we don't reset the LockWord back to |
| 1687 | // kUnlocked until we get a GC. In cases where this hasn't happened yet we will have a fat |
| 1688 | // lock without an owner. |
| 1689 | if (owner_ != nullptr) { |
| 1690 | entry_count_ = 1 + mon->lock_count_; |
| 1691 | } else { |
| 1692 | DCHECK_EQ(mon->lock_count_, 0) << "Monitor is fat-locked without any owner!"; |
| 1693 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1694 | for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1695 | waiters_.push_back(waiter); |
| 1696 | } |
| 1697 | break; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | } |
| 1701 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1702 | } // namespace art |