您现在的位置是:主页 > news > 网站团队人数/阳城seo排名

网站团队人数/阳城seo排名

admin2025/5/5 9:02:27news

简介网站团队人数,阳城seo排名,汉阳网站推广公司,西安网页设计培训文章目录1. 前言锁对象的对象头结构2. 获取锁的源码流程3. 释放锁的源码流程1. 前言 在 synchronized 同步代码块实现原理一节中,我们知道 Java 同步代码块的执行依赖 monitorenter 和 monitorexit 指令,synchronized 关键字基于这两个指令进行锁的获取…

网站团队人数,阳城seo排名,汉阳网站推广公司,西安网页设计培训文章目录1. 前言锁对象的对象头结构2. 获取锁的源码流程3. 释放锁的源码流程1. 前言 在 synchronized 同步代码块实现原理一节中,我们知道 Java 同步代码块的执行依赖 monitorenter 和 monitorexit 指令,synchronized 关键字基于这两个指令进行锁的获取…

文章目录

  • 1. 前言
      • 锁对象的对象头结构
  • 2. 获取锁的源码流程
  • 3. 释放锁的源码流程

1. 前言

在 synchronized 同步代码块实现原理一节中,我们知道 Java 同步代码块的执行依赖 monitorentermonitorexit 指令,synchronized 关键字基于这两个指令进行锁的获取和释放。在 JVM 的实现中,解释器执行monitorenter指令时会进入到interpreterRuntime.cpp#InterpreterRuntime::monitorenter()函数,这里也就是锁竞争的入口

锁其实就是实现共享资源分配的一种方式,在 JVM 的实现中锁语义的实现依赖于作为锁的对象的对象头结构,这个对象头将作为共享资源由线程竞争,竞争成功的线程获取到锁,竞争失败的线程挂起等待唤醒。本文源码基于 HotSpot

锁对象的对象头结构

以下为32位JVM中锁对象的MarkWord可能的存储结构:以下为 32 位 JVM 中锁对象的 Mark Word 可能的存储结构:以下为32JVM中锁对象的MarkWord可能的存储结构:

锁状态25bit4bit1bit2bit
23bit2bit是否是偏向锁锁标志位
无锁对象的HashCode对象分代年龄001
偏向锁线程IDEpoch: 偏向锁时间戳对象分代年龄101
轻量级锁指向栈帧中锁记录的指针00
重量级锁指向重量级锁的指针10
GC标志11

以下为 markOop.hpp 文件中定义的锁状态的枚举,可以和以上表格对应

enum {   locked_value             = 0,   // 00,轻量级锁unlocked_value           = 1, // 01,无锁monitor_value            = 2, // 10,重量级锁marked_value             = 3, // 11,GC 标志biased_lock_pattern      = 5  // 101,偏向锁};

2. 获取锁的源码流程

在这里插入图片描述

interpreterRuntime.cpp#InterpreterRuntime::monitorenter()函数会在 JVM 解释器执行monitorenter指令时触发,该函数的逻辑比较简单:

  1. 首先判断是否启用了偏向锁,启用了偏向锁则调用 ObjectSynchronizer::fast_enter() 函数
  2. 未启用偏向锁则调用 ObjectSynchronizer::slow_enter() 函数,该函数在 fast_enter 未成功的时候也会被调用
IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* thread, BasicObjectLock* elem))
#ifdef ASSERTthread->last_frame().interpreter_frame_verify_monitor(elem);
#endifif (PrintBiasedLockingStatistics) {Atomic::inc(BiasedLocking::slow_path_entry_count_addr());}Handle h_obj(thread, elem->obj());assert(Universe::heap()->is_in_reserved_or_null(h_obj()),"must be NULL or an object");// UseBiasedLocking 是一个全局的运行时配置,标识虚拟机是否开启偏向锁if (UseBiasedLocking) {// Retry fast entry if bias is revoked to avoid unnecessary inflationObjectSynchronizer::fast_enter(h_obj, elem->lock(), true, CHECK);} else {ObjectSynchronizer::slow_enter(h_obj, elem->lock(), CHECK);}assert(Universe::heap()->is_in_reserved_or_null(elem->obj()),"must be NULL or an object");
#ifdef ASSERTthread->last_frame().interpreter_frame_verify_monitor(elem);
#endif
IRT_END

