鎖記錄被用于偏向鎖優(yōu)化和輕量級鎖優(yōu)化撤蟆,這篇文章只是總結(jié)一下相關(guān)性質(zhì)奕塑。
基本問題
- 數(shù)據(jù)結(jié)構(gòu)及其openjdk實(shí)現(xiàn)
- 什么時候被創(chuàng)建?在哪里創(chuàng)建家肯?
- 有什么作用龄砰?
一、數(shù)據(jù)結(jié)構(gòu)及其openjdk實(shí)現(xiàn)
lock record的數(shù)據(jù)結(jié)構(gòu)
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實(shí)現(xiàn)
在openjdk中通過兩個類BasicObjectLock
和BasicLock
來實(shí)現(xiàn)讨衣,數(shù)據(jù)結(jié)構(gòu)如下:
// 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;
};
二换棚、什么時候被創(chuàng)建?在哪里創(chuàng)建反镇?
lock record的創(chuàng)建時機(jī)
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.
當(dāng)字節(jié)碼解釋器執(zhí)行monitorenter字節(jié)碼輕度鎖住一個對象時固蚤,就會在獲取鎖的線程的棧上顯式或者隱式分配一個lock record。
lock record的創(chuàng)建位置
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和鎖住對象的元數(shù)據(jù);
- 解釋器使用lock record來檢測非法的鎖狀態(tài)辆亏;
- 隱式地充當(dāng)鎖重入機(jī)制的計(jì)數(shù)器风秤;
參考資料
- 《Eliminating Synchronization-Related Atomic Operations with Biased Locking and Bulk Rebiasing》