image.png

image.png

interpreterRuntime.cpp

  1. //%note monitor_1
  2. IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* thread, BasicObjectLock* elem))
  3. #ifdef ASSERT
  4. thread->last_frame().interpreter_frame_verify_monitor(elem);
  5. #endif
  6. if (PrintBiasedLockingStatistics) {
  7. Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
  8. }
  9. Handle h_obj(thread, elem->obj());
  10. assert(Universe::heap()->is_in_reserved_or_null(h_obj()),
  11. "must be NULL or an object");
  12. //是否开启了偏向锁模式
  13. if (UseBiasedLocking) {
  14. // Retry fast entry if bias is revoked to avoid unnecessary inflation
  15. ObjectSynchronizer::fast_enter(h_obj, elem->lock(), true, CHECK);
  16. } else {
  17. ObjectSynchronizer::slow_enter(h_obj, elem->lock(), CHECK);
  18. }
  19. assert(Universe::heap()->is_in_reserved_or_null(elem->obj()),
  20. "must be NULL or an object");
  21. #ifdef ASSERT
  22. thread->last_frame().interpreter_frame_verify_monitor(elem);
  23. #endif
  24. IRT_END

synchronizer.cpp

void ObjectSynchronizer::fast_enter(Handle obj, BasicLock *lock,
                                    bool attempt_rebias, TRAPS) {
    if (UseBiasedLocking) {
        if (!SafepointSynchronize::is_at_safepoint()) {
            //撤销偏向锁或者重偏向
            BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD);
            if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) {
                return;
            }
        } else {
            assert(!attempt_rebias, "can not rebias toward VM thread");
            BiasedLocking::revoke_at_safepoint(obj);
        }
        assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
    }

    slow_enter(obj, lock, THREAD);
}

void ObjectSynchronizer::slow_enter(Handle obj, BasicLock *lock, TRAPS) {
    markOop mark = obj->mark();
    assert(!mark->has_bias_pattern(), "should not see bias pattern here");

    if (mark->is_neutral()) {
        // Anticipate successful CAS -- the ST of the displaced mark must
        // be visible <= the ST performed by the CAS.
        lock->set_displaced_header(mark);
        //在markword中存入 lockRecord的引用 
        if (mark == obj()->cas_set_mark((markOop) lock, mark)) {
            //轻量级锁加锁成功
            return;
        }
        // Fall through to inflate() ...
    } else if (mark->has_locker() &&
               THREAD->is_lock_owned((address) mark->locker())) {
        assert(lock != mark->locker(), "must not re-lock the same lock");
        assert(lock != (BasicLock *) obj->mark(), "don't relock with same BasicLock");
        lock->set_displaced_header(NULL);
        return;
    }

    // The object header will never be displaced to this lock,
    // so it does not matter what the value is, except that it
    // must be non-zero to avoid looking like a re-entrant lock,
    // and must not look locked either.
    lock->set_displaced_header(markOopDesc::unused_mark());
    //开始膨胀成重量级锁 10
    ObjectSynchronizer::inflate(THREAD,obj(),inflate_cause_monitor_enter)->enter(THREAD);
}