coderplay
5/16/2012 - 9:58 AM

Memory Barrier

Memory Barrier

class X {
  int a, b;
  volatile int v, u;
  void f() {
    int i, j;
   
    i = a;
    j = b;
    i = v;
   
    j = u;
   
    a = i;
    b = j;
   
    v = i;
   
    u = j;
   
    i = u;
   
   
    j = b;
    a = i;
  }

  public static void main(String[] args) throws Exception {
        Thread.sleep(5000);
	X x = new X();
	for(int i = 0; i < 50000; i++) {
		x.f();
	}
  }
}
$ java -XX:+UnlockDiagnosticVMOptions -XX:PrintAssemblyOptions=hsdis-print-bytes -XX:CompileCommand=print,X.f X
CompilerOracle: print X.f
Java HotSpot(TM) Server VM warning: printing of assembly code is enabled; turning on DebugNonSafepoints to gain additional output
Compiled (c2)    5066   1      nmethod X::f (56 bytes)
 total in heap  [0xb3a1e188,0xb3a1e394] = 524
 relocation     [0xb3a1e254,0xb3a1e264] = 16
 main code      [0xb3a1e280,0xb3a1e2c0] = 64
 stub code      [0xb3a1e2c0,0xb3a1e2d0] = 16
 oops           [0xb3a1e2d0,0xb3a1e2d4] = 4
 scopes data    [0xb3a1e2d4,0xb3a1e300] = 44
 scopes pcs     [0xb3a1e300,0xb3a1e390] = 144
 dependencies   [0xb3a1e390,0xb3a1e394] = 4
Loaded disassembler from hsdis-i386.so
Decoding compiled method 0xb3a1e188:
Code:
[Disassembling for mach='i386']
[Entry Point]
[Constants]
  # {method} 'f' '()V' in 'X'
  #           [sp+0x10]  (sp of caller)
  0xb3a1e280: cmp    0x4(%ecx),%eax     ;...3b4104
  0xb3a1e283: jne    0xb39ffea0         ;...0f85171c feff
                                        ;   {runtime_call}
  0xb3a1e289: xchg   %ax,%ax            ;...666690
[Verified Entry Point]
  0xb3a1e28c: push   %ebp               ;...55
  0xb3a1e28d: sub    $0x8,%esp          ;...81ec0800 0000
                                        ;*synchronization entry
                                        ; - X::f@-1 (line 7)
  0xb3a1e293: mov    0x10(%ecx),%ebx    ;...8b5910
                                        ;*getfield v
                                        ; - X::f@11 (line 9)
  0xb3a1e296: mov    0x14(%ecx),%edi    ;...8b7914
                                        ;*getfield u
                                        ; - X::f@16 (line 11)
  0xb3a1e299: mov    %ebx,0x8(%ecx)     ;...895908
                                        ;*putfield a
                                        ; - X::f@22 (line 13)
  0xb3a1e29c: mov    %edi,0xc(%ecx)     ;...89790c
  0xb3a1e29f: mov    %ebx,0x10(%ecx)    ;...895910
  0xb3a1e2a2: mov    %edi,0x14(%ecx)    ;...897914
  0xb3a1e2a5: lock addl $0x0,(%esp)     ;...f0830424 00
                                        ;*putfield u
                                        ; - X::f@37 (line 18)
  0xb3a1e2aa: mov    0x14(%ecx),%ebp    ;...8b6914
                                        ;*getfield u
                                        ; - X::f@41 (line 20)
  0xb3a1e2ad: mov    %ebp,0x8(%ecx)     ;...896908
                                        ;*putfield v
                                        ; - X::f@32 (line 16)
  0xb3a1e2b0: add    $0x8,%esp          ;...83c408
  0xb3a1e2b3: pop    %ebp               ;...5d
  0xb3a1e2b4: test   %eax,0xb78ab000    ;...850500b0 8ab7
                                        ;   {poll_return}
  0xb3a1e2ba: ret                       ;...c3
  0xb3a1e2bb: hlt                       ;...f4
  0xb3a1e2bc: hlt                       ;...f4
  0xb3a1e2bd: hlt                       ;...f4
  0xb3a1e2be: hlt                       ;...f4
  0xb3a1e2bf: hlt                       ;...f4
[Exception Handler]
[Stub Code]
  0xb3a1e2c0: jmp    0xb3a1aba0         ;...e9dbc8ff ff
                                        ;   {no_reloc}
[Deopt Handler Code]
  0xb3a1e2c5: push   $0xb3a1e2c5        ;...68c5e2a1 b3
                                        ;   {section_word}
  0xb3a1e2ca: jmp    0xb3a00be0         ;...e91129fe ff
                                        ;   {runtime_call}
  0xb3a1e2cf: .byte 0x0                 ;...00
OopMapSet contains 0 OopMaps