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