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