当前位置: 移动技术网 > IT编程>开发语言>Java > 面试题深入解析:Synchronized底层实现

面试题深入解析:Synchronized底层实现

2019年08月06日  | 移动技术网IT编程  | 我要评论

本文为synchronized系列第二篇。主要内容为分析偏向锁的实现。

偏向锁的诞生背景和基本原理在上文中已经讲过了,强烈建议在有看过上篇的基础下阅读本文。

 

本文将分为几块内容:

1.偏向锁的入口

2.偏向锁的获取流程

3.偏向锁的撤销流程

4.偏向锁的释放流程

5.偏向锁的批量重偏向和批量撤销

本文分析的jvm版本是jvm8,具体版本号以及代码可以在看到。

 

偏向锁入口

目前网上的很多文章,关于偏向锁源码入口都找错地方了,导致我之前对于偏向锁的很多逻辑一直想不通,走了很多弯路。

synchronized分为synchronized代码块和synchronized方法,其底层获取锁的逻辑都是一样的,本文讲解的是synchronized代码块的实现。上篇文章也说过,synchronized代码块是由monitorentermonitorexit两个指令实现的。

关于hotspot虚拟机中获取锁的入口,网上很多文章要么给出的方法入口为,要么给出的入口为。包括占小狼的这篇关于锁入口的位置说法也是有问题的(当然文章还是很好的,在我刚开始研究synchronized的时候,小狼哥的这篇文章给了我很多帮助)。

要找锁的入口,肯定是要在源码中找到对monitorenter指令解析的地方。在hotspot的中有两处地方对monitorenter指令进行解析:一个是在 ,另一个是在。

前者是jvm中的字节码解释器(bytecodeinterpreter),用c++实现了每条jvm指令(如monitorenterinvokevirtual等),其优点是实现相对简单且容易理解,缺点是执行慢。后者是模板解释器(templateinterpreter),其对每个指令都写了一段对应的汇编代码,启动时将每个指令与对应汇编代码入口绑定,可以说是效率做到了极致。模板解释器的实现可以看这篇,在研究的过程中也请教过文章作者‘汪先生’一些问题,这里感谢一下。

在hotspot中,只用到了模板解释器,字节码解释器根本就没用到,r大的中说的很清楚了,大家可以看看,这里不再赘述。

所以montorenter的解析入口在模板解释器中,其代码位于。通过调用路径:templatetable_x86_64#monitorenter->interp_masm_x86_64#lock_object进入到偏向锁入口macroassembler_x86#biased_locking_enter,在这里大家可以看到会生成对应的汇编代码。需要注意的是,不是说每次解析monitorenter指令都会调用biased_locking_enter,而是只会在jvm启动的时候调用该方法生成汇编代码,之后对指令的解析是通过直接执行汇编代码。

其实bytecodeinterpreter的逻辑和templateinterpreter的逻辑是大同小异的,因为templateinterpreter中都是汇编代码,比较晦涩,所以看bytecodeinterpreter的实现会便于理解一点。但这里有个坑,在jdk8u之前,bytecodeinterpreter并没有实现偏向锁的逻辑。我之前看的jdk8-87ee5ee27509这个版本就没有实现偏向锁的逻辑,导致我看了很久都没看懂。在这个中对bytecodeinterpreter加入了偏向锁的支持,我大致了看了下和templateinterpreter对比除了不同外,其他逻辑大致相同,所以下文就按bytecodeinterpreter中的代码对偏向锁逻辑进行讲解。templateinterpreter的汇编代码讲解可以看这篇,其实汇编源码中都有英文注释,了解了汇编几个基本指令的作用再结合注释理解起来也不是很难。

 

偏向锁获取流程

下面开始偏向锁获取流程分析,代码在。注意本文代码都有所删减。

