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