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