case(_monitorenter): {
  // lockee 就是锁对象
  oop lockee = stack_object(-1);
  // derefing's lockee ought to provoke implicit null check
  check_null(lockee);
  // code 1:找到一个空闲的lock record
  basicobjectlock* limit = istate->monitor_base();
  basicobjectlock* most_recent = (basicobjectlock*) istate->stack_base();
  basicobjectlock* entry = null;
  while (most_recent != limit ) {
    if (most_recent->obj() == null) entry = most_recent;
    else if (most_recent->obj() == lockee) break;
    most_recent++;
  }
  //entry不为null,代表还有空闲的lock record
  if (entry != null) {
    // code 2:将lock record的obj指针指向锁对象
    entry->set_obj(lockee);
    int success = false;
    uintptr_t epoch_mask_in_place = (uintptr_t)markoopdesc::epoch_mask_in_place;
	// markoop即对象头的mark word
    markoop mark = lockee->mark();
    intptr_t hash = (intptr_t) markoopdesc::no_hash;
    // code 3:如果锁对象的mark word的状态是偏向模式
    if (mark->has_bias_pattern()) {
      uintptr_t thread_ident;
      uintptr_t anticipated_bias_locking_value;
      thread_ident = (uintptr_t)istate->thread();
     // code 4:这里有几步操作,下文分析
      anticipated_bias_locking_value =
        (((uintptr_t)lockee->klass()->prototype_header() | thread_ident) ^ (uintptr_t)mark) &
        ~((uintptr_t) markoopdesc::age_mask_in_place);
	 // code 5:如果偏向的线程是自己且epoch等于class的epoch
      if  (anticipated_bias_locking_value == 0) {
        // already biased towards this thread, nothing to do
        if (printbiasedlockingstatistics) {
          (* biasedlocking::biased_lock_entry_count_addr())++;
        }
        success = true;
      }
       // code 6:如果偏向模式关闭,则尝试撤销偏向锁
      else if ((anticipated_bias_locking_value & markoopdesc::biased_lock_mask_in_place) != 0) {
        markoop header = lockee->klass()->prototype_header();
        if (hash != markoopdesc::no_hash) {
          header = header->copy_set_hash(hash);
        }
        // 利用cas操作将mark word替换为class中的mark word
        if (atomic::cmpxchg_ptr(header, lockee->mark_addr(), mark) == mark) {
          if (printbiasedlockingstatistics)
            (*biasedlocking::revoked_lock_entry_count_addr())++;
        }
      }
         // code 7:如果epoch不等于class中的epoch,则尝试重偏向
      else if ((anticipated_bias_locking_value & epoch_mask_in_place) !=0) {
        // 构造一个偏向当前线程的mark word
        markoop new_header = (markoop) ( (intptr_t) lockee->klass()->prototype_header() | thread_ident);
        if (hash != markoopdesc::no_hash) {
          new_header = new_header->copy_set_hash(hash);
        }
        // cas替换对象头的mark word  
        if (atomic::cmpxchg_ptr((void*)new_header, lockee->mark_addr(), mark) == mark) {
          if (printbiasedlockingstatistics)
            (* biasedlocking::rebiased_lock_entry_count_addr())++;
        }
        else {
          // 重偏向失败,代表存在多线程竞争,则调用monitorenter方法进行锁升级
          call_vm(interpreterruntime::monitorenter(thread, entry), handle_exception);
        }
        success = true;
      }
      else {
         // 走到这里说明当前要么偏向别的线程,要么是匿名偏向(即没有偏向任何线程)
       	// code 8:下面构建一个匿名偏向的mark word,尝试用cas指令替换掉锁对象的mark word
        markoop header = (markoop) ((uintptr_t) mark & ((uintptr_t)markoopdesc::biased_lock_mask_in_place |(uintptr_t)markoopdesc::age_mask_in_place |epoch_mask_in_place));
        if (hash != markoopdesc::no_hash) {
          header = header->copy_set_hash(hash);
        }
        markoop new_header = (markoop) ((uintptr_t) header | thread_ident);
        // debugging hint
        debug_only(entry->lock()->set_displaced_header((markoop) (uintptr_t) 0xdeaddead);)
        if (atomic::cmpxchg_ptr((void*)new_header, lockee->mark_addr(), header) == header) {
           // cas修改成功
          if (printbiasedlockingstatistics)
            (* biasedlocking::anonymously_biased_lock_entry_count_addr())++;
        }
        else {
          // 如果修改失败说明存在多线程竞争,所以进入monitorenter方法
          call_vm(interpreterruntime::monitorenter(thread, entry), handle_exception);
        }
        success = true;
      }
    }

    // 如果偏向线程不是当前线程或没有开启偏向模式等原因都会导致success==false
    if (!success) {
      // 轻量级锁的逻辑
      //code 9: 构造一个无锁状态的displaced mark word,并将lock record的lock指向它
      markoop displaced = lockee->mark()->set_unlocked();
      entry->lock()->set_displaced_header(displaced);
      //如果指定了-xx:+useheavymonitors,则call_vm=true,代表禁用偏向锁和轻量级锁
      bool call_vm = useheavymonitors;
      // 利用cas将对象头的mark word替换为指向lock record的指针
      if (call_vm || atomic::cmpxchg_ptr(entry, lockee->mark_addr(), displaced) != displaced) {
        // 判断是不是锁重入
        if (!call_vm && thread->is_lock_owned((address) displaced->clear_lock_bits())) {		//code 10: 如果是锁重入,则直接将displaced mark word设置为null
          entry->lock()->set_displaced_header(null);
        } else {
          call_vm(interpreterruntime::monitorenter(thread, entry), handle_exception);
        }
      }
    }
    update_pc_and_tos_and_continue(1, -1);
  } else {
    // lock record不够,重新执行
    istate->set_msg(more_monitors);
    update_pc_and_return(0); // re-execute
  }
}

