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