image.png

轻量级锁释放后的状态

image.png

轻量级锁加锁成功后的状态

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

  1. void ObjectSynchronizer::fast_enter(Handle obj, BasicLock *lock,
  2. bool attempt_rebias, TRAPS) {
  3. if (UseBiasedLocking) {
  4. if (!SafepointSynchronize::is_at_safepoint()) {
  5. //撤销偏向锁或者重偏向
  6. BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD);
  7. if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) {
  8. return;
  9. }
  10. } else {
  11. assert(!attempt_rebias, "can not rebias toward VM thread");
  12. BiasedLocking::revoke_at_safepoint(obj);
  13. }
  14. assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
  15. }
  16. slow_enter(obj, lock, THREAD);
  17. }
  18. void ObjectSynchronizer::slow_enter(Handle obj, BasicLock *lock, TRAPS) {
  19. markOop mark = obj->mark();
  20. assert(!mark->has_bias_pattern(), "should not see bias pattern here");
  21. if (mark->is_neutral()) {
  22. // Anticipate successful CAS -- the ST of the displaced mark must
  23. // be visible <= the ST performed by the CAS.
  24. lock->set_displaced_header(mark);
  25. //在markword中存入 lockRecord的引用
  26. if (mark == obj()->cas_set_mark((markOop) lock, mark)) {
  27. //轻量级锁加锁成功
  28. return;
  29. }
  30. // Fall through to inflate() ...
  31. } else if (mark->has_locker() &&
  32. THREAD->is_lock_owned((address) mark->locker())) {
  33. assert(lock != mark->locker(), "must not re-lock the same lock");
  34. assert(lock != (BasicLock *) obj->mark(), "don't relock with same BasicLock");
  35. lock->set_displaced_header(NULL);
  36. return;
  37. }
  38. // The object header will never be displaced to this lock,
  39. // so it does not matter what the value is, except that it
  40. // must be non-zero to avoid looking like a re-entrant lock,
  41. // and must not look locked either.
  42. lock->set_displaced_header(markOopDesc::unused_mark());
  43. //开始膨胀成重量级锁 10
  44. ObjectSynchronizer::inflate(THREAD,obj(),inflate_cause_monitor_enter)->enter(THREAD);
  45. }