Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 17 | #include "monitor.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 21 | #include "base/mutex.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 22 | #include "base/stl_util.h" |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 23 | #include "class_linker.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 24 | #include "dex_file-inl.h" |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 25 | #include "dex_instruction.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 26 | #include "lock_word-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 27 | #include "mirror/art_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 28 | #include "mirror/class-inl.h" |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 29 | #include "mirror/object-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "mirror/object_array-inl.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 31 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 32 | #include "thread.h" |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 33 | #include "thread_list.h" |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 34 | #include "verifier/method_verifier.h" |
Elliott Hughes | 044288f | 2012-06-25 14:46:39 -0700 | [diff] [blame] | 35 | #include "well_known_classes.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 36 | |
| 37 | namespace art { |
| 38 | |
Mathieu Chartier | b9001ab | 2014-10-03 13:28:46 -0700 | [diff] [blame^] | 39 | static constexpr uint64_t kLongWaitMs = 100; |
| 40 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 41 | /* |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 42 | * Every Object has a monitor associated with it, but not every Object is actually locked. Even |
| 43 | * the ones that are locked do not need a full-fledged monitor until a) there is actual contention |
| 44 | * or b) wait() is called on the Object. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 45 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 46 | * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s |
| 47 | * "Thin locks: featherweight synchronization for Java" (ACM 1998). Things are even easier for us, |
| 48 | * though, because we have a full 32 bits to work with. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 49 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 50 | * The two states of an Object's lock are referred to as "thin" and "fat". A lock may transition |
| 51 | * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once |
| 52 | * a lock has been inflated it remains in the "fat" state indefinitely. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 53 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 54 | * The lock value itself is stored in mirror::Object::monitor_ and the representation is described |
| 55 | * in the LockWord value type. |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 56 | * |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 57 | * Monitors provide: |
| 58 | * - mutually exclusive access to resources |
| 59 | * - a way for multiple threads to wait for notification |
| 60 | * |
| 61 | * In effect, they fill the role of both mutexes and condition variables. |
| 62 | * |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 63 | * Only one thread can own the monitor at any time. There may be several threads waiting on it |
| 64 | * (the wait call unlocks it). One or more waiting threads may be getting interrupted or notified |
| 65 | * at any given time. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 66 | */ |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 68 | bool (*Monitor::is_sensitive_thread_hook_)() = NULL; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 69 | uint32_t Monitor::lock_profiling_threshold_ = 0; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 71 | bool Monitor::IsSensitiveThread() { |
| 72 | if (is_sensitive_thread_hook_ != NULL) { |
| 73 | return (*is_sensitive_thread_hook_)(); |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 78 | 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] | 79 | lock_profiling_threshold_ = lock_profiling_threshold; |
| 80 | is_sensitive_thread_hook_ = is_sensitive_thread_hook; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 83 | 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] | 84 | : monitor_lock_("a monitor lock", kMonitorLock), |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 85 | monitor_contenders_("monitor contenders", monitor_lock_), |
Mathieu Chartier | 46bc778 | 2013-11-12 17:03:02 -0800 | [diff] [blame] | 86 | num_waiters_(0), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 87 | owner_(owner), |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 88 | lock_count_(0), |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 89 | obj_(GcRoot<mirror::Object>(obj)), |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 90 | wait_set_(NULL), |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 91 | hash_code_(hash_code), |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 92 | locking_method_(NULL), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 93 | locking_dex_pc_(0), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 94 | monitor_id_(MonitorPool::ComputeMonitorId(this, self)) { |
| 95 | #ifdef __LP64__ |
| 96 | DCHECK(false) << "Should not be reached in 64b"; |
| 97 | next_free_ = nullptr; |
| 98 | #endif |
| 99 | // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race |
| 100 | // with the owner unlocking the thin-lock. |
| 101 | CHECK(owner == nullptr || owner == self || owner->IsSuspended()); |
| 102 | // The identity hash code is set for the life time of the monitor. |
| 103 | } |
| 104 | |
| 105 | Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code, |
| 106 | MonitorId id) |
| 107 | : monitor_lock_("a monitor lock", kMonitorLock), |
| 108 | monitor_contenders_("monitor contenders", monitor_lock_), |
| 109 | num_waiters_(0), |
| 110 | owner_(owner), |
| 111 | lock_count_(0), |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 112 | obj_(GcRoot<mirror::Object>(obj)), |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 113 | wait_set_(NULL), |
| 114 | hash_code_(hash_code), |
| 115 | locking_method_(NULL), |
| 116 | locking_dex_pc_(0), |
| 117 | monitor_id_(id) { |
| 118 | #ifdef __LP64__ |
| 119 | next_free_ = nullptr; |
| 120 | #endif |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 121 | // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race |
| 122 | // with the owner unlocking the thin-lock. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 123 | CHECK(owner == nullptr || owner == self || owner->IsSuspended()); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 124 | // 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] | 125 | } |
| 126 | |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 127 | int32_t Monitor::GetHashCode() { |
| 128 | while (!HasHashCode()) { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 129 | if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) { |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 130 | break; |
| 131 | } |
| 132 | } |
| 133 | DCHECK(HasHashCode()); |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 134 | return hash_code_.LoadRelaxed(); |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 137 | bool Monitor::Install(Thread* self) { |
| 138 | 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] | 139 | CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 140 | // Propagate the lock state. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 141 | LockWord lw(GetObject()->GetLockWord(false)); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 142 | switch (lw.GetState()) { |
| 143 | case LockWord::kThinLocked: { |
| 144 | CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner()); |
| 145 | lock_count_ = lw.ThinLockCount(); |
| 146 | break; |
| 147 | } |
| 148 | case LockWord::kHashCode: { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 149 | CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode())); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 150 | break; |
| 151 | } |
| 152 | case LockWord::kFatLocked: { |
| 153 | // The owner_ is suspended but another thread beat us to install a monitor. |
| 154 | return false; |
| 155 | } |
| 156 | case LockWord::kUnlocked: { |
| 157 | LOG(FATAL) << "Inflating unlocked lock word"; |
| 158 | break; |
| 159 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 160 | default: { |
| 161 | LOG(FATAL) << "Invalid monitor state " << lw.GetState(); |
| 162 | return false; |
| 163 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 164 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 165 | LockWord fat(this); |
| 166 | // Publish the updated lock word, which may race with other threads. |
Ian Rogers | 228602f | 2014-07-10 02:07:54 -0700 | [diff] [blame] | 167 | bool success = GetObject()->CasLockWordWeakSequentiallyConsistent(lw, fat); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 168 | // Lock profiling. |
Mathieu Chartier | 9728f91 | 2013-10-30 09:45:13 -0700 | [diff] [blame] | 169 | if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) { |
Andreas Gampe | 6ec8ebd | 2014-07-25 13:36:56 -0700 | [diff] [blame] | 170 | // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on |
| 171 | // abort. |
| 172 | locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 173 | } |
| 174 | return success; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | Monitor::~Monitor() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 178 | // Deflated monitors have a null object. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Links a thread into a monitor's wait set. The monitor lock must be |
| 183 | * held by the caller of this routine. |
| 184 | */ |
| 185 | void Monitor::AppendToWaitSet(Thread* thread) { |
| 186 | DCHECK(owner_ == Thread::Current()); |
| 187 | DCHECK(thread != NULL); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 188 | DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 189 | if (wait_set_ == NULL) { |
| 190 | wait_set_ = thread; |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // push_back. |
| 195 | Thread* t = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 196 | while (t->GetWaitNext() != nullptr) { |
| 197 | t = t->GetWaitNext(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 198 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 199 | t->SetWaitNext(thread); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Unlinks a thread from a monitor's wait set. The monitor lock must |
| 204 | * be held by the caller of this routine. |
| 205 | */ |
| 206 | void Monitor::RemoveFromWaitSet(Thread *thread) { |
| 207 | DCHECK(owner_ == Thread::Current()); |
| 208 | DCHECK(thread != NULL); |
| 209 | if (wait_set_ == NULL) { |
| 210 | return; |
| 211 | } |
| 212 | if (wait_set_ == thread) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 213 | wait_set_ = thread->GetWaitNext(); |
| 214 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
| 218 | Thread* t = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 219 | while (t->GetWaitNext() != NULL) { |
| 220 | if (t->GetWaitNext() == thread) { |
| 221 | t->SetWaitNext(thread->GetWaitNext()); |
| 222 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 223 | return; |
| 224 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 225 | t = t->GetWaitNext(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 229 | void Monitor::SetObject(mirror::Object* object) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 230 | obj_ = GcRoot<mirror::Object>(object); |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 233 | void Monitor::Lock(Thread* self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 234 | MutexLock mu(self, monitor_lock_); |
| 235 | while (true) { |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 236 | if (owner_ == nullptr) { // Unowned. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 237 | owner_ = self; |
| 238 | CHECK_EQ(lock_count_, 0); |
| 239 | // When debugging, save the current monitor holder for future |
| 240 | // acquisition failures to use in sampled logging. |
| 241 | if (lock_profiling_threshold_ != 0) { |
| 242 | locking_method_ = self->GetCurrentMethod(&locking_dex_pc_); |
| 243 | } |
| 244 | return; |
| 245 | } else if (owner_ == self) { // Recursive. |
| 246 | lock_count_++; |
| 247 | return; |
| 248 | } |
| 249 | // Contended. |
| 250 | const bool log_contention = (lock_profiling_threshold_ != 0); |
Xin Guan | b894a19 | 2014-08-22 11:55:37 -0500 | [diff] [blame] | 251 | uint64_t wait_start_ms = log_contention ? MilliTime() : 0; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 252 | mirror::ArtMethod* owners_method = locking_method_; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 253 | uint32_t owners_dex_pc = locking_dex_pc_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 254 | // 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^] | 255 | size_t num_waiters = num_waiters_; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 256 | ++num_waiters_; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 257 | monitor_lock_.Unlock(self); // Let go of locks in order. |
Mathieu Chartier | a6e7f08 | 2014-05-22 14:43:37 -0700 | [diff] [blame] | 258 | self->SetMonitorEnterObject(GetObject()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 259 | { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 260 | ScopedThreadStateChange tsc(self, kBlocked); // Change to blocked and give up mutator_lock_. |
| 261 | MutexLock mu2(self, monitor_lock_); // Reacquire monitor_lock_ without mutator_lock_ for Wait. |
| 262 | if (owner_ != NULL) { // Did the owner_ give the lock up? |
| 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 | } |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 286 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 287 | } |
Mathieu Chartier | a6e7f08 | 2014-05-22 14:43:37 -0700 | [diff] [blame] | 288 | self->SetMonitorEnterObject(nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 289 | monitor_lock_.Lock(self); // Reacquire locks in order. |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 290 | --num_waiters_; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 291 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 294 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
| 295 | __attribute__((format(printf, 1, 2))); |
| 296 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 297 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 298 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 299 | va_list args; |
| 300 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 301 | Thread* self = Thread::Current(); |
| 302 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 303 | self->ThrowNewExceptionV(throw_location, "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) |
| 308 | << self->GetException(NULL)->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 | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 407 | /* |
| 408 | * Wait on a monitor until timeout, interrupt, or notification. Used for |
| 409 | * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join(). |
| 410 | * |
| 411 | * If another thread calls Thread.interrupt(), we throw InterruptedException |
| 412 | * and return immediately if one of the following are true: |
| 413 | * - blocked in wait(), wait(long), or wait(long, int) methods of Object |
| 414 | * - blocked in join(), join(long), or join(long, int) methods of Thread |
| 415 | * - blocked in sleep(long), or sleep(long, int) methods of Thread |
| 416 | * Otherwise, we set the "interrupted" flag. |
| 417 | * |
| 418 | * Checks to make sure that "ns" is in the range 0-999999 |
| 419 | * (i.e. fractions of a millisecond) and throws the appropriate |
| 420 | * exception if it isn't. |
| 421 | * |
| 422 | * The spec allows "spurious wakeups", and recommends that all code using |
| 423 | * Object.wait() do so in a loop. This appears to derive from concerns |
| 424 | * about pthread_cond_wait() on multiprocessor systems. Some commentary |
| 425 | * on the web casts doubt on whether these can/should occur. |
| 426 | * |
| 427 | * Since we're allowed to wake up "early", we clamp extremely long durations |
| 428 | * to return at the end of the 32-bit time epoch. |
| 429 | */ |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 430 | void Monitor::Wait(Thread* self, int64_t ms, int32_t ns, |
| 431 | bool interruptShouldThrow, ThreadState why) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 432 | DCHECK(self != NULL); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 433 | DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 434 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 435 | monitor_lock_.Lock(self); |
| 436 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 437 | // Make sure that we hold the lock. |
| 438 | if (owner_ != self) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 439 | monitor_lock_.Unlock(self); |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 440 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 441 | return; |
| 442 | } |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 443 | |
Elliott Hughes | df42c48 | 2013-01-09 12:49:02 -0800 | [diff] [blame] | 444 | // We need to turn a zero-length timed wait into a regular wait because |
| 445 | // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait(). |
| 446 | if (why == kTimedWaiting && (ms == 0 && ns == 0)) { |
| 447 | why = kWaiting; |
| 448 | } |
| 449 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 450 | // Enforce the timeout range. |
| 451 | if (ms < 0 || ns < 0 || ns > 999999) { |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 452 | monitor_lock_.Unlock(self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 453 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 454 | self->ThrowNewExceptionF(throw_location, "Ljava/lang/IllegalArgumentException;", |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 455 | "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 456 | return; |
| 457 | } |
| 458 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 459 | /* |
| 460 | * Add ourselves to the set of threads waiting on this monitor, and |
| 461 | * release our hold. We need to let it go even if we're a few levels |
| 462 | * deep in a recursive lock, and we need to restore that later. |
| 463 | * |
| 464 | * We append to the wait set ahead of clearing the count and owner |
| 465 | * fields so the subroutine can check that the calling thread owns |
| 466 | * the monitor. Aside from that, the order of member updates is |
| 467 | * not order sensitive as we hold the pthread mutex. |
| 468 | */ |
| 469 | AppendToWaitSet(self); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 470 | ++num_waiters_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 471 | int prev_lock_count = lock_count_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 472 | lock_count_ = 0; |
| 473 | owner_ = NULL; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 474 | mirror::ArtMethod* saved_method = locking_method_; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 475 | locking_method_ = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 476 | uintptr_t saved_dex_pc = locking_dex_pc_; |
| 477 | locking_dex_pc_ = 0; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 478 | |
| 479 | /* |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 480 | * 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] | 481 | * that we won't touch any references in this state, and we'll check |
| 482 | * our suspend mode before we transition out. |
| 483 | */ |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 484 | self->TransitionFromRunnableToSuspended(why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 485 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 486 | bool was_interrupted = false; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 487 | { |
| 488 | // 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] | 489 | MutexLock mu(self, *self->GetWaitMutex()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 490 | |
| 491 | // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is |
| 492 | // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it |
| 493 | // up. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 494 | DCHECK(self->GetWaitMonitor() == nullptr); |
| 495 | self->SetWaitMonitor(this); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 496 | |
| 497 | // Release the monitor lock. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 498 | monitor_contenders_.Signal(self); |
| 499 | monitor_lock_.Unlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 500 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 501 | // Handle the case where the thread was interrupted before we called wait(). |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 502 | if (self->IsInterruptedLocked()) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 503 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 504 | } else { |
| 505 | // Wait for a notification or a timeout to occur. |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 506 | if (why == kWaiting) { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 507 | self->GetWaitConditionVariable()->Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 508 | } else { |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 509 | DCHECK(why == kTimedWaiting || why == kSleeping) << why; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 510 | self->GetWaitConditionVariable()->TimedWait(self, ms, ns); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 511 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 512 | if (self->IsInterruptedLocked()) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 513 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 514 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 515 | self->SetInterruptedLocked(false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 516 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 519 | // Set self->status back to kRunnable, and self-suspend if needed. |
| 520 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 521 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 522 | { |
| 523 | // We reset the thread's wait_monitor_ field after transitioning back to runnable so |
| 524 | // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging |
| 525 | // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads |
| 526 | // are waiting on "null".) |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 527 | MutexLock mu(self, *self->GetWaitMutex()); |
| 528 | DCHECK(self->GetWaitMonitor() != nullptr); |
| 529 | self->SetWaitMonitor(nullptr); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 532 | // Re-acquire the monitor and lock. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 533 | Lock(self); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 534 | monitor_lock_.Lock(self); |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 535 | self->GetWaitMutex()->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 536 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 537 | /* |
| 538 | * We remove our thread from wait set after restoring the count |
| 539 | * and owner fields so the subroutine can check that the calling |
| 540 | * thread owns the monitor. Aside from that, the order of member |
| 541 | * updates is not order sensitive as we hold the pthread mutex. |
| 542 | */ |
| 543 | owner_ = self; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 544 | lock_count_ = prev_lock_count; |
| 545 | locking_method_ = saved_method; |
| 546 | locking_dex_pc_ = saved_dex_pc; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 547 | --num_waiters_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 548 | RemoveFromWaitSet(self); |
| 549 | |
Elena Sayapina | 1af6a1f | 2014-06-20 16:58:37 +0700 | [diff] [blame] | 550 | monitor_lock_.Unlock(self); |
| 551 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 552 | if (was_interrupted) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 553 | /* |
| 554 | * We were interrupted while waiting, or somebody interrupted an |
| 555 | * un-interruptible thread earlier and we're bailing out immediately. |
| 556 | * |
| 557 | * The doc sayeth: "The interrupted status of the current thread is |
| 558 | * cleared when this exception is thrown." |
| 559 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 560 | { |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 561 | MutexLock mu(self, *self->GetWaitMutex()); |
| 562 | self->SetInterruptedLocked(false); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 563 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 564 | if (interruptShouldThrow) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 565 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 566 | self->ThrowNewException(throw_location, "Ljava/lang/InterruptedException;", NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | void Monitor::Notify(Thread* self) { |
| 572 | DCHECK(self != NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 573 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 574 | // Make sure that we hold the lock. |
| 575 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 576 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 577 | return; |
| 578 | } |
| 579 | // Signal the first waiting thread in the wait set. |
| 580 | while (wait_set_ != NULL) { |
| 581 | Thread* thread = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 582 | wait_set_ = thread->GetWaitNext(); |
| 583 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 584 | |
| 585 | // Check to see if the thread is still waiting. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 586 | MutexLock mu(self, *thread->GetWaitMutex()); |
| 587 | if (thread->GetWaitMonitor() != nullptr) { |
| 588 | thread->GetWaitConditionVariable()->Signal(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void Monitor::NotifyAll(Thread* self) { |
| 595 | DCHECK(self != NULL); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 596 | MutexLock mu(self, monitor_lock_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 597 | // Make sure that we hold the lock. |
| 598 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 599 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 600 | return; |
| 601 | } |
| 602 | // Signal all threads in the wait set. |
| 603 | while (wait_set_ != NULL) { |
| 604 | Thread* thread = wait_set_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 605 | wait_set_ = thread->GetWaitNext(); |
| 606 | thread->SetWaitNext(nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 607 | thread->Notify(); |
| 608 | } |
| 609 | } |
| 610 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 611 | bool Monitor::Deflate(Thread* self, mirror::Object* obj) { |
| 612 | DCHECK(obj != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 613 | // Don't need volatile since we only deflate with mutators suspended. |
| 614 | LockWord lw(obj->GetLockWord(false)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 615 | // If the lock isn't an inflated monitor, then we don't need to deflate anything. |
| 616 | if (lw.GetState() == LockWord::kFatLocked) { |
| 617 | Monitor* monitor = lw.FatLockMonitor(); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 618 | DCHECK(monitor != nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 619 | MutexLock mu(self, monitor->monitor_lock_); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 620 | // Can't deflate if we have anybody waiting on the CV. |
| 621 | if (monitor->num_waiters_ > 0) { |
| 622 | return false; |
| 623 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 624 | Thread* owner = monitor->owner_; |
| 625 | if (owner != nullptr) { |
| 626 | // Can't deflate if we are locked and have a hash code. |
| 627 | if (monitor->HasHashCode()) { |
| 628 | return false; |
| 629 | } |
| 630 | // Can't deflate if our lock count is too high. |
| 631 | if (monitor->lock_count_ > LockWord::kThinLockMaxCount) { |
| 632 | return false; |
| 633 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 634 | // Deflate to a thin lock. |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 635 | obj->SetLockWord(LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_), false); |
| 636 | VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / " |
| 637 | << monitor->lock_count_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 638 | } else if (monitor->HasHashCode()) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 639 | obj->SetLockWord(LockWord::FromHashCode(monitor->GetHashCode()), false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 640 | VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 641 | } else { |
| 642 | // 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] | 643 | obj->SetLockWord(LockWord(), false); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 644 | VLOG(monitor) << "Deflated" << obj << " to empty lock word"; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 645 | } |
| 646 | // The monitor is deflated, mark the object as nullptr so that we know to delete it during the |
| 647 | // next GC. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 648 | monitor->obj_ = GcRoot<mirror::Object>(nullptr); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 649 | } |
| 650 | return true; |
| 651 | } |
| 652 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 653 | 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] | 654 | DCHECK(self != nullptr); |
| 655 | DCHECK(obj != nullptr); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 656 | // Allocate and acquire a new monitor. |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 657 | Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code); |
| 658 | DCHECK(m != nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 659 | if (m->Install(self)) { |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 660 | if (owner != nullptr) { |
| 661 | VLOG(monitor) << "monitor: thread" << owner->GetThreadId() |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 662 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 663 | } else { |
| 664 | VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 665 | << " created monitor " << m << " for object " << obj; |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 666 | } |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 667 | Runtime::Current()->GetMonitorList()->Add(m); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 668 | CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 669 | } else { |
| 670 | MonitorPool::ReleaseMonitor(self, m); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 671 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 674 | void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 675 | uint32_t hash_code) { |
| 676 | DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked); |
| 677 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 678 | if (owner_thread_id == self->GetThreadId()) { |
| 679 | // We own the monitor, we can easily inflate it. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 680 | Inflate(self, self, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 681 | } else { |
| 682 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 683 | // 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] | 684 | self->SetMonitorEnterObject(obj.Get()); |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 685 | bool timed_out; |
| 686 | Thread* owner; |
| 687 | { |
| 688 | ScopedThreadStateChange tsc(self, kBlocked); |
Ian Rogers | f3d874c | 2014-07-17 18:52:42 -0700 | [diff] [blame] | 689 | // Take suspend thread lock to avoid races with threads trying to suspend this one. |
| 690 | MutexLock mu(self, *Locks::thread_list_suspend_thread_lock_); |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 691 | owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out); |
| 692 | } |
| 693 | if (owner != nullptr) { |
| 694 | // We succeeded in suspending the thread, check the lock's status didn't change. |
| 695 | lock_word = obj->GetLockWord(true); |
| 696 | if (lock_word.GetState() == LockWord::kThinLocked && |
| 697 | lock_word.ThinLockOwner() == owner_thread_id) { |
| 698 | // Go ahead and inflate the lock. |
| 699 | Inflate(self, owner, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 700 | } |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 701 | thread_list->Resume(owner, false); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 702 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 703 | self->SetMonitorEnterObject(nullptr); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 704 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 707 | // Fool annotalysis into thinking that the lock on obj is acquired. |
| 708 | static mirror::Object* FakeLock(mirror::Object* obj) |
| 709 | EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 710 | return obj; |
| 711 | } |
| 712 | |
| 713 | // Fool annotalysis into thinking that the lock on obj is release. |
| 714 | static mirror::Object* FakeUnlock(mirror::Object* obj) |
| 715 | UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 716 | return obj; |
| 717 | } |
| 718 | |
Mathieu Chartier | e7e8a5f | 2014-02-14 16:59:41 -0800 | [diff] [blame] | 719 | mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 720 | DCHECK(self != NULL); |
| 721 | DCHECK(obj != NULL); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 722 | obj = FakeLock(obj); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 723 | uint32_t thread_id = self->GetThreadId(); |
| 724 | size_t contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 725 | StackHandleScope<1> hs(self); |
| 726 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 727 | while (true) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 728 | LockWord lock_word = h_obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 729 | switch (lock_word.GetState()) { |
| 730 | case LockWord::kUnlocked: { |
| 731 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0)); |
Ian Rogers | 228602f | 2014-07-10 02:07:54 -0700 | [diff] [blame] | 732 | if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) { |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 733 | // CasLockWord enforces more than the acquire ordering we need here. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 734 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 735 | } |
| 736 | continue; // Go again. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 737 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 738 | case LockWord::kThinLocked: { |
| 739 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 740 | if (owner_thread_id == thread_id) { |
| 741 | // We own the lock, increase the recursion count. |
| 742 | uint32_t new_count = lock_word.ThinLockCount() + 1; |
| 743 | if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) { |
| 744 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 745 | h_obj->SetLockWord(thin_locked, true); |
| 746 | return h_obj.Get(); // Success! |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 747 | } else { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 748 | // We'd overflow the recursion count, so inflate the monitor. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 749 | InflateThinLocked(self, h_obj, lock_word, 0); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 750 | } |
| 751 | } else { |
| 752 | // Contention. |
| 753 | contention_count++; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 754 | Runtime* runtime = Runtime::Current(); |
| 755 | if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) { |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 756 | // TODO: Consider switching the thread state to kBlocked when we are yielding. |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 757 | // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the |
| 758 | // 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] | 759 | // and make long pauses. See b/16307460. |
Mathieu Chartier | 251755c | 2014-07-15 18:10:25 -0700 | [diff] [blame] | 760 | sched_yield(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 761 | } else { |
| 762 | contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 763 | InflateThinLocked(self, h_obj, lock_word, 0); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 764 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 765 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 766 | continue; // Start from the beginning. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 767 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 768 | case LockWord::kFatLocked: { |
| 769 | Monitor* mon = lock_word.FatLockMonitor(); |
| 770 | mon->Lock(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 771 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 772 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 773 | case LockWord::kHashCode: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 774 | // Inflate with the existing hashcode. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 775 | Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode()); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 776 | continue; // Start from the beginning. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 777 | default: { |
| 778 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 779 | return h_obj.Get(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 780 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 781 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 785 | bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 786 | DCHECK(self != NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 787 | DCHECK(obj != NULL); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 788 | obj = FakeUnlock(obj); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 789 | LockWord lock_word = obj->GetLockWord(true); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 790 | StackHandleScope<1> hs(self); |
| 791 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 792 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 793 | case LockWord::kHashCode: |
| 794 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 795 | case LockWord::kUnlocked: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 796 | FailedUnlock(h_obj.Get(), self, nullptr, nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 797 | return false; // Failure. |
| 798 | case LockWord::kThinLocked: { |
| 799 | uint32_t thread_id = self->GetThreadId(); |
| 800 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 801 | if (owner_thread_id != thread_id) { |
| 802 | // TODO: there's a race here with the owner dying while we unlock. |
| 803 | Thread* owner = |
| 804 | Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 805 | FailedUnlock(h_obj.Get(), self, owner, nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 806 | return false; // Failure. |
| 807 | } else { |
| 808 | // We own the lock, decrease the recursion count. |
| 809 | if (lock_word.ThinLockCount() != 0) { |
| 810 | uint32_t new_count = lock_word.ThinLockCount() - 1; |
| 811 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 812 | h_obj->SetLockWord(thin_locked, true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 813 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 814 | h_obj->SetLockWord(LockWord(), true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 815 | } |
| 816 | return true; // Success! |
| 817 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 818 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 819 | case LockWord::kFatLocked: { |
| 820 | Monitor* mon = lock_word.FatLockMonitor(); |
| 821 | return mon->Unlock(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 822 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 823 | default: { |
| 824 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 825 | return false; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 826 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 827 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Object.wait(). Also called for class init. |
| 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 | 6f22fc1 | 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 | 6f22fc1 | 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 | 6f22fc1 | 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"; |
| 923 | return ThreadList::kInvalidThreadId; |
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>) |
| 963 | os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(), |
| 964 | PrettyTypeOf(pretty_object).c_str()); |
| 965 | } |
| 966 | } |
| 967 | // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5 |
| 968 | if (lock_owner != ThreadList::kInvalidThreadId) { |
| 969 | os << " held by thread " << lock_owner; |
| 970 | } |
| 971 | os << "\n"; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 972 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 975 | mirror::Object* Monitor::GetContendedMonitor(Thread* thread) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 976 | // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre |
| 977 | // 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] | 978 | mirror::Object* result = thread->GetMonitorEnterObject(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 979 | if (result == NULL) { |
| 980 | // ...but also a monitor that the thread is waiting on. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 981 | MutexLock mu(Thread::Current(), *thread->GetWaitMutex()); |
| 982 | Monitor* monitor = thread->GetWaitMonitor(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 983 | if (monitor != NULL) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 984 | result = monitor->GetObject(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 985 | } |
| 986 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 987 | return result; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 988 | } |
| 989 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 990 | void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*), |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 991 | void* callback_context, bool abort_on_failure) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 992 | mirror::ArtMethod* m = stack_visitor->GetMethod(); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 993 | CHECK(m != NULL); |
| 994 | |
| 995 | // Native methods are an easy special case. |
| 996 | // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. |
| 997 | if (m->IsNative()) { |
| 998 | if (m->IsSynchronized()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 999 | mirror::Object* jni_this = stack_visitor->GetCurrentHandleScope()->GetReference(0); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 1000 | callback(jni_this, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1001 | } |
| 1002 | return; |
| 1003 | } |
| 1004 | |
jeffhao | 61f916c | 2012-10-25 17:48:51 -0700 | [diff] [blame] | 1005 | // Proxy methods should not be synchronized. |
| 1006 | if (m->IsProxyMethod()) { |
| 1007 | CHECK(!m->IsSynchronized()); |
| 1008 | return; |
| 1009 | } |
| 1010 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1011 | // 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] | 1012 | const DexFile::CodeItem* code_item = m->GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 1013 | CHECK(code_item != NULL) << PrettyMethod(m); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1014 | if (code_item->tries_size_ == 0) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1015 | return; // No "tries" implies no synchronization, so no held locks to report. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1018 | // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot |
| 1019 | // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an |
| 1020 | // inconsistent stack anyways. |
| 1021 | uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure); |
| 1022 | if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) { |
| 1023 | LOG(ERROR) << "Could not find dex_pc for " << PrettyMethod(m); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1027 | // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to |
| 1028 | // the locks held in this stack frame. |
| 1029 | std::vector<uint32_t> monitor_enter_dex_pcs; |
Andreas Gampe | 760172c | 2014-08-16 13:41:10 -0700 | [diff] [blame] | 1030 | verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs); |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1031 | if (monitor_enter_dex_pcs.empty()) { |
| 1032 | return; |
| 1033 | } |
| 1034 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1035 | for (size_t i = 0; i < monitor_enter_dex_pcs.size(); ++i) { |
| 1036 | // The verifier works in terms of the dex pcs of the monitor-enter instructions. |
| 1037 | // We want the registers used by those instructions (so we can read the values out of them). |
| 1038 | uint32_t dex_pc = monitor_enter_dex_pcs[i]; |
| 1039 | uint16_t monitor_enter_instruction = code_item->insns_[dex_pc]; |
| 1040 | |
| 1041 | // Quick sanity check. |
| 1042 | if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) { |
| 1043 | LOG(FATAL) << "expected monitor-enter @" << dex_pc << "; was " |
| 1044 | << reinterpret_cast<void*>(monitor_enter_instruction); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1047 | uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1048 | mirror::Object* o = reinterpret_cast<mirror::Object*>(stack_visitor->GetVReg(m, monitor_register, |
| 1049 | kReferenceVReg)); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 1050 | callback(o, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1054 | bool Monitor::IsValidLockWord(LockWord lock_word) { |
| 1055 | switch (lock_word.GetState()) { |
| 1056 | case LockWord::kUnlocked: |
| 1057 | // Nothing to check. |
| 1058 | return true; |
| 1059 | case LockWord::kThinLocked: |
| 1060 | // Basic sanity check of owner. |
| 1061 | return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId; |
| 1062 | case LockWord::kFatLocked: { |
| 1063 | // Check the monitor appears in the monitor list. |
| 1064 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1065 | MonitorList* list = Runtime::Current()->GetMonitorList(); |
| 1066 | MutexLock mu(Thread::Current(), list->monitor_list_lock_); |
| 1067 | for (Monitor* list_mon : list->list_) { |
| 1068 | if (mon == list_mon) { |
| 1069 | return true; // Found our monitor. |
| 1070 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1071 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1072 | return false; // Fail - unowned monitor in an object. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1073 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1074 | case LockWord::kHashCode: |
| 1075 | return true; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1076 | default: |
| 1077 | LOG(FATAL) << "Unreachable"; |
| 1078 | return false; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1082 | bool Monitor::IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1083 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1084 | return owner_ != nullptr; |
| 1085 | } |
| 1086 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1087 | void Monitor::TranslateLocation(mirror::ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1088 | const char** source_file, uint32_t* line_number) const { |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1089 | // If method is null, location is unknown |
| 1090 | if (method == NULL) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1091 | *source_file = ""; |
| 1092 | *line_number = 0; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1093 | return; |
| 1094 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1095 | *source_file = method->GetDeclaringClassSourceFile(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1096 | if (*source_file == NULL) { |
| 1097 | *source_file = ""; |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 1098 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1099 | *line_number = method->GetLineNumFromDexPC(dex_pc); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | uint32_t Monitor::GetOwnerThreadId() { |
| 1103 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1104 | Thread* owner = owner_; |
| 1105 | if (owner != NULL) { |
| 1106 | return owner->GetThreadId(); |
| 1107 | } else { |
| 1108 | return ThreadList::kInvalidThreadId; |
| 1109 | } |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1112 | MonitorList::MonitorList() |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1113 | : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock), |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1114 | monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | MonitorList::~MonitorList() { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1118 | Thread* self = Thread::Current(); |
| 1119 | MutexLock mu(self, monitor_list_lock_); |
| 1120 | // Release all monitors to the pool. |
| 1121 | // TODO: Is it an invariant that *all* open monitors are in the list? Then we could |
| 1122 | // clear faster in the pool. |
| 1123 | MonitorPool::ReleaseMonitors(self, &list_); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1126 | void MonitorList::DisallowNewMonitors() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1127 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1128 | allow_new_monitors_ = false; |
| 1129 | } |
| 1130 | |
| 1131 | void MonitorList::AllowNewMonitors() { |
| 1132 | Thread* self = Thread::Current(); |
| 1133 | MutexLock mu(self, monitor_list_lock_); |
| 1134 | allow_new_monitors_ = true; |
| 1135 | monitor_add_condition_.Broadcast(self); |
| 1136 | } |
| 1137 | |
| 1138 | void MonitorList::Add(Monitor* m) { |
| 1139 | Thread* self = Thread::Current(); |
| 1140 | MutexLock mu(self, monitor_list_lock_); |
| 1141 | while (UNLIKELY(!allow_new_monitors_)) { |
| 1142 | monitor_add_condition_.WaitHoldingLocks(self); |
| 1143 | } |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1144 | list_.push_front(m); |
| 1145 | } |
| 1146 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1147 | void MonitorList::SweepMonitorList(IsMarkedCallback* callback, void* arg) { |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1148 | Thread* self = Thread::Current(); |
| 1149 | MutexLock mu(self, monitor_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1150 | for (auto it = list_.begin(); it != list_.end(); ) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1151 | Monitor* m = *it; |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1152 | // Disable the read barrier in GetObject() as this is called by GC. |
| 1153 | mirror::Object* obj = m->GetObject<kWithoutReadBarrier>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1154 | // 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] | 1155 | mirror::Object* new_obj = obj != nullptr ? callback(obj, arg) : nullptr; |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1156 | if (new_obj == nullptr) { |
| 1157 | VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 1158 | << obj; |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 1159 | MonitorPool::ReleaseMonitor(self, m); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1160 | it = list_.erase(it); |
| 1161 | } else { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1162 | m->SetObject(new_obj); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1163 | ++it; |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1168 | struct MonitorDeflateArgs { |
| 1169 | MonitorDeflateArgs() : self(Thread::Current()), deflate_count(0) {} |
| 1170 | Thread* const self; |
| 1171 | size_t deflate_count; |
| 1172 | }; |
| 1173 | |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1174 | static mirror::Object* MonitorDeflateCallback(mirror::Object* object, void* arg) |
| 1175 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1176 | MonitorDeflateArgs* args = reinterpret_cast<MonitorDeflateArgs*>(arg); |
| 1177 | if (Monitor::Deflate(args->self, object)) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1178 | DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1179 | ++args->deflate_count; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1180 | // If we deflated, return nullptr so that the monitor gets removed from the array. |
| 1181 | return nullptr; |
| 1182 | } |
| 1183 | return object; // Monitor was not deflated. |
| 1184 | } |
| 1185 | |
Mathieu Chartier | 48ab687 | 2014-06-24 11:21:59 -0700 | [diff] [blame] | 1186 | size_t MonitorList::DeflateMonitors() { |
| 1187 | MonitorDeflateArgs args; |
| 1188 | Locks::mutator_lock_->AssertExclusiveHeld(args.self); |
| 1189 | SweepMonitorList(MonitorDeflateCallback, &args); |
| 1190 | return args.deflate_count; |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1193 | MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(NULL), entry_count_(0) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1194 | DCHECK(obj != nullptr); |
| 1195 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1196 | switch (lock_word.GetState()) { |
| 1197 | case LockWord::kUnlocked: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1198 | // Fall-through. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1199 | case LockWord::kForwardingAddress: |
| 1200 | // Fall-through. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1201 | case LockWord::kHashCode: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1202 | break; |
| 1203 | case LockWord::kThinLocked: |
| 1204 | owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
| 1205 | entry_count_ = 1 + lock_word.ThinLockCount(); |
| 1206 | // Thin locks have no waiters. |
| 1207 | break; |
| 1208 | case LockWord::kFatLocked: { |
| 1209 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1210 | owner_ = mon->owner_; |
| 1211 | entry_count_ = 1 + mon->lock_count_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1212 | for (Thread* waiter = mon->wait_set_; waiter != NULL; waiter = waiter->GetWaitNext()) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1213 | waiters_.push_back(waiter); |
| 1214 | } |
| 1215 | break; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1220 | } // namespace art |