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