再回顾下对象头中mark word的格式:《死磕synchronized底层实现--偏向锁》

jvm中的每个类也有一个类似mark word的prototype_header,用来标记该class的epoch和偏向开关等信息。上面的代码中lockee->klass()->prototype_header()即获取class的prototype_header。

code 1,从当前线程的栈中找到一个空闲的lock record(即代码中的basicobjectlock,下文都用lock record代指),判断lock record是否空闲的依据是其obj字段 是否为null。注意这里是按内存地址从低往高找到最后一个可用的lock record,换而言之,就是找到内存地址最高的可用lock record

 

code 2,获取到lock record后,首先要做的就是为其obj字段赋值。

 

code 3,判断锁对象的mark word是否是偏向模式,即低3位是否为101。

 

code 4,这里有几步位运算的操作 anticipated_bias_locking_value = (((uintptr_t)lockee->klass()->prototype_header() | thread_ident) ^ (uintptr_t)mark) & ​ ~((uintptr_t) markoopdesc::age_mask_in_place); 这个位运算可以分为3个部分。

 

第一部分((uintptr_t)lockee->klass()->prototype_header() | thread_ident) 将当前线程id和类的prototype_header相或,这样得到的值为(当前线程id + prototype_header中的(epoch + 分代年龄 + 偏向锁标志 + 锁标志位)),注意prototype_header的分代年龄那4个字节为0

 

第二部分 ^ (uintptr_t)mark 将上面计算得到的结果与锁对象的markoop进行异或,相等的位全部被置为0,只剩下不相等的位。

 

第三部分 & ~((uintptr_t) markoopdesc::age_mask_in_place) markoopdesc::age_mask_in_place为…0001111000,取反后,变成了…1110000111,除了分代年龄那4位,其他位全为1;将取反后的结果再与上面的结果相与,将上面异或得到的结果中分代年龄给忽略掉。

 

code 5anticipated_bias_locking_value==0代表偏向的线程是当前线程且mark word的epoch等于class的epoch,这种情况下什么都不用做。

 

code 6(anticipated_bias_locking_value & markoopdesc::biased_lock_mask_in_place) != 0代表class的prototype_header或对象的mark word中偏向模式是关闭的,又因为能走到这已经通过了mark->has_bias_pattern()判断,即对象的mark word中偏向模式是开启的,那也就是说class的prototype_header不是偏向模式。

 

然后利用cas指令atomic::cmpxchg_ptr(header, lockee->mark_addr(), mark) == mark撤销偏向锁,我们知道cas会有几个参数,1是预期的原值,2是预期修改后的值 ,3是要修改的对象,与之对应,cmpxchg_ptr方法第一个参数是预期修改后的值,第2个参数是修改的对象,第3个参数是预期原值,方法返回实际原值,如果等于预期原值则说明修改成功。

 

