Lock Record--锁记录

锁记录被用于偏向锁优化和轻量级锁优化,这篇文章只是总结一下相关性质。
基本问题

  • 数据结构及其openjdk实现
  • 什么时候被创建?在哪里创建?
  • 有什么作用?

一、数据结构及其openjdk实现
lock record的数据结构

The lock record holds the original value of the object’s mark word and also contains metadata necessary to identify which object is locked.

lock record的openjdk实现
在openjdk中通过两个类BasicObjectLockBasicLock来实现,数据结构如下:

// A BasicObjectLock associates a specific Java object with a BasicLock.
// It is currently embedded in an interpreter frame.
class BasicObjectLock VALUE_OBJ_CLASS_SPEC {
 private:
  BasicLock _lock;                        // the lock, must be double word aligned
  oop       _obj;                         // object holds the lock;
};
class BasicLock VALUE_OBJ_CLASS_SPEC {
 private:
  volatile markOop _displaced_header;
};

二、什么时候被创建?在哪里创建?
lock record的创建时机

When an object is lightweight locked by a monitorenter bytecode, a lock record is either implicitly or explicitly allocated on the stack of the thread performing the lock acquisition operation.
当字节码解释器执行monitorenter字节码轻度锁住一个对象时,就会在获取锁的线程的栈上显式或者隐式分配一个lock record。

lock record的创建位置

Interpreted frames contain a region which holds the lock records for all monitors owned by the activation. During interpreted method execution this region grows or shrinks depending upon the number of locks held.
lock record在线程的Interpretered Frame上(解释帧)分配

三、有什么作用?

  • 持有displaced word和锁住对象的元数据;
  • 解释器使用lock record来检测非法的锁状态;
  • 隐式地充当锁重入机制的计数器;

参考资料

  1. 《Eliminating Synchronization-Related Atomic Operations with Biased Locking and Bulk Rebiasing》

你可能感兴趣的:(Lock Record--锁记录)