synchronizer.cpp#ObjectSynchronizer::fast_enter() 函数的实现比较清晰:

  1. 首先再次进行偏向锁启用判断,此处为了分析方便,默认当前不在安全点,则调用 BiasedLocking::revoke_and_rebias() 函数去撤销偏向锁并且尝试将偏向锁重新偏向
  2. 如果偏向锁重偏向没有成功,则进入 ObjectSynchronizer::slow_enter() 流程
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) ;
}

biasedLocking.cpp#BiasedLocking::revoke_and_rebias() 函数的主要作用是撤销偏向锁,并尝试将偏向锁重新偏向当前请求锁的线程。该函数比较长,大致流程分为以下几步:

  1. 首先取出锁对象的对象头 markOop,如果对象头标志开启了偏向锁但是没有偏向任何线程,并且方法入参 attempt_rebiasfalse,则 CAS 将锁对象当前有偏向锁的对象头替换为无锁的对象头,也就是直接撤销偏向锁。根据上文可知,当前分析的流程不会进入这条分支
  2. 如果锁对象头标志开启偏向锁,则需要判断锁对象所属的 class 属性头里面是否开启了偏向锁模式:
    1. 锁对象所属的 class 属性头未开启偏向锁,此时 CAS 将锁对象当前有偏向锁的对象头替换为无锁的对象头,直接撤销偏向锁
    2. 如果锁对象的偏向锁时间戳与其所属的 class 属性头的偏向锁时间戳不一致,说明偏向锁过期了。此时根据 attempt_rebias 入参尝试偏向锁重偏向或者撤销偏向锁,重偏向也就是将当前线程封装到对象头中,CAS 替换锁对象的对象头,CAS 成功则重偏向成功,fast_enter 流程结束
  3. 以上条件都未满足,则需要判断进行偏向锁单个撤销还是批量撤销。此处以单个撤销为例:
    1. 首先判断如果偏向锁的持有者是当前线程并且偏向锁的时间戳与其锁对象所属的 class 属性头的偏向锁时间戳一致,则直接调用 biasedLocking.cpp#revoke_bias() 函数撤销偏向锁
    2. 不满足以上条件则包装一个 VM_RevokeBias 撤销偏向锁的异步任务(相当于 Java 中的 Runnable 任务)提交到虚拟机线程执行,执行时会调用 VM_RevokeBias#doit() 函数,最终也是调用 biasedLocking.cpp#revoke_bias() 函数撤销偏向锁