code 7,如果epoch已过期,则需要重偏向,利用cas指令将锁对象的mark word替换为一个偏向当前线程且epoch为类的epoch的新的mark word

 

code 8,cas将偏向线程改为当前线程,如果当前是匿名偏向则能修改成功,否则进入锁升级的逻辑。

 

code 9,这一步已经是轻量级锁的逻辑了。从上图的mark word的格式可以看到,轻量级锁中mark word存的是指向lock record的指针。这里构造一个无锁状态的mark word,然后存储到lock recordlock record的格式可以看第一篇文章)。设置mark word是无锁状态的原因是:轻量级锁解锁时是将对象头的mark word设置为lock record中的displaced mark word,所以创建时设置为无锁状态,解锁时直接用cas替换就好了。

 

code 10, 如果是锁重入,则将lock recorddisplaced mark word设置为null,起到一个锁重入计数的作用。

 

以上是偏向锁加锁的流程(包括部分轻量级锁的加锁流程),如果当前锁已偏向其他线程||epoch值过期||偏向模式关闭||获取偏向锁的过程中存在并发冲突,都会进入到interpreterruntime::monitorenter方法, 在该方法中会对偏向锁撤销和升级。

 

偏向锁的撤销

这里说的撤销是指在获取偏向锁的过程因为不满足条件导致要将锁对象改为非偏向锁状态;释放是指退出同步块时的过程,释放锁的逻辑会在下一小节阐述。请读者注意本文中撤销与释放的区别。

如果获取偏向锁失败会进入到方法

irt_entry_no_async(void, interpreterruntime::monitorenter(javathread* thread, basicobjectlock* elem))
  ...
  handle h_obj(thread, elem->obj());
  assert(universe::heap()->is_in_reserved_or_null(h_obj()),
         "must be null or an object");
  if (usebiasedlocking) {
    // retry fast entry if bias is revoked to avoid unnecessary inflation
    objectsynchronizer::fast_enter(h_obj, elem->lock(), true, check);
  } else {
    objectsynchronizer::slow_enter(h_obj, elem->lock(), check);
  }
  ...
irt_end

可以看到如果开启了jvm偏向锁,那会进入到objectsynchronizer::fast_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) ;
}

如果是正常的java线程,会走上面的逻辑进入到biasedlocking::revoke_and_rebias方法,如果是vm线程则会走到下面的biasedlocking::revoke_at_safepoint。我们主要看biasedlocking::revoke_and_rebias方法。这个方法的主要作用像它的方法名:撤销或者重偏向,第一个参数封装了锁对象和当前线程,第二个参数代表是否允许重偏向,这里是true。

biasedlocking::condition biasedlocking::revoke_and_rebias(handle obj, bool attempt_rebias, traps) {
  assert(!safepointsynchronize::is_at_safepoint(), "must not be called while at safepoint");
    
  markoop mark = obj->mark();
  if (mark->is_biased_anonymously() && !attempt_rebias) {
     //如果是匿名偏向且attempt_rebias==false会走到这里,如锁对象的hashcode方法被调用会出现这种情况,需要撤销偏向锁。
    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();
    //code 1: 如果对应class关闭了偏向模式
    if (!prototype_header->has_bias_pattern()) {
      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;
    //code2: 如果epoch过期
    } else if (prototype_header->bias_epoch() != mark->bias_epoch()) {
      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;
        }
      }
    }
  }
  //code 3:批量重偏向与批量撤销的逻辑
  heuristicsresult heuristics = update_heuristics(obj(), attempt_rebias);
  if (heuristics == hr_not_biased) {
    return not_biased;
  } else if (heuristics == hr_single_revoke) {
    //code 4:撤销单个线程
    klass *k = obj->klass();
    markoop prototype_header = k->prototype_header();
    if (mark->biased_locker() == thread &&
        prototype_header->bias_epoch() == mark->bias_epoch()) {
      // 走到这里说明需要撤销的是偏向当前线程的锁,当调用object#hashcode方法时会走到这一步
      // 因为只要遍历当前线程的栈就好了,所以不需要等到safepoint再撤销。
      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线程中的safepoint调用revoke_bias方法
      vm_revokebias revoke(&obj, (javathread*) thread);
      vmthread::execute(&revoke);
      return revoke.status_code();
    }
  }
	
  assert((heuristics == hr_bulk_revoke) ||
         (heuristics == hr_bulk_rebias), "?");
   //code5:批量撤销、批量重偏向的逻辑
  vm_bulkrevokebias bulk_revoke(&obj, (javathread*) thread,
                                (heuristics == hr_bulk_rebias),
                                attempt_rebias);
  vmthread::execute(&bulk_revoke);
  return bulk_revoke.status_code();
}

