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 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 168 | LockWord fat(this); |
| 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(); |
| 303 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 304 | self->ThrowNewExceptionV(throw_location, "Ljava/lang/IllegalMonitorStateException;", fmt, args); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 305 | if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) { |
Brian Carlstrom | 64277f3 | 2012-03-26 23:53:34 -0700 | [diff] [blame] | 306 | std::ostringstream ss; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 307 | self->Dump(ss); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 308 | LOG(Runtime::Current()->IsStarted() ? INFO : ERROR) |
| 309 | << self->GetException(NULL)->Dump() << "\n" << ss.str(); |
Brian Carlstrom | 64277f3 | 2012-03-26 23:53:34 -0700 | [diff] [blame] | 310 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 311 | va_end(args); |
| 312 | } |
| 313 | |
Elliott Hughes | d423741 | 2012-02-21 11:24:45 -0800 | [diff] [blame] | 314 | static std::string ThreadToString(Thread* thread) { |
| 315 | if (thread == NULL) { |
| 316 | return "NULL"; |
| 317 | } |
| 318 | std::ostringstream oss; |
| 319 | // TODO: alternatively, we could just return the thread's name. |
| 320 | oss << *thread; |
| 321 | return oss.str(); |
| 322 | } |
| 323 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 324 | void Monitor::FailedUnlock(mirror::Object* o, Thread* expected_owner, Thread* found_owner, |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 325 | Monitor* monitor) { |
| 326 | Thread* current_owner = NULL; |
| 327 | std::string current_owner_string; |
| 328 | std::string expected_owner_string; |
| 329 | std::string found_owner_string; |
| 330 | { |
| 331 | // TODO: isn't this too late to prevent threads from disappearing? |
| 332 | // Acquire thread list lock so threads won't disappear from under us. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 333 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 334 | // Re-read owner now that we hold lock. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 335 | current_owner = (monitor != NULL) ? monitor->GetOwner() : NULL; |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 336 | // Get short descriptions of the threads involved. |
| 337 | current_owner_string = ThreadToString(current_owner); |
| 338 | expected_owner_string = ThreadToString(expected_owner); |
| 339 | found_owner_string = ThreadToString(found_owner); |
| 340 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 341 | if (current_owner == NULL) { |
| 342 | if (found_owner == NULL) { |
| 343 | ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'" |
| 344 | " on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 345 | PrettyTypeOf(o).c_str(), |
| 346 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 347 | } else { |
| 348 | // Race: the original read found an owner but now there is none |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 349 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 350 | " (where now the monitor appears unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 351 | found_owner_string.c_str(), |
| 352 | PrettyTypeOf(o).c_str(), |
| 353 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 354 | } |
| 355 | } else { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 356 | if (found_owner == NULL) { |
| 357 | // Race: originally there was no owner, there is now |
| 358 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 359 | " (originally believed to be unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 360 | current_owner_string.c_str(), |
| 361 | PrettyTypeOf(o).c_str(), |
| 362 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 363 | } else { |
| 364 | if (found_owner != current_owner) { |
| 365 | // Race: originally found and current owner have changed |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 366 | ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now" |
| 367 | " owned by '%s') on object of type '%s' on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 368 | found_owner_string.c_str(), |
| 369 | current_owner_string.c_str(), |
| 370 | PrettyTypeOf(o).c_str(), |
| 371 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 372 | } else { |
| 373 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 374 | " on thread '%s", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 375 | current_owner_string.c_str(), |
| 376 | PrettyTypeOf(o).c_str(), |
| 377 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 383 | bool Monitor::Unlock(Thread* self) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 384 | DCHECK(self != NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 385 | MutexLock mu(self, monitor_lock_); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 386 | Thread* owner = owner_; |
| 387 | if (owner == self) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 388 | // We own the monitor, so nobody else can be in here. |
| 389 | if (lock_count_ == 0) { |
| 390 | owner_ = NULL; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 391 | locking_method_ = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 392 | locking_dex_pc_ = 0; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 393 | // Wake a contender. |
| 394 | monitor_contenders_.Signal(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 395 | } else { |
| 396 | --lock_count_; |
| 397 | } |
| 398 | } else { |
| 399 | // We don't own this, so we're not allowed to unlock it. |
| 400 | // The JNI spec says that we should throw IllegalMonitorStateException |
| 401 | // in this case. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 402 | FailedUnlock(GetObject(), self, owner, this); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | return true; |
| 406 | } |
| 407 | |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 408 | void Monitor::Wait(Thread* self, int64_t ms, int32_t ns, |
| 409 | bool interruptShouldThrow, ThreadState why) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 410 | DCHECK(self != NULL); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 411 | DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 412 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 413 | monitor_lock_.Lock(self); |
| 414 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 415 | // Make sure that we hold the lock. |
| 416 | if (owner_ != self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 417 | monitor_lock_.Unlock(self); |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 418 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 419 | return; |
| 420 | } |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 421 | |
Elliott Hughes | df42c48 | 2013-01-09 12:49:02 -0800 | [diff] [blame] | 422 | // We need to turn a zero-length timed wait into a regular wait because |
| 423 | // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait(). |
| 424 | if (why == kTimedWaiting && (ms == 0 && ns == 0)) { |
| 425 | why = kWaiting; |
| 426 | } |
| 427 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 428 | // Enforce the timeout range. |
| 429 | if (ms < 0 || ns < 0 || ns > 999999) { |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 430 | monitor_lock_.Unlock(self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 431 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 432 | self->ThrowNewExceptionF(throw_location, "Ljava/lang/IllegalArgumentException;", |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 433 | "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 437 | /* |
| 438 | * Add ourselves to the set of threads waiting on this monitor, and |
| 439 | * release our hold. We need to let it go even if we're a few levels |
| 440 | * deep in a recursive lock, and we need to restore that later. |
| 441 | * |
| 442 | * We append to the wait set ahead of clearing the count and owner |
| 443 | * fields so the subroutine can check that the calling thread owns |
| 444 | * the monitor. Aside from that, the order of member updates is |
| 445 | * not order sensitive as we hold the pthread mutex. |
| 446 | */ |
| 447 | AppendToWaitSet(self); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 448 | ++num_waiters_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 449 | int prev_lock_count = lock_count_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 450 | lock_count_ = 0; |
| 451 | owner_ = NULL; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 452 | mirror::ArtMethod* saved_method = locking_method_; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 453 | locking_method_ = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 454 | uintptr_t saved_dex_pc = locking_dex_pc_; |
| 455 | locking_dex_pc_ = 0; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 456 | |
| 457 | /* |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 458 | * 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] | 459 | * that we won't touch any references in this state, and we'll check |
| 460 | * our suspend mode before we transition out. |
| 461 | */ |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 462 | self->TransitionFromRunnableToSuspended(why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 463 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 464 | bool was_interrupted = false; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 465 | { |
| 466 | // 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] | 467 | MutexLock mu(self, *self->GetWaitMutex()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 468 | |
| 469 | // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is |
| 470 | // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it |
| 471 | // up. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 472 | DCHECK(self->GetWaitMonitor() == nullptr); |
| 473 | self->SetWaitMonitor(this); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 474 | |
| 475 | // Release the monitor lock. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 476 | monitor_contenders_.Signal(self); |
| 477 | monitor_lock_.Unlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 478 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 479 | // Handle the case where the thread was interrupted before we called wait(). |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 480 | if (self->IsInterruptedLocked()) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 481 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 482 | } else { |
| 483 | // Wait for a notification or a timeout to occur. |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 484 | if (why == kWaiting) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 485 | self->GetWaitConditionVariable()->Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 486 | } else { |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 487 | DCHECK(why == kTimedWaiting || why == kSleeping) << why; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 488 | self->GetWaitConditionVariable()->TimedWait(self, ms, ns); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 489 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 490 | if (self->IsInterruptedLocked()) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 491 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 492 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 493 | self->SetInterruptedLocked(false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 494 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 497 | // Set self->status back to kRunnable, and self-suspend if needed. |
| 498 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 499 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 500 | { |
| 501 | // We reset the thread's wait_monitor_ field after transitioning back to runnable so |
| 502 | // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging |
| 503 | // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads |
| 504 | // are waiting on "null".) |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 505 | MutexLock mu(self, *self->GetWaitMutex()); |
| 506 | DCHECK(self->GetWaitMonitor() != nullptr); |
| 507 | self->SetWaitMonitor(nullptr); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 510 | // Re-acquire the monitor and lock. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 511 | Lock(self); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 512 | monitor_lock_.Lock(self); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 513 | self->GetWaitMutex()->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 514 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 515 | /* |
| 516 | * We remove our thread from wait set after restoring the count |
| 517 | * and owner fields so the subroutine can check that the calling |
| 518 | * thread owns the monitor. Aside from that, the order of member |
| 519 | * updates is not order sensitive as we hold the pthread mutex. |
| 520 | */ |
| 521 | owner_ = self; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 522 | lock_count_ = prev_lock_count; |
| 523 | locking_method_ = saved_method; |
| 524 | locking_dex_pc_ = saved_dex_pc; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 525 | --num_waiters_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 526 | RemoveFromWaitSet(self); |
| 527 | |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 528 | monitor_lock_.Unlock(self); |
| 529 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 530 | if (was_interrupted) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 531 | /* |
| 532 | * We were interrupted while waiting, or somebody interrupted an |
| 533 | * un-interruptible thread earlier and we're bailing out immediately. |
| 534 | * |
| 535 | * The doc sayeth: "The interrupted status of the current thread is |
| 536 | * cleared when this exception is thrown." |
| 537 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 538 | { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 539 | MutexLock mu(self, *self->GetWaitMutex()); |
| 540 | self->SetInterruptedLocked(false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 541 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 542 | if (interruptShouldThrow) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 543 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 544 | self->ThrowNewException(throw_location, "Ljava/lang/InterruptedException;", NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | void Monitor::Notify(Thread* self) { |
| 550 | DCHECK(self != NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 551 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 552 | // Make sure that we hold the lock. |
| 553 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 554 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 555 | return; |
| 556 | } |
| 557 | // Signal the first waiting thread in the wait set. |
| 558 | while (wait_set_ != NULL) { |
| 559 | Thread* thread = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 560 | wait_set_ = thread->GetWaitNext(); |
| 561 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 562 | |
| 563 | // Check to see if the thread is still waiting. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 564 | MutexLock wait_mu(self, *thread->GetWaitMutex()); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 565 | if (thread->GetWaitMonitor() != nullptr) { |
| 566 | thread->GetWaitConditionVariable()->Signal(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 567 | return; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | void Monitor::NotifyAll(Thread* self) { |
| 573 | DCHECK(self != NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 574 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 575 | // Make sure that we hold the lock. |
| 576 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 577 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 578 | return; |
| 579 | } |
| 580 | // Signal all threads in the wait set. |
| 581 | while (wait_set_ != NULL) { |
| 582 | Thread* thread = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 583 | wait_set_ = thread->GetWaitNext(); |
| 584 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 585 | thread->Notify(); |
| 586 | } |
| 587 | } |
| 588 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 589 | bool Monitor::Deflate(Thread* self, mirror::Object* obj) { |
| 590 | DCHECK(obj != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 591 | // Don't need volatile since we only deflate with mutators suspended. |
| 592 | LockWord lw(obj->GetLockWord(false)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 593 | // If the lock isn't an inflated monitor, then we don't need to deflate anything. |
| 594 | if (lw.GetState() == LockWord::kFatLocked) { |
| 595 | Monitor* monitor = lw.FatLockMonitor(); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 596 | DCHECK(monitor != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 597 | MutexLock mu(self, monitor->monitor_lock_); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 598 | // Can't deflate if we have anybody waiting on the CV. |
| 599 | if (monitor->num_waiters_ > 0) { |
| 600 | return false; |
| 601 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 602 | Thread* owner = monitor->owner_; |
| 603 | if (owner != nullptr) { |
| 604 | // Can't deflate if we are locked and have a hash code. |
| 605 | if (monitor->HasHashCode()) { |
| 606 | return false; |
| 607 | } |
| 608 | // Can't deflate if our lock count is too high. |
| 609 | if (monitor->lock_count_ > LockWord::kThinLockMaxCount) { |
| 610 | return false; |
| 611 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 612 | // Deflate to a thin lock. |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 613 | obj->SetLockWord(LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_), false); |
| 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()) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 617 | obj->SetLockWord(LockWord::FromHashCode(monitor->GetHashCode()), false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 618 | VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 619 | } else { |
| 620 | // No lock and no hash, just put an empty lock word inside the object. |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 621 | obj->SetLockWord(LockWord(), false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 622 | VLOG(monitor) << "Deflated" << obj << " to empty lock word"; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 623 | } |
| 624 | // The monitor is deflated, mark the object as nullptr so that we know to delete it during the |
| 625 | // next GC. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 626 | monitor->obj_ = GcRoot<mirror::Object>(nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 627 | } |
| 628 | return true; |
| 629 | } |
| 630 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 631 | 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] | 632 | DCHECK(self != nullptr); |
| 633 | DCHECK(obj != nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 634 | // Allocate and acquire a new monitor. |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 635 | Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code); |
| 636 | DCHECK(m != nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 637 | if (m->Install(self)) { |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 638 | if (owner != nullptr) { |
| 639 | VLOG(monitor) << "monitor: thread" << owner->GetThreadId() |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 640 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 641 | } else { |
| 642 | VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 643 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 644 | } |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 645 | Runtime::Current()->GetMonitorList()->Add(m); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 646 | CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 647 | } else { |
| 648 | MonitorPool::ReleaseMonitor(self, m); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 649 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 652 | void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 653 | uint32_t hash_code) { |
| 654 | DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked); |
| 655 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 656 | if (owner_thread_id == self->GetThreadId()) { |
| 657 | // We own the monitor, we can easily inflate it. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 658 | Inflate(self, self, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 659 | } else { |
| 660 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 661 | // 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] | 662 | self->SetMonitorEnterObject(obj.Get()); |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 663 | bool timed_out; |
| 664 | Thread* owner; |
| 665 | { |
| 666 | ScopedThreadStateChange tsc(self, kBlocked); |
| 667 | owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out); |
| 668 | } |
| 669 | if (owner != nullptr) { |
| 670 | // We succeeded in suspending the thread, check the lock's status didn't change. |
| 671 | lock_word = obj->GetLockWord(true); |
| 672 | if (lock_word.GetState() == LockWord::kThinLocked && |
| 673 | lock_word.ThinLockOwner() == owner_thread_id) { |
| 674 | // Go ahead and inflate the lock. |
| 675 | Inflate(self, owner, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 676 | } |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 677 | thread_list->Resume(owner, false); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 678 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 679 | self->SetMonitorEnterObject(nullptr); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 680 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 683 | // Fool annotalysis into thinking that the lock on obj is acquired. |
| 684 | static mirror::Object* FakeLock(mirror::Object* obj) |
| 685 | EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 686 | return obj; |
| 687 | } |
| 688 | |
| 689 | // Fool annotalysis into thinking that the lock on obj is release. |
| 690 | static mirror::Object* FakeUnlock(mirror::Object* obj) |
| 691 | UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 692 | return obj; |
| 693 | } |
| 694 | |
Mathieu Chartier | e7e8a5f | 2014-02-14 16:59:41 -0800 | [diff] [blame] | 695 | mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 696 | DCHECK(self != NULL); |
| 697 | DCHECK(obj != NULL); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 698 | obj = FakeLock(obj); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 699 | uint32_t thread_id = self->GetThreadId(); |
| 700 | size_t contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 701 | StackHandleScope<1> hs(self); |
| 702 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 703 | while (true) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 704 | LockWord lock_word = h_obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 705 | switch (lock_word.GetState()) { |
| 706 | case LockWord::kUnlocked: { |
| 707 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0)); |
Ian Rogers | 228602f | 2014-07-10 02:07:54 -0700 | [diff] [blame] | 708 | if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 709 | // CasLockWord enforces more than the acquire ordering we need here. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 710 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 711 | } |
| 712 | continue; // Go again. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 713 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 714 | case LockWord::kThinLocked: { |
| 715 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 716 | if (owner_thread_id == thread_id) { |
| 717 | // We own the lock, increase the recursion count. |
| 718 | uint32_t new_count = lock_word.ThinLockCount() + 1; |
| 719 | if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) { |
| 720 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 721 | h_obj->SetLockWord(thin_locked, true); |
| 722 | return h_obj.Get(); // Success! |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 723 | } else { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 724 | // We'd overflow the recursion count, so inflate the monitor. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 725 | InflateThinLocked(self, h_obj, lock_word, 0); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 726 | } |
| 727 | } else { |
| 728 | // Contention. |
| 729 | contention_count++; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 730 | Runtime* runtime = Runtime::Current(); |
| 731 | if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 732 | // TODO: Consider switching the thread state to kBlocked when we are yielding. |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 733 | // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the |
| 734 | // 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] | 735 | // and make long pauses. See b/16307460. |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 736 | sched_yield(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 737 | } else { |
| 738 | contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 739 | InflateThinLocked(self, h_obj, lock_word, 0); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 740 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 741 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 742 | continue; // Start from the beginning. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 743 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 744 | case LockWord::kFatLocked: { |
| 745 | Monitor* mon = lock_word.FatLockMonitor(); |
| 746 | mon->Lock(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 747 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 748 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 749 | case LockWord::kHashCode: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 750 | // Inflate with the existing hashcode. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 751 | Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode()); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 752 | continue; // Start from the beginning. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 753 | default: { |
| 754 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 755 | return h_obj.Get(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 756 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 757 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 761 | bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 762 | DCHECK(self != NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 763 | DCHECK(obj != NULL); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 764 | obj = FakeUnlock(obj); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 765 | LockWord lock_word = obj->GetLockWord(true); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 766 | StackHandleScope<1> hs(self); |
| 767 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 768 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 769 | case LockWord::kHashCode: |
| 770 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 771 | case LockWord::kUnlocked: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 772 | FailedUnlock(h_obj.Get(), self, nullptr, nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 773 | return false; // Failure. |
| 774 | case LockWord::kThinLocked: { |
| 775 | uint32_t thread_id = self->GetThreadId(); |
| 776 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 777 | if (owner_thread_id != thread_id) { |
| 778 | // TODO: there's a race here with the owner dying while we unlock. |
| 779 | Thread* owner = |
| 780 | Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 781 | FailedUnlock(h_obj.Get(), self, owner, nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 782 | return false; // Failure. |
| 783 | } else { |
| 784 | // We own the lock, decrease the recursion count. |
| 785 | if (lock_word.ThinLockCount() != 0) { |
| 786 | uint32_t new_count = lock_word.ThinLockCount() - 1; |
| 787 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 788 | h_obj->SetLockWord(thin_locked, true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 789 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 790 | h_obj->SetLockWord(LockWord(), true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 791 | } |
| 792 | return true; // Success! |
| 793 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 794 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 795 | case LockWord::kFatLocked: { |
| 796 | Monitor* mon = lock_word.FatLockMonitor(); |
| 797 | return mon->Unlock(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 798 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 799 | default: { |
| 800 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 801 | return false; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 802 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 803 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 806 | 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] | 807 | bool interruptShouldThrow, ThreadState why) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 808 | DCHECK(self != nullptr); |
| 809 | DCHECK(obj != nullptr); |
| 810 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 811 | while (lock_word.GetState() != LockWord::kFatLocked) { |
| 812 | switch (lock_word.GetState()) { |
| 813 | case LockWord::kHashCode: |
| 814 | // Fall-through. |
| 815 | case LockWord::kUnlocked: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 816 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
| 817 | return; // Failure. |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 818 | case LockWord::kThinLocked: { |
| 819 | uint32_t thread_id = self->GetThreadId(); |
| 820 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 821 | if (owner_thread_id != thread_id) { |
| 822 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
| 823 | return; // Failure. |
| 824 | } else { |
| 825 | // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so |
| 826 | // re-load. |
| 827 | Inflate(self, self, obj, 0); |
| 828 | lock_word = obj->GetLockWord(true); |
| 829 | } |
| 830 | break; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 831 | } |
Ian Rogers | 43c69cc | 2014-08-15 11:09:28 -0700 | [diff] [blame] | 832 | case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through. |
| 833 | default: { |
| 834 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 835 | return; |
| 836 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 837 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 838 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 839 | Monitor* mon = lock_word.FatLockMonitor(); |
| 840 | mon->Wait(self, ms, ns, interruptShouldThrow, why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Ian Rogers | 13c479e | 2013-10-11 07:59:01 -0700 | [diff] [blame] | 843 | void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 844 | DCHECK(self != nullptr); |
| 845 | DCHECK(obj != nullptr); |
| 846 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 847 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 848 | case LockWord::kHashCode: |
| 849 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 850 | case LockWord::kUnlocked: |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 851 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 852 | return; // Failure. |
| 853 | case LockWord::kThinLocked: { |
| 854 | uint32_t thread_id = self->GetThreadId(); |
| 855 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 856 | if (owner_thread_id != thread_id) { |
| 857 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
| 858 | return; // Failure. |
| 859 | } else { |
| 860 | // We own the lock but there's no Monitor and therefore no waiters. |
| 861 | return; // Success. |
| 862 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 863 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 864 | case LockWord::kFatLocked: { |
| 865 | Monitor* mon = lock_word.FatLockMonitor(); |
| 866 | if (notify_all) { |
| 867 | mon->NotifyAll(self); |
| 868 | } else { |
| 869 | mon->Notify(self); |
| 870 | } |
| 871 | return; // Success. |
| 872 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 873 | default: { |
| 874 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 875 | return; |
| 876 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 880 | uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 881 | DCHECK(obj != nullptr); |
| 882 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 883 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 884 | case LockWord::kHashCode: |
| 885 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 886 | case LockWord::kUnlocked: |
| 887 | return ThreadList::kInvalidThreadId; |
| 888 | case LockWord::kThinLocked: |
| 889 | return lock_word.ThinLockOwner(); |
| 890 | case LockWord::kFatLocked: { |
| 891 | Monitor* mon = lock_word.FatLockMonitor(); |
| 892 | return mon->GetOwnerThreadId(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 893 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 894 | default: { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 895 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 896 | UNREACHABLE(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 897 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 901 | void Monitor::DescribeWait(std::ostream& os, const Thread* thread) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 902 | // Determine the wait message and object we're waiting or blocked upon. |
| 903 | mirror::Object* pretty_object = nullptr; |
| 904 | const char* wait_message = nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 905 | uint32_t lock_owner = ThreadList::kInvalidThreadId; |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 906 | ThreadState state = thread->GetState(); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 907 | if (state == kWaiting || state == kTimedWaiting || state == kSleeping) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 908 | wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on "; |
| 909 | Thread* self = Thread::Current(); |
| 910 | MutexLock mu(self, *thread->GetWaitMutex()); |
| 911 | Monitor* monitor = thread->GetWaitMonitor(); |
| 912 | if (monitor != nullptr) { |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 913 | pretty_object = monitor->GetObject(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 914 | } |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 915 | } else if (state == kBlocked) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 916 | wait_message = " - waiting to lock "; |
| 917 | pretty_object = thread->GetMonitorEnterObject(); |
| 918 | if (pretty_object != nullptr) { |
| 919 | lock_owner = pretty_object->GetLockOwnerThreadId(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 920 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 921 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 922 | |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 923 | if (wait_message != nullptr) { |
| 924 | if (pretty_object == nullptr) { |
| 925 | os << wait_message << "an unknown object"; |
| 926 | } else { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 927 | if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) && |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 928 | Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) { |
| 929 | // Getting the identity hashcode here would result in lock inflation and suspension of the |
| 930 | // current thread, which isn't safe if this is the only runnable thread. |
| 931 | os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)", |
| 932 | reinterpret_cast<intptr_t>(pretty_object), |
| 933 | PrettyTypeOf(pretty_object).c_str()); |
| 934 | } else { |
| 935 | // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>) |
Mathieu Chartier | 4936159 | 2015-01-22 16:36:10 -0800 | [diff] [blame^] | 936 | // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread |
| 937 | // suspension and move pretty_object. |
| 938 | const std::string pretty_type(PrettyTypeOf(pretty_object)); |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 939 | os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(), |
Mathieu Chartier | 4936159 | 2015-01-22 16:36:10 -0800 | [diff] [blame^] | 940 | pretty_type.c_str()); |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5 |
| 944 | if (lock_owner != ThreadList::kInvalidThreadId) { |
| 945 | os << " held by thread " << lock_owner; |
| 946 | } |
| 947 | os << "\n"; |
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 | } |
| 950 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 951 | mirror::Object* Monitor::GetContendedMonitor(Thread* thread) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 952 | // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre |
| 953 | // 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] | 954 | mirror::Object* result = thread->GetMonitorEnterObject(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 955 | if (result == NULL) { |
| 956 | // ...but also a monitor that the thread is waiting on. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 957 | MutexLock mu(Thread::Current(), *thread->GetWaitMutex()); |
| 958 | Monitor* monitor = thread->GetWaitMonitor(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 959 | if (monitor != NULL) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 960 | result = monitor->GetObject(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 961 | } |
| 962 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 963 | return result; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 966 | void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*), |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 967 | void* callback_context, bool abort_on_failure) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 968 | mirror::ArtMethod* m = stack_visitor->GetMethod(); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 969 | CHECK(m != NULL); |
| 970 | |
| 971 | // Native methods are an easy special case. |
| 972 | // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. |
| 973 | if (m->IsNative()) { |
| 974 | if (m->IsSynchronized()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 975 | mirror::Object* jni_this = stack_visitor->GetCurrentHandleScope()->GetReference(0); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 976 | callback(jni_this, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 977 | } |
| 978 | return; |
| 979 | } |
| 980 | |
jeffhao | 61f916c | 2012-10-25 17:48:51 -0700 | [diff] [blame] | 981 | // Proxy methods should not be synchronized. |
| 982 | if (m->IsProxyMethod()) { |
| 983 | CHECK(!m->IsSynchronized()); |
| 984 | return; |
| 985 | } |
| 986 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 987 | // 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] | 988 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 989 | CHECK(code_item != NULL) << PrettyMethod(m); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 990 | if (code_item->tries_size_ == 0) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 991 | return; // No "tries" implies no synchronization, so no held locks to report. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 994 | // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot |
| 995 | // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an |
| 996 | // inconsistent stack anyways. |
| 997 | uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure); |
| 998 | if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) { |
| 999 | LOG(ERROR) << "Could not find dex_pc for " << PrettyMethod(m); |
| 1000 | return; |
| 1001 | } |
| 1002 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1003 | // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to |
| 1004 | // the locks held in this stack frame. |
| 1005 | std::vector<uint32_t> monitor_enter_dex_pcs; |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1006 | verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs); |
Mathieu Chartier | e6a8eec | 2015-01-06 14:17:57 -0800 | [diff] [blame] | 1007 | for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) { |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1008 | // The verifier works in terms of the dex pcs of the monitor-enter instructions. |
| 1009 | // 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] | 1010 | uint16_t monitor_enter_instruction = code_item->insns_[monitor_dex_pc]; |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1011 | |
| 1012 | // Quick sanity check. |
| 1013 | if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1014 | LOG(FATAL) << "expected monitor-enter @" << monitor_dex_pc << "; was " |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1015 | << reinterpret_cast<void*>(monitor_enter_instruction); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1018 | uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff); |
Mathieu Chartier | e6a8eec | 2015-01-06 14:17:57 -0800 | [diff] [blame] | 1019 | mirror::Object* o = reinterpret_cast<mirror::Object*>( |
| 1020 | stack_visitor->GetVReg(m, monitor_register, kReferenceVReg)); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 1021 | callback(o, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1025 | bool Monitor::IsValidLockWord(LockWord lock_word) { |
| 1026 | switch (lock_word.GetState()) { |
| 1027 | case LockWord::kUnlocked: |
| 1028 | // Nothing to check. |
| 1029 | return true; |
| 1030 | case LockWord::kThinLocked: |
| 1031 | // Basic sanity check of owner. |
| 1032 | return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId; |
| 1033 | case LockWord::kFatLocked: { |
| 1034 | // Check the monitor appears in the monitor list. |
| 1035 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1036 | MonitorList* list = Runtime::Current()->GetMonitorList(); |
| 1037 | MutexLock mu(Thread::Current(), list->monitor_list_lock_); |
| 1038 | for (Monitor* list_mon : list->list_) { |
| 1039 | if (mon == list_mon) { |
| 1040 | return true; // Found our monitor. |
| 1041 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1042 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1043 | return false; // Fail - unowned monitor in an object. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1044 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1045 | case LockWord::kHashCode: |
| 1046 | return true; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1047 | default: |
| 1048 | LOG(FATAL) << "Unreachable"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 1049 | UNREACHABLE(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1053 | bool Monitor::IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1054 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1055 | return owner_ != nullptr; |
| 1056 | } |
| 1057 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1058 | void Monitor::TranslateLocation(mirror::ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1059 | const char** source_file, uint32_t* line_number) const { |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1060 | // If method is null, location is unknown |
| 1061 | if (method == NULL) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1062 | *source_file = ""; |
| 1063 | *line_number = 0; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1064 | return; |
| 1065 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1066 | *source_file = method->GetDeclaringClassSourceFile(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1067 | if (*source_file == NULL) { |
| 1068 | *source_file = ""; |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 1069 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1070 | *line_number = method->GetLineNumFromDexPC(dex_pc); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | uint32_t Monitor::GetOwnerThreadId() { |
| 1074 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1075 | Thread* owner = owner_; |
| 1076 | if (owner != NULL) { |
| 1077 | return owner->GetThreadId(); |
| 1078 | } else { |
| 1079 | return ThreadList::kInvalidThreadId; |
| 1080 | } |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1083 | MonitorList::MonitorList() |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1084 | : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock), |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1085 | monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | MonitorList::~MonitorList() { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1089 | Thread* self = Thread::Current(); |
| 1090 | MutexLock mu(self, monitor_list_lock_); |
| 1091 | // Release all monitors to the pool. |
| 1092 | // TODO: Is it an invariant that *all* open monitors are in the list? Then we could |
| 1093 | // clear faster in the pool. |
| 1094 | MonitorPool::ReleaseMonitors(self, &list_); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1097 | void MonitorList::DisallowNewMonitors() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1098 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1099 | allow_new_monitors_ = false; |
| 1100 | } |
| 1101 | |
| 1102 | void MonitorList::AllowNewMonitors() { |
| 1103 | Thread* self = Thread::Current(); |
| 1104 | MutexLock mu(self, monitor_list_lock_); |
| 1105 | allow_new_monitors_ = true; |
| 1106 | monitor_add_condition_.Broadcast(self); |
| 1107 | } |
| 1108 | |
| 1109 | void MonitorList::Add(Monitor* m) { |
| 1110 | Thread* self = Thread::Current(); |
| 1111 | MutexLock mu(self, monitor_list_lock_); |
| 1112 | while (UNLIKELY(!allow_new_monitors_)) { |
| 1113 | monitor_add_condition_.WaitHoldingLocks(self); |
| 1114 | } |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1115 | list_.push_front(m); |
| 1116 | } |
| 1117 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1118 | void MonitorList::SweepMonitorList(IsMarkedCallback* callback, void* arg) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1119 | Thread* self = Thread::Current(); |
| 1120 | MutexLock mu(self, monitor_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1121 | for (auto it = list_.begin(); it != list_.end(); ) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1122 | Monitor* m = *it; |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1123 | // Disable the read barrier in GetObject() as this is called by GC. |
| 1124 | mirror::Object* obj = m->GetObject<kWithoutReadBarrier>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1125 | // 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] | 1126 | mirror::Object* new_obj = obj != nullptr ? callback(obj, arg) : nullptr; |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1127 | if (new_obj == nullptr) { |
| 1128 | VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1129 | << obj; |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1130 | MonitorPool::ReleaseMonitor(self, m); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1131 | it = list_.erase(it); |
| 1132 | } else { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1133 | m->SetObject(new_obj); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1134 | ++it; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1139 | struct MonitorDeflateArgs { |
| 1140 | MonitorDeflateArgs() : self(Thread::Current()), deflate_count(0) {} |
| 1141 | Thread* const self; |
| 1142 | size_t deflate_count; |
| 1143 | }; |
| 1144 | |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1145 | static mirror::Object* MonitorDeflateCallback(mirror::Object* object, void* arg) |
| 1146 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1147 | MonitorDeflateArgs* args = reinterpret_cast<MonitorDeflateArgs*>(arg); |
| 1148 | if (Monitor::Deflate(args->self, object)) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1149 | DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1150 | ++args->deflate_count; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1151 | // If we deflated, return nullptr so that the monitor gets removed from the array. |
| 1152 | return nullptr; |
| 1153 | } |
| 1154 | return object; // Monitor was not deflated. |
| 1155 | } |
| 1156 | |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1157 | size_t MonitorList::DeflateMonitors() { |
| 1158 | MonitorDeflateArgs args; |
| 1159 | Locks::mutator_lock_->AssertExclusiveHeld(args.self); |
| 1160 | SweepMonitorList(MonitorDeflateCallback, &args); |
| 1161 | return args.deflate_count; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1164 | MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(NULL), entry_count_(0) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1165 | DCHECK(obj != nullptr); |
| 1166 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1167 | switch (lock_word.GetState()) { |
| 1168 | case LockWord::kUnlocked: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1169 | // Fall-through. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1170 | case LockWord::kForwardingAddress: |
| 1171 | // Fall-through. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1172 | case LockWord::kHashCode: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1173 | break; |
| 1174 | case LockWord::kThinLocked: |
| 1175 | owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
| 1176 | entry_count_ = 1 + lock_word.ThinLockCount(); |
| 1177 | // Thin locks have no waiters. |
| 1178 | break; |
| 1179 | case LockWord::kFatLocked: { |
| 1180 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1181 | owner_ = mon->owner_; |
| 1182 | entry_count_ = 1 + mon->lock_count_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1183 | for (Thread* waiter = mon->wait_set_; waiter != NULL; waiter = waiter->GetWaitNext()) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1184 | waiters_.push_back(waiter); |
| 1185 | } |
| 1186 | break; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1191 | } // namespace art |