BiasedLocking::Condition BiasedLocking::revoke_and_rebias(Handle obj, bool attempt_rebias, TRAPS) {assert(!SafepointSynchronize::is_at_safepoint(), "must not be called while at safepoint");// We can revoke the biases of anonymously-biased objects// efficiently enough that we should not cause these revocations to// update the heuristics because doing so may cause unwanted bulk// revocations (which are expensive) to occur.markOop mark = obj->mark();if (mark->is_biased_anonymously() && !attempt_rebias) {// We are probably trying to revoke the bias of this object due to// an identity hash code computation. Try to revoke the bias// without a safepoint. This is possible if we can successfully// compare-and-exchange an unbiased header into the mark word of// the object, meaning that no other thread has raced to acquire// the bias of the object.markOop biased_value       = mark;markOop unbiased_prototype = markOopDesc::prototype()->set_age(mark->age());markOop res_mark = (markOop) Atomic::cmpxchg_ptr(unbiased_prototype, obj->mark_addr(), mark);if (res_mark == biased_value) {return BIAS_REVOKED;}} else if (mark->has_bias_pattern()) {Klass* k = obj->klass();markOop prototype_header = k->prototype_header();if (!prototype_header->has_bias_pattern()) {// This object has a stale bias from before the bulk revocation// for this data type occurred. It's pointless to update the// heuristics at this point so simply update the header with a// CAS. If we fail this race, the object's bias has been revoked// by another thread so we simply return and let the caller deal// with it.markOop biased_value       = mark;markOop res_mark = (markOop) Atomic::cmpxchg_ptr(prototype_header, obj->mark_addr(), mark);assert(!(*(obj->mark_addr()))->has_bias_pattern(), "even if we raced, should still be revoked");return BIAS_REVOKED;} else if (prototype_header->bias_epoch() != mark->bias_epoch()) {// The epoch of this biasing has expired indicating that the// object is effectively unbiased. Depending on whether we need// to rebias or revoke the bias of this object we can do it// efficiently enough with a CAS that we shouldn't update the// heuristics. This is normally done in the assembly code but we// can reach this point due to various points in the runtime// needing to revoke biases.if (attempt_rebias) {assert(THREAD->is_Java_thread(), "");markOop biased_value       = mark;markOop rebiased_prototype = markOopDesc::encode((JavaThread*) THREAD, mark->age(), prototype_header->bias_epoch());markOop res_mark = (markOop) Atomic::cmpxchg_ptr(rebiased_prototype, obj->mark_addr(), mark);if (res_mark == biased_value) {return BIAS_REVOKED_AND_REBIASED;}} else {markOop biased_value       = mark;markOop unbiased_prototype = markOopDesc::prototype()->set_age(mark->age());markOop res_mark = (markOop) Atomic::cmpxchg_ptr(unbiased_prototype, obj->mark_addr(), mark);if (res_mark == biased_value) {return BIAS_REVOKED;}}}}HeuristicsResult heuristics = update_heuristics(obj(), attempt_rebias);if (heuristics == HR_NOT_BIASED) {return NOT_BIASED;} else if (heuristics == HR_SINGLE_REVOKE) {Klass *k = obj->klass();markOop prototype_header = k->prototype_header();if (mark->biased_locker() == THREAD &&prototype_header->bias_epoch() == mark->bias_epoch()) {// A thread is trying to revoke the bias of an object biased// toward it, again likely due to an identity hash code// computation. We can again avoid a safepoint in this case// since we are only going to walk our own stack. There are no// races with revocations occurring in other threads because we// reach no safepoints in the revocation path.// Also check the epoch because even if threads match, another thread// can come in with a CAS to steal the bias of an object that has a// stale epoch.ResourceMark rm;if (TraceBiasedLocking) {tty->print_cr("Revoking bias by walking my own stack:");}BiasedLocking::Condition cond = revoke_bias(obj(), false, false, (JavaThread*) THREAD);((JavaThread*) THREAD)->set_cached_monitor_info(NULL);assert(cond == BIAS_REVOKED, "why not?");return cond;} else {VM_RevokeBias revoke(&obj, (JavaThread*) THREAD);VMThread::execute(&revoke);return revoke.status_code();}}assert((heuristics == HR_BULK_REVOKE) ||(heuristics == HR_BULK_REBIAS), "?");VM_BulkRevokeBias bulk_revoke(&obj, (JavaThread*) THREAD,(heuristics == HR_BULK_REBIAS),attempt_rebias);VMThread::execute(&bulk_revoke);return bulk_revoke.status_code();
}

biasedLocking.cpp#revoke_bias() 撤销偏向锁的过程稍微有点繁琐,大致为以下流程:

  1. 首先取锁对象的对象头,判断其是否有偏向锁,如果没有偏向锁直接返回即可
  2. 从锁对象的对象头里取出其偏向的线程,如果不存在偏向线程则根据入参 allow_rebias 更新锁对象的对象头,完成后 return 返回
  3. 检查持有偏向锁的线程是否依然存活,如果持有锁的线程不再存活,则根据 入参 allow_rebias 更新锁对象的对象头,完成后 return 返回
  4. 如果持有偏向锁的线程依然存活,则调用 biasedLocking.cpp#get_or_compute_monitor_info() 函数遍历该线程的栈帧,查找其中属于当前锁对象的 MonitorInfo 对象。如果能找到,则从这个 MonitorInfo 对象中取出 锁记录(Lock Record)赋值给 highest_lock
  5. 如果highest_lock 不为 NULL说明持有偏向锁的线程还没有释放锁,此时则将锁对象的对象头替换为该线程栈中的锁记录,需注意此处实际完成了偏向锁升级为轻量级锁,持有偏向锁的线程继续持有轻量级锁
  6. 如果highest_lock NULL说明持有偏向锁的线程已经释放锁,则将锁对象的对象头更新为无锁状态或者开启了偏向锁但是未偏向任何线程的状态,完成偏向锁撤销
