java에서volatile와lock 원리 분석

18297 단어 javavolatilelock
java에서volatile와lock 원리 분석
volatile와 lock은 자바에서 스레드 협동 동기화에 사용되는 두 가지 메커니즘이다.
Volatile
volatile은 Java의 키워드로 그 역할은
  • 변수의 가시성을 확보한다
  • 순서 재정리 방지
  • 64비트 변수(long,double)의 원자성 읽기와 쓰기 보장
  • volatile이 자바 언어 규범에 규정한 것은
    
    The Java programming language allows threads to access shared variables (§17.1). As a rule, to ensure 
    that shared variables are consistently and reliably updated, a thread should ensure that it
     has exclusive use of such variables by obtaining a lock that, conventionally, enforces mutual 
    exclusion for those shared variables.
    The Java programming language provides a second mechanism, volatile fields, that is more convenient 
    than locking for some purposes.
    A field may be declared volatile, in which case the Java Memory Model ensures that all threads 
    see a consistent value for the variable .
    It is a compile-time error if a final variable is also declared volatile.
    
    
    자바 메모리 모델에서volatile의happen-before 효과가 규정되어 있으며,volatile 변수에 대한 쓰기 작업happen-before는 후속적으로 읽습니다.이렇게 하면volatile 변수는 하나의 라인의 수정이 다른 라인에 보일 수 있도록 확보할 수 있다.volatile는 원자성을 보장할 수 없기 때문에 제약이나 검사 조건이 있는 장면에서 사용할 수 없다. 예를 들어 i++, 자주 사용하는 장면은 stop 변수 보증 시스템이 다른 라인에 대한 표시를 멈추고 더블-checklock 단례에서 순서를 바꾸어 안전한 발표를 보장하는 등이다.
    아래 코드를 예로 들다
    
    public class TestVolatile {
      private static volatile boolean stop = false;
      public static void main(String[] args) {
        stop = true;
        boolean b = stop;
      }
    }
    
    stop 필드가volatile 형식으로 선언된 후, 컴파일된 바이트 코드의 변수에 대한access_flag 중 ACC_VOLATILE 위치는 1입니다.
    중요한 바이트 내용은 다음과 같다.
    
     public static void main(java.lang.String[]);
      descriptor: ([Ljava/lang/String;)V
      flags: ACC_PUBLIC, ACC_STATIC
      Code:
       stack=1, locals=2, args_size=1
         0: iconst_1
         1: putstatic   #2         // Field stop:Z
         4: getstatic   #2         // Field stop:Z
         7: istore_1
         8: return
       LineNumberTable:
        line 14: 0
        line 15: 4
        line 16: 8
       LocalVariableTable:
        Start Length Slot Name  Signature
          0    9   0 args  [Ljava/lang/String;
          8    1   1   b  Z
     static {};
      descriptor: ()V
      flags: ACC_STATIC
      Code:
       stack=1, locals=0, args_size=0
         0: iconst_0
         1: putstatic   #2         // Field stop:Z
         4: return
       LineNumberTable:
        line 11: 0
    }
    
    hsdis를 통해 가상 컴퓨터에서 생성된 어셈블리 코드를 보십시오.
    테스트 환경은 java version "1.8.0_45", MACOS10.12.1 i386: x86-64
    실행 매개변수에 추가
    
    -XX:+UnlockDiagnosticVMOptions
    -XX:+LogCompilation
    -XX:+PrintAssembly
    -Xcomp
    -XX:CompileCommand=dontinline,*TestVolatile.main
    -XX:CompileCommand=compileonly,*TestVolatile.main
    
    main 방법의 어셈블리 명령 결과 보기
    
    Decoding compiled method 0x000000010c732c50:
    Code:
    [Disassembling for mach='i386:x86-64']
    [Entry Point]
    [Verified Entry Point]
    [Constants]
     # {method} {0x000000012422a2c8} 'main' '([Ljava/lang/String;)V' in 'com/concurrent/volatiles/TestVolatile'
     # parm0:  rsi:rsi  = '[Ljava/lang/String;'
     #      [sp+0x40] (sp of caller)
     0x000000010c732da0: mov  %eax,-0x14000(%rsp)
     0x000000010c732da7: push  %rbp
     0x000000010c732da8: sub  $0x30,%rsp
     0x000000010c732dac: movabs $0x12422a448,%rdi ;  {metadata(method data for {method} {0x000000012422a2c8} 'main' '([Ljava/lang/String;)V' in 'com/concurrent/volatiles/TestVolatile')}
     0x000000010c732db6: mov  0xdc(%rdi),%ebx
     0x000000010c732dbc: add  $0x8,%ebx
     0x000000010c732dbf: mov  %ebx,0xdc(%rdi)
     0x000000010c732dc5: movabs $0x12422a2c8,%rdi ;  {metadata({method} {0x000000012422a2c8} 'main' '([Ljava/lang/String;)V' in 'com/concurrent/volatiles/TestVolatile')}
     0x000000010c732dcf: and  $0x0,%ebx
     0x000000010c732dd2: cmp  $0x0,%ebx
     0x000000010c732dd5: je   0x000000010c732e03 ;*iconst_1
                            ; - com.concurrent.volatiles.TestVolatile::main@0 (line 14)
     0x000000010c732ddb: movabs $0x76adce798,%rsi ;  {oop(a 'java/lang/Class' = 'com/concurrent/volatiles/TestVolatile')}
     0x000000010c732de5: mov  $0x1,%edi
     0x000000010c732dea: mov  %dil,0x68(%rsi)
     0x000000010c732dee: lock addl $0x0,(%rsp)   ;*putstatic stop
                            ; - com.concurrent.volatiles.TestVolatile::main@1 (line 14)
     0x000000010c732df3: movsbl 0x68(%rsi),%esi  ;*getstatic stop
                            ; - com.concurrent.volatiles.TestVolatile::main@4 (line 15)
     0x000000010c732df7: add  $0x30,%rsp
     0x000000010c732dfb: pop  %rbp
     0x000000010c732dfc: test  %eax,-0x3adbd02(%rip)    # 0x0000000108c57100
                            ;  {poll_return}
     0x000000010c732e02: retq  
     0x000000010c732e03: mov  %rdi,0x8(%rsp)
     0x000000010c732e08: movq  $0xffffffffffffffff,(%rsp)
     0x000000010c732e10: callq 0x000000010c7267e0 ; OopMap{rsi=Oop off=117}
                            ;*synchronization entry
                            ; - com.concurrent.volatiles.TestVolatile::main@-1 (line 14)
                            ;  {runtime_call}
     0x000000010c732e15: jmp  0x000000010c732ddb
     0x000000010c732e17: nop
     0x000000010c732e18: nop
     0x000000010c732e19: mov  0x2a8(%r15),%rax
     0x000000010c732e20: movabs $0x0,%r10
     0x000000010c732e2a: mov  %r10,0x2a8(%r15)
     0x000000010c732e31: movabs $0x0,%r10
     0x000000010c732e3b: mov  %r10,0x2b0(%r15)
     0x000000010c732e42: add  $0x30,%rsp
     0x000000010c732e46: pop  %rbp
     0x000000010c732e47: jmpq  0x000000010c6940e0 ;  {runtime_call}  
    [Exception Handler]
    mov%dil, 0x68(%rsi)에stop에 값을 부여한 후 lockaddl $0x0, (%rsp)
    IA32에서 lock에 대한 설명은요.
    
    The LOCK # signal is asserted during execution of the instruction following 
    the lock prefix. This signal can be used in a multiprocessor system to ensure 
    exclusive use of shared memory while LOCK # is asserted
    lock은 다중 프로세서에서 명령을 실행할 때 공유 메모리를 독점적으로 사용하는 데 사용됩니다.그것의 부작용은 현재 프로세서의 캐시 내용을 메모리에 리셋하고 다른 프로세서의 캐시를 무효화시키는 것이다.이 메모리 장벽을 넘을 수 없는 질서정연한 지령도 제공했다.
    Lock
    자바에서 제공하는 자물쇠의 키워드는synchronized입니다. 방법 블록에 추가할 수도 있고 방법 설명에 추가할 수도 있습니다.
    synchronized 키워드가 하는 역할은 임계 구역에 독점적으로 접근하는 것을 설정하는 것이다. 이 임계 구역에 들어가기 전에 대응하는 모니터 자물쇠를 가져와야 한다. 모든 자바 대상은 모니터 자물쇠가 될 수 있다. 정적 방법에서 모니터 자물쇠는 현재 클래스의 클래스 대상이고 실제 방법은 현재 실례이다.
    synchronized는 원자성, 가시성, 재배열 방지 보증을 제공합니다.
    JMM에서 모니터 자물쇠의 방출 작업happen-before와 후속적인 동일한 모니터 자물쇠 획득 작업을 정의합니다.프로그램 순서 규칙을 결합하면 메모리 전달 가시성 보증을 형성할 수 있다.
    다음은 코드로 각 단계의 실현을 본다
    
    public class TestSynchronize {
      private int count;
      private void inc() {
        synchronized (this) {
          count++;
        }
      }
      public static void main(String[] args) {
        new TestSynchronize().inc();
      }
    }
    
    인코딩 후 inc 방법의 바이트 번호는
    
    private void inc();
      descriptor: ()V
      flags: ACC_PRIVATE
      Code:
       stack=3, locals=3, args_size=1
        0: aload_0
        1: dup
        2: astore_1
        3: monitorenter
        4: aload_0
        5: dup
        6: getfield   #2         // Field count:I
        9: iconst_1
        10: iadd
        11: putfield   #2         // Field count:I
        14: aload_1
        15: monitorexit
        16: goto     24
        19: astore_2
        20: aload_1
        21: monitorexit
        22: aload_2
        23: athrow
        24: return
       Exception table:
        from  to target type
          4  16  19  any
          19  22  19  any
       LineNumberTable:
        line 14: 0
        line 15: 4
    
    synchronized 코드 블록 전후에 추가된 monitorenter와 monitorexist 두 개의 JVM 바이트 코드 명령은 명령의 매개 변수는this 참조입니다.
    hotspot에서 모니터에 대한_enter 및 monitor_exit의 처리는
    
    void LIRGenerator::monitor_enter(LIR_Opr object, LIR_Opr lock, LIR_Opr hdr, LIR_Opr scratch, int monitor_no, CodeEmitInfo* info_for_exception, CodeEmitInfo* info) {
     if (!GenerateSynchronizationCode) return;
     // for slow path, use debug info for state after successful locking
     CodeStub* slow_path = new MonitorEnterStub(object, lock, info);
     __ load_stack_address_monitor(monitor_no, lock);
     // for handling NullPointerException, use debug info representing just the lock stack before this monitorenter
     __ lock_object(hdr, object, lock, scratch, slow_path, info_for_exception);
    }
    void LIRGenerator::monitor_exit(LIR_Opr object, LIR_Opr lock, LIR_Opr new_hdr, LIR_Opr scratch, int monitor_no) {
     if (!GenerateSynchronizationCode) return;
     // setup registers
     LIR_Opr hdr = lock;
     lock = new_hdr;
     CodeStub* slow_path = new MonitorExitStub(lock, UseFastLocking, monitor_no);
     __ load_stack_address_monitor(monitor_no, lock);
     __ unlock_object(hdr, object, lock, scratch, slow_path);
    }
    
    inc 방법이 이 컴퓨터에서 출력하는 어셈블리 코드는
    
    Decoding compiled method 0x0000000115be3e50:
    Code:
    [Entry Point]
    [Constants]
     # {method} {0x0000000113082328} 'inc' '()V' in 'com/concurrent/lock/TestSynchronize'
     #      [sp+0x50] (sp of caller)
     0x0000000115be3fc0: mov  0x8(%rsi),%r10d
     0x0000000115be3fc4: shl  $0x3,%r10
     0x0000000115be3fc8: cmp  %rax,%r10
     0x0000000115be3fcb: jne  0x0000000115b1de20 ;  {runtime_call}
     0x0000000115be3fd1: data32 data32 nopw 0x0(%rax,%rax,1)
     0x0000000115be3fdc: data32 data32 xchg %ax,%ax
    [Verified Entry Point]
     0x0000000115be3fe0: mov  %eax,-0x14000(%rsp)
     0x0000000115be3fe7: push  %rbp
     0x0000000115be3fe8: sub  $0x40,%rsp
     0x0000000115be3fec: movabs $0x113082848,%rax ;  {metadata(method data for {method} {0x0000000113082328} 'inc' '()V' in 'com/concurrent/lock/TestSynchronize')}
     0x0000000115be3ff6: mov  0xdc(%rax),%edi
     0x0000000115be3ffc: add  $0x8,%edi
     0x0000000115be3fff: mov  %edi,0xdc(%rax)
     0x0000000115be4005: movabs $0x113082328,%rax ;  {metadata({method} {0x0000000113082328} 'inc' '()V' in 'com/concurrent/lock/TestSynchronize')}
     0x0000000115be400f: and  $0x0,%edi
     0x0000000115be4012: cmp  $0x0,%edi
     0x0000000115be4015: je   0x0000000115be418d ;*aload_0
                            ; - com.concurrent.lock.TestSynchronize::inc@0 (line 14)
     0x0000000115be401b: lea  0x20(%rsp),%rdi
     0x0000000115be4020: mov  %rsi,0x8(%rdi)
     0x0000000115be4024: mov  (%rsi),%rax
     0x0000000115be4027: mov  %rax,%rbx
     0x0000000115be402a: and  $0x7,%rbx
     0x0000000115be402e: cmp  $0x5,%rbx
     0x0000000115be4032: jne  0x0000000115be40b9
     0x0000000115be4038: mov  0x8(%rsi),%ebx
     0x0000000115be403b: shl  $0x3,%rbx
     0x0000000115be403f: mov  0xa8(%rbx),%rbx
     0x0000000115be4046: or   %r15,%rbx
     0x0000000115be4049: xor  %rax,%rbx
     0x0000000115be404c: and  $0xffffffffffffff87,%rbx
     0x0000000115be4050: je   0x0000000115be40e1
     0x0000000115be4056: test  $0x7,%rbx
     0x0000000115be405d: jne  0x0000000115be40a6
     0x0000000115be405f: test  $0x300,%rbx
     0x0000000115be4066: jne  0x0000000115be4085
     0x0000000115be4068: and  $0x37f,%rax
     0x0000000115be406f: mov  %rax,%rbx
     0x0000000115be4072: or   %r15,%rbx
     0x0000000115be4075: lock cmpxchg %rbx,(%rsi)
     0x0000000115be407a: jne  0x0000000115be41a4
     0x0000000115be4080: jmpq  0x0000000115be40e1
     0x0000000115be4085: mov  0x8(%rsi),%ebx
     0x0000000115be4088: shl  $0x3,%rbx
     0x0000000115be408c: mov  0xa8(%rbx),%rbx
     0x0000000115be4093: or   %r15,%rbx
     0x0000000115be4096: lock cmpxchg %rbx,(%rsi)
     0x0000000115be409b: jne  0x0000000115be41a4
     0x0000000115be40a1: jmpq  0x0000000115be40e1
     0x0000000115be40a6: mov  0x8(%rsi),%ebx
     0x0000000115be40a9: shl  $0x3,%rbx
     0x0000000115be40ad: mov  0xa8(%rbx),%rbx
     0x0000000115be40b4: lock cmpxchg %rbx,(%rsi)
     0x0000000115be40b9: mov  (%rsi),%rax
     0x0000000115be40bc: or   $0x1,%rax
     0x0000000115be40c0: mov  %rax,(%rdi)
     0x0000000115be40c3: lock cmpxchg %rdi,(%rsi)
     0x0000000115be40c8: je   0x0000000115be40e1
     0x0000000115be40ce: sub  %rsp,%rax
     0x0000000115be40d1: and  $0xfffffffffffff007,%rax
     0x0000000115be40d8: mov  %rax,(%rdi)
     0x0000000115be40db: jne  0x0000000115be41a4 ;*monitorenter
                            ; - com.concurrent.lock.TestSynchronize::inc@3 (line 14)
     0x0000000115be40e1: mov  0xc(%rsi),%eax   ;*getfield count
                            ; - com.concurrent.lock.TestSynchronize::inc@6 (line 15)
     0x0000000115be40e4: inc  %eax
     0x0000000115be40e6: mov  %eax,0xc(%rsi)   ;*putfield count
                            ; - com.concurrent.lock.TestSynchronize::inc@11 (line 15)
     0x0000000115be40e9: lea  0x20(%rsp),%rax
     0x0000000115be40ee: mov  0x8(%rax),%rdi
     0x0000000115be40f2: mov  (%rdi),%rsi
     0x0000000115be40f5: and  $0x7,%rsi
     0x0000000115be40f9: cmp  $0x5,%rsi
     0x0000000115be40fd: je   0x0000000115be411a
     0x0000000115be4103: mov  (%rax),%rsi
     0x0000000115be4106: test  %rsi,%rsi
     0x0000000115be4109: je   0x0000000115be411a
     0x0000000115be410f: lock cmpxchg %rsi,(%rdi)
     0x0000000115be4114: jne  0x0000000115be41b7 ;*monitorexit
                            ; - com.concurrent.lock.TestSynchronize::inc@15 (line 16)
     0x0000000115be411a: movabs $0x113082848,%rax ;  {metadata(method data for {method} {0x0000000113082328} 'inc' '()V' in 'com/concurrent/lock/TestSynchronize')}
     0x0000000115be4124: incl  0x108(%rax)    ;*goto
                            ; - com.concurrent.lock.TestSynchronize::inc@16 (line 16)
     0x0000000115be412a: add  $0x40,%rsp
     0x0000000115be412e: pop  %rbp
     0x0000000115be412f: test  %eax,-0x684e035(%rip)    # 0x000000010f396100
                            ;  {poll_return}
     0x0000000115be4135: retq           ;*return
                            ; - com.concurrent.lock.TestSynchronize::inc@24 (line 17)
     0x0000000115be4136: mov  0x2a8(%r15),%rax
     0x0000000115be413d: xor  %r10,%r10
     0x0000000115be4140: mov  %r10,0x2a8(%r15)
     0x0000000115be4147: xor  %r10,%r10
     0x0000000115be414a: mov  %r10,0x2b0(%r15)
     0x0000000115be4151: mov  %rax,%rsi
     0x0000000115be4154: lea  0x20(%rsp),%rax
     0x0000000115be4159: mov  0x8(%rax),%rbx
     0x0000000115be415d: mov  (%rbx),%rdi
     0x0000000115be4160: and  $0x7,%rdi
     0x0000000115be4164: cmp  $0x5,%rdi
     0x0000000115be4168: je   0x0000000115be4185
     0x0000000115be416e: mov  (%rax),%rdi
     0x0000000115be4171: test  %rdi,%rdi
     0x0000000115be4174: je   0x0000000115be4185
     0x0000000115be417a: lock cmpxchg %rdi,(%rbx)
     0x0000000115be417f: jne  0x0000000115be41ca ;*monitorexit
                            ; - com.concurrent.lock.TestSynchronize::inc@21 (line 16)
     0x0000000115be4185: mov  %rsi,%rax
     0x0000000115be4188: jmpq  0x0000000115be4205
     0x0000000115be418d: mov  %rax,0x8(%rsp)
     0x0000000115be4192: movq  $0xffffffffffffffff,(%rsp)
     0x0000000115be419a: callq 0x0000000115bd5be0 ; OopMap{rsi=Oop off=479}
                            ;*synchronization entry
                            ; - com.concurrent.lock.TestSynchronize::inc@-1 (line 14)
                            ;  {runtime_call}
     0x0000000115be419f: jmpq  0x0000000115be401b
     0x0000000115be41a4: mov  %rsi,0x8(%rsp)
     0x0000000115be41a9: mov  %rdi,(%rsp)
     0x0000000115be41ad: callq 0x0000000115bd4060 ; OopMap{rsi=Oop [40]=Oop off=498}
                            ;*monitorenter
                            ; - com.concurrent.lock.TestSynchronize::inc@3 (line 14)
                            ;  {runtime_call}
     0x0000000115be41b2: jmpq  0x0000000115be40e1
     0x0000000115be41b7: lea  0x20(%rsp),%rax
     0x0000000115be41bc: mov  %rax,(%rsp)
     0x0000000115be41c0: callq 0x0000000115bd4420 ;  {runtime_call}
     0x0000000115be41c5: jmpq  0x0000000115be411a
     0x0000000115be41ca: lea  0x20(%rsp),%rax
     0x0000000115be41cf: mov  %rax,(%rsp)
     0x0000000115be41d3: callq 0x0000000115bd4420 ;  {runtime_call}
     0x0000000115be41d8: jmp  0x0000000115be4185
     0x0000000115be41da: nop
     0x0000000115be41db: nop
     0x0000000115be41dc: mov  0x2a8(%r15),%rax
     0x0000000115be41e3: movabs $0x0,%r10
     0x0000000115be41ed: mov  %r10,0x2a8(%r15)
     0x0000000115be41f4: movabs $0x0,%r10
     0x0000000115be41fe: mov  %r10,0x2b0(%r15)
     0x0000000115be4205: add  $0x40,%rsp
     0x0000000115be4209: pop  %rbp
     0x0000000115be420a: jmpq  0x0000000115b440e0 ;  {runtime_call}  
    [Exception Handler]
    여기서 lock cmpxchg는 Compare And Exchange
    
    CMPXCHG compares its destination (first) operand to the
     value in AL, AX or EAX (depending on the size of the instruction).
     If they are equal, it copies its source (second) operand into the destination
     and sets the zero flag. Otherwise, it clears the zero flag and leaves the destination alone.
    CMPXCHG is intended to be used for atomic operations in multitasking or multiprocessor environments. To safely update a value in shared memory, for example, you might load the value into EAX, load the updated value into EBX, and then execute the instruction lock cmpxchg [value],ebx. If value has not changed since being loaded, it is updated with your desired new value, and the zero flag is set to let you know it has worked. (The LOCK prefix prevents another processor doing anything in the middle of this operation: it guarantees atomicity.) However, if another processor has modified the value in between your load and your attempted store, the store does not happen, and you are notified of the failure by a cleared zero flag, so you can go round and try again.
    읽어주셔서 감사합니다. 여러분에게 도움이 되었으면 좋겠습니다. 본 사이트에 대한 지지에 감사드립니다!

    좋은 웹페이지 즐겨찾기