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 | |
Mathieu Chartier | f0dc8b5 | 2014-12-17 10:13:30 -0800 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 20 | |
| 21 | #include <cutils/trace.h> |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
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" |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 26 | #include "class_linker.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 27 | #include "dex_file-inl.h" |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 28 | #include "dex_instruction.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 29 | #include "lock_word-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 30 | #include "mirror/art_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 31 | #include "mirror/class-inl.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 32 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "mirror/object_array-inl.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 34 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 35 | #include "thread.h" |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 36 | #include "thread_list.h" |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 37 | #include "verifier/method_verifier.h" |
Elliott Hughes | 044288f | 2012-06-25 14:46:39 -0700 | [diff] [blame] | 38 | #include "well_known_classes.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
Mathieu Chartier | b9001ab | 2014-10-03 13:28:46 -0700 | [diff] [blame] | 42 | static constexpr uint64_t kLongWaitMs = 100; |
| 43 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 44 | /* |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 45 | * Every Object has a monitor associated with it, but not every Object is actually locked. Even |
| 46 | * the ones that are locked do not need a full-fledged monitor until a) there is actual contention |
| 47 | * or b) wait() is called on the Object. |
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 | * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s |
| 50 | * "Thin locks: featherweight synchronization for Java" (ACM 1998). Things are even easier for us, |
| 51 | * though, because we have a full 32 bits to work with. |
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 | * The two states of an Object's lock are referred to as "thin" and "fat". A lock may transition |
| 54 | * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once |
| 55 | * a lock has been inflated it remains in the "fat" state indefinitely. |
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 lock value itself is stored in mirror::Object::monitor_ and the representation is described |
| 58 | * in the LockWord value type. |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 59 | * |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 60 | * Monitors provide: |
| 61 | * - mutually exclusive access to resources |
| 62 | * - a way for multiple threads to wait for notification |
| 63 | * |
| 64 | * In effect, they fill the role of both mutexes and condition variables. |
| 65 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 66 | * Only one thread can own the monitor at any time. There may be several threads waiting on it |
| 67 | * (the wait call unlocks it). One or more waiting threads may be getting interrupted or notified |
| 68 | * at any given time. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 69 | */ |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 71 | bool (*Monitor::is_sensitive_thread_hook_)() = NULL; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 72 | uint32_t Monitor::lock_profiling_threshold_ = 0; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 74 | bool Monitor::IsSensitiveThread() { |
| 75 | if (is_sensitive_thread_hook_ != NULL) { |
| 76 | return (*is_sensitive_thread_hook_)(); |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 81 | void Monitor::Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()) { |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 82 | lock_profiling_threshold_ = lock_profiling_threshold; |
| 83 | is_sensitive_thread_hook_ = is_sensitive_thread_hook; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 86 | 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] | 87 | : monitor_lock_("a monitor lock", kMonitorLock), |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 88 | monitor_contenders_("monitor contenders", monitor_lock_), |
Mathieu Chartier | 46bc778 | 2013-11-12 17:03:02 -0800 | [diff] [blame] | 89 | num_waiters_(0), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 90 | owner_(owner), |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 91 | lock_count_(0), |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 92 | obj_(GcRoot<mirror::Object>(obj)), |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 93 | wait_set_(NULL), |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 94 | hash_code_(hash_code), |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 95 | locking_method_(NULL), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 96 | locking_dex_pc_(0), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 97 | monitor_id_(MonitorPool::ComputeMonitorId(this, self)) { |
| 98 | #ifdef __LP64__ |
| 99 | DCHECK(false) << "Should not be reached in 64b"; |
| 100 | next_free_ = nullptr; |
| 101 | #endif |
| 102 | // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race |
| 103 | // with the owner unlocking the thin-lock. |
| 104 | CHECK(owner == nullptr || owner == self || owner->IsSuspended()); |
| 105 | // The identity hash code is set for the life time of the monitor. |
| 106 | } |
| 107 | |
| 108 | Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code, |
| 109 | MonitorId id) |
| 110 | : monitor_lock_("a monitor lock", kMonitorLock), |
| 111 | monitor_contenders_("monitor contenders", monitor_lock_), |
| 112 | num_waiters_(0), |
| 113 | owner_(owner), |
| 114 | lock_count_(0), |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 115 | obj_(GcRoot<mirror::Object>(obj)), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 116 | wait_set_(NULL), |
| 117 | hash_code_(hash_code), |
| 118 | locking_method_(NULL), |
| 119 | locking_dex_pc_(0), |
| 120 | monitor_id_(id) { |
| 121 | #ifdef __LP64__ |
| 122 | next_free_ = nullptr; |
| 123 | #endif |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 124 | // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race |
| 125 | // with the owner unlocking the thin-lock. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 126 | CHECK(owner == nullptr || owner == self || owner->IsSuspended()); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 127 | // 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] | 128 | } |
| 129 | |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 130 | int32_t Monitor::GetHashCode() { |
| 131 | while (!HasHashCode()) { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 132 | if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) { |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 133 | break; |
| 134 | } |
| 135 | } |
| 136 | DCHECK(HasHashCode()); |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 137 | return hash_code_.LoadRelaxed(); |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 140 | bool Monitor::Install(Thread* self) { |
| 141 | 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] | 142 | CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 143 | // Propagate the lock state. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 144 | LockWord lw(GetObject()->GetLockWord(false)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 145 | switch (lw.GetState()) { |
| 146 | case LockWord::kThinLocked: { |
| 147 | CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner()); |
| 148 | lock_count_ = lw.ThinLockCount(); |
| 149 | break; |
| 150 | } |
| 151 | case LockWord::kHashCode: { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 152 | CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode())); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | case LockWord::kFatLocked: { |
| 156 | // The owner_ is suspended but another thread beat us to install a monitor. |
| 157 | return false; |
| 158 | } |
| 159 | case LockWord::kUnlocked: { |
| 160 | LOG(FATAL) << "Inflating unlocked lock word"; |
| 161 | break; |
| 162 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 163 | default: { |
| 164 | LOG(FATAL) << "Invalid monitor state " << lw.GetState(); |
| 165 | return false; |
| 166 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 167 | } |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 168 | LockWord fat(this, lw.ReadBarrierState()); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 169 | // Publish the updated lock word, which may race with other threads. |
Ian Rogers | 228602f | 2014-07-10 02:07:54 -0700 | [diff] [blame] | 170 | bool success = GetObject()->CasLockWordWeakSequentiallyConsistent(lw, fat); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 171 | // Lock profiling. |
Mathieu Chartier | 9728f91 | 2013-10-30 09:45:13 -0700 | [diff] [blame] | 172 | if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) { |
Andreas Gampe | 6ec8ebd | 2014-07-25 13:36:56 -0700 | [diff] [blame] | 173 | // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on |
| 174 | // abort. |
| 175 | locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 176 | } |
| 177 | return success; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | Monitor::~Monitor() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 181 | // Deflated monitors have a null object. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 184 | void Monitor::AppendToWaitSet(Thread* thread) { |
| 185 | DCHECK(owner_ == Thread::Current()); |
| 186 | DCHECK(thread != NULL); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 187 | DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 188 | if (wait_set_ == NULL) { |
| 189 | wait_set_ = thread; |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | // push_back. |
| 194 | Thread* t = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 195 | while (t->GetWaitNext() != nullptr) { |
| 196 | t = t->GetWaitNext(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 197 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 198 | t->SetWaitNext(thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 201 | void Monitor::RemoveFromWaitSet(Thread *thread) { |
| 202 | DCHECK(owner_ == Thread::Current()); |
| 203 | DCHECK(thread != NULL); |
| 204 | if (wait_set_ == NULL) { |
| 205 | return; |
| 206 | } |
| 207 | if (wait_set_ == thread) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 208 | wait_set_ = thread->GetWaitNext(); |
| 209 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | |
| 213 | Thread* t = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 214 | while (t->GetWaitNext() != NULL) { |
| 215 | if (t->GetWaitNext() == thread) { |
| 216 | t->SetWaitNext(thread->GetWaitNext()); |
| 217 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 218 | return; |
| 219 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 220 | t = t->GetWaitNext(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 224 | void Monitor::SetObject(mirror::Object* object) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 225 | obj_ = GcRoot<mirror::Object>(object); |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 228 | void Monitor::Lock(Thread* self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 229 | MutexLock mu(self, monitor_lock_); |
| 230 | while (true) { |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 231 | if (owner_ == nullptr) { // Unowned. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 232 | owner_ = self; |
| 233 | CHECK_EQ(lock_count_, 0); |
| 234 | // When debugging, save the current monitor holder for future |
| 235 | // acquisition failures to use in sampled logging. |
| 236 | if (lock_profiling_threshold_ != 0) { |
| 237 | locking_method_ = self->GetCurrentMethod(&locking_dex_pc_); |
| 238 | } |
| 239 | return; |
| 240 | } else if (owner_ == self) { // Recursive. |
| 241 | lock_count_++; |
| 242 | return; |
| 243 | } |
| 244 | // Contended. |
| 245 | const bool log_contention = (lock_profiling_threshold_ != 0); |
Xin Guan | b894a19 | 2014-08-22 11:55:37 -0500 | [diff] [blame] | 246 | uint64_t wait_start_ms = log_contention ? MilliTime() : 0; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 247 | mirror::ArtMethod* owners_method = locking_method_; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 248 | uint32_t owners_dex_pc = locking_dex_pc_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 249 | // 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] | 250 | size_t num_waiters = num_waiters_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 251 | ++num_waiters_; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 252 | monitor_lock_.Unlock(self); // Let go of locks in order. |
Mathieu Chartier | a6e7f08 | 2014-05-22 14:43:37 -0700 | [diff] [blame] | 253 | self->SetMonitorEnterObject(GetObject()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 254 | { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 255 | ScopedThreadStateChange tsc(self, kBlocked); // Change to blocked and give up mutator_lock_. |
| 256 | MutexLock mu2(self, monitor_lock_); // Reacquire monitor_lock_ without mutator_lock_ for Wait. |
Mathieu Chartier | f0dc8b5 | 2014-12-17 10:13:30 -0800 | [diff] [blame] | 257 | if (owner_ != nullptr) { // Did the owner_ give the lock up? |
| 258 | if (ATRACE_ENABLED()) { |
| 259 | std::string name; |
| 260 | owner_->GetThreadName(name); |
| 261 | ATRACE_BEGIN(("Contended on monitor with owner " + name).c_str()); |
| 262 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 263 | monitor_contenders_.Wait(self); // Still contended so wait. |
| 264 | // Woken from contention. |
| 265 | if (log_contention) { |
| 266 | uint64_t wait_ms = MilliTime() - wait_start_ms; |
| 267 | uint32_t sample_percent; |
| 268 | if (wait_ms >= lock_profiling_threshold_) { |
| 269 | sample_percent = 100; |
| 270 | } else { |
| 271 | sample_percent = 100 * wait_ms / lock_profiling_threshold_; |
| 272 | } |
| 273 | if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) { |
| 274 | const char* owners_filename; |
| 275 | uint32_t owners_line_number; |
| 276 | TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number); |
Mathieu Chartier | b9001ab | 2014-10-03 13:28:46 -0700 | [diff] [blame] | 277 | if (wait_ms > kLongWaitMs && owners_method != nullptr) { |
| 278 | LOG(WARNING) << "Long monitor contention event with owner method=" |
| 279 | << PrettyMethod(owners_method) << " from " << owners_filename << ":" |
| 280 | << owners_line_number << " waiters=" << num_waiters << " for " |
| 281 | << PrettyDuration(MsToNs(wait_ms)); |
| 282 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 283 | LogContentionEvent(self, wait_ms, sample_percent, owners_filename, owners_line_number); |
| 284 | } |
| 285 | } |
Mathieu Chartier | f0dc8b5 | 2014-12-17 10:13:30 -0800 | [diff] [blame] | 286 | ATRACE_END(); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 287 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 288 | } |
Mathieu Chartier | a6e7f08 | 2014-05-22 14:43:37 -0700 | [diff] [blame] | 289 | self->SetMonitorEnterObject(nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 290 | monitor_lock_.Lock(self); // Reacquire locks in order. |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 291 | --num_waiters_; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 292 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 295 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
| 296 | __attribute__((format(printf, 1, 2))); |
| 297 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 298 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 299 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 300 | va_list args; |
| 301 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 302 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 303 | self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 304 | if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) { |
Brian Carlstrom | 64277f3 | 2012-03-26 23:53:34 -0700 | [diff] [blame] | 305 | std::ostringstream ss; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 306 | self->Dump(ss); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 307 | LOG(Runtime::Current()->IsStarted() ? INFO : ERROR) |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 308 | << self->GetException()->Dump() << "\n" << ss.str(); |
Brian Carlstrom | 64277f3 | 2012-03-26 23:53:34 -0700 | [diff] [blame] | 309 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 310 | va_end(args); |
| 311 | } |
| 312 | |
Elliott Hughes | d423741 | 2012-02-21 11:24:45 -0800 | [diff] [blame] | 313 | static std::string ThreadToString(Thread* thread) { |
| 314 | if (thread == NULL) { |
| 315 | return "NULL"; |
| 316 | } |
| 317 | std::ostringstream oss; |
| 318 | // TODO: alternatively, we could just return the thread's name. |
| 319 | oss << *thread; |
| 320 | return oss.str(); |
| 321 | } |
| 322 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 323 | void Monitor::FailedUnlock(mirror::Object* o, Thread* expected_owner, Thread* found_owner, |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 324 | Monitor* monitor) { |
| 325 | Thread* current_owner = NULL; |
| 326 | std::string current_owner_string; |
| 327 | std::string expected_owner_string; |
| 328 | std::string found_owner_string; |
| 329 | { |
| 330 | // TODO: isn't this too late to prevent threads from disappearing? |
| 331 | // Acquire thread list lock so threads won't disappear from under us. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 332 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 333 | // Re-read owner now that we hold lock. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 334 | current_owner = (monitor != NULL) ? monitor->GetOwner() : NULL; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 335 | // Get short descriptions of the threads involved. |
| 336 | current_owner_string = ThreadToString(current_owner); |
| 337 | expected_owner_string = ThreadToString(expected_owner); |
| 338 | found_owner_string = ThreadToString(found_owner); |
| 339 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 340 | if (current_owner == NULL) { |
| 341 | if (found_owner == NULL) { |
| 342 | ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'" |
| 343 | " on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 344 | PrettyTypeOf(o).c_str(), |
| 345 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 346 | } else { |
| 347 | // Race: the original read found an owner but now there is none |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 348 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 349 | " (where now the monitor appears unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 350 | found_owner_string.c_str(), |
| 351 | PrettyTypeOf(o).c_str(), |
| 352 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 353 | } |
| 354 | } else { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 355 | if (found_owner == NULL) { |
| 356 | // Race: originally there was no owner, there is now |
| 357 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 358 | " (originally believed to be unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 359 | current_owner_string.c_str(), |
| 360 | PrettyTypeOf(o).c_str(), |
| 361 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 362 | } else { |
| 363 | if (found_owner != current_owner) { |
| 364 | // Race: originally found and current owner have changed |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 365 | ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now" |
| 366 | " owned by '%s') on object of type '%s' on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 367 | found_owner_string.c_str(), |
| 368 | current_owner_string.c_str(), |
| 369 | PrettyTypeOf(o).c_str(), |
| 370 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 371 | } else { |
| 372 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 373 | " on thread '%s", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 374 | current_owner_string.c_str(), |
| 375 | PrettyTypeOf(o).c_str(), |
| 376 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 382 | bool Monitor::Unlock(Thread* self) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 383 | DCHECK(self != NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 384 | MutexLock mu(self, monitor_lock_); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 385 | Thread* owner = owner_; |
| 386 | if (owner == self) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 387 | // We own the monitor, so nobody else can be in here. |
| 388 | if (lock_count_ == 0) { |
| 389 | owner_ = NULL; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 390 | locking_method_ = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 391 | locking_dex_pc_ = 0; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 392 | // Wake a contender. |
| 393 | monitor_contenders_.Signal(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 394 | } else { |
| 395 | --lock_count_; |
| 396 | } |
| 397 | } else { |
| 398 | // We don't own this, so we're not allowed to unlock it. |
| 399 | // The JNI spec says that we should throw IllegalMonitorStateException |
| 400 | // in this case. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 401 | FailedUnlock(GetObject(), self, owner, this); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 402 | return false; |
| 403 | } |
| 404 | return true; |
| 405 | } |
| 406 | |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 407 | void Monitor::Wait(Thread* self, int64_t ms, int32_t ns, |
| 408 | bool interruptShouldThrow, ThreadState why) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 409 | DCHECK(self != NULL); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 410 | DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 411 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 412 | monitor_lock_.Lock(self); |
| 413 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 414 | // Make sure that we hold the lock. |
| 415 | if (owner_ != self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 416 | monitor_lock_.Unlock(self); |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 417 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 418 | return; |
| 419 | } |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 420 | |
Elliott Hughes | df42c48 | 2013-01-09 12:49:02 -0800 | [diff] [blame] | 421 | // We need to turn a zero-length timed wait into a regular wait because |
| 422 | // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait(). |
| 423 | if (why == kTimedWaiting && (ms == 0 && ns == 0)) { |
| 424 | why = kWaiting; |
| 425 | } |
| 426 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 427 | // Enforce the timeout range. |
| 428 | if (ms < 0 || ns < 0 || ns > 999999) { |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 429 | monitor_lock_.Unlock(self); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 430 | self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 431 | "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 435 | /* |
| 436 | * Add ourselves to the set of threads waiting on this monitor, and |
| 437 | * release our hold. We need to let it go even if we're a few levels |
| 438 | * deep in a recursive lock, and we need to restore that later. |
| 439 | * |
| 440 | * We append to the wait set ahead of clearing the count and owner |
| 441 | * fields so the subroutine can check that the calling thread owns |
| 442 | * the monitor. Aside from that, the order of member updates is |
| 443 | * not order sensitive as we hold the pthread mutex. |
| 444 | */ |
| 445 | AppendToWaitSet(self); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 446 | ++num_waiters_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 447 | int prev_lock_count = lock_count_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 448 | lock_count_ = 0; |
| 449 | owner_ = NULL; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 450 | mirror::ArtMethod* saved_method = locking_method_; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 451 | locking_method_ = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 452 | uintptr_t saved_dex_pc = locking_dex_pc_; |
| 453 | locking_dex_pc_ = 0; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 454 | |
| 455 | /* |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 456 | * Update thread state. If the GC wakes up, it'll ignore us, knowing |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 457 | * that we won't touch any references in this state, and we'll check |
| 458 | * our suspend mode before we transition out. |
| 459 | */ |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 460 | self->TransitionFromRunnableToSuspended(why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 461 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 462 | bool was_interrupted = false; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 463 | { |
| 464 | // 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] | 465 | MutexLock mu(self, *self->GetWaitMutex()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 466 | |
| 467 | // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is |
| 468 | // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it |
| 469 | // up. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 470 | DCHECK(self->GetWaitMonitor() == nullptr); |
| 471 | self->SetWaitMonitor(this); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 472 | |
| 473 | // Release the monitor lock. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 474 | monitor_contenders_.Signal(self); |
| 475 | monitor_lock_.Unlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 476 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 477 | // Handle the case where the thread was interrupted before we called wait(). |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 478 | if (self->IsInterruptedLocked()) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 479 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 480 | } else { |
| 481 | // Wait for a notification or a timeout to occur. |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 482 | if (why == kWaiting) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 483 | self->GetWaitConditionVariable()->Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 484 | } else { |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 485 | DCHECK(why == kTimedWaiting || why == kSleeping) << why; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 486 | self->GetWaitConditionVariable()->TimedWait(self, ms, ns); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 487 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 488 | if (self->IsInterruptedLocked()) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 489 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 490 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 491 | self->SetInterruptedLocked(false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 492 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 495 | // Set self->status back to kRunnable, and self-suspend if needed. |
| 496 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 497 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 498 | { |
| 499 | // We reset the thread's wait_monitor_ field after transitioning back to runnable so |
| 500 | // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging |
| 501 | // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads |
| 502 | // are waiting on "null".) |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 503 | MutexLock mu(self, *self->GetWaitMutex()); |
| 504 | DCHECK(self->GetWaitMonitor() != nullptr); |
| 505 | self->SetWaitMonitor(nullptr); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 508 | // Re-acquire the monitor and lock. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 509 | Lock(self); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 510 | monitor_lock_.Lock(self); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 511 | self->GetWaitMutex()->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 512 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 513 | /* |
| 514 | * We remove our thread from wait set after restoring the count |
| 515 | * and owner fields so the subroutine can check that the calling |
| 516 | * thread owns the monitor. Aside from that, the order of member |
| 517 | * updates is not order sensitive as we hold the pthread mutex. |
| 518 | */ |
| 519 | owner_ = self; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 520 | lock_count_ = prev_lock_count; |
| 521 | locking_method_ = saved_method; |
| 522 | locking_dex_pc_ = saved_dex_pc; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 523 | --num_waiters_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 524 | RemoveFromWaitSet(self); |
| 525 | |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 526 | monitor_lock_.Unlock(self); |
| 527 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 528 | if (was_interrupted) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 529 | /* |
| 530 | * We were interrupted while waiting, or somebody interrupted an |
| 531 | * un-interruptible thread earlier and we're bailing out immediately. |
| 532 | * |
| 533 | * The doc sayeth: "The interrupted status of the current thread is |
| 534 | * cleared when this exception is thrown." |
| 535 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 536 | { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 537 | MutexLock mu(self, *self->GetWaitMutex()); |
| 538 | self->SetInterruptedLocked(false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 539 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 540 | if (interruptShouldThrow) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 541 | self->ThrowNewException("Ljava/lang/InterruptedException;", NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | void Monitor::Notify(Thread* self) { |
| 547 | DCHECK(self != NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 548 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 549 | // Make sure that we hold the lock. |
| 550 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 551 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | // Signal the first waiting thread in the wait set. |
| 555 | while (wait_set_ != NULL) { |
| 556 | Thread* thread = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 557 | wait_set_ = thread->GetWaitNext(); |
| 558 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 559 | |
| 560 | // Check to see if the thread is still waiting. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 561 | MutexLock wait_mu(self, *thread->GetWaitMutex()); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 562 | if (thread->GetWaitMonitor() != nullptr) { |
| 563 | thread->GetWaitConditionVariable()->Signal(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 564 | return; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void Monitor::NotifyAll(Thread* self) { |
| 570 | DCHECK(self != NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 571 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 572 | // Make sure that we hold the lock. |
| 573 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 574 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 575 | return; |
| 576 | } |
| 577 | // Signal all threads in the wait set. |
| 578 | while (wait_set_ != NULL) { |
| 579 | Thread* thread = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 580 | wait_set_ = thread->GetWaitNext(); |
| 581 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 582 | thread->Notify(); |
| 583 | } |
| 584 | } |
| 585 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 586 | bool Monitor::Deflate(Thread* self, mirror::Object* obj) { |
| 587 | DCHECK(obj != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 588 | // Don't need volatile since we only deflate with mutators suspended. |
| 589 | LockWord lw(obj->GetLockWord(false)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 590 | // If the lock isn't an inflated monitor, then we don't need to deflate anything. |
| 591 | if (lw.GetState() == LockWord::kFatLocked) { |
| 592 | Monitor* monitor = lw.FatLockMonitor(); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 593 | DCHECK(monitor != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 594 | MutexLock mu(self, monitor->monitor_lock_); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 595 | // Can't deflate if we have anybody waiting on the CV. |
| 596 | if (monitor->num_waiters_ > 0) { |
| 597 | return false; |
| 598 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 599 | Thread* owner = monitor->owner_; |
| 600 | if (owner != nullptr) { |
| 601 | // Can't deflate if we are locked and have a hash code. |
| 602 | if (monitor->HasHashCode()) { |
| 603 | return false; |
| 604 | } |
| 605 | // Can't deflate if our lock count is too high. |
| 606 | if (monitor->lock_count_ > LockWord::kThinLockMaxCount) { |
| 607 | return false; |
| 608 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 609 | // Deflate to a thin lock. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 610 | LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_, |
| 611 | lw.ReadBarrierState()); |
| 612 | // Assume no concurrent read barrier state changes as mutators are suspended. |
| 613 | obj->SetLockWord(new_lw, false); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 614 | VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / " |
| 615 | << monitor->lock_count_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 616 | } else if (monitor->HasHashCode()) { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 617 | LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.ReadBarrierState()); |
| 618 | // Assume no concurrent read barrier state changes as mutators are suspended. |
| 619 | obj->SetLockWord(new_lw, false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 620 | VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 621 | } else { |
| 622 | // No lock and no hash, just put an empty lock word inside the object. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 623 | LockWord new_lw = LockWord::FromDefault(lw.ReadBarrierState()); |
| 624 | // Assume no concurrent read barrier state changes as mutators are suspended. |
| 625 | obj->SetLockWord(new_lw, false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 626 | VLOG(monitor) << "Deflated" << obj << " to empty lock word"; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 627 | } |
| 628 | // The monitor is deflated, mark the object as nullptr so that we know to delete it during the |
| 629 | // next GC. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 630 | monitor->obj_ = GcRoot<mirror::Object>(nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 631 | } |
| 632 | return true; |
| 633 | } |
| 634 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 635 | 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] | 636 | DCHECK(self != nullptr); |
| 637 | DCHECK(obj != nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 638 | // Allocate and acquire a new monitor. |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 639 | Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code); |
| 640 | DCHECK(m != nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 641 | if (m->Install(self)) { |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 642 | if (owner != nullptr) { |
| 643 | VLOG(monitor) << "monitor: thread" << owner->GetThreadId() |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 644 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 645 | } else { |
| 646 | VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 647 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 648 | } |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 649 | Runtime::Current()->GetMonitorList()->Add(m); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 650 | CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 651 | } else { |
| 652 | MonitorPool::ReleaseMonitor(self, m); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 653 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 656 | void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 657 | uint32_t hash_code) { |
| 658 | DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked); |
| 659 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 660 | if (owner_thread_id == self->GetThreadId()) { |
| 661 | // We own the monitor, we can easily inflate it. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 662 | Inflate(self, self, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 663 | } else { |
| 664 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 665 | // 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] | 666 | self->SetMonitorEnterObject(obj.Get()); |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 667 | bool timed_out; |
| 668 | Thread* owner; |
| 669 | { |
| 670 | ScopedThreadStateChange tsc(self, kBlocked); |
| 671 | owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out); |
| 672 | } |
| 673 | if (owner != nullptr) { |
| 674 | // We succeeded in suspending the thread, check the lock's status didn't change. |
| 675 | lock_word = obj->GetLockWord(true); |
| 676 | if (lock_word.GetState() == LockWord::kThinLocked && |
| 677 | lock_word.ThinLockOwner() == owner_thread_id) { |
| 678 | // Go ahead and inflate the lock. |
| 679 | Inflate(self, owner, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 680 | } |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 681 | thread_list->Resume(owner, false); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 682 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 683 | self->SetMonitorEnterObject(nullptr); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 684 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 687 | // Fool annotalysis into thinking that the lock on obj is acquired. |
| 688 | static mirror::Object* FakeLock(mirror::Object* obj) |
| 689 | EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 690 | return obj; |
| 691 | } |
| 692 | |
| 693 | // Fool annotalysis into thinking that the lock on obj is release. |
| 694 | static mirror::Object* FakeUnlock(mirror::Object* obj) |
| 695 | UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 696 | return obj; |
| 697 | } |
| 698 | |
Mathieu Chartier | e7e8a5f | 2014-02-14 16:59:41 -0800 | [diff] [blame] | 699 | mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 700 | DCHECK(self != NULL); |
| 701 | DCHECK(obj != NULL); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 702 | obj = FakeLock(obj); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 703 | uint32_t thread_id = self->GetThreadId(); |
| 704 | size_t contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 705 | StackHandleScope<1> hs(self); |
| 706 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 707 | while (true) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 708 | LockWord lock_word = h_obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 709 | switch (lock_word.GetState()) { |
| 710 | case LockWord::kUnlocked: { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 711 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.ReadBarrierState())); |
Ian Rogers | 228602f | 2014-07-10 02:07:54 -0700 | [diff] [blame] | 712 | if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 713 | // CasLockWord enforces more than the acquire ordering we need here. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 714 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 715 | } |
| 716 | continue; // Go again. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 717 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 718 | case LockWord::kThinLocked: { |
| 719 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 720 | if (owner_thread_id == thread_id) { |
| 721 | // We own the lock, increase the recursion count. |
| 722 | uint32_t new_count = lock_word.ThinLockCount() + 1; |
| 723 | if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 724 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count, |
| 725 | lock_word.ReadBarrierState())); |
| 726 | if (!kUseReadBarrier) { |
| 727 | h_obj->SetLockWord(thin_locked, true); |
| 728 | return h_obj.Get(); // Success! |
| 729 | } else { |
| 730 | // Use CAS to preserve the read barrier state. |
| 731 | if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) { |
| 732 | return h_obj.Get(); // Success! |
| 733 | } |
| 734 | } |
| 735 | continue; // Go again. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 736 | } else { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 737 | // We'd overflow the recursion count, so inflate the monitor. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 738 | InflateThinLocked(self, h_obj, lock_word, 0); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 739 | } |
| 740 | } else { |
| 741 | // Contention. |
| 742 | contention_count++; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 743 | Runtime* runtime = Runtime::Current(); |
| 744 | if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 745 | // TODO: Consider switching the thread state to kBlocked when we are yielding. |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 746 | // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the |
| 747 | // 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] | 748 | // and make long pauses. See b/16307460. |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 749 | sched_yield(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 750 | } else { |
| 751 | contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 752 | InflateThinLocked(self, h_obj, lock_word, 0); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 753 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 754 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 755 | continue; // Start from the beginning. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 756 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 757 | case LockWord::kFatLocked: { |
| 758 | Monitor* mon = lock_word.FatLockMonitor(); |
| 759 | mon->Lock(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 760 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 761 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 762 | case LockWord::kHashCode: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 763 | // Inflate with the existing hashcode. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 764 | Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode()); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 765 | continue; // Start from the beginning. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 766 | default: { |
| 767 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 768 | return h_obj.Get(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 769 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 770 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 774 | bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 775 | DCHECK(self != NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 776 | DCHECK(obj != NULL); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 777 | obj = FakeUnlock(obj); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 778 | StackHandleScope<1> hs(self); |
| 779 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 780 | while (true) { |
| 781 | LockWord lock_word = obj->GetLockWord(true); |
| 782 | switch (lock_word.GetState()) { |
| 783 | case LockWord::kHashCode: |
| 784 | // Fall-through. |
| 785 | case LockWord::kUnlocked: |
| 786 | FailedUnlock(h_obj.Get(), self, nullptr, nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 787 | return false; // Failure. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 788 | case LockWord::kThinLocked: { |
| 789 | uint32_t thread_id = self->GetThreadId(); |
| 790 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 791 | if (owner_thread_id != thread_id) { |
| 792 | // TODO: there's a race here with the owner dying while we unlock. |
| 793 | Thread* owner = |
| 794 | Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
| 795 | FailedUnlock(h_obj.Get(), self, owner, nullptr); |
| 796 | return false; // Failure. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 797 | } else { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 798 | // We own the lock, decrease the recursion count. |
| 799 | LockWord new_lw = LockWord::Default(); |
| 800 | if (lock_word.ThinLockCount() != 0) { |
| 801 | uint32_t new_count = lock_word.ThinLockCount() - 1; |
| 802 | new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.ReadBarrierState()); |
| 803 | } else { |
| 804 | new_lw = LockWord::FromDefault(lock_word.ReadBarrierState()); |
| 805 | } |
| 806 | if (!kUseReadBarrier) { |
| 807 | DCHECK_EQ(new_lw.ReadBarrierState(), 0U); |
| 808 | h_obj->SetLockWord(new_lw, true); |
| 809 | // Success! |
| 810 | return true; |
| 811 | } else { |
| 812 | // Use CAS to preserve the read barrier state. |
| 813 | if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, new_lw)) { |
| 814 | // Success! |
| 815 | return true; |
| 816 | } |
| 817 | } |
| 818 | continue; // Go again. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 819 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 820 | } |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 821 | case LockWord::kFatLocked: { |
| 822 | Monitor* mon = lock_word.FatLockMonitor(); |
| 823 | return mon->Unlock(self); |
| 824 | } |
| 825 | default: { |
| 826 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 827 | return false; |
| 828 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 829 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 830 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 833 | 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] | 834 | bool interruptShouldThrow, ThreadState why) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 835 | DCHECK(self != nullptr); |
| 836 | DCHECK(obj != nullptr); |
| 837 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 838 | while (lock_word.GetState() != LockWord::kFatLocked) { |
| 839 | switch (lock_word.GetState()) { |
| 840 | case LockWord::kHashCode: |
| 841 | // Fall-through. |
| 842 | case LockWord::kUnlocked: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 843 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
| 844 | return; // Failure. |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 845 | case LockWord::kThinLocked: { |
| 846 | uint32_t thread_id = self->GetThreadId(); |
| 847 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 848 | if (owner_thread_id != thread_id) { |
| 849 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
| 850 | return; // Failure. |
| 851 | } else { |
| 852 | // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so |
| 853 | // re-load. |
| 854 | Inflate(self, self, obj, 0); |
| 855 | lock_word = obj->GetLockWord(true); |
| 856 | } |
| 857 | break; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 858 | } |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 859 | case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through. |
| 860 | default: { |
| 861 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 862 | return; |
| 863 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 864 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 865 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 866 | Monitor* mon = lock_word.FatLockMonitor(); |
| 867 | mon->Wait(self, ms, ns, interruptShouldThrow, why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Ian Rogers | 13c479e | 2013-10-11 07:59:01 -0700 | [diff] [blame] | 870 | void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 871 | DCHECK(self != nullptr); |
| 872 | DCHECK(obj != nullptr); |
| 873 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 874 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 875 | case LockWord::kHashCode: |
| 876 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 877 | case LockWord::kUnlocked: |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 878 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 879 | return; // Failure. |
| 880 | case LockWord::kThinLocked: { |
| 881 | uint32_t thread_id = self->GetThreadId(); |
| 882 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 883 | if (owner_thread_id != thread_id) { |
| 884 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
| 885 | return; // Failure. |
| 886 | } else { |
| 887 | // We own the lock but there's no Monitor and therefore no waiters. |
| 888 | return; // Success. |
| 889 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 890 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 891 | case LockWord::kFatLocked: { |
| 892 | Monitor* mon = lock_word.FatLockMonitor(); |
| 893 | if (notify_all) { |
| 894 | mon->NotifyAll(self); |
| 895 | } else { |
| 896 | mon->Notify(self); |
| 897 | } |
| 898 | return; // Success. |
| 899 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 900 | default: { |
| 901 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 902 | return; |
| 903 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 907 | uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 908 | DCHECK(obj != nullptr); |
| 909 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 910 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 911 | case LockWord::kHashCode: |
| 912 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 913 | case LockWord::kUnlocked: |
| 914 | return ThreadList::kInvalidThreadId; |
| 915 | case LockWord::kThinLocked: |
| 916 | return lock_word.ThinLockOwner(); |
| 917 | case LockWord::kFatLocked: { |
| 918 | Monitor* mon = lock_word.FatLockMonitor(); |
| 919 | return mon->GetOwnerThreadId(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 920 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 921 | default: { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 922 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 923 | UNREACHABLE(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 924 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 928 | void Monitor::DescribeWait(std::ostream& os, const Thread* thread) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 929 | // Determine the wait message and object we're waiting or blocked upon. |
| 930 | mirror::Object* pretty_object = nullptr; |
| 931 | const char* wait_message = nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 932 | uint32_t lock_owner = ThreadList::kInvalidThreadId; |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 933 | ThreadState state = thread->GetState(); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 934 | if (state == kWaiting || state == kTimedWaiting || state == kSleeping) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 935 | wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on "; |
| 936 | Thread* self = Thread::Current(); |
| 937 | MutexLock mu(self, *thread->GetWaitMutex()); |
| 938 | Monitor* monitor = thread->GetWaitMonitor(); |
| 939 | if (monitor != nullptr) { |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 940 | pretty_object = monitor->GetObject(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 941 | } |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 942 | } else if (state == kBlocked) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 943 | wait_message = " - waiting to lock "; |
| 944 | pretty_object = thread->GetMonitorEnterObject(); |
| 945 | if (pretty_object != nullptr) { |
| 946 | lock_owner = pretty_object->GetLockOwnerThreadId(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 947 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 948 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 949 | |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 950 | if (wait_message != nullptr) { |
| 951 | if (pretty_object == nullptr) { |
| 952 | os << wait_message << "an unknown object"; |
| 953 | } else { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 954 | if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) && |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 955 | Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) { |
| 956 | // Getting the identity hashcode here would result in lock inflation and suspension of the |
| 957 | // current thread, which isn't safe if this is the only runnable thread. |
| 958 | os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)", |
| 959 | reinterpret_cast<intptr_t>(pretty_object), |
| 960 | PrettyTypeOf(pretty_object).c_str()); |
| 961 | } else { |
| 962 | // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>) |
Mathieu Chartier | 4936159 | 2015-01-22 16:36:10 -0800 | [diff] [blame] | 963 | // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread |
| 964 | // suspension and move pretty_object. |
| 965 | const std::string pretty_type(PrettyTypeOf(pretty_object)); |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 966 | os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(), |
Mathieu Chartier | 4936159 | 2015-01-22 16:36:10 -0800 | [diff] [blame] | 967 | pretty_type.c_str()); |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5 |
| 971 | if (lock_owner != ThreadList::kInvalidThreadId) { |
| 972 | os << " held by thread " << lock_owner; |
| 973 | } |
| 974 | os << "\n"; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 975 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 978 | mirror::Object* Monitor::GetContendedMonitor(Thread* thread) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 979 | // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre |
| 980 | // 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] | 981 | mirror::Object* result = thread->GetMonitorEnterObject(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 982 | if (result == NULL) { |
| 983 | // ...but also a monitor that the thread is waiting on. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 984 | MutexLock mu(Thread::Current(), *thread->GetWaitMutex()); |
| 985 | Monitor* monitor = thread->GetWaitMonitor(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 986 | if (monitor != NULL) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 987 | result = monitor->GetObject(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 988 | } |
| 989 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 990 | return result; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 993 | void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*), |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 994 | void* callback_context, bool abort_on_failure) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 995 | mirror::ArtMethod* m = stack_visitor->GetMethod(); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 996 | CHECK(m != NULL); |
| 997 | |
| 998 | // Native methods are an easy special case. |
| 999 | // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. |
| 1000 | if (m->IsNative()) { |
| 1001 | if (m->IsSynchronized()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1002 | mirror::Object* jni_this = stack_visitor->GetCurrentHandleScope()->GetReference(0); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 1003 | callback(jni_this, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1004 | } |
| 1005 | return; |
| 1006 | } |
| 1007 | |
jeffhao | 61f916c | 2012-10-25 17:48:51 -0700 | [diff] [blame] | 1008 | // Proxy methods should not be synchronized. |
| 1009 | if (m->IsProxyMethod()) { |
| 1010 | CHECK(!m->IsSynchronized()); |
| 1011 | return; |
| 1012 | } |
| 1013 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1014 | // 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] | 1015 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1016 | CHECK(code_item != NULL) << PrettyMethod(m); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1017 | if (code_item->tries_size_ == 0) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1018 | return; // No "tries" implies no synchronization, so no held locks to report. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1021 | // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot |
| 1022 | // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an |
| 1023 | // inconsistent stack anyways. |
| 1024 | uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure); |
| 1025 | if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) { |
| 1026 | LOG(ERROR) << "Could not find dex_pc for " << PrettyMethod(m); |
| 1027 | return; |
| 1028 | } |
| 1029 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1030 | // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to |
| 1031 | // the locks held in this stack frame. |
| 1032 | std::vector<uint32_t> monitor_enter_dex_pcs; |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1033 | verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs); |
Mathieu Chartier | e6a8eec | 2015-01-06 14:17:57 -0800 | [diff] [blame] | 1034 | for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) { |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1035 | // The verifier works in terms of the dex pcs of the monitor-enter instructions. |
| 1036 | // We want the registers used by those instructions (so we can read the values out of them). |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1037 | uint16_t monitor_enter_instruction = code_item->insns_[monitor_dex_pc]; |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1038 | |
| 1039 | // Quick sanity check. |
| 1040 | if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1041 | LOG(FATAL) << "expected monitor-enter @" << monitor_dex_pc << "; was " |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1042 | << reinterpret_cast<void*>(monitor_enter_instruction); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1045 | uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff); |
Nicolas Geoffray | 15b9d52 | 2015-03-12 15:05:13 +0000 | [diff] [blame] | 1046 | uint32_t value; |
| 1047 | bool success = stack_visitor->GetVReg(m, monitor_register, kReferenceVReg, &value); |
| 1048 | CHECK(success) << "Failed to read v" << monitor_register << " of kind " |
| 1049 | << kReferenceVReg << " in method " << PrettyMethod(m); |
| 1050 | mirror::Object* o = reinterpret_cast<mirror::Object*>(value); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 1051 | callback(o, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1055 | bool Monitor::IsValidLockWord(LockWord lock_word) { |
| 1056 | switch (lock_word.GetState()) { |
| 1057 | case LockWord::kUnlocked: |
| 1058 | // Nothing to check. |
| 1059 | return true; |
| 1060 | case LockWord::kThinLocked: |
| 1061 | // Basic sanity check of owner. |
| 1062 | return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId; |
| 1063 | case LockWord::kFatLocked: { |
| 1064 | // Check the monitor appears in the monitor list. |
| 1065 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1066 | MonitorList* list = Runtime::Current()->GetMonitorList(); |
| 1067 | MutexLock mu(Thread::Current(), list->monitor_list_lock_); |
| 1068 | for (Monitor* list_mon : list->list_) { |
| 1069 | if (mon == list_mon) { |
| 1070 | return true; // Found our monitor. |
| 1071 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1072 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1073 | return false; // Fail - unowned monitor in an object. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1074 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1075 | case LockWord::kHashCode: |
| 1076 | return true; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1077 | default: |
| 1078 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1079 | UNREACHABLE(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1083 | bool Monitor::IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1084 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1085 | return owner_ != nullptr; |
| 1086 | } |
| 1087 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1088 | void Monitor::TranslateLocation(mirror::ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1089 | const char** source_file, uint32_t* line_number) const { |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1090 | // If method is null, location is unknown |
| 1091 | if (method == NULL) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1092 | *source_file = ""; |
| 1093 | *line_number = 0; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1094 | return; |
| 1095 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1096 | *source_file = method->GetDeclaringClassSourceFile(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1097 | if (*source_file == NULL) { |
| 1098 | *source_file = ""; |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 1099 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1100 | *line_number = method->GetLineNumFromDexPC(dex_pc); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | uint32_t Monitor::GetOwnerThreadId() { |
| 1104 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1105 | Thread* owner = owner_; |
| 1106 | if (owner != NULL) { |
| 1107 | return owner->GetThreadId(); |
| 1108 | } else { |
| 1109 | return ThreadList::kInvalidThreadId; |
| 1110 | } |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1113 | MonitorList::MonitorList() |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1114 | : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock), |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1115 | monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | MonitorList::~MonitorList() { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1119 | Thread* self = Thread::Current(); |
| 1120 | MutexLock mu(self, monitor_list_lock_); |
| 1121 | // Release all monitors to the pool. |
| 1122 | // TODO: Is it an invariant that *all* open monitors are in the list? Then we could |
| 1123 | // clear faster in the pool. |
| 1124 | MonitorPool::ReleaseMonitors(self, &list_); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1127 | void MonitorList::DisallowNewMonitors() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1128 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1129 | allow_new_monitors_ = false; |
| 1130 | } |
| 1131 | |
| 1132 | void MonitorList::AllowNewMonitors() { |
| 1133 | Thread* self = Thread::Current(); |
| 1134 | MutexLock mu(self, monitor_list_lock_); |
| 1135 | allow_new_monitors_ = true; |
| 1136 | monitor_add_condition_.Broadcast(self); |
| 1137 | } |
| 1138 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 1139 | void MonitorList::EnsureNewMonitorsDisallowed() { |
| 1140 | // Lock and unlock once to ensure that no threads are still in the |
| 1141 | // middle of adding new monitors. |
| 1142 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
| 1143 | CHECK(!allow_new_monitors_); |
| 1144 | } |
| 1145 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1146 | void MonitorList::Add(Monitor* m) { |
| 1147 | Thread* self = Thread::Current(); |
| 1148 | MutexLock mu(self, monitor_list_lock_); |
| 1149 | while (UNLIKELY(!allow_new_monitors_)) { |
| 1150 | monitor_add_condition_.WaitHoldingLocks(self); |
| 1151 | } |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1152 | list_.push_front(m); |
| 1153 | } |
| 1154 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1155 | void MonitorList::SweepMonitorList(IsMarkedCallback* callback, void* arg) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1156 | Thread* self = Thread::Current(); |
| 1157 | MutexLock mu(self, monitor_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1158 | for (auto it = list_.begin(); it != list_.end(); ) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1159 | Monitor* m = *it; |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1160 | // Disable the read barrier in GetObject() as this is called by GC. |
| 1161 | mirror::Object* obj = m->GetObject<kWithoutReadBarrier>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1162 | // The object of a monitor can be null if we have deflated it. |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1163 | mirror::Object* new_obj = obj != nullptr ? callback(obj, arg) : nullptr; |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1164 | if (new_obj == nullptr) { |
| 1165 | VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1166 | << obj; |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1167 | MonitorPool::ReleaseMonitor(self, m); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1168 | it = list_.erase(it); |
| 1169 | } else { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1170 | m->SetObject(new_obj); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1171 | ++it; |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1176 | struct MonitorDeflateArgs { |
| 1177 | MonitorDeflateArgs() : self(Thread::Current()), deflate_count(0) {} |
| 1178 | Thread* const self; |
| 1179 | size_t deflate_count; |
| 1180 | }; |
| 1181 | |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1182 | static mirror::Object* MonitorDeflateCallback(mirror::Object* object, void* arg) |
| 1183 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1184 | MonitorDeflateArgs* args = reinterpret_cast<MonitorDeflateArgs*>(arg); |
| 1185 | if (Monitor::Deflate(args->self, object)) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1186 | DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1187 | ++args->deflate_count; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1188 | // If we deflated, return nullptr so that the monitor gets removed from the array. |
| 1189 | return nullptr; |
| 1190 | } |
| 1191 | return object; // Monitor was not deflated. |
| 1192 | } |
| 1193 | |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1194 | size_t MonitorList::DeflateMonitors() { |
| 1195 | MonitorDeflateArgs args; |
| 1196 | Locks::mutator_lock_->AssertExclusiveHeld(args.self); |
| 1197 | SweepMonitorList(MonitorDeflateCallback, &args); |
| 1198 | return args.deflate_count; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1201 | MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(NULL), entry_count_(0) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1202 | DCHECK(obj != nullptr); |
| 1203 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1204 | switch (lock_word.GetState()) { |
| 1205 | case LockWord::kUnlocked: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1206 | // Fall-through. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1207 | case LockWord::kForwardingAddress: |
| 1208 | // Fall-through. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1209 | case LockWord::kHashCode: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1210 | break; |
| 1211 | case LockWord::kThinLocked: |
| 1212 | owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
| 1213 | entry_count_ = 1 + lock_word.ThinLockCount(); |
| 1214 | // Thin locks have no waiters. |
| 1215 | break; |
| 1216 | case LockWord::kFatLocked: { |
| 1217 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1218 | owner_ = mon->owner_; |
| 1219 | entry_count_ = 1 + mon->lock_count_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1220 | for (Thread* waiter = mon->wait_set_; waiter != NULL; waiter = waiter->GetWaitNext()) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1221 | waiters_.push_back(waiter); |
| 1222 | } |
| 1223 | break; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1228 | } // namespace art |