static BiasedLocking::Condition revoke_bias(oop obj, bool allow_rebias, bool is_bulk, JavaThread* requesting_thread) {markOop mark = obj->mark();if (!mark->has_bias_pattern()) {if (TraceBiasedLocking) {ResourceMark rm;tty->print_cr("  (Skipping revocation of object of type %s because it's no longer biased)",obj->klass()->external_name());}return BiasedLocking::NOT_BIASED;}uint age = mark->age();markOop   biased_prototype = markOopDesc::biased_locking_prototype()->set_age(age);markOop unbiased_prototype = markOopDesc::prototype()->set_age(age);if (TraceBiasedLocking && (Verbose || !is_bulk)) {ResourceMark rm;tty->print_cr("Revoking bias of object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s , prototype header " INTPTR_FORMAT " , allow rebias %d , requesting thread " INTPTR_FORMAT,p2i((void *)obj), (intptr_t) mark, obj->klass()->external_name(), (intptr_t) obj->klass()->prototype_header(), (allow_rebias ? 1 : 0), (intptr_t) requesting_thread);}JavaThread* biased_thread = mark->biased_locker();if (biased_thread == NULL) {// Object is anonymously biased. We can get here if, for// example, we revoke the bias due to an identity hash code// being computed for an object.if (!allow_rebias) {obj->set_mark(unbiased_prototype);}if (TraceBiasedLocking && (Verbose || !is_bulk)) {tty->print_cr("  Revoked bias of anonymously-biased object");}return BiasedLocking::BIAS_REVOKED;}// Handle case where the thread toward which the object was biased has exitedbool thread_is_alive = false;if (requesting_thread == biased_thread) {thread_is_alive = true;} else {for (JavaThread* cur_thread = Threads::first(); cur_thread != NULL; cur_thread = cur_thread->next()) {if (cur_thread == biased_thread) {thread_is_alive = true;break;}}}if (!thread_is_alive) {if (allow_rebias) {obj->set_mark(biased_prototype);} else {obj->set_mark(unbiased_prototype);}if (TraceBiasedLocking && (Verbose || !is_bulk)) {tty->print_cr("  Revoked bias of object biased toward dead thread");}return BiasedLocking::BIAS_REVOKED;}// Thread owning bias is alive.// Check to see whether it currently owns the lock and, if so,// write down the needed displaced headers to the thread's stack.// Otherwise, restore the object's header either to the unlocked// or unbiased state.GrowableArray<MonitorInfo*>* cached_monitor_info = get_or_compute_monitor_info(biased_thread);BasicLock* highest_lock = NULL;for (int i = 0; i < cached_monitor_info->length(); i++) {MonitorInfo* mon_info = cached_monitor_info->at(i);if (mon_info->owner() == obj) {if (TraceBiasedLocking && Verbose) {tty->print_cr("   mon_info->owner (" PTR_FORMAT ") == obj (" PTR_FORMAT ")",p2i((void *) mon_info->owner()),p2i((void *) obj));}// Assume recursive case and fix up highest lock latermarkOop mark = markOopDesc::encode((BasicLock*) NULL);highest_lock = mon_info->lock();highest_lock->set_displaced_header(mark);} else {if (TraceBiasedLocking && Verbose) {tty->print_cr("   mon_info->owner (" PTR_FORMAT ") != obj (" PTR_FORMAT ")",p2i((void *) mon_info->owner()),p2i((void *) obj));}}}if (highest_lock != NULL) {// Fix up highest lock to contain displaced header and point// object at ithighest_lock->set_displaced_header(unbiased_prototype);// Reset object header to point to displaced mark.// Must release storing the lock address for platforms without TSO// ordering (e.g. ppc).obj->release_set_mark(markOopDesc::encode(highest_lock));assert(!obj->mark()->has_bias_pattern(), "illegal mark state: stack lock used bias bit");if (TraceBiasedLocking && (Verbose || !is_bulk)) {tty->print_cr("  Revoked bias of currently-locked object");}} else {if (TraceBiasedLocking && (Verbose || !is_bulk)) {tty->print_cr("  Revoked bias of currently-unlocked object");}if (allow_rebias) {obj->set_mark(biased_prototype);} else {// Store the unlocked value into the object's header.obj->set_mark(unbiased_prototype);}}return BiasedLocking::BIAS_REVOKED;
}

ObjectSynchronizer::fast_enter()函数的处理流程中,如果偏向锁只是撤销了,重新偏向没有成功,则进入 synchronizer.cpp#ObjectSynchronizer::slow_enter() 流程,这部分主要分为了以下几步:

  1. 首先判断锁对象的对象头是否是无锁状态,如果是的话将锁对象的对象头设置进请求锁的线程栈帧中的锁记录,然后 CAS 将锁对象的对象头替换为栈帧锁记录,CAS 操作成功则请求锁的线程获取轻量级锁成功
  2. 判断当前锁的持有者是不是请求锁的线程,是的话处理锁重入即可
  3. 以上条件都不满足,则进入锁膨胀和锁竞争的流程,该部分主要分为两步:
    1. 调用 ObjectSynchronizer::inflate() 函数将锁对象的对象头替换为ObjectMonitor对象,标志锁膨胀为重量级锁
    2. 调用 objectMonitor.cpp#ObjectMonitor::enter() 函数进入重量级锁的竞争处理,这部分可参考 Java synchronized 关键字(3)-JVM 重量级锁 Monitor 的实现
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);if (mark == (markOop) Atomic::cmpxchg_ptr(lock, obj()->mark_addr(), mark)) {TEVENT (slow_enter: release stacklock) ;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;}#if 0// The following optimization isn't particularly useful.if (mark->has_monitor() && mark->monitor()->is_entered(THREAD)) {lock->set_displaced_header (NULL) ;return ;}
#endif// 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());ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD);
}

