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. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame^] | 114 | LockWord lw(GetObject()->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. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame^] | 140 | bool success = GetObject()->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_. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame^] | 231 | self->SetMonitorEnterObject(GetObject()); |
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. |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame^] | 366 | FailedUnlock(GetObject(), 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 | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 626 | std::unique_ptr<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)) { |
Haifeng Li | 86ab791 | 2014-05-16 10:47:59 +0800 | [diff] [blame] | 628 | if (owner != nullptr) { |
| 629 | VLOG(monitor) << "monitor: thread" << owner->GetThreadId() |
| 630 | << " created monitor " << m.get() << " for object " << obj; |
| 631 | } else { |
| 632 | VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code |
| 633 | << " created monitor " << m.get() << " for object " << obj; |
| 634 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 635 | Runtime::Current()->GetMonitorList()->Add(m.release()); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 636 | CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 637 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 640 | void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object>& obj, LockWord lock_word, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 641 | uint32_t hash_code) { |
| 642 | DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked); |
| 643 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 644 | if (owner_thread_id == self->GetThreadId()) { |
| 645 | // We own the monitor, we can easily inflate it. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 646 | Inflate(self, self, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 647 | } else { |
| 648 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 649 | // 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] | 650 | self->SetMonitorEnterObject(obj.Get()); |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 651 | bool timed_out; |
| 652 | Thread* owner; |
| 653 | { |
| 654 | ScopedThreadStateChange tsc(self, kBlocked); |
| 655 | owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out); |
| 656 | } |
| 657 | if (owner != nullptr) { |
| 658 | // We succeeded in suspending the thread, check the lock's status didn't change. |
| 659 | lock_word = obj->GetLockWord(true); |
| 660 | if (lock_word.GetState() == LockWord::kThinLocked && |
| 661 | lock_word.ThinLockOwner() == owner_thread_id) { |
| 662 | // Go ahead and inflate the lock. |
| 663 | Inflate(self, owner, obj.Get(), hash_code); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 664 | } |
Mathieu Chartier | a1ee14f | 2014-05-14 16:51:03 -0700 | [diff] [blame] | 665 | thread_list->Resume(owner, false); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 666 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 667 | self->SetMonitorEnterObject(nullptr); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 668 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 671 | // Fool annotalysis into thinking that the lock on obj is acquired. |
| 672 | static mirror::Object* FakeLock(mirror::Object* obj) |
| 673 | EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 674 | return obj; |
| 675 | } |
| 676 | |
| 677 | // Fool annotalysis into thinking that the lock on obj is release. |
| 678 | static mirror::Object* FakeUnlock(mirror::Object* obj) |
| 679 | UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS { |
| 680 | return obj; |
| 681 | } |
| 682 | |
Mathieu Chartier | e7e8a5f | 2014-02-14 16:59:41 -0800 | [diff] [blame] | 683 | mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 684 | DCHECK(self != NULL); |
| 685 | DCHECK(obj != NULL); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 686 | obj = FakeLock(obj); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 687 | uint32_t thread_id = self->GetThreadId(); |
| 688 | size_t contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 689 | StackHandleScope<1> hs(self); |
| 690 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 691 | while (true) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 692 | LockWord lock_word = h_obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 693 | switch (lock_word.GetState()) { |
| 694 | case LockWord::kUnlocked: { |
| 695 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 696 | if (h_obj->CasLockWord(lock_word, thin_locked)) { |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 697 | QuasiAtomic::MembarLoadLoad(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 698 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 699 | } |
| 700 | continue; // Go again. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 701 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 702 | case LockWord::kThinLocked: { |
| 703 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 704 | if (owner_thread_id == thread_id) { |
| 705 | // We own the lock, increase the recursion count. |
| 706 | uint32_t new_count = lock_word.ThinLockCount() + 1; |
| 707 | if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) { |
| 708 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 709 | h_obj->SetLockWord(thin_locked, true); |
| 710 | return h_obj.Get(); // Success! |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 711 | } else { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 712 | // We'd overflow the recursion count, so inflate the monitor. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 713 | InflateThinLocked(self, h_obj, lock_word, 0); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 714 | } |
| 715 | } else { |
| 716 | // Contention. |
| 717 | contention_count++; |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 718 | Runtime* runtime = Runtime::Current(); |
| 719 | if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 720 | NanoSleep(1000); // Sleep for 1us and re-attempt. |
| 721 | } else { |
| 722 | contention_count = 0; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 723 | InflateThinLocked(self, h_obj, lock_word, 0); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 724 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 725 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 726 | continue; // Start from the beginning. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 727 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 728 | case LockWord::kFatLocked: { |
| 729 | Monitor* mon = lock_word.FatLockMonitor(); |
| 730 | mon->Lock(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 731 | return h_obj.Get(); // Success! |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 732 | } |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 733 | case LockWord::kHashCode: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 734 | // Inflate with the existing hashcode. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 735 | Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode()); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 736 | continue; // Start from the beginning. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 737 | default: { |
| 738 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 739 | return h_obj.Get(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 740 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 741 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 745 | bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 746 | DCHECK(self != NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 747 | DCHECK(obj != NULL); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 748 | obj = FakeUnlock(obj); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 749 | LockWord lock_word = obj->GetLockWord(true); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 750 | StackHandleScope<1> hs(self); |
| 751 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 752 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 753 | case LockWord::kHashCode: |
| 754 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 755 | case LockWord::kUnlocked: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 756 | FailedUnlock(h_obj.Get(), self, nullptr, nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 757 | return false; // Failure. |
| 758 | case LockWord::kThinLocked: { |
| 759 | uint32_t thread_id = self->GetThreadId(); |
| 760 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 761 | if (owner_thread_id != thread_id) { |
| 762 | // TODO: there's a race here with the owner dying while we unlock. |
| 763 | Thread* owner = |
| 764 | Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 765 | FailedUnlock(h_obj.Get(), self, owner, nullptr); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 766 | return false; // Failure. |
| 767 | } else { |
| 768 | // We own the lock, decrease the recursion count. |
| 769 | if (lock_word.ThinLockCount() != 0) { |
| 770 | uint32_t new_count = lock_word.ThinLockCount() - 1; |
| 771 | LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 772 | h_obj->SetLockWord(thin_locked, true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 773 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 774 | h_obj->SetLockWord(LockWord(), true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 775 | } |
| 776 | return true; // Success! |
| 777 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 778 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 779 | case LockWord::kFatLocked: { |
| 780 | Monitor* mon = lock_word.FatLockMonitor(); |
| 781 | return mon->Unlock(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 782 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 783 | default: { |
| 784 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 785 | return false; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 786 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 787 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Object.wait(). Also called for class init. |
| 792 | */ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 793 | 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] | 794 | bool interruptShouldThrow, ThreadState why) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 795 | DCHECK(self != nullptr); |
| 796 | DCHECK(obj != nullptr); |
| 797 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 798 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 799 | case LockWord::kHashCode: |
| 800 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 801 | case LockWord::kUnlocked: |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 802 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 803 | return; // Failure. |
| 804 | case LockWord::kThinLocked: { |
| 805 | uint32_t thread_id = self->GetThreadId(); |
| 806 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 807 | if (owner_thread_id != thread_id) { |
| 808 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
| 809 | return; // Failure. |
| 810 | } else { |
| 811 | // We own the lock, inflate to enqueue ourself on the Monitor. |
Mathieu Chartier | 4e6a31e | 2013-10-31 10:35:05 -0700 | [diff] [blame] | 812 | Inflate(self, self, obj, 0); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 813 | lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 814 | } |
| 815 | break; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 816 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 817 | case LockWord::kFatLocked: |
| 818 | break; // Already set for a wait. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 819 | default: { |
| 820 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 821 | return; |
| 822 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 823 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 824 | Monitor* mon = lock_word.FatLockMonitor(); |
| 825 | mon->Wait(self, ms, ns, interruptShouldThrow, why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Ian Rogers | 13c479e | 2013-10-11 07:59:01 -0700 | [diff] [blame] | 828 | void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 829 | DCHECK(self != nullptr); |
| 830 | DCHECK(obj != nullptr); |
| 831 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 832 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 833 | case LockWord::kHashCode: |
| 834 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 835 | case LockWord::kUnlocked: |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 836 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 837 | return; // Failure. |
| 838 | case LockWord::kThinLocked: { |
| 839 | uint32_t thread_id = self->GetThreadId(); |
| 840 | uint32_t owner_thread_id = lock_word.ThinLockOwner(); |
| 841 | if (owner_thread_id != thread_id) { |
| 842 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
| 843 | return; // Failure. |
| 844 | } else { |
| 845 | // We own the lock but there's no Monitor and therefore no waiters. |
| 846 | return; // Success. |
| 847 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 848 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 849 | case LockWord::kFatLocked: { |
| 850 | Monitor* mon = lock_word.FatLockMonitor(); |
| 851 | if (notify_all) { |
| 852 | mon->NotifyAll(self); |
| 853 | } else { |
| 854 | mon->Notify(self); |
| 855 | } |
| 856 | return; // Success. |
| 857 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 858 | default: { |
| 859 | LOG(FATAL) << "Invalid monitor state " << lock_word.GetState(); |
| 860 | return; |
| 861 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 865 | uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 866 | DCHECK(obj != nullptr); |
| 867 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 868 | switch (lock_word.GetState()) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 869 | case LockWord::kHashCode: |
| 870 | // Fall-through. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 871 | case LockWord::kUnlocked: |
| 872 | return ThreadList::kInvalidThreadId; |
| 873 | case LockWord::kThinLocked: |
| 874 | return lock_word.ThinLockOwner(); |
| 875 | case LockWord::kFatLocked: { |
| 876 | Monitor* mon = lock_word.FatLockMonitor(); |
| 877 | return mon->GetOwnerThreadId(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 878 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 879 | default: { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 880 | LOG(FATAL) << "Unreachable"; |
| 881 | return ThreadList::kInvalidThreadId; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 882 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 886 | void Monitor::DescribeWait(std::ostream& os, const Thread* thread) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 887 | // Determine the wait message and object we're waiting or blocked upon. |
| 888 | mirror::Object* pretty_object = nullptr; |
| 889 | const char* wait_message = nullptr; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 890 | uint32_t lock_owner = ThreadList::kInvalidThreadId; |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 891 | ThreadState state = thread->GetState(); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 892 | if (state == kWaiting || state == kTimedWaiting || state == kSleeping) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 893 | wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on "; |
| 894 | Thread* self = Thread::Current(); |
| 895 | MutexLock mu(self, *thread->GetWaitMutex()); |
| 896 | Monitor* monitor = thread->GetWaitMonitor(); |
| 897 | if (monitor != nullptr) { |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame^] | 898 | pretty_object = monitor->GetObject(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 899 | } |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 900 | } else if (state == kBlocked) { |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 901 | wait_message = " - waiting to lock "; |
| 902 | pretty_object = thread->GetMonitorEnterObject(); |
| 903 | if (pretty_object != nullptr) { |
| 904 | lock_owner = pretty_object->GetLockOwnerThreadId(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 905 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 906 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 907 | |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 908 | if (wait_message != nullptr) { |
| 909 | if (pretty_object == nullptr) { |
| 910 | os << wait_message << "an unknown object"; |
| 911 | } else { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 912 | if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) && |
Ian Rogers | d803bc7 | 2014-04-01 15:33:03 -0700 | [diff] [blame] | 913 | Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) { |
| 914 | // Getting the identity hashcode here would result in lock inflation and suspension of the |
| 915 | // current thread, which isn't safe if this is the only runnable thread. |
| 916 | os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)", |
| 917 | reinterpret_cast<intptr_t>(pretty_object), |
| 918 | PrettyTypeOf(pretty_object).c_str()); |
| 919 | } else { |
| 920 | // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>) |
| 921 | os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(), |
| 922 | PrettyTypeOf(pretty_object).c_str()); |
| 923 | } |
| 924 | } |
| 925 | // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5 |
| 926 | if (lock_owner != ThreadList::kInvalidThreadId) { |
| 927 | os << " held by thread " << lock_owner; |
| 928 | } |
| 929 | os << "\n"; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 930 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 933 | mirror::Object* Monitor::GetContendedMonitor(Thread* thread) { |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 934 | // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre |
| 935 | // 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] | 936 | mirror::Object* result = thread->GetMonitorEnterObject(); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 937 | if (result == NULL) { |
| 938 | // ...but also a monitor that the thread is waiting on. |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 939 | MutexLock mu(Thread::Current(), *thread->GetWaitMutex()); |
| 940 | Monitor* monitor = thread->GetWaitMonitor(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 941 | if (monitor != NULL) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 942 | result = monitor->GetObject(); |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 943 | } |
| 944 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 945 | return result; |
Elliott Hughes | f950170 | 2013-01-11 11:22:27 -0800 | [diff] [blame] | 946 | } |
| 947 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 948 | void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*), |
| 949 | void* callback_context) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 950 | mirror::ArtMethod* m = stack_visitor->GetMethod(); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 951 | CHECK(m != NULL); |
| 952 | |
| 953 | // Native methods are an easy special case. |
| 954 | // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. |
| 955 | if (m->IsNative()) { |
| 956 | if (m->IsSynchronized()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 957 | mirror::Object* jni_this = stack_visitor->GetCurrentHandleScope()->GetReference(0); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 958 | callback(jni_this, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 959 | } |
| 960 | return; |
| 961 | } |
| 962 | |
jeffhao | 61f916c | 2012-10-25 17:48:51 -0700 | [diff] [blame] | 963 | // Proxy methods should not be synchronized. |
| 964 | if (m->IsProxyMethod()) { |
| 965 | CHECK(!m->IsSynchronized()); |
| 966 | return; |
| 967 | } |
| 968 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 969 | // <clinit> is another special case. The runtime holds the class lock while calling <clinit>. |
| 970 | MethodHelper mh(m); |
| 971 | if (mh.IsClassInitializer()) { |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 972 | callback(m->GetDeclaringClass(), callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 973 | // Fall through because there might be synchronization in the user code too. |
| 974 | } |
| 975 | |
| 976 | // Is there any reason to believe there's any synchronization in this method? |
| 977 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 978 | CHECK(code_item != NULL) << PrettyMethod(m); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 979 | if (code_item->tries_size_ == 0) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 980 | return; // No "tries" implies no synchronization, so no held locks to report. |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 983 | // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to |
| 984 | // the locks held in this stack frame. |
| 985 | std::vector<uint32_t> monitor_enter_dex_pcs; |
| 986 | verifier::MethodVerifier::FindLocksAtDexPc(m, stack_visitor->GetDexPc(), monitor_enter_dex_pcs); |
| 987 | if (monitor_enter_dex_pcs.empty()) { |
| 988 | return; |
| 989 | } |
| 990 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 991 | for (size_t i = 0; i < monitor_enter_dex_pcs.size(); ++i) { |
| 992 | // The verifier works in terms of the dex pcs of the monitor-enter instructions. |
| 993 | // We want the registers used by those instructions (so we can read the values out of them). |
| 994 | uint32_t dex_pc = monitor_enter_dex_pcs[i]; |
| 995 | uint16_t monitor_enter_instruction = code_item->insns_[dex_pc]; |
| 996 | |
| 997 | // Quick sanity check. |
| 998 | if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) { |
| 999 | LOG(FATAL) << "expected monitor-enter @" << dex_pc << "; was " |
| 1000 | << reinterpret_cast<void*>(monitor_enter_instruction); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 1003 | uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1004 | mirror::Object* o = reinterpret_cast<mirror::Object*>(stack_visitor->GetVReg(m, monitor_register, |
| 1005 | kReferenceVReg)); |
Elliott Hughes | 4993bbc | 2013-01-10 15:41:25 -0800 | [diff] [blame] | 1006 | callback(o, callback_context); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1010 | bool Monitor::IsValidLockWord(LockWord lock_word) { |
| 1011 | switch (lock_word.GetState()) { |
| 1012 | case LockWord::kUnlocked: |
| 1013 | // Nothing to check. |
| 1014 | return true; |
| 1015 | case LockWord::kThinLocked: |
| 1016 | // Basic sanity check of owner. |
| 1017 | return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId; |
| 1018 | case LockWord::kFatLocked: { |
| 1019 | // Check the monitor appears in the monitor list. |
| 1020 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1021 | MonitorList* list = Runtime::Current()->GetMonitorList(); |
| 1022 | MutexLock mu(Thread::Current(), list->monitor_list_lock_); |
| 1023 | for (Monitor* list_mon : list->list_) { |
| 1024 | if (mon == list_mon) { |
| 1025 | return true; // Found our monitor. |
| 1026 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1027 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1028 | return false; // Fail - unowned monitor in an object. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1029 | } |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1030 | case LockWord::kHashCode: |
| 1031 | return true; |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1032 | default: |
| 1033 | LOG(FATAL) << "Unreachable"; |
| 1034 | return false; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1038 | bool Monitor::IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1039 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1040 | return owner_ != nullptr; |
| 1041 | } |
| 1042 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1043 | void Monitor::TranslateLocation(mirror::ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1044 | const char** source_file, uint32_t* line_number) const { |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1045 | // If method is null, location is unknown |
| 1046 | if (method == NULL) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1047 | *source_file = ""; |
| 1048 | *line_number = 0; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1049 | return; |
| 1050 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1051 | MethodHelper mh(method); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1052 | *source_file = mh.GetDeclaringClassSourceFile(); |
| 1053 | if (*source_file == NULL) { |
| 1054 | *source_file = ""; |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 1055 | } |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1056 | *line_number = mh.GetLineNumFromDexPC(dex_pc); |
| 1057 | } |
| 1058 | |
| 1059 | uint32_t Monitor::GetOwnerThreadId() { |
| 1060 | MutexLock mu(Thread::Current(), monitor_lock_); |
| 1061 | Thread* owner = owner_; |
| 1062 | if (owner != NULL) { |
| 1063 | return owner->GetThreadId(); |
| 1064 | } else { |
| 1065 | return ThreadList::kInvalidThreadId; |
| 1066 | } |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1069 | MonitorList::MonitorList() |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1070 | : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock), |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1071 | monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | MonitorList::~MonitorList() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1075 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1076 | STLDeleteElements(&list_); |
| 1077 | } |
| 1078 | |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1079 | void MonitorList::DisallowNewMonitors() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1080 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Mathieu Chartier | c11d9b8 | 2013-09-19 10:01:59 -0700 | [diff] [blame] | 1081 | allow_new_monitors_ = false; |
| 1082 | } |
| 1083 | |
| 1084 | void MonitorList::AllowNewMonitors() { |
| 1085 | Thread* self = Thread::Current(); |
| 1086 | MutexLock mu(self, monitor_list_lock_); |
| 1087 | allow_new_monitors_ = true; |
| 1088 | monitor_add_condition_.Broadcast(self); |
| 1089 | } |
| 1090 | |
| 1091 | void MonitorList::Add(Monitor* m) { |
| 1092 | Thread* self = Thread::Current(); |
| 1093 | MutexLock mu(self, monitor_list_lock_); |
| 1094 | while (UNLIKELY(!allow_new_monitors_)) { |
| 1095 | monitor_add_condition_.WaitHoldingLocks(self); |
| 1096 | } |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1097 | list_.push_front(m); |
| 1098 | } |
| 1099 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 1100 | void MonitorList::SweepMonitorList(IsMarkedCallback* callback, void* arg) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1101 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1102 | for (auto it = list_.begin(); it != list_.end(); ) { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1103 | Monitor* m = *it; |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame^] | 1104 | // Disable the read barrier in GetObject() as this is called by GC. |
| 1105 | mirror::Object* obj = m->GetObject<kWithoutReadBarrier>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1106 | // 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] | 1107 | mirror::Object* new_obj = obj != nullptr ? callback(obj, arg) : nullptr; |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1108 | if (new_obj == nullptr) { |
| 1109 | VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame^] | 1110 | << obj; |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1111 | delete m; |
| 1112 | it = list_.erase(it); |
| 1113 | } else { |
Mathieu Chartier | 6aa3df9 | 2013-09-17 15:17:28 -0700 | [diff] [blame] | 1114 | m->SetObject(new_obj); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 1115 | ++it; |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1120 | static mirror::Object* MonitorDeflateCallback(mirror::Object* object, void* arg) |
| 1121 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1122 | if (Monitor::Deflate(reinterpret_cast<Thread*>(arg), object)) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1123 | DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked); |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 1124 | // If we deflated, return nullptr so that the monitor gets removed from the array. |
| 1125 | return nullptr; |
| 1126 | } |
| 1127 | return object; // Monitor was not deflated. |
| 1128 | } |
| 1129 | |
| 1130 | void MonitorList::DeflateMonitors() { |
| 1131 | Thread* self = Thread::Current(); |
| 1132 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 1133 | SweepMonitorList(MonitorDeflateCallback, reinterpret_cast<Thread*>(self)); |
| 1134 | } |
| 1135 | |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1136 | MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(NULL), entry_count_(0) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1137 | DCHECK(obj != nullptr); |
| 1138 | LockWord lock_word = obj->GetLockWord(true); |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1139 | switch (lock_word.GetState()) { |
| 1140 | case LockWord::kUnlocked: |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1141 | // Fall-through. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1142 | case LockWord::kForwardingAddress: |
| 1143 | // Fall-through. |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1144 | case LockWord::kHashCode: |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1145 | break; |
| 1146 | case LockWord::kThinLocked: |
| 1147 | owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner()); |
| 1148 | entry_count_ = 1 + lock_word.ThinLockCount(); |
| 1149 | // Thin locks have no waiters. |
| 1150 | break; |
| 1151 | case LockWord::kFatLocked: { |
| 1152 | Monitor* mon = lock_word.FatLockMonitor(); |
| 1153 | owner_ = mon->owner_; |
| 1154 | entry_count_ = 1 + mon->lock_count_; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1155 | for (Thread* waiter = mon->wait_set_; waiter != NULL; waiter = waiter->GetWaitNext()) { |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 1156 | waiters_.push_back(waiter); |
| 1157 | } |
| 1158 | break; |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1163 | } // namespace art |