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