HSDIS对volatile的浅析

HSDIS有什么用?用来看汇编,有用么? 答案是有用的。

举个最简单的例子,volatile,是java关键字,那在汇编层面是如何做的呢?

示例代码:

import java.util.*;

public class VolatileTest {  
    public volatile long value = 0;  
    
    public static void main(String[] args) {  

        VolatileTest t = new VolatileTest();

        t.value = 3;
  
        System.out.println("value is: " + t.value);  
 
    }  
}  


编译
root@ubuntu:~/Downloads# javac VolatileTest.java 


运行
root@ubuntu:~/Downloads# java -Xcomp  -server -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:+DebugNonSafepoints VolatileTest > disass.log 


查看disass.log,寻找相关的汇编代码:

 
0x00007f8f9446d688: lock addl $0x0,(%rsp)     ;*putfield value
                                                ; - VolatileTest::<init>@6 (line 4)


结合putfield value 和 bytecode, 可以知道这条指令就是 对value的赋值操作。

看见了什么? lock


补充:


关于lock,可以参考64-ia-32-architectures-software-developer-manual

截取2段:

Intel 64 and IA-32 processors provide a LOCK# signal that is asserted automatically during certain critical memory operations to lock the system bus or equivalent link. While this output signal is asserted, requests from other processors or bus agents for control of the bus are blocked. 


To explicitly force the LOCK semantics, software can use the LOCK prefix with the following instructions when they are used to modify a memory location.

你可能感兴趣的:(HSDIS对volatile的浅析)