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" |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 24 | #include "dex_instruction.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 25 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 26 | #include "object_utils.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 27 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 28 | #include "thread.h" |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 29 | #include "thread_list.h" |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 30 | #include "verifier/method_verifier.h" |
Elliott Hughes | 044288f | 2012-06-25 14:46:39 -0700 | [diff] [blame] | 31 | #include "well_known_classes.h" |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | /* |
| 36 | * Every Object has a monitor associated with it, but not every Object is |
| 37 | * actually locked. Even the ones that are locked do not need a |
| 38 | * full-fledged monitor until a) there is actual contention or b) wait() |
| 39 | * is called on the Object. |
| 40 | * |
| 41 | * For Android, we have implemented a scheme similar to the one described |
| 42 | * in Bacon et al.'s "Thin locks: featherweight synchronization for Java" |
| 43 | * (ACM 1998). Things are even easier for us, though, because we have |
| 44 | * a full 32 bits to work with. |
| 45 | * |
| 46 | * The two states of an Object's lock are referred to as "thin" and |
| 47 | * "fat". A lock may transition from the "thin" state to the "fat" |
| 48 | * state and this transition is referred to as inflation. Once a lock |
| 49 | * has been inflated it remains in the "fat" state indefinitely. |
| 50 | * |
| 51 | * The lock value itself is stored in Object.lock. The LSB of the |
| 52 | * lock encodes its state. When cleared, the lock is in the "thin" |
| 53 | * state and its bits are formatted as follows: |
| 54 | * |
| 55 | * [31 ---- 19] [18 ---- 3] [2 ---- 1] [0] |
| 56 | * lock count thread id hash state 0 |
| 57 | * |
| 58 | * When set, the lock is in the "fat" state and its bits are formatted |
| 59 | * as follows: |
| 60 | * |
| 61 | * [31 ---- 3] [2 ---- 1] [0] |
| 62 | * pointer hash state 1 |
| 63 | * |
| 64 | * For an in-depth description of the mechanics of thin-vs-fat locking, |
| 65 | * read the paper referred to above. |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 66 | * |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 67 | * Monitors provide: |
| 68 | * - mutually exclusive access to resources |
| 69 | * - a way for multiple threads to wait for notification |
| 70 | * |
| 71 | * In effect, they fill the role of both mutexes and condition variables. |
| 72 | * |
| 73 | * Only one thread can own the monitor at any time. There may be several |
| 74 | * threads waiting on it (the wait call unlocks it). One or more waiting |
| 75 | * threads may be getting interrupted or notified at any given time. |
| 76 | * |
| 77 | * TODO: the various members of monitor are not SMP-safe. |
| 78 | */ |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 79 | |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame^] | 80 | // The shape is the bottom bit; either LW_SHAPE_THIN or LW_SHAPE_FAT. |
| 81 | #define LW_SHAPE_MASK 0x1 |
| 82 | #define LW_SHAPE(x) static_cast<int>((x) & LW_SHAPE_MASK) |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Monitor accessor. Extracts a monitor structure pointer from a fat |
| 86 | * lock. Performs no error checking. |
| 87 | */ |
| 88 | #define LW_MONITOR(x) \ |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 89 | (reinterpret_cast<Monitor*>((x) & ~((LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT) | LW_SHAPE_MASK))) |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Lock recursion count field. Contains a count of the number of times |
| 93 | * a lock has been recursively acquired. |
| 94 | */ |
| 95 | #define LW_LOCK_COUNT_MASK 0x1fff |
| 96 | #define LW_LOCK_COUNT_SHIFT 19 |
| 97 | #define LW_LOCK_COUNT(x) (((x) >> LW_LOCK_COUNT_SHIFT) & LW_LOCK_COUNT_MASK) |
| 98 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 99 | bool (*Monitor::is_sensitive_thread_hook_)() = NULL; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 100 | uint32_t Monitor::lock_profiling_threshold_ = 0; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 101 | |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 102 | bool Monitor::IsSensitiveThread() { |
| 103 | if (is_sensitive_thread_hook_ != NULL) { |
| 104 | return (*is_sensitive_thread_hook_)(); |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 109 | 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] | 110 | lock_profiling_threshold_ = lock_profiling_threshold; |
| 111 | is_sensitive_thread_hook_ = is_sensitive_thread_hook; |
Elliott Hughes | 32d6e1e | 2011-10-11 14:47:44 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 114 | Monitor::Monitor(Thread* owner, Object* obj) |
| 115 | : monitor_lock_("a monitor lock", kMonitorLock), |
| 116 | owner_(owner), |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 117 | lock_count_(0), |
| 118 | obj_(obj), |
| 119 | wait_set_(NULL), |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 120 | locking_method_(NULL), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 121 | locking_dex_pc_(0) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 122 | monitor_lock_.Lock(owner); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 123 | // Propagate the lock state. |
| 124 | uint32_t thin = *obj->GetRawLockWordAddress(); |
| 125 | lock_count_ = LW_LOCK_COUNT(thin); |
| 126 | thin &= LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT; |
| 127 | thin |= reinterpret_cast<uint32_t>(this) | LW_SHAPE_FAT; |
| 128 | // Publish the updated lock word. |
| 129 | android_atomic_release_store(thin, obj->GetRawLockWordAddress()); |
| 130 | // Lock profiling. |
| 131 | if (lock_profiling_threshold_ != 0) { |
| 132 | locking_method_ = owner->GetCurrentMethod(&locking_dex_pc_); |
| 133 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | Monitor::~Monitor() { |
| 137 | DCHECK(obj_ != NULL); |
| 138 | DCHECK_EQ(LW_SHAPE(*obj_->GetRawLockWordAddress()), LW_SHAPE_FAT); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Links a thread into a monitor's wait set. The monitor lock must be |
| 143 | * held by the caller of this routine. |
| 144 | */ |
| 145 | void Monitor::AppendToWaitSet(Thread* thread) { |
| 146 | DCHECK(owner_ == Thread::Current()); |
| 147 | DCHECK(thread != NULL); |
Elliott Hughes | dc33ad5 | 2011-09-16 19:46:51 -0700 | [diff] [blame] | 148 | DCHECK(thread->wait_next_ == NULL) << thread->wait_next_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 149 | if (wait_set_ == NULL) { |
| 150 | wait_set_ = thread; |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // push_back. |
| 155 | Thread* t = wait_set_; |
| 156 | while (t->wait_next_ != NULL) { |
| 157 | t = t->wait_next_; |
| 158 | } |
| 159 | t->wait_next_ = thread; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Unlinks a thread from a monitor's wait set. The monitor lock must |
| 164 | * be held by the caller of this routine. |
| 165 | */ |
| 166 | void Monitor::RemoveFromWaitSet(Thread *thread) { |
| 167 | DCHECK(owner_ == Thread::Current()); |
| 168 | DCHECK(thread != NULL); |
| 169 | if (wait_set_ == NULL) { |
| 170 | return; |
| 171 | } |
| 172 | if (wait_set_ == thread) { |
| 173 | wait_set_ = thread->wait_next_; |
| 174 | thread->wait_next_ = NULL; |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | Thread* t = wait_set_; |
| 179 | while (t->wait_next_ != NULL) { |
| 180 | if (t->wait_next_ == thread) { |
| 181 | t->wait_next_ = thread->wait_next_; |
| 182 | thread->wait_next_ = NULL; |
| 183 | return; |
| 184 | } |
| 185 | t = t->wait_next_; |
| 186 | } |
| 187 | } |
| 188 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 189 | Object* Monitor::GetObject() { |
| 190 | return obj_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 193 | void Monitor::Lock(Thread* self) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 194 | if (owner_ == self) { |
| 195 | lock_count_++; |
| 196 | return; |
| 197 | } |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 198 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 199 | if (!monitor_lock_.TryLock(self)) { |
Mathieu Chartier | 2542d66 | 2012-06-21 17:14:11 -0700 | [diff] [blame] | 200 | uint64_t waitStart = 0; |
| 201 | uint64_t waitEnd = 0; |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 202 | uint32_t wait_threshold = lock_profiling_threshold_; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 203 | const AbstractMethod* current_locking_method = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 204 | uint32_t current_locking_dex_pc = 0; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 205 | { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 206 | ScopedThreadStateChange tsc(self, kBlocked); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 207 | if (wait_threshold != 0) { |
| 208 | waitStart = NanoTime() / 1000; |
| 209 | } |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 210 | current_locking_method = locking_method_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 211 | current_locking_dex_pc = locking_dex_pc_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 212 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 213 | monitor_lock_.Lock(self); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 214 | if (wait_threshold != 0) { |
| 215 | waitEnd = NanoTime() / 1000; |
| 216 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 217 | } |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 218 | |
| 219 | if (wait_threshold != 0) { |
| 220 | uint64_t wait_ms = (waitEnd - waitStart) / 1000; |
| 221 | uint32_t sample_percent; |
| 222 | if (wait_ms >= wait_threshold) { |
| 223 | sample_percent = 100; |
| 224 | } else { |
| 225 | sample_percent = 100 * wait_ms / wait_threshold; |
| 226 | } |
| 227 | if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) { |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 228 | const char* current_locking_filename; |
| 229 | uint32_t current_locking_line_number; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 230 | TranslateLocation(current_locking_method, current_locking_dex_pc, |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 231 | current_locking_filename, current_locking_line_number); |
| 232 | LogContentionEvent(self, wait_ms, sample_percent, current_locking_filename, current_locking_line_number); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 235 | } |
| 236 | owner_ = self; |
| 237 | DCHECK_EQ(lock_count_, 0); |
| 238 | |
| 239 | // When debugging, save the current monitor holder for future |
| 240 | // acquisition failures to use in sampled logging. |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 241 | if (lock_profiling_threshold_ != 0) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 242 | locking_method_ = self->GetCurrentMethod(&locking_dex_pc_); |
Elliott Hughes | fc86162 | 2011-10-17 17:57:47 -0700 | [diff] [blame] | 243 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 246 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
| 247 | __attribute__((format(printf, 1, 2))); |
| 248 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 249 | static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 250 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 251 | va_list args; |
| 252 | va_start(args, fmt); |
| 253 | Thread::Current()->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args); |
Brian Carlstrom | 64277f3 | 2012-03-26 23:53:34 -0700 | [diff] [blame] | 254 | if (!Runtime::Current()->IsStarted()) { |
| 255 | std::ostringstream ss; |
| 256 | Thread::Current()->Dump(ss); |
| 257 | std::string str(ss.str()); |
| 258 | LOG(ERROR) << "IllegalMonitorStateException: " << str; |
| 259 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 260 | va_end(args); |
| 261 | } |
| 262 | |
Elliott Hughes | d423741 | 2012-02-21 11:24:45 -0800 | [diff] [blame] | 263 | static std::string ThreadToString(Thread* thread) { |
| 264 | if (thread == NULL) { |
| 265 | return "NULL"; |
| 266 | } |
| 267 | std::ostringstream oss; |
| 268 | // TODO: alternatively, we could just return the thread's name. |
| 269 | oss << *thread; |
| 270 | return oss.str(); |
| 271 | } |
| 272 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 273 | void Monitor::FailedUnlock(Object* o, Thread* expected_owner, Thread* found_owner, |
| 274 | Monitor* monitor) { |
| 275 | Thread* current_owner = NULL; |
| 276 | std::string current_owner_string; |
| 277 | std::string expected_owner_string; |
| 278 | std::string found_owner_string; |
| 279 | { |
| 280 | // TODO: isn't this too late to prevent threads from disappearing? |
| 281 | // Acquire thread list lock so threads won't disappear from under us. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 282 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 283 | // Re-read owner now that we hold lock. |
| 284 | current_owner = (monitor != NULL) ? monitor->owner_ : NULL; |
| 285 | // Get short descriptions of the threads involved. |
| 286 | current_owner_string = ThreadToString(current_owner); |
| 287 | expected_owner_string = ThreadToString(expected_owner); |
| 288 | found_owner_string = ThreadToString(found_owner); |
| 289 | } |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 290 | if (current_owner == NULL) { |
| 291 | if (found_owner == NULL) { |
| 292 | ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'" |
| 293 | " on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 294 | PrettyTypeOf(o).c_str(), |
| 295 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 296 | } else { |
| 297 | // Race: the original read found an owner but now there is none |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 298 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 299 | " (where now the monitor appears unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 300 | found_owner_string.c_str(), |
| 301 | PrettyTypeOf(o).c_str(), |
| 302 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 303 | } |
| 304 | } else { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 305 | if (found_owner == NULL) { |
| 306 | // Race: originally there was no owner, there is now |
| 307 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 308 | " (originally believed to be unowned) on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 309 | current_owner_string.c_str(), |
| 310 | PrettyTypeOf(o).c_str(), |
| 311 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 312 | } else { |
| 313 | if (found_owner != current_owner) { |
| 314 | // Race: originally found and current owner have changed |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 315 | ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now" |
| 316 | " owned by '%s') on object of type '%s' on thread '%s'", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 317 | found_owner_string.c_str(), |
| 318 | current_owner_string.c_str(), |
| 319 | PrettyTypeOf(o).c_str(), |
| 320 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 321 | } else { |
| 322 | ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" |
| 323 | " on thread '%s", |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 324 | current_owner_string.c_str(), |
| 325 | PrettyTypeOf(o).c_str(), |
| 326 | expected_owner_string.c_str()); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 332 | bool Monitor::Unlock(Thread* self, bool for_wait) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 333 | DCHECK(self != NULL); |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 334 | Thread* owner = owner_; |
| 335 | if (owner == self) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 336 | // We own the monitor, so nobody else can be in here. |
| 337 | if (lock_count_ == 0) { |
| 338 | owner_ = NULL; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 339 | locking_method_ = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 340 | locking_dex_pc_ = 0; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 341 | monitor_lock_.Unlock(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 342 | } else { |
| 343 | --lock_count_; |
| 344 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 345 | } else if (for_wait) { |
| 346 | // Wait should have already cleared the fields. |
| 347 | DCHECK_EQ(lock_count_, 0); |
| 348 | DCHECK(owner == NULL); |
| 349 | DCHECK(locking_method_ == NULL); |
| 350 | DCHECK_EQ(locking_dex_pc_, 0u); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 351 | monitor_lock_.Unlock(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 352 | } else { |
| 353 | // We don't own this, so we're not allowed to unlock it. |
| 354 | // The JNI spec says that we should throw IllegalMonitorStateException |
| 355 | // in this case. |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 356 | FailedUnlock(obj_, self, owner, this); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | return true; |
| 360 | } |
| 361 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 362 | /* |
| 363 | * Wait on a monitor until timeout, interrupt, or notification. Used for |
| 364 | * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join(). |
| 365 | * |
| 366 | * If another thread calls Thread.interrupt(), we throw InterruptedException |
| 367 | * and return immediately if one of the following are true: |
| 368 | * - blocked in wait(), wait(long), or wait(long, int) methods of Object |
| 369 | * - blocked in join(), join(long), or join(long, int) methods of Thread |
| 370 | * - blocked in sleep(long), or sleep(long, int) methods of Thread |
| 371 | * Otherwise, we set the "interrupted" flag. |
| 372 | * |
| 373 | * Checks to make sure that "ns" is in the range 0-999999 |
| 374 | * (i.e. fractions of a millisecond) and throws the appropriate |
| 375 | * exception if it isn't. |
| 376 | * |
| 377 | * The spec allows "spurious wakeups", and recommends that all code using |
| 378 | * Object.wait() do so in a loop. This appears to derive from concerns |
| 379 | * about pthread_cond_wait() on multiprocessor systems. Some commentary |
| 380 | * on the web casts doubt on whether these can/should occur. |
| 381 | * |
| 382 | * Since we're allowed to wake up "early", we clamp extremely long durations |
| 383 | * to return at the end of the 32-bit time epoch. |
| 384 | */ |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 385 | void Monitor::Wait(Thread* self, int64_t ms, int32_t ns, |
| 386 | bool interruptShouldThrow, ThreadState why) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 387 | DCHECK(self != NULL); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 388 | DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 389 | |
| 390 | // Make sure that we hold the lock. |
| 391 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 392 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 393 | return; |
| 394 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 395 | monitor_lock_.AssertHeld(self); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 396 | |
Elliott Hughes | df42c48 | 2013-01-09 12:49:02 -0800 | [diff] [blame] | 397 | // We need to turn a zero-length timed wait into a regular wait because |
| 398 | // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait(). |
| 399 | if (why == kTimedWaiting && (ms == 0 && ns == 0)) { |
| 400 | why = kWaiting; |
| 401 | } |
| 402 | |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 403 | WaitWithLock(self, ms, ns, interruptShouldThrow, why); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 404 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 405 | |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 406 | void Monitor::WaitWithLock(Thread* self, int64_t ms, int32_t ns, |
| 407 | bool interruptShouldThrow, ThreadState why) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 408 | // Enforce the timeout range. |
| 409 | if (ms < 0 || ns < 0 || ns > 999999) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 410 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 411 | "timeout arguments out of range: ms=%lld ns=%d", ms, ns); |
| 412 | return; |
| 413 | } |
| 414 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 415 | /* |
| 416 | * Add ourselves to the set of threads waiting on this monitor, and |
| 417 | * release our hold. We need to let it go even if we're a few levels |
| 418 | * deep in a recursive lock, and we need to restore that later. |
| 419 | * |
| 420 | * We append to the wait set ahead of clearing the count and owner |
| 421 | * fields so the subroutine can check that the calling thread owns |
| 422 | * the monitor. Aside from that, the order of member updates is |
| 423 | * not order sensitive as we hold the pthread mutex. |
| 424 | */ |
| 425 | AppendToWaitSet(self); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 426 | int prev_lock_count = lock_count_; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 427 | lock_count_ = 0; |
| 428 | owner_ = NULL; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 429 | const AbstractMethod* saved_method = locking_method_; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 430 | locking_method_ = NULL; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 431 | uintptr_t saved_dex_pc = locking_dex_pc_; |
| 432 | locking_dex_pc_ = 0; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 433 | |
| 434 | /* |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 435 | * 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] | 436 | * that we won't touch any references in this state, and we'll check |
| 437 | * our suspend mode before we transition out. |
| 438 | */ |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 439 | self->TransitionFromRunnableToSuspended(why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 440 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 441 | bool was_interrupted = false; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 442 | { |
| 443 | // 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] | 444 | MutexLock mu(self, *self->wait_mutex_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 445 | |
| 446 | // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is |
| 447 | // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it |
| 448 | // up. |
| 449 | DCHECK(self->wait_monitor_ == NULL); |
| 450 | self->wait_monitor_ = this; |
| 451 | |
| 452 | // Release the monitor lock. |
| 453 | Unlock(self, true); |
| 454 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 455 | // Handle the case where the thread was interrupted before we called wait(). |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 456 | if (self->interrupted_) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 457 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 458 | } else { |
| 459 | // Wait for a notification or a timeout to occur. |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 460 | if (why == kWaiting) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 461 | self->wait_cond_->Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 462 | } else { |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 463 | DCHECK(why == kTimedWaiting || why == kSleeping) << why; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 464 | self->wait_cond_->TimedWait(self, ms, ns); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 465 | } |
| 466 | if (self->interrupted_) { |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 467 | was_interrupted = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 468 | } |
| 469 | self->interrupted_ = false; |
| 470 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 473 | // Set self->status back to kRunnable, and self-suspend if needed. |
| 474 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 475 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 476 | { |
| 477 | // We reset the thread's wait_monitor_ field after transitioning back to runnable so |
| 478 | // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging |
| 479 | // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads |
| 480 | // are waiting on "null".) |
| 481 | MutexLock mu(self, *self->wait_mutex_); |
| 482 | DCHECK(self->wait_monitor_ != NULL); |
| 483 | self->wait_monitor_ = NULL; |
| 484 | } |
| 485 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 486 | // Re-acquire the monitor lock. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 487 | Lock(self); |
| 488 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 489 | self->wait_mutex_->AssertNotHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 490 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 491 | /* |
| 492 | * We remove our thread from wait set after restoring the count |
| 493 | * and owner fields so the subroutine can check that the calling |
| 494 | * thread owns the monitor. Aside from that, the order of member |
| 495 | * updates is not order sensitive as we hold the pthread mutex. |
| 496 | */ |
| 497 | owner_ = self; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 498 | lock_count_ = prev_lock_count; |
| 499 | locking_method_ = saved_method; |
| 500 | locking_dex_pc_ = saved_dex_pc; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 501 | RemoveFromWaitSet(self); |
| 502 | |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 503 | if (was_interrupted) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 504 | /* |
| 505 | * We were interrupted while waiting, or somebody interrupted an |
| 506 | * un-interruptible thread earlier and we're bailing out immediately. |
| 507 | * |
| 508 | * The doc sayeth: "The interrupted status of the current thread is |
| 509 | * cleared when this exception is thrown." |
| 510 | */ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 511 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 512 | MutexLock mu(self, *self->wait_mutex_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 513 | self->interrupted_ = false; |
| 514 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 515 | if (interruptShouldThrow) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 516 | Thread::Current()->ThrowNewException("Ljava/lang/InterruptedException;", NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | void Monitor::Notify(Thread* self) { |
| 522 | DCHECK(self != NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 523 | // Make sure that we hold the lock. |
| 524 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 525 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 526 | return; |
| 527 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 528 | monitor_lock_.AssertHeld(self); |
| 529 | NotifyWithLock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 532 | void Monitor::NotifyWithLock(Thread* self) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 533 | // Signal the first waiting thread in the wait set. |
| 534 | while (wait_set_ != NULL) { |
| 535 | Thread* thread = wait_set_; |
| 536 | wait_set_ = thread->wait_next_; |
| 537 | thread->wait_next_ = NULL; |
| 538 | |
| 539 | // Check to see if the thread is still waiting. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 540 | MutexLock mu(self, *thread->wait_mutex_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 541 | if (thread->wait_monitor_ != NULL) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 542 | thread->wait_cond_->Signal(self); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | void Monitor::NotifyAll(Thread* self) { |
| 549 | DCHECK(self != NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 550 | // Make sure that we hold the lock. |
| 551 | if (owner_ != self) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 552 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 553 | return; |
| 554 | } |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 555 | monitor_lock_.AssertHeld(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 556 | NotifyAllWithLock(); |
| 557 | } |
| 558 | |
| 559 | void Monitor::NotifyAllWithLock() { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 560 | // Signal all threads in the wait set. |
| 561 | while (wait_set_ != NULL) { |
| 562 | Thread* thread = wait_set_; |
| 563 | wait_set_ = thread->wait_next_; |
| 564 | thread->wait_next_ = NULL; |
| 565 | thread->Notify(); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Changes the shape of a monitor from thin to fat, preserving the |
| 571 | * internal lock state. The calling thread must own the lock. |
| 572 | */ |
| 573 | void Monitor::Inflate(Thread* self, Object* obj) { |
| 574 | DCHECK(self != NULL); |
| 575 | DCHECK(obj != NULL); |
| 576 | DCHECK_EQ(LW_SHAPE(*obj->GetRawLockWordAddress()), LW_SHAPE_THIN); |
Elliott Hughes | f8e0127 | 2011-10-17 11:29:05 -0700 | [diff] [blame] | 577 | DCHECK_EQ(LW_LOCK_OWNER(*obj->GetRawLockWordAddress()), static_cast<int32_t>(self->GetThinLockId())); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 578 | |
| 579 | // Allocate and acquire a new monitor. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 580 | Monitor* m = new Monitor(self, obj); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 581 | VLOG(monitor) << "monitor: thread " << self->GetThinLockId() |
| 582 | << " created monitor " << m << " for object " << obj; |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 583 | Runtime::Current()->GetMonitorList()->Add(m); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void Monitor::MonitorEnter(Thread* self, Object* obj) { |
| 587 | volatile int32_t* thinp = obj->GetRawLockWordAddress(); |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 588 | timespec tm; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 589 | uint32_t sleepDelayNs; |
| 590 | uint32_t minSleepDelayNs = 1000000; /* 1 millisecond */ |
| 591 | uint32_t maxSleepDelayNs = 1000000000; /* 1 second */ |
Elliott Hughes | f8e0127 | 2011-10-17 11:29:05 -0700 | [diff] [blame] | 592 | uint32_t thin, newThin; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 593 | |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 594 | DCHECK(self != NULL); |
| 595 | DCHECK(obj != NULL); |
Elliott Hughes | f8e0127 | 2011-10-17 11:29:05 -0700 | [diff] [blame] | 596 | uint32_t threadId = self->GetThinLockId(); |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 597 | retry: |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 598 | thin = *thinp; |
| 599 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
| 600 | /* |
| 601 | * The lock is a thin lock. The owner field is used to |
| 602 | * determine the acquire method, ordered by cost. |
| 603 | */ |
| 604 | if (LW_LOCK_OWNER(thin) == threadId) { |
| 605 | /* |
| 606 | * The calling thread owns the lock. Increment the |
| 607 | * value of the recursion count field. |
| 608 | */ |
| 609 | *thinp += 1 << LW_LOCK_COUNT_SHIFT; |
| 610 | if (LW_LOCK_COUNT(*thinp) == LW_LOCK_COUNT_MASK) { |
| 611 | /* |
| 612 | * The reacquisition limit has been reached. Inflate |
| 613 | * the lock so the next acquire will not overflow the |
| 614 | * recursion count field. |
| 615 | */ |
| 616 | Inflate(self, obj); |
| 617 | } |
| 618 | } else if (LW_LOCK_OWNER(thin) == 0) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 619 | // The lock is unowned. Install the thread id of the calling thread into the owner field. |
| 620 | // This is the common case: compiled code will have tried this before calling back into |
| 621 | // the runtime. |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 622 | newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); |
| 623 | if (android_atomic_acquire_cas(thin, newThin, thinp) != 0) { |
| 624 | // The acquire failed. Try again. |
| 625 | goto retry; |
| 626 | } |
| 627 | } else { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 628 | VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p (a %s) owned by %d", |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 629 | threadId, thinp, PrettyTypeOf(obj).c_str(), LW_LOCK_OWNER(thin)); |
| 630 | // The lock is owned by another thread. Notify the runtime that we are about to wait. |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 631 | self->monitor_enter_object_ = obj; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 632 | self->TransitionFromRunnableToSuspended(kBlocked); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 633 | // Spin until the thin lock is released or inflated. |
| 634 | sleepDelayNs = 0; |
| 635 | for (;;) { |
| 636 | thin = *thinp; |
| 637 | // Check the shape of the lock word. Another thread |
| 638 | // may have inflated the lock while we were waiting. |
| 639 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
| 640 | if (LW_LOCK_OWNER(thin) == 0) { |
| 641 | // The lock has been released. Install the thread id of the |
| 642 | // calling thread into the owner field. |
| 643 | newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); |
| 644 | if (android_atomic_acquire_cas(thin, newThin, thinp) == 0) { |
| 645 | // The acquire succeed. Break out of the loop and proceed to inflate the lock. |
| 646 | break; |
| 647 | } |
| 648 | } else { |
| 649 | // The lock has not been released. Yield so the owning thread can run. |
| 650 | if (sleepDelayNs == 0) { |
| 651 | sched_yield(); |
| 652 | sleepDelayNs = minSleepDelayNs; |
| 653 | } else { |
| 654 | tm.tv_sec = 0; |
| 655 | tm.tv_nsec = sleepDelayNs; |
| 656 | nanosleep(&tm, NULL); |
| 657 | // Prepare the next delay value. Wrap to avoid once a second polls for eternity. |
| 658 | if (sleepDelayNs < maxSleepDelayNs / 2) { |
| 659 | sleepDelayNs *= 2; |
| 660 | } else { |
| 661 | sleepDelayNs = minSleepDelayNs; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | } else { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 666 | // The thin lock was inflated by another thread. Let the runtime know we are no longer |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 667 | // waiting and try again. |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 668 | VLOG(monitor) << StringPrintf("monitor: thread %d found lock %p surprise-fattened by another thread", threadId, thinp); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 669 | self->monitor_enter_object_ = NULL; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 670 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 671 | goto retry; |
| 672 | } |
| 673 | } |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 674 | VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p done", threadId, thinp); |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 675 | // We have acquired the thin lock. Let the runtime know that we are no longer waiting. |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 676 | self->monitor_enter_object_ = NULL; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 677 | self->TransitionFromSuspendedToRunnable(); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 678 | // Fatten the lock. |
| 679 | Inflate(self, obj); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 680 | VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p", threadId, thinp); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 681 | } |
| 682 | } else { |
| 683 | // The lock is a fat lock. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 684 | VLOG(monitor) << StringPrintf("monitor: thread %d locking fat lock %p (%p) %p on a %s", |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 685 | threadId, thinp, LW_MONITOR(*thinp), |
| 686 | reinterpret_cast<void*>(*thinp), PrettyTypeOf(obj).c_str()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 687 | DCHECK(LW_MONITOR(*thinp) != NULL); |
| 688 | LW_MONITOR(*thinp)->Lock(self); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | bool Monitor::MonitorExit(Thread* self, Object* obj) { |
| 693 | volatile int32_t* thinp = obj->GetRawLockWordAddress(); |
| 694 | |
| 695 | DCHECK(self != NULL); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 696 | //DCHECK_EQ(self->GetState(), kRunnable); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 697 | DCHECK(obj != NULL); |
| 698 | |
| 699 | /* |
| 700 | * Cache the lock word as its value can change while we are |
| 701 | * examining its state. |
| 702 | */ |
| 703 | uint32_t thin = *thinp; |
| 704 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
| 705 | /* |
| 706 | * The lock is thin. We must ensure that the lock is owned |
| 707 | * by the given thread before unlocking it. |
| 708 | */ |
Elliott Hughes | f8e0127 | 2011-10-17 11:29:05 -0700 | [diff] [blame] | 709 | if (LW_LOCK_OWNER(thin) == self->GetThinLockId()) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 710 | /* |
| 711 | * We are the lock owner. It is safe to update the lock |
| 712 | * without CAS as lock ownership guards the lock itself. |
| 713 | */ |
| 714 | if (LW_LOCK_COUNT(thin) == 0) { |
| 715 | /* |
| 716 | * The lock was not recursively acquired, the common |
| 717 | * case. Unlock by clearing all bits except for the |
| 718 | * hash state. |
| 719 | */ |
| 720 | thin &= (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT); |
| 721 | android_atomic_release_store(thin, thinp); |
| 722 | } else { |
| 723 | /* |
| 724 | * The object was recursively acquired. Decrement the |
| 725 | * lock recursion count field. |
| 726 | */ |
| 727 | *thinp -= 1 << LW_LOCK_COUNT_SHIFT; |
| 728 | } |
| 729 | } else { |
| 730 | /* |
| 731 | * We do not own the lock. The JVM spec requires that we |
| 732 | * throw an exception in this case. |
| 733 | */ |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 734 | FailedUnlock(obj, self, NULL, NULL); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 735 | return false; |
| 736 | } |
| 737 | } else { |
| 738 | /* |
| 739 | * The lock is fat. We must check to see if Unlock has |
| 740 | * raised any exceptions before continuing. |
| 741 | */ |
| 742 | DCHECK(LW_MONITOR(*thinp) != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 743 | if (!LW_MONITOR(*thinp)->Unlock(self, false)) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 744 | // An exception has been raised. Do not fall through. |
| 745 | return false; |
| 746 | } |
| 747 | } |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * Object.wait(). Also called for class init. |
| 753 | */ |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 754 | void Monitor::Wait(Thread* self, Object *obj, int64_t ms, int32_t ns, |
| 755 | bool interruptShouldThrow, ThreadState why) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 756 | volatile int32_t* thinp = obj->GetRawLockWordAddress(); |
| 757 | |
| 758 | // If the lock is still thin, we need to fatten it. |
| 759 | uint32_t thin = *thinp; |
| 760 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
| 761 | // Make sure that 'self' holds the lock. |
Elliott Hughes | f8e0127 | 2011-10-17 11:29:05 -0700 | [diff] [blame] | 762 | if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 763 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 764 | return; |
| 765 | } |
| 766 | |
| 767 | /* This thread holds the lock. We need to fatten the lock |
| 768 | * so 'self' can block on it. Don't update the object lock |
| 769 | * field yet, because 'self' needs to acquire the lock before |
| 770 | * any other thread gets a chance. |
| 771 | */ |
| 772 | Inflate(self, obj); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 773 | VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p by wait()", self->GetThinLockId(), thinp); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 774 | } |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 775 | LW_MONITOR(*thinp)->Wait(self, ms, ns, interruptShouldThrow, why); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | void Monitor::Notify(Thread* self, Object *obj) { |
| 779 | uint32_t thin = *obj->GetRawLockWordAddress(); |
| 780 | |
| 781 | // If the lock is still thin, there aren't any waiters; |
| 782 | // waiting on an object forces lock fattening. |
| 783 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
| 784 | // Make sure that 'self' holds the lock. |
Elliott Hughes | f8e0127 | 2011-10-17 11:29:05 -0700 | [diff] [blame] | 785 | if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 786 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 787 | return; |
| 788 | } |
| 789 | // no-op; there are no waiters to notify. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 790 | Inflate(self, obj); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 791 | } else { |
| 792 | // It's a fat lock. |
| 793 | LW_MONITOR(thin)->Notify(self); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | void Monitor::NotifyAll(Thread* self, Object *obj) { |
| 798 | uint32_t thin = *obj->GetRawLockWordAddress(); |
| 799 | |
| 800 | // If the lock is still thin, there aren't any waiters; |
| 801 | // waiting on an object forces lock fattening. |
| 802 | if (LW_SHAPE(thin) == LW_SHAPE_THIN) { |
| 803 | // Make sure that 'self' holds the lock. |
Elliott Hughes | f8e0127 | 2011-10-17 11:29:05 -0700 | [diff] [blame] | 804 | if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { |
Ian Rogers | 6d0b13e | 2012-02-07 09:25:29 -0800 | [diff] [blame] | 805 | ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 806 | return; |
| 807 | } |
| 808 | // no-op; there are no waiters to notify. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 809 | Inflate(self, obj); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 810 | } else { |
| 811 | // It's a fat lock. |
| 812 | LW_MONITOR(thin)->NotifyAll(self); |
| 813 | } |
| 814 | } |
| 815 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 816 | uint32_t Monitor::GetThinLockId(uint32_t raw_lock_word) { |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 817 | if (LW_SHAPE(raw_lock_word) == LW_SHAPE_THIN) { |
| 818 | return LW_LOCK_OWNER(raw_lock_word); |
| 819 | } else { |
| 820 | Thread* owner = LW_MONITOR(raw_lock_word)->owner_; |
| 821 | return owner ? owner->GetThinLockId() : 0; |
| 822 | } |
| 823 | } |
| 824 | |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 825 | void Monitor::DescribeWait(std::ostream& os, const Thread* thread) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 826 | ThreadState state; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 827 | state = thread->GetState(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 828 | |
| 829 | Object* object = NULL; |
| 830 | uint32_t lock_owner = ThreadList::kInvalidId; |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 831 | if (state == kWaiting || state == kTimedWaiting || state == kSleeping) { |
| 832 | if (state == kSleeping) { |
| 833 | os << " - sleeping on "; |
| 834 | } else { |
| 835 | os << " - waiting on "; |
| 836 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 837 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 838 | MutexLock mu(Thread::Current(), *thread->wait_mutex_); |
Elliott Hughes | b4e94fd | 2013-01-08 14:41:26 -0800 | [diff] [blame] | 839 | Monitor* monitor = thread->wait_monitor_; |
| 840 | if (monitor != NULL) { |
| 841 | object = monitor->obj_; |
| 842 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 843 | } |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 844 | } else if (state == kBlocked) { |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 845 | os << " - waiting to lock "; |
| 846 | object = thread->monitor_enter_object_; |
| 847 | if (object != NULL) { |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 848 | lock_owner = object->GetThinLockId(); |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 849 | } |
| 850 | } else { |
| 851 | // We're not waiting on anything. |
| 852 | return; |
| 853 | } |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 854 | |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 855 | // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>) |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 856 | os << "<" << object << "> (a " << PrettyTypeOf(object) << ")"; |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 857 | |
Elliott Hughes | c5dc2ff | 2013-01-09 13:44:30 -0800 | [diff] [blame] | 858 | // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5 |
Elliott Hughes | 8e4aac5 | 2011-09-26 17:03:36 -0700 | [diff] [blame] | 859 | if (lock_owner != ThreadList::kInvalidId) { |
| 860 | os << " held by thread " << lock_owner; |
| 861 | } |
| 862 | |
| 863 | os << "\n"; |
| 864 | } |
| 865 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 866 | static void DumpLockedObject(std::ostream& os, Object* o) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 867 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 868 | os << " - locked <" << o << "> (a " << PrettyTypeOf(o) << ")\n"; |
| 869 | } |
| 870 | |
| 871 | void Monitor::DescribeLocks(std::ostream& os, StackVisitor* stack_visitor) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 872 | AbstractMethod* m = stack_visitor->GetMethod(); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 873 | CHECK(m != NULL); |
| 874 | |
| 875 | // Native methods are an easy special case. |
| 876 | // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. |
| 877 | if (m->IsNative()) { |
| 878 | if (m->IsSynchronized()) { |
| 879 | Object* jni_this = stack_visitor->GetCurrentSirt()->GetReference(0); |
| 880 | DumpLockedObject(os, jni_this); |
| 881 | } |
| 882 | return; |
| 883 | } |
| 884 | |
jeffhao | 61f916c | 2012-10-25 17:48:51 -0700 | [diff] [blame] | 885 | // Proxy methods should not be synchronized. |
| 886 | if (m->IsProxyMethod()) { |
| 887 | CHECK(!m->IsSynchronized()); |
| 888 | return; |
| 889 | } |
| 890 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 891 | // <clinit> is another special case. The runtime holds the class lock while calling <clinit>. |
| 892 | MethodHelper mh(m); |
| 893 | if (mh.IsClassInitializer()) { |
| 894 | DumpLockedObject(os, m->GetDeclaringClass()); |
| 895 | // Fall through because there might be synchronization in the user code too. |
| 896 | } |
| 897 | |
| 898 | // Is there any reason to believe there's any synchronization in this method? |
| 899 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Elliott Hughes | caf7654 | 2012-06-28 16:08:22 -0700 | [diff] [blame] | 900 | CHECK(code_item != NULL) << PrettyMethod(m); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 901 | if (code_item->tries_size_ == 0) { |
| 902 | return; // No "tries" implies no synchronization, so no held locks to report. |
| 903 | } |
| 904 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 905 | // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to |
| 906 | // the locks held in this stack frame. |
| 907 | std::vector<uint32_t> monitor_enter_dex_pcs; |
| 908 | verifier::MethodVerifier::FindLocksAtDexPc(m, stack_visitor->GetDexPc(), monitor_enter_dex_pcs); |
| 909 | if (monitor_enter_dex_pcs.empty()) { |
| 910 | return; |
| 911 | } |
| 912 | |
| 913 | // Verification is an iterative process, so it can visit the same monitor-enter instruction |
| 914 | // repeatedly with increasingly accurate type information. We don't want duplicates. |
| 915 | // TODO: is this fixed if we share the other std::vector-returning verifier code? |
| 916 | STLSortAndRemoveDuplicates(&monitor_enter_dex_pcs); |
| 917 | |
| 918 | for (size_t i = 0; i < monitor_enter_dex_pcs.size(); ++i) { |
| 919 | // The verifier works in terms of the dex pcs of the monitor-enter instructions. |
| 920 | // We want the registers used by those instructions (so we can read the values out of them). |
| 921 | uint32_t dex_pc = monitor_enter_dex_pcs[i]; |
| 922 | uint16_t monitor_enter_instruction = code_item->insns_[dex_pc]; |
| 923 | |
| 924 | // Quick sanity check. |
| 925 | if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) { |
| 926 | LOG(FATAL) << "expected monitor-enter @" << dex_pc << "; was " |
| 927 | << reinterpret_cast<void*>(monitor_enter_instruction); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Elliott Hughes | 80537bb | 2013-01-04 16:37:26 -0800 | [diff] [blame] | 930 | uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff); |
| 931 | Object* o = reinterpret_cast<Object*>(stack_visitor->GetVReg(m, monitor_register, |
| 932 | kReferenceVReg)); |
| 933 | DumpLockedObject(os, o); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 937 | void Monitor::TranslateLocation(const AbstractMethod* method, uint32_t dex_pc, |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 938 | const char*& source_file, uint32_t& line_number) const { |
| 939 | // If method is null, location is unknown |
| 940 | if (method == NULL) { |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 941 | source_file = ""; |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 942 | line_number = 0; |
| 943 | return; |
| 944 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 945 | MethodHelper mh(method); |
| 946 | source_file = mh.GetDeclaringClassSourceFile(); |
Elliott Hughes | 12c51e3 | 2012-01-17 20:25:05 -0800 | [diff] [blame] | 947 | if (source_file == NULL) { |
| 948 | source_file = ""; |
| 949 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 950 | line_number = mh.GetLineNumFromDexPC(dex_pc); |
jeffhao | 33dc771 | 2011-11-09 17:54:24 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 953 | MonitorList::MonitorList() : monitor_list_lock_("MonitorList lock") { |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | MonitorList::~MonitorList() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 957 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 958 | STLDeleteElements(&list_); |
| 959 | } |
| 960 | |
| 961 | void MonitorList::Add(Monitor* m) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 962 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 963 | list_.push_front(m); |
| 964 | } |
| 965 | |
| 966 | void MonitorList::SweepMonitorList(Heap::IsMarkedTester is_marked, void* arg) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 967 | MutexLock mu(Thread::Current(), monitor_list_lock_); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 968 | typedef std::list<Monitor*>::iterator It; // TODO: C++0x auto |
| 969 | It it = list_.begin(); |
| 970 | while (it != list_.end()) { |
| 971 | Monitor* m = *it; |
| 972 | if (!is_marked(m->GetObject(), arg)) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 973 | VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " << m->GetObject(); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 974 | delete m; |
| 975 | it = list_.erase(it); |
| 976 | } else { |
| 977 | ++it; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
Elliott Hughes | f327e07 | 2013-01-09 16:01:26 -0800 | [diff] [blame^] | 982 | MonitorInfo::MonitorInfo(Object* o) : owner(NULL), entry_count(0) { |
| 983 | uint32_t lock_word = *o->GetRawLockWordAddress(); |
| 984 | if (LW_SHAPE(lock_word) == LW_SHAPE_THIN) { |
| 985 | uint32_t owner_thin_lock_id = LW_LOCK_OWNER(lock_word); |
| 986 | if (owner_thin_lock_id != 0) { |
| 987 | owner = Runtime::Current()->GetThreadList()->FindThreadByThinLockId(owner_thin_lock_id); |
| 988 | entry_count = 1 + LW_LOCK_COUNT(lock_word); |
| 989 | } |
| 990 | // Thin locks have no waiters. |
| 991 | } else { |
| 992 | CHECK_EQ(LW_SHAPE(lock_word), LW_SHAPE_FAT); |
| 993 | Monitor* monitor = LW_MONITOR(lock_word); |
| 994 | owner = monitor->owner_; |
| 995 | entry_count = 1 + monitor->lock_count_; |
| 996 | for (Thread* waiter = monitor->wait_set_; waiter != NULL; waiter = waiter->wait_next_) { |
| 997 | waiters.push_back(waiter); |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 1002 | } // namespace art |