synchronizer.cpp#ObjectSynchronizer::inflate() 函数体比较长,不过主要的处理大致如下:

  1. 锁膨胀包裹在一个 for 循环中,这是为了处理多线程同时进行锁膨胀的情况,其内部会根据锁对象的状态进行不同的处理:
    1. 对象头锁标志如果已经是重量级锁,说明膨胀已经完成,直接返回
    2. 对象头如果是膨胀中状态,则进行忙等待
    3. 对象头处于轻量级锁定,则需要进行膨胀操作
    4. 最后对象头是无锁状态,也需要进行膨胀操作
  2. 轻量级锁膨胀的处理流程如下:
    1. 根据当前线程新建一个 ObjectMonitor 对象,初始化其基本属性
    2. 将锁对象的对象头 CAS 替换为膨胀中(INFLATING)状态的对象头
    3. 设置 ObjectMonitor 对象的关键属性,比较关键的是设置其 _owner字段为持有轻量级锁的线程的栈帧中的锁记录,也就是轻量级锁的持有者继续持有重量级锁
    4. 设置锁对象的对象头为 ObjectMonitor 对象,完成锁膨胀返回
  3. 无锁状态下的锁膨胀处理如下:
    1. 根据当前线程新建一个 ObjectMonitor 对象,初始化其基本属性,包括设置 _owner字段为 NULL,表示暂时没有线程获取到锁
    2. 设置锁对象的对象头为 ObjectMonitor 对象,成功则完成锁膨胀返回
ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {// Inflate mutates the heap ...// Relaxing assertion for bug 6320749.assert (Universe::verify_in_progress() ||!SafepointSynchronize::is_at_safepoint(), "invariant") ;for (;;) {const markOop mark = object->mark() ;assert (!mark->has_bias_pattern(), "invariant") ;// The mark can be in one of the following states:// *  Inflated     - just return// *  Stack-locked - coerce it to inflated// *  INFLATING    - busy wait for conversion to complete// *  Neutral      - aggressively inflate the object.// *  BIASED       - Illegal.  We should never see this// CASE: inflatedif (mark->has_monitor()) {ObjectMonitor * inf = mark->monitor() ;assert (inf->header()->is_neutral(), "invariant");assert (inf->object() == object, "invariant") ;assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");return inf ;}// CASE: inflation in progress - inflating over a stack-lock.// Some other thread is converting from stack-locked to inflated.// Only that thread can complete inflation -- other threads must wait.// The INFLATING value is transient.// Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.// We could always eliminate polling by parking the thread on some auxiliary list.if (mark == markOopDesc::INFLATING()) {TEVENT (Inflate: spin while INFLATING) ;ReadStableMark(object) ;continue ;}// CASE: stack-locked// Could be stack-locked either by this thread or by some other thread.//// Note that we allocate the objectmonitor speculatively, _before_ attempting// to install INFLATING into the mark word.  We originally installed INFLATING,// allocated the objectmonitor, and then finally STed the address of the// objectmonitor into the mark.  This was correct, but artificially lengthened// the interval in which INFLATED appeared in the mark, thus increasing// the odds of inflation contention.//// We now use per-thread private objectmonitor free lists.// These list are reprovisioned from the global free list outside the// critical INFLATING...ST interval.  A thread can transfer// multiple objectmonitors en-mass from the global free list to its local free list.// This reduces coherency traffic and lock contention on the global free list.// Using such local free lists, it doesn't matter if the omAlloc() call appears// before or after the CAS(INFLATING) operation.// See the comments in omAlloc().if (mark->has_locker()) {ObjectMonitor * m = omAlloc (Self) ;// Optimistically prepare the objectmonitor - anticipate successful CAS// We do this before the CAS in order to minimize the length of time// in which INFLATING appears in the mark.m->Recycle();m->_Responsible  = NULL ;m->OwnerIsThread = 0 ;m->_recursions   = 0 ;m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;   // Consider: maintain by type/classmarkOop cmp = (markOop) Atomic::cmpxchg_ptr (markOopDesc::INFLATING(), object->mark_addr(), mark) ;if (cmp != mark) {omRelease (Self, m, true) ;continue ;       // Interference -- just retry}// We've successfully installed INFLATING (0) into the mark-word.// This is the only case where 0 will appear in a mark-work.// Only the singular thread that successfully swings the mark-word// to 0 can perform (or more precisely, complete) inflation.//// Why do we CAS a 0 into the mark-word instead of just CASing the// mark-word from the stack-locked value directly to the new inflated state?// Consider what happens when a thread unlocks a stack-locked object.// It attempts to use CAS to swing the displaced header value from the// on-stack basiclock back into the object header.  Recall also that the// header value (hashcode, etc) can reside in (a) the object header, or// (b) a displaced header associated with the stack-lock, or (c) a displaced// header in an objectMonitor.  The inflate() routine must copy the header// value from the basiclock on the owner's stack to the objectMonitor, all// the while preserving the hashCode stability invariants.  If the owner// decides to release the lock while the value is 0, the unlock will fail// and control will eventually pass from slow_exit() to inflate.  The owner// will then spin, waiting for the 0 value to disappear.   Put another way,// the 0 causes the owner to stall if the owner happens to try to// drop the lock (restoring the header from the basiclock to the object)// while inflation is in-progress.  This protocol avoids races that might// would otherwise permit hashCode values to change or "flicker" for an object.// Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.// 0 serves as a "BUSY" inflate-in-progress indicator.// fetch the displaced mark from the owner's stack.// The owner can't die or unwind past the lock while our INFLATING// object is in the mark.  Furthermore the owner can't complete// an unlock on the object, either.markOop dmw = mark->displaced_mark_helper() ;assert (dmw->is_neutral(), "invariant") ;// Setup monitor fields to proper values -- prepare the monitorm->set_header(dmw) ;// Optimization: if the mark->locker stack address is associated// with this thread we could simply set m->_owner = Self and// m->OwnerIsThread = 1. Note that a thread can inflate an object// that it has stack-locked -- as might happen in wait() -- directly// with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.m->set_owner(mark->locker());m->set_object(object);// TODO-FIXME: assert BasicLock->dhw != 0.// Must preserve store ordering. The monitor state must// be stable at the time of publishing the monitor address.guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;object->release_set_mark(markOopDesc::encode(m));// Hopefully the performance counters are allocated on distinct cache lines// to avoid false sharing on MP systems ...OM_PERFDATA_OP(Inflations, inc());TEVENT(Inflate: overwrite stacklock);if (TraceMonitorInflation) {if (object->is_instance()) {ResourceMark rm;tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",(void *) object, (intptr_t) object->mark(),object->klass()->external_name());}}return m;}// CASE: neutral// TODO-FIXME: for entry we currently inflate and then try to CAS _owner.// If we know we're inflating for entry it's better to inflate by swinging a// pre-locked objectMonitor pointer into the object header.   A successful// CAS inflates the object *and* confers ownership to the inflating thread.// In the current implementation we use a 2-step mechanism where we CAS()// to inflate and then CAS() again to try to swing _owner from NULL to Self.// An inflateTry() method that we could call from fast_enter() and slow_enter()// would be useful.assert (mark->is_neutral(), "invariant");ObjectMonitor * m = omAlloc (Self) ;// prepare m for installation - set monitor to initial statem->Recycle();m->set_header(mark);m->set_owner(NULL);m->set_object(object);m->OwnerIsThread = 1 ;m->_recursions   = 0 ;m->_Responsible  = NULL ;m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ;       // consider: keep metastats by type/classif (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) {m->set_object (NULL) ;m->set_owner  (NULL) ;m->OwnerIsThread = 0 ;m->Recycle() ;omRelease (Self, m, true) ;m = NULL ;continue ;// interference - the markword changed - just retry.// The state-transitions are one-way, so there's no chance of// live-lock -- "Inflated" is an absorbing state.}// Hopefully the performance counters are allocated on distinct// cache lines to avoid false sharing on MP systems ...OM_PERFDATA_OP(Inflations, inc());TEVENT(Inflate: overwrite neutral);if (TraceMonitorInflation) {if (object->is_instance()) {ResourceMark rm;tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",(void *) object, (intptr_t) object->mark(),object->klass()->external_name());}}return m;}
}

