转载

JVM 的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中通过两个类BasicObjectLock和BasicLock来实现,数据结构如下:

// 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来检测非法的锁状态;
隐式地充当锁重入机制的计数器;
参考资料

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

正文到此结束
本文目录