会走到该方法的逻辑有很多,我们只分析最常见的情况:假设锁已经偏向线程a,这时b线程尝试获得锁。

上面的code 1code 2b线程都不会走到,最终会走到code 4处,如果要撤销的锁偏向的是当前线程则直接调用revoke_bias撤销偏向锁,否则会将该操作push到vm thread中等到safepoint的时候再执行。

关于vm thread这里介绍下:在jvm中有个专门的vm thread,该线程会源源不断的从vmoperationqueue中取出请求,比如gc请求。对于需要safepoint的操作(vm_operationevaluate_at_safepoint返回true)必须要等到所有的java线程进入到safepoint才开始执行。

接下来我们着重分析下revoke_bias方法。第一个参数为锁对象,第2、3个参数为都为false

static biasedlocking::condition revoke_bias(oop obj, bool allow_rebias, bool is_bulk, javathread* requesting_thread) {
  markoop mark = obj->mark();
  // 如果没有开启偏向模式,则直接返回not_biased
  if (!mark->has_bias_pattern()) {
    ...
    return biasedlocking::not_biased;
  }

  uint age = mark->age();
  // 构建两个mark word,一个是匿名偏向模式(101),一个是无锁模式(001)
  markoop   biased_prototype = markoopdesc::biased_locking_prototype()->set_age(age);
  markoop unbiased_prototype = markoopdesc::prototype()->set_age(age);

  ...

  javathread* biased_thread = mark->biased_locker();
  if (biased_thread == null) {
     // 匿名偏向。当调用锁对象的hashcode()方法可能会导致走到这个逻辑
     // 如果不允许重偏向,则将对象的mark word设置为无锁模式
    if (!allow_rebias) {
      obj->set_mark(unbiased_prototype);
    }
    ...
    return biasedlocking::bias_revoked;
  }

  // code 1:判断偏向线程是否还存活
  bool thread_is_alive = false;
  // 如果当前线程就是偏向线程 
  if (requesting_thread == biased_thread) {
    thread_is_alive = true;
  } else {
     // 遍历当前jvm的所有线程,如果能找到,则说明偏向的线程还存活
    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) {
    // 允许重偏向则将对象mark word设置为匿名偏向状态,否则设置为无锁状态
    if (allow_rebias) {
      obj->set_mark(biased_prototype);
    } else {
      obj->set_mark(unbiased_prototype);
    }
    ...
    return biasedlocking::bias_revoked;
  }

  // 线程还存活则遍历线程栈中所有的lock record
  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);
    // 如果能找到对应的lock record说明偏向的线程还在执行同步代码块中的代码
    if (mon_info->owner() == obj) {
      ...
      // 需要升级为轻量级锁,直接修改偏向线程栈中的lock record。为了处理锁重入的case,在这里将lock record的displaced mark word设置为null,第一个lock record会在下面的代码中再处理
      markoop mark = markoopdesc::encode((basiclock*) null);
      highest_lock = mon_info->lock();
      highest_lock->set_displaced_header(mark);
    } else {
      ...
    }
  }
  if (highest_lock != null) {
    // 修改第一个lock record为无锁状态,然后将obj的mark word设置为指向该lock record的指针
    highest_lock->set_displaced_header(unbiased_prototype);
    obj->release_set_mark(markoopdesc::encode(highest_lock));
    ...
  } else {
    // 走到这里说明偏向线程已经不在同步块中了
    ...
    if (allow_rebias) {
       //设置为匿名偏向状态
      obj->set_mark(biased_prototype);
    } else {
      // 将mark word设置为无锁状态
      obj->set_mark(unbiased_prototype);
    }
  }

  return biasedlocking::bias_revoked;
}