3. 释放锁的源码流程

在这里插入图片描述

释放锁的流程相对简单,JVM 执行monitorexit指令的时候会触发interpreterRuntime.cpp#InterpreterRuntime::monitorexit()函数,该函数比较简单,就是调用 ObjectSynchronizer::slow_exit() 函数

IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorexit(JavaThread* thread, BasicObjectLock* elem))
#ifdef ASSERTthread->last_frame().interpreter_frame_verify_monitor(elem);
#endifHandle h_obj(thread, elem->obj());assert(Universe::heap()->is_in_reserved_or_null(h_obj()),"must be NULL or an object");if (elem == NULL || h_obj()->is_unlocked()) {THROW(vmSymbols::java_lang_IllegalMonitorStateException());}ObjectSynchronizer::slow_exit(h_obj(), elem->lock(), thread);// Free entry. This must be done here, since a pending exception might be installed on// exit. If it is not cleared, the exception handling code will try to unlock the monitor again.elem->set_obj(NULL);
#ifdef ASSERTthread->last_frame().interpreter_frame_verify_monitor(elem);
#endif
IRT_END

synchronizer.cpp#ObjectSynchronizer::slow_exit() 函数其实只是一个入口,真正的逻辑在 synchronizer.cpp#ObjectSynchronizer::fast_exit() 函数中,可以看到其处理主要有以下几步:

  1. 首先处理锁重入的逻辑,锁重入不需要做什么
  2. 轻量级锁直接 CAS 替换锁对象的对象头即可,成功则释放锁
  3. 以上条件都不满足,说明是重量级锁或者发生了锁竞争,处理分为两步:
    1. 持有锁的线程首先进行锁膨胀,这部分和上一节锁膨胀流程完全一致
    2. 然后调用重量级锁的 ObjectMonitor::exit() 函数释放锁,这部分可参考 Java synchronized 关键字(3)-JVM 重量级锁 Monitor 的实现
