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