2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "base/mutex.h"
22 #include "base/stl_util.h"
23 #include "class_linker.h"
24 #include "dex_file-inl.h"
25 #include "dex_instruction.h"
26 #include "lock_word-inl.h"
27 #include "mirror/art_method-inl.h"
28 #include "mirror/class-inl.h"
29 #include "mirror/object-inl.h"
30 #include "mirror/object_array-inl.h"
31 #include "object_utils.h"
32 #include "scoped_thread_state_change.h"
34 #include "thread_list.h"
35 #include "verifier/method_verifier.h"
36 #include "well_known_classes.h"
41 * Every Object has a monitor associated with it, but not every Object is actually locked. Even
42 * the ones that are locked do not need a full-fledged monitor until a) there is actual contention
43 * or b) wait() is called on the Object.
45 * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s
46 * "Thin locks: featherweight synchronization for Java" (ACM 1998). Things are even easier for us,
47 * though, because we have a full 32 bits to work with.
49 * The two states of an Object's lock are referred to as "thin" and "fat". A lock may transition
50 * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once
51 * a lock has been inflated it remains in the "fat" state indefinitely.
53 * The lock value itself is stored in mirror::Object::monitor_ and the representation is described
54 * in the LockWord value type.
57 * - mutually exclusive access to resources
58 * - a way for multiple threads to wait for notification
60 * In effect, they fill the role of both mutexes and condition variables.
62 * Only one thread can own the monitor at any time. There may be several threads waiting on it
63 * (the wait call unlocks it). One or more waiting threads may be getting interrupted or notified
67 bool (*Monitor::is_sensitive_thread_hook_)() = NULL;
68 uint32_t Monitor::lock_profiling_threshold_ = 0;
70 bool Monitor::IsSensitiveThread() {
71 if (is_sensitive_thread_hook_ != NULL) {
72 return (*is_sensitive_thread_hook_)();
77 void Monitor::Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()) {
78 lock_profiling_threshold_ = lock_profiling_threshold;
79 is_sensitive_thread_hook_ = is_sensitive_thread_hook;
82 Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
83 : monitor_lock_("a monitor lock", kMonitorLock),
84 monitor_contenders_("monitor contenders", monitor_lock_),
90 hash_code_(hash_code),
91 locking_method_(NULL),
93 monitor_id_(MonitorPool::CreateMonitorId(self, this)) {
94 // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
95 // with the owner unlocking the thin-lock.
96 CHECK(owner == nullptr || owner == self || owner->IsSuspended());
97 // The identity hash code is set for the life time of the monitor.
100 int32_t Monitor::GetHashCode() {
101 while (!HasHashCode()) {
102 if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) {
106 DCHECK(HasHashCode());
107 return hash_code_.LoadRelaxed();
110 bool Monitor::Install(Thread* self) {
111 MutexLock mu(self, monitor_lock_); // Uncontended mutex acquisition as monitor isn't yet public.
112 CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended());
113 // Propagate the lock state.
114 LockWord lw(GetObject()->GetLockWord(false));
115 switch (lw.GetState()) {
116 case LockWord::kThinLocked: {
117 CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner());
118 lock_count_ = lw.ThinLockCount();
121 case LockWord::kHashCode: {
122 CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode()));
125 case LockWord::kFatLocked: {
126 // The owner_ is suspended but another thread beat us to install a monitor.
129 case LockWord::kUnlocked: {
130 LOG(FATAL) << "Inflating unlocked lock word";
134 LOG(FATAL) << "Invalid monitor state " << lw.GetState();
139 // Publish the updated lock word, which may race with other threads.
140 bool success = GetObject()->CasLockWord(lw, fat);
142 if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) {
143 locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_);
148 Monitor::~Monitor() {
149 MonitorPool::ReleaseMonitorId(monitor_id_);
150 // Deflated monitors have a null object.
154 * Links a thread into a monitor's wait set. The monitor lock must be
155 * held by the caller of this routine.
157 void Monitor::AppendToWaitSet(Thread* thread) {
158 DCHECK(owner_ == Thread::Current());
159 DCHECK(thread != NULL);
160 DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext();
161 if (wait_set_ == NULL) {
167 Thread* t = wait_set_;
168 while (t->GetWaitNext() != nullptr) {
169 t = t->GetWaitNext();
171 t->SetWaitNext(thread);
175 * Unlinks a thread from a monitor's wait set. The monitor lock must
176 * be held by the caller of this routine.
178 void Monitor::RemoveFromWaitSet(Thread *thread) {
179 DCHECK(owner_ == Thread::Current());
180 DCHECK(thread != NULL);
181 if (wait_set_ == NULL) {
184 if (wait_set_ == thread) {
185 wait_set_ = thread->GetWaitNext();
186 thread->SetWaitNext(nullptr);
190 Thread* t = wait_set_;
191 while (t->GetWaitNext() != NULL) {
192 if (t->GetWaitNext() == thread) {
193 t->SetWaitNext(thread->GetWaitNext());
194 thread->SetWaitNext(nullptr);
197 t = t->GetWaitNext();
201 void Monitor::SetObject(mirror::Object* object) {
205 void Monitor::Lock(Thread* self) {
206 MutexLock mu(self, monitor_lock_);
208 if (owner_ == nullptr) { // Unowned.
210 CHECK_EQ(lock_count_, 0);
211 // When debugging, save the current monitor holder for future
212 // acquisition failures to use in sampled logging.
213 if (lock_profiling_threshold_ != 0) {
214 locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
217 } else if (owner_ == self) { // Recursive.
222 const bool log_contention = (lock_profiling_threshold_ != 0);
223 uint64_t wait_start_ms = log_contention ? 0 : MilliTime();
224 mirror::ArtMethod* owners_method = locking_method_;
225 uint32_t owners_dex_pc = locking_dex_pc_;
226 // Do this before releasing the lock so that we don't get deflated.
228 monitor_lock_.Unlock(self); // Let go of locks in order.
229 self->SetMonitorEnterObject(GetObject());
231 ScopedThreadStateChange tsc(self, kBlocked); // Change to blocked and give up mutator_lock_.
232 MutexLock mu2(self, monitor_lock_); // Reacquire monitor_lock_ without mutator_lock_ for Wait.
233 if (owner_ != NULL) { // Did the owner_ give the lock up?
234 monitor_contenders_.Wait(self); // Still contended so wait.
235 // Woken from contention.
236 if (log_contention) {
237 uint64_t wait_ms = MilliTime() - wait_start_ms;
238 uint32_t sample_percent;
239 if (wait_ms >= lock_profiling_threshold_) {
240 sample_percent = 100;
242 sample_percent = 100 * wait_ms / lock_profiling_threshold_;
244 if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
245 const char* owners_filename;
246 uint32_t owners_line_number;
247 TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
248 LogContentionEvent(self, wait_ms, sample_percent, owners_filename, owners_line_number);
253 self->SetMonitorEnterObject(nullptr);
254 monitor_lock_.Lock(self); // Reacquire locks in order.
259 static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
260 __attribute__((format(printf, 1, 2)));
262 static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
266 Thread* self = Thread::Current();
267 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
268 self->ThrowNewExceptionV(throw_location, "Ljava/lang/IllegalMonitorStateException;", fmt, args);
269 if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
270 std::ostringstream ss;
272 LOG(Runtime::Current()->IsStarted() ? INFO : ERROR)
273 << self->GetException(NULL)->Dump() << "\n" << ss.str();
278 static std::string ThreadToString(Thread* thread) {
279 if (thread == NULL) {
282 std::ostringstream oss;
283 // TODO: alternatively, we could just return the thread's name.
288 void Monitor::FailedUnlock(mirror::Object* o, Thread* expected_owner, Thread* found_owner,
290 Thread* current_owner = NULL;
291 std::string current_owner_string;
292 std::string expected_owner_string;
293 std::string found_owner_string;
295 // TODO: isn't this too late to prevent threads from disappearing?
296 // Acquire thread list lock so threads won't disappear from under us.
297 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
298 // Re-read owner now that we hold lock.
299 current_owner = (monitor != NULL) ? monitor->GetOwner() : NULL;
300 // Get short descriptions of the threads involved.
301 current_owner_string = ThreadToString(current_owner);
302 expected_owner_string = ThreadToString(expected_owner);
303 found_owner_string = ThreadToString(found_owner);
305 if (current_owner == NULL) {
306 if (found_owner == NULL) {
307 ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
309 PrettyTypeOf(o).c_str(),
310 expected_owner_string.c_str());
312 // Race: the original read found an owner but now there is none
313 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
314 " (where now the monitor appears unowned) on thread '%s'",
315 found_owner_string.c_str(),
316 PrettyTypeOf(o).c_str(),
317 expected_owner_string.c_str());
320 if (found_owner == NULL) {
321 // Race: originally there was no owner, there is now
322 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
323 " (originally believed to be unowned) on thread '%s'",
324 current_owner_string.c_str(),
325 PrettyTypeOf(o).c_str(),
326 expected_owner_string.c_str());
328 if (found_owner != current_owner) {
329 // Race: originally found and current owner have changed
330 ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
331 " owned by '%s') on object of type '%s' on thread '%s'",
332 found_owner_string.c_str(),
333 current_owner_string.c_str(),
334 PrettyTypeOf(o).c_str(),
335 expected_owner_string.c_str());
337 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
339 current_owner_string.c_str(),
340 PrettyTypeOf(o).c_str(),
341 expected_owner_string.c_str());
347 bool Monitor::Unlock(Thread* self) {
348 DCHECK(self != NULL);
349 MutexLock mu(self, monitor_lock_);
350 Thread* owner = owner_;
352 // We own the monitor, so nobody else can be in here.
353 if (lock_count_ == 0) {
355 locking_method_ = NULL;
358 monitor_contenders_.Signal(self);
363 // We don't own this, so we're not allowed to unlock it.
364 // The JNI spec says that we should throw IllegalMonitorStateException
366 FailedUnlock(GetObject(), self, owner, this);
373 * Wait on a monitor until timeout, interrupt, or notification. Used for
374 * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join().
376 * If another thread calls Thread.interrupt(), we throw InterruptedException
377 * and return immediately if one of the following are true:
378 * - blocked in wait(), wait(long), or wait(long, int) methods of Object
379 * - blocked in join(), join(long), or join(long, int) methods of Thread
380 * - blocked in sleep(long), or sleep(long, int) methods of Thread
381 * Otherwise, we set the "interrupted" flag.
383 * Checks to make sure that "ns" is in the range 0-999999
384 * (i.e. fractions of a millisecond) and throws the appropriate
385 * exception if it isn't.
387 * The spec allows "spurious wakeups", and recommends that all code using
388 * Object.wait() do so in a loop. This appears to derive from concerns
389 * about pthread_cond_wait() on multiprocessor systems. Some commentary
390 * on the web casts doubt on whether these can/should occur.
392 * Since we're allowed to wake up "early", we clamp extremely long durations
393 * to return at the end of the 32-bit time epoch.
395 void Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
396 bool interruptShouldThrow, ThreadState why) {
397 DCHECK(self != NULL);
398 DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
400 monitor_lock_.Lock(self);
402 // Make sure that we hold the lock.
403 if (owner_ != self) {
404 monitor_lock_.Unlock(self);
405 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
409 // We need to turn a zero-length timed wait into a regular wait because
410 // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
411 if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
415 // Enforce the timeout range.
416 if (ms < 0 || ns < 0 || ns > 999999) {
417 monitor_lock_.Unlock(self);
418 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
419 self->ThrowNewExceptionF(throw_location, "Ljava/lang/IllegalArgumentException;",
420 "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
425 * Add ourselves to the set of threads waiting on this monitor, and
426 * release our hold. We need to let it go even if we're a few levels
427 * deep in a recursive lock, and we need to restore that later.
429 * We append to the wait set ahead of clearing the count and owner
430 * fields so the subroutine can check that the calling thread owns
431 * the monitor. Aside from that, the order of member updates is
432 * not order sensitive as we hold the pthread mutex.
434 AppendToWaitSet(self);
436 int prev_lock_count = lock_count_;
439 mirror::ArtMethod* saved_method = locking_method_;
440 locking_method_ = NULL;
441 uintptr_t saved_dex_pc = locking_dex_pc_;
445 * Update thread state. If the GC wakes up, it'll ignore us, knowing
446 * that we won't touch any references in this state, and we'll check
447 * our suspend mode before we transition out.
449 self->TransitionFromRunnableToSuspended(why);
451 bool was_interrupted = false;
453 // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
454 MutexLock mu(self, *self->GetWaitMutex());
456 // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
457 // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
459 DCHECK(self->GetWaitMonitor() == nullptr);
460 self->SetWaitMonitor(this);
462 // Release the monitor lock.
463 monitor_contenders_.Signal(self);
464 monitor_lock_.Unlock(self);
466 // Handle the case where the thread was interrupted before we called wait().
467 if (self->IsInterruptedLocked()) {
468 was_interrupted = true;
470 // Wait for a notification or a timeout to occur.
471 if (why == kWaiting) {
472 self->GetWaitConditionVariable()->Wait(self);
474 DCHECK(why == kTimedWaiting || why == kSleeping) << why;
475 self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
477 if (self->IsInterruptedLocked()) {
478 was_interrupted = true;
480 self->SetInterruptedLocked(false);
484 // Set self->status back to kRunnable, and self-suspend if needed.
485 self->TransitionFromSuspendedToRunnable();
488 // We reset the thread's wait_monitor_ field after transitioning back to runnable so
489 // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
490 // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
491 // are waiting on "null".)
492 MutexLock mu(self, *self->GetWaitMutex());
493 DCHECK(self->GetWaitMonitor() != nullptr);
494 self->SetWaitMonitor(nullptr);
497 // Re-acquire the monitor and lock.
499 monitor_lock_.Lock(self);
500 self->GetWaitMutex()->AssertNotHeld(self);
503 * We remove our thread from wait set after restoring the count
504 * and owner fields so the subroutine can check that the calling
505 * thread owns the monitor. Aside from that, the order of member
506 * updates is not order sensitive as we hold the pthread mutex.
509 lock_count_ = prev_lock_count;
510 locking_method_ = saved_method;
511 locking_dex_pc_ = saved_dex_pc;
513 RemoveFromWaitSet(self);
515 monitor_lock_.Unlock(self);
517 if (was_interrupted) {
519 * We were interrupted while waiting, or somebody interrupted an
520 * un-interruptible thread earlier and we're bailing out immediately.
522 * The doc sayeth: "The interrupted status of the current thread is
523 * cleared when this exception is thrown."
526 MutexLock mu(self, *self->GetWaitMutex());
527 self->SetInterruptedLocked(false);
529 if (interruptShouldThrow) {
530 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
531 self->ThrowNewException(throw_location, "Ljava/lang/InterruptedException;", NULL);
536 void Monitor::Notify(Thread* self) {
537 DCHECK(self != NULL);
538 MutexLock mu(self, monitor_lock_);
539 // Make sure that we hold the lock.
540 if (owner_ != self) {
541 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
544 // Signal the first waiting thread in the wait set.
545 while (wait_set_ != NULL) {
546 Thread* thread = wait_set_;
547 wait_set_ = thread->GetWaitNext();
548 thread->SetWaitNext(nullptr);
550 // Check to see if the thread is still waiting.
551 MutexLock mu(self, *thread->GetWaitMutex());
552 if (thread->GetWaitMonitor() != nullptr) {
553 thread->GetWaitConditionVariable()->Signal(self);
559 void Monitor::NotifyAll(Thread* self) {
560 DCHECK(self != NULL);
561 MutexLock mu(self, monitor_lock_);
562 // Make sure that we hold the lock.
563 if (owner_ != self) {
564 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
567 // Signal all threads in the wait set.
568 while (wait_set_ != NULL) {
569 Thread* thread = wait_set_;
570 wait_set_ = thread->GetWaitNext();
571 thread->SetWaitNext(nullptr);
576 bool Monitor::Deflate(Thread* self, mirror::Object* obj) {
577 DCHECK(obj != nullptr);
578 // Don't need volatile since we only deflate with mutators suspended.
579 LockWord lw(obj->GetLockWord(false));
580 // If the lock isn't an inflated monitor, then we don't need to deflate anything.
581 if (lw.GetState() == LockWord::kFatLocked) {
582 Monitor* monitor = lw.FatLockMonitor();
583 DCHECK(monitor != nullptr);
584 MutexLock mu(self, monitor->monitor_lock_);
585 // Can't deflate if we have anybody waiting on the CV.
586 if (monitor->num_waiters_ > 0) {
589 Thread* owner = monitor->owner_;
590 if (owner != nullptr) {
591 // Can't deflate if we are locked and have a hash code.
592 if (monitor->HasHashCode()) {
595 // Can't deflate if our lock count is too high.
596 if (monitor->lock_count_ > LockWord::kThinLockMaxCount) {
599 // Deflate to a thin lock.
600 obj->SetLockWord(LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_), false);
601 VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / "
602 << monitor->lock_count_;
603 } else if (monitor->HasHashCode()) {
604 obj->SetLockWord(LockWord::FromHashCode(monitor->GetHashCode()), false);
605 VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode();
607 // No lock and no hash, just put an empty lock word inside the object.
608 obj->SetLockWord(LockWord(), false);
609 VLOG(monitor) << "Deflated" << obj << " to empty lock word";
611 // The monitor is deflated, mark the object as nullptr so that we know to delete it during the
613 monitor->obj_ = nullptr;
619 * Changes the shape of a monitor from thin to fat, preserving the internal lock state. The calling
620 * thread must own the lock or the owner must be suspended. There's a race with other threads
621 * inflating the lock and so the caller should read the monitor following the call.
623 void Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
624 DCHECK(self != NULL);
626 // Allocate and acquire a new monitor.
627 std::unique_ptr<Monitor> m(new Monitor(self, owner, obj, hash_code));
628 if (m->Install(self)) {
629 if (owner != nullptr) {
630 VLOG(monitor) << "monitor: thread" << owner->GetThreadId()
631 << " created monitor " << m.get() << " for object " << obj;
633 VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code
634 << " created monitor " << m.get() << " for object " << obj;
636 Runtime::Current()->GetMonitorList()->Add(m.release());
637 CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked);
641 void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
642 uint32_t hash_code) {
643 DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
644 uint32_t owner_thread_id = lock_word.ThinLockOwner();
645 if (owner_thread_id == self->GetThreadId()) {
646 // We own the monitor, we can easily inflate it.
647 Inflate(self, self, obj.Get(), hash_code);
649 ThreadList* thread_list = Runtime::Current()->GetThreadList();
650 // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
651 self->SetMonitorEnterObject(obj.Get());
655 ScopedThreadStateChange tsc(self, kBlocked);
656 owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out);
658 if (owner != nullptr) {
659 // We succeeded in suspending the thread, check the lock's status didn't change.
660 lock_word = obj->GetLockWord(true);
661 if (lock_word.GetState() == LockWord::kThinLocked &&
662 lock_word.ThinLockOwner() == owner_thread_id) {
663 // Go ahead and inflate the lock.
664 Inflate(self, owner, obj.Get(), hash_code);
666 thread_list->Resume(owner, false);
668 self->SetMonitorEnterObject(nullptr);
672 // Fool annotalysis into thinking that the lock on obj is acquired.
673 static mirror::Object* FakeLock(mirror::Object* obj)
674 EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
678 // Fool annotalysis into thinking that the lock on obj is release.
679 static mirror::Object* FakeUnlock(mirror::Object* obj)
680 UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
684 mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) {
685 DCHECK(self != NULL);
688 uint32_t thread_id = self->GetThreadId();
689 size_t contention_count = 0;
690 StackHandleScope<1> hs(self);
691 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
693 LockWord lock_word = h_obj->GetLockWord(true);
694 switch (lock_word.GetState()) {
695 case LockWord::kUnlocked: {
696 LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0));
697 if (h_obj->CasLockWord(lock_word, thin_locked)) {
698 // CasLockWord enforces more than the acquire ordering we need here.
699 return h_obj.Get(); // Success!
701 continue; // Go again.
703 case LockWord::kThinLocked: {
704 uint32_t owner_thread_id = lock_word.ThinLockOwner();
705 if (owner_thread_id == thread_id) {
706 // We own the lock, increase the recursion count.
707 uint32_t new_count = lock_word.ThinLockCount() + 1;
708 if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
709 LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count));
710 h_obj->SetLockWord(thin_locked, true);
711 return h_obj.Get(); // Success!
713 // We'd overflow the recursion count, so inflate the monitor.
714 InflateThinLocked(self, h_obj, lock_word, 0);
719 Runtime* runtime = Runtime::Current();
720 if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) {
721 NanoSleep(1000); // Sleep for 1us and re-attempt.
723 contention_count = 0;
724 InflateThinLocked(self, h_obj, lock_word, 0);
727 continue; // Start from the beginning.
729 case LockWord::kFatLocked: {
730 Monitor* mon = lock_word.FatLockMonitor();
732 return h_obj.Get(); // Success!
734 case LockWord::kHashCode:
735 // Inflate with the existing hashcode.
736 Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
737 continue; // Start from the beginning.
739 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
746 bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
747 DCHECK(self != NULL);
749 obj = FakeUnlock(obj);
750 LockWord lock_word = obj->GetLockWord(true);
751 StackHandleScope<1> hs(self);
752 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
753 switch (lock_word.GetState()) {
754 case LockWord::kHashCode:
756 case LockWord::kUnlocked:
757 FailedUnlock(h_obj.Get(), self, nullptr, nullptr);
758 return false; // Failure.
759 case LockWord::kThinLocked: {
760 uint32_t thread_id = self->GetThreadId();
761 uint32_t owner_thread_id = lock_word.ThinLockOwner();
762 if (owner_thread_id != thread_id) {
763 // TODO: there's a race here with the owner dying while we unlock.
765 Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
766 FailedUnlock(h_obj.Get(), self, owner, nullptr);
767 return false; // Failure.
769 // We own the lock, decrease the recursion count.
770 if (lock_word.ThinLockCount() != 0) {
771 uint32_t new_count = lock_word.ThinLockCount() - 1;
772 LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count));
773 h_obj->SetLockWord(thin_locked, true);
775 h_obj->SetLockWord(LockWord(), true);
777 return true; // Success!
780 case LockWord::kFatLocked: {
781 Monitor* mon = lock_word.FatLockMonitor();
782 return mon->Unlock(self);
785 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
792 * Object.wait(). Also called for class init.
794 void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
795 bool interruptShouldThrow, ThreadState why) {
796 DCHECK(self != nullptr);
797 DCHECK(obj != nullptr);
798 LockWord lock_word = obj->GetLockWord(true);
799 switch (lock_word.GetState()) {
800 case LockWord::kHashCode:
802 case LockWord::kUnlocked:
803 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
805 case LockWord::kThinLocked: {
806 uint32_t thread_id = self->GetThreadId();
807 uint32_t owner_thread_id = lock_word.ThinLockOwner();
808 if (owner_thread_id != thread_id) {
809 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
812 // We own the lock, inflate to enqueue ourself on the Monitor.
813 Inflate(self, self, obj, 0);
814 lock_word = obj->GetLockWord(true);
818 case LockWord::kFatLocked:
819 break; // Already set for a wait.
821 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
825 Monitor* mon = lock_word.FatLockMonitor();
826 mon->Wait(self, ms, ns, interruptShouldThrow, why);
829 void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
830 DCHECK(self != nullptr);
831 DCHECK(obj != nullptr);
832 LockWord lock_word = obj->GetLockWord(true);
833 switch (lock_word.GetState()) {
834 case LockWord::kHashCode:
836 case LockWord::kUnlocked:
837 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
839 case LockWord::kThinLocked: {
840 uint32_t thread_id = self->GetThreadId();
841 uint32_t owner_thread_id = lock_word.ThinLockOwner();
842 if (owner_thread_id != thread_id) {
843 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
846 // We own the lock but there's no Monitor and therefore no waiters.
850 case LockWord::kFatLocked: {
851 Monitor* mon = lock_word.FatLockMonitor();
853 mon->NotifyAll(self);
860 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
866 uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
867 DCHECK(obj != nullptr);
868 LockWord lock_word = obj->GetLockWord(true);
869 switch (lock_word.GetState()) {
870 case LockWord::kHashCode:
872 case LockWord::kUnlocked:
873 return ThreadList::kInvalidThreadId;
874 case LockWord::kThinLocked:
875 return lock_word.ThinLockOwner();
876 case LockWord::kFatLocked: {
877 Monitor* mon = lock_word.FatLockMonitor();
878 return mon->GetOwnerThreadId();
881 LOG(FATAL) << "Unreachable";
882 return ThreadList::kInvalidThreadId;
887 void Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
888 // Determine the wait message and object we're waiting or blocked upon.
889 mirror::Object* pretty_object = nullptr;
890 const char* wait_message = nullptr;
891 uint32_t lock_owner = ThreadList::kInvalidThreadId;
892 ThreadState state = thread->GetState();
893 if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
894 wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on ";
895 Thread* self = Thread::Current();
896 MutexLock mu(self, *thread->GetWaitMutex());
897 Monitor* monitor = thread->GetWaitMonitor();
898 if (monitor != nullptr) {
899 pretty_object = monitor->GetObject();
901 } else if (state == kBlocked) {
902 wait_message = " - waiting to lock ";
903 pretty_object = thread->GetMonitorEnterObject();
904 if (pretty_object != nullptr) {
905 lock_owner = pretty_object->GetLockOwnerThreadId();
909 if (wait_message != nullptr) {
910 if (pretty_object == nullptr) {
911 os << wait_message << "an unknown object";
913 if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
914 Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
915 // Getting the identity hashcode here would result in lock inflation and suspension of the
916 // current thread, which isn't safe if this is the only runnable thread.
917 os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
918 reinterpret_cast<intptr_t>(pretty_object),
919 PrettyTypeOf(pretty_object).c_str());
921 // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
922 os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(),
923 PrettyTypeOf(pretty_object).c_str());
926 // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
927 if (lock_owner != ThreadList::kInvalidThreadId) {
928 os << " held by thread " << lock_owner;
934 mirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
935 // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
936 // definition of contended that includes a monitor a thread is trying to enter...
937 mirror::Object* result = thread->GetMonitorEnterObject();
938 if (result == NULL) {
939 // ...but also a monitor that the thread is waiting on.
940 MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
941 Monitor* monitor = thread->GetWaitMonitor();
942 if (monitor != NULL) {
943 result = monitor->GetObject();
949 void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
950 void* callback_context) {
951 mirror::ArtMethod* m = stack_visitor->GetMethod();
954 // Native methods are an easy special case.
955 // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
957 if (m->IsSynchronized()) {
958 mirror::Object* jni_this = stack_visitor->GetCurrentHandleScope()->GetReference(0);
959 callback(jni_this, callback_context);
964 // Proxy methods should not be synchronized.
965 if (m->IsProxyMethod()) {
966 CHECK(!m->IsSynchronized());
970 // <clinit> is another special case. The runtime holds the class lock while calling <clinit>.
971 if (m->IsClassInitializer()) {
972 callback(m->GetDeclaringClass(), callback_context);
973 // Fall through because there might be synchronization in the user code too.
976 // Is there any reason to believe there's any synchronization in this method?
977 const DexFile::CodeItem* code_item = m->GetCodeItem();
978 CHECK(code_item != NULL) << PrettyMethod(m);
979 if (code_item->tries_size_ == 0) {
980 return; // No "tries" implies no synchronization, so no held locks to report.
983 // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
984 // the locks held in this stack frame.
985 std::vector<uint32_t> monitor_enter_dex_pcs;
986 verifier::MethodVerifier::FindLocksAtDexPc(m, stack_visitor->GetDexPc(), &monitor_enter_dex_pcs);
987 if (monitor_enter_dex_pcs.empty()) {
991 for (size_t i = 0; i < monitor_enter_dex_pcs.size(); ++i) {
992 // The verifier works in terms of the dex pcs of the monitor-enter instructions.
993 // We want the registers used by those instructions (so we can read the values out of them).
994 uint32_t dex_pc = monitor_enter_dex_pcs[i];
995 uint16_t monitor_enter_instruction = code_item->insns_[dex_pc];
997 // Quick sanity check.
998 if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) {
999 LOG(FATAL) << "expected monitor-enter @" << dex_pc << "; was "
1000 << reinterpret_cast<void*>(monitor_enter_instruction);
1003 uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff);
1004 mirror::Object* o = reinterpret_cast<mirror::Object*>(stack_visitor->GetVReg(m, monitor_register,
1006 callback(o, callback_context);
1010 bool Monitor::IsValidLockWord(LockWord lock_word) {
1011 switch (lock_word.GetState()) {
1012 case LockWord::kUnlocked:
1013 // Nothing to check.
1015 case LockWord::kThinLocked:
1016 // Basic sanity check of owner.
1017 return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1018 case LockWord::kFatLocked: {
1019 // Check the monitor appears in the monitor list.
1020 Monitor* mon = lock_word.FatLockMonitor();
1021 MonitorList* list = Runtime::Current()->GetMonitorList();
1022 MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1023 for (Monitor* list_mon : list->list_) {
1024 if (mon == list_mon) {
1025 return true; // Found our monitor.
1028 return false; // Fail - unowned monitor in an object.
1030 case LockWord::kHashCode:
1033 LOG(FATAL) << "Unreachable";
1038 bool Monitor::IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1039 MutexLock mu(Thread::Current(), monitor_lock_);
1040 return owner_ != nullptr;
1043 void Monitor::TranslateLocation(mirror::ArtMethod* method, uint32_t dex_pc,
1044 const char** source_file, uint32_t* line_number) const {
1045 // If method is null, location is unknown
1046 if (method == NULL) {
1051 *source_file = method->GetDeclaringClassSourceFile();
1052 if (*source_file == NULL) {
1055 *line_number = method->GetLineNumFromDexPC(dex_pc);
1058 uint32_t Monitor::GetOwnerThreadId() {
1059 MutexLock mu(Thread::Current(), monitor_lock_);
1060 Thread* owner = owner_;
1061 if (owner != NULL) {
1062 return owner->GetThreadId();
1064 return ThreadList::kInvalidThreadId;
1068 MonitorList::MonitorList()
1069 : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
1070 monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
1073 MonitorList::~MonitorList() {
1074 MutexLock mu(Thread::Current(), monitor_list_lock_);
1075 STLDeleteElements(&list_);
1078 void MonitorList::DisallowNewMonitors() {
1079 MutexLock mu(Thread::Current(), monitor_list_lock_);
1080 allow_new_monitors_ = false;
1083 void MonitorList::AllowNewMonitors() {
1084 Thread* self = Thread::Current();
1085 MutexLock mu(self, monitor_list_lock_);
1086 allow_new_monitors_ = true;
1087 monitor_add_condition_.Broadcast(self);
1090 void MonitorList::Add(Monitor* m) {
1091 Thread* self = Thread::Current();
1092 MutexLock mu(self, monitor_list_lock_);
1093 while (UNLIKELY(!allow_new_monitors_)) {
1094 monitor_add_condition_.WaitHoldingLocks(self);
1096 list_.push_front(m);
1099 void MonitorList::SweepMonitorList(IsMarkedCallback* callback, void* arg) {
1100 MutexLock mu(Thread::Current(), monitor_list_lock_);
1101 for (auto it = list_.begin(); it != list_.end(); ) {
1103 // Disable the read barrier in GetObject() as this is called by GC.
1104 mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
1105 // The object of a monitor can be null if we have deflated it.
1106 mirror::Object* new_obj = obj != nullptr ? callback(obj, arg) : nullptr;
1107 if (new_obj == nullptr) {
1108 VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
1111 it = list_.erase(it);
1113 m->SetObject(new_obj);
1119 struct MonitorDeflateArgs {
1120 MonitorDeflateArgs() : self(Thread::Current()), deflate_count(0) {}
1122 size_t deflate_count;
1125 static mirror::Object* MonitorDeflateCallback(mirror::Object* object, void* arg)
1126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1127 MonitorDeflateArgs* args = reinterpret_cast<MonitorDeflateArgs*>(arg);
1128 if (Monitor::Deflate(args->self, object)) {
1129 DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
1130 ++args->deflate_count;
1131 // If we deflated, return nullptr so that the monitor gets removed from the array.
1134 return object; // Monitor was not deflated.
1137 size_t MonitorList::DeflateMonitors() {
1138 MonitorDeflateArgs args;
1139 Locks::mutator_lock_->AssertExclusiveHeld(args.self);
1140 SweepMonitorList(MonitorDeflateCallback, &args);
1141 return args.deflate_count;
1144 MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(NULL), entry_count_(0) {
1145 DCHECK(obj != nullptr);
1146 LockWord lock_word = obj->GetLockWord(true);
1147 switch (lock_word.GetState()) {
1148 case LockWord::kUnlocked:
1150 case LockWord::kForwardingAddress:
1152 case LockWord::kHashCode:
1154 case LockWord::kThinLocked:
1155 owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
1156 entry_count_ = 1 + lock_word.ThinLockCount();
1157 // Thin locks have no waiters.
1159 case LockWord::kFatLocked: {
1160 Monitor* mon = lock_word.FatLockMonitor();
1161 owner_ = mon->owner_;
1162 entry_count_ = 1 + mon->lock_count_;
1163 for (Thread* waiter = mon->wait_set_; waiter != NULL; waiter = waiter->GetWaitNext()) {
1164 waiters_.push_back(waiter);