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