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