void ObjectSynchronizer::slow_exit(oop object, BasicLock* lock, TRAPS) {fast_exit (object, lock, THREAD) ;
}void ObjectSynchronizer::fast_exit(oop object, BasicLock* lock, TRAPS) {assert(!object->mark()->has_bias_pattern(), "should not see bias pattern here");// if displaced header is null, the previous enter is recursive enter, no-opmarkOop dhw = lock->displaced_header();markOop mark ;if (dhw == NULL) {// Recursive stack-lock.// Diagnostics -- Could be: stack-locked, inflating, inflated.mark = object->mark() ;assert (!mark->is_neutral(), "invariant") ;if (mark->has_locker() && mark != markOopDesc::INFLATING()) {assert(THREAD->is_lock_owned((address)mark->locker()), "invariant") ;}if (mark->has_monitor()) {ObjectMonitor * m = mark->monitor() ;assert(((oop)(m->object()))->mark() == mark, "invariant") ;assert(m->is_entered(THREAD), "invariant") ;}return ;}mark = object->mark() ;// If the object is stack-locked by the current thread, try to// swing the displaced header from the box back to the mark.if (mark == (markOop) lock) {assert (dhw->is_neutral(), "invariant") ;if ((markOop) Atomic::cmpxchg_ptr (dhw, object->mark_addr(), mark) == mark) {TEVENT (fast_exit: release stacklock) ;return;}}ObjectSynchronizer::inflate(THREAD, object)->exit (true, THREAD) ;
}