需要注意下,当调用锁对象的object#hashsystem.identityhashcode()方法会导致该对象的偏向锁或轻量级锁升级。这是因为在java中一个对象的hashcode是在调用这两个方法时才生成的,如果是无锁状态则存放在mark word中,如果是重量级锁则存放在对应的monitor中,而偏向锁是没有地方能存放该信息的,所以必须升级。

言归正传,revoke_bias方法逻辑:

  1. 查看偏向的线程是否存活,如果已经不存活了,则直接撤销偏向锁。jvm维护了一个集合存放所有存活的线程,通过遍历该集合判断某个线程是否存活。
  2. 偏向的线程是否还在同步块中,如果不在了,则撤销偏向锁。我们回顾一下偏向锁的加锁流程:每次进入同步块(即执行monitorenter)的时候都会以从高往低的顺序在栈中找到第一个可用的lock record,将其obj字段指向锁对象。每次解锁(即执行monitorexit)的时候都会将最低的一个相关lock record移除掉。所以可以通过遍历线程栈中的lock record来判断线程是否还在同步块中。
  3. 将偏向线程所有相关lock recorddisplaced mark word设置为null,然后将最高位的lock recorddisplaced mark word 设置为无锁状态,最高位的lock record也就是第一次获得锁时的lock record(这里的第一次是指重入获取锁时的第一次),然后将对象头指向最高位的lock record,这里不需要用cas指令,因为是在safepoint。 执行完后,就升级成了轻量级锁。原偏向线程的所有lock record都已经变成轻量级锁的状态。这里如果看不明白,请回顾上篇文章的轻量级锁加锁过程。

 

偏向锁的释放

偏向锁的释放入口在

case(_monitorexit): {
  oop lockee = stack_object(-1);
  check_null(lockee);
  // derefing's lockee ought to provoke implicit null check
  // find our monitor slot
  basicobjectlock* limit = istate->monitor_base();
  basicobjectlock* most_recent = (basicobjectlock*) istate->stack_base();
  // 从低往高遍历栈的lock record
  while (most_recent != limit ) {
    // 如果lock record关联的是该锁对象
    if ((most_recent)->obj() == lockee) {
      basiclock* lock = most_recent->lock();
      markoop header = lock->displaced_header();
      // 释放lock record
      most_recent->set_obj(null);
      // 如果是偏向模式,仅仅释放lock record就好了。否则要走轻量级锁or重量级锁的释放流程
      if (!lockee->mark()->has_bias_pattern()) {
        bool call_vm = useheavymonitors;
        // header!=null说明不是重入,则需要将displaced mark word cas到对象头的mark word
        if (header != null || call_vm) {
          if (call_vm || atomic::cmpxchg_ptr(header, lockee->mark_addr(), lock) != lock) {
            // cas失败或者是重量级锁则会走到这里,先将obj还原,然后调用monitorexit方法
            most_recent->set_obj(lockee);
            call_vm(interpreterruntime::monitorexit(thread, most_recent), handle_exception);
          }
        }
      }
      //执行下一条命令
      update_pc_and_tos_and_continue(1, -1);
    }
    //处理下一条lock record
    most_recent++;
  }
  // need to throw illegal monitor state exception
  call_vm(interpreterruntime::throw_illegal_monitor_state_exception(thread), handle_exception);
  shouldnotreachhere();
}

上面的代码结合注释理解起来应该不难,偏向锁的释放很简单,只要将对应lock record释放就好了,而轻量级锁则需要将displaced mark word替换到对象头的mark word中。如果cas失败或者是重量级锁则进入到interpreterruntime::monitorexit方法中。该方法会在轻量级与重量级锁的文章中讲解。

 

批量重偏向和批量撤销

批量重偏向和批量撤销的背景可以看上篇文章,相关实现在biasedlocking::revoke_and_rebias中:

