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