biasedlocking::condition biasedlocking::revoke_and_rebias(handle obj, bool attempt_rebias, traps) {
  ...
  //code 1:重偏向的逻辑
  heuristicsresult heuristics = update_heuristics(obj(), attempt_rebias);
  // 非重偏向的逻辑
  ...
      
  assert((heuristics == hr_bulk_revoke) ||
         (heuristics == hr_bulk_rebias), "?");	
   //code 2:批量撤销、批量重偏向的逻辑
  vm_bulkrevokebias bulk_revoke(&obj, (javathread*) thread,
                                (heuristics == hr_bulk_rebias),
                                attempt_rebias);
  vmthread::execute(&bulk_revoke);
  return bulk_revoke.status_code();
}

在每次撤销偏向锁的时候都通过update_heuristics方法记录下来,以类为单位,当某个类的对象撤销偏向次数达到一定阈值的时候jvm就认为该类不适合偏向模式或者需要重新偏向另一个对象,update_heuristics就会返回hr_bulk_revokehr_bulk_rebias。进行批量撤销或批量重偏向。

先看update_heuristics方法。

static heuristicsresult update_heuristics(oop o, bool allow_rebias) {
  markoop mark = o->mark();
  //如果不是偏向模式直接返回
  if (!mark->has_bias_pattern()) {
    return hr_not_biased;
  }
 
  // 锁对象的类
  klass* k = o->klass();
  // 当前时间
  jlong cur_time = os::javatimemillis();
  // 该类上一次批量撤销的时间
  jlong last_bulk_revocation_time = k->last_biased_lock_bulk_revocation_time();
  // 该类偏向锁撤销的次数
  int revocation_count = k->biased_lock_revocation_count();
  // biasedlockingbulkrebiasthreshold是重偏向阈值(默认20),biasedlockingbulkrevokethreshold是批量撤销阈值(默认40),biasedlockingdecaytime是开启一次新的批量重偏向距离上次批量重偏向的后的延迟时间,默认25000。也就是开启批量重偏向后,经过了一段较长的时间(>=biasedlockingdecaytime),撤销计数器才超过阈值,那我们会重置计数器。
  if ((revocation_count >= biasedlockingbulkrebiasthreshold) &&
      (revocation_count <  biasedlockingbulkrevokethreshold) &&
      (last_bulk_revocation_time != 0) &&
      (cur_time - last_bulk_revocation_time >= biasedlockingdecaytime)) {
    // this is the first revocation we've seen in a while of an
    // object of this type since the last time we performed a bulk
    // rebiasing operation. the application is allocating objects in
    // bulk which are biased toward a thread and then handing them
    // off to another thread. we can cope with this allocation
    // pattern via the bulk rebiasing mechanism so we reset the
    // klass's revocation count rather than allow it to increase
    // monotonically. if we see the need to perform another bulk
    // rebias operation later, we will, and if subsequently we see
    // many more revocation operations in a short period of time we
    // will completely disable biasing for this type.
    k->set_biased_lock_revocation_count(0);
    revocation_count = 0;
  }

  // 自增撤销计数器
  if (revocation_count <= biasedlockingbulkrevokethreshold) {
    revocation_count = k->atomic_incr_biased_lock_revocation_count();
  }
  // 如果达到批量撤销阈值则返回hr_bulk_revoke
  if (revocation_count == biasedlockingbulkrevokethreshold) {
    return hr_bulk_revoke;
  }
  // 如果达到批量重偏向阈值则返回hr_bulk_rebias
  if (revocation_count == biasedlockingbulkrebiasthreshold) {
    return hr_bulk_rebias;
  }
  // 没有达到阈值则撤销单个对象的锁
  return hr_single_revoke;
}

当达到阈值的时候就会通过vm 线程在safepoint调用bulk_revoke_or_rebias_at_safepoint, 参数bulk_rebias如果是true代表是批量重偏向否则为批量撤销。attempt_rebias_of_object代表对操作的锁对象o是否运行重偏向,这里是true

static biasedlocking::condition bulk_revoke_or_rebias_at_safepoint(oop o,
                                                                   bool bulk_rebias,
                                                                   bool attempt_rebias_of_object,
                                                                   javathread* requesting_thread) {
  ...
  jlong cur_time = os::javatimemillis();
  o->klass()->set_last_biased_lock_bulk_revocation_time(cur_time);


  klass* k_o = o->klass();
  klass* klass = k_o;

  if (bulk_rebias) {
    // 批量重偏向的逻辑
    if (klass->prototype_header()->has_bias_pattern()) {
      // 自增前类中的的epoch
      int prev_epoch = klass->prototype_header()->bias_epoch();
      // code 1:类中的epoch自增
      klass->set_prototype_header(klass->prototype_header()->incr_bias_epoch());
      int cur_epoch = klass->prototype_header()->bias_epoch();

      // code 2:遍历所有线程的栈,更新类型为该klass的所有锁实例的epoch
      for (javathread* thr = threads::first(); thr != null; thr = thr->next()) {
        growablearray<monitorinfo*>* cached_monitor_info = get_or_compute_monitor_info(thr);
        for (int i = 0; i < cached_monitor_info->length(); i++) {
          monitorinfo* mon_info = cached_monitor_info->at(i);
          oop owner = mon_info->owner();
          markoop mark = owner->mark();
          if ((owner->klass() == k_o) && mark->has_bias_pattern()) {
            // we might have encountered this object already in the case of recursive locking
            assert(mark->bias_epoch() == prev_epoch || mark->bias_epoch() == cur_epoch, "error in bias epoch adjustment");
            owner->set_mark(mark->set_bias_epoch(cur_epoch));
          }
        }
      }
    }

    // 接下来对当前锁对象进行重偏向
    revoke_bias(o, attempt_rebias_of_object && klass->prototype_header()->has_bias_pattern(), true, requesting_thread);
  } else {
    ...

    // code 3:批量撤销的逻辑,将类中的偏向标记关闭,markoopdesc::prototype()返回的是一个关闭偏向模式的prototype
    klass->set_prototype_header(markoopdesc::prototype());

    // code 4:遍历所有线程的栈,撤销该类所有锁的偏向
    for (javathread* thr = threads::first(); thr != null; thr = thr->next()) {
      growablearray<monitorinfo*>* cached_monitor_info = get_or_compute_monitor_info(thr);
      for (int i = 0; i < cached_monitor_info->length(); i++) {
        monitorinfo* mon_info = cached_monitor_info->at(i);
        oop owner = mon_info->owner();
        markoop mark = owner->mark();
        if ((owner->klass() == k_o) && mark->has_bias_pattern()) {
          revoke_bias(owner, false, true, requesting_thread);
        }
      }
    }

    // 撤销当前锁对象的偏向模式
    revoke_bias(o, false, true, requesting_thread);
  }

  ...
  
  biasedlocking::condition status_code = biasedlocking::bias_revoked;

  if (attempt_rebias_of_object &&
      o->mark()->has_bias_pattern() &&
      klass->prototype_header()->has_bias_pattern()) {
    // 构造一个偏向请求线程的mark word
    markoop new_mark = markoopdesc::encode(requesting_thread, o->mark()->age(),
                                           klass->prototype_header()->bias_epoch());
    // 更新当前锁对象的mark word
    o->set_mark(new_mark);
    status_code = biasedlocking::bias_revoked_and_rebiased;
    ...
  }

  ...

  return status_code;
}

该方法分为两个逻辑:批量重偏向和批量撤销。

先看批量重偏向,分为两步:

code 1 将类中的撤销计数器自增1,之后当该类已存在的实例获得锁时,就会尝试重偏向,相关逻辑在偏向锁获取流程小节中。

code 2 处理当前正在被使用的锁对象,通过遍历所有存活线程的栈,找到所有正在使用的偏向锁对象,然后更新它们的epoch值。也就是说不会重偏向正在使用的锁,否则会破坏锁的线程安全性。

批量撤销逻辑如下:

code 3将类的偏向标记关闭,之后当该类已存在的实例获得锁时,就会升级为轻量级锁;该类新分配的对象的mark word则是无锁模式。

code 4处理当前正在被使用的锁对象,通过遍历所有存活线程的栈,找到所有正在使用的偏向锁对象,然后撤销偏向锁。

 

免费java高级资料需要自己领取,涵盖了java、redis、mongodb、mysql、zookeeper、spring cloud、dubbo高并发分布式等教程,一共30g。                  
传送门:https://mp.weixin.qq.com/s/jzddfh-7ynudmkjt0irl8q

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网