Fastest inline-assembly spinlock

后端 未结 5 1401
梦如初夏
梦如初夏 2020-12-01 05:24

I\'m writing a multithreaded application in c++, where performance is critical. I need to use a lot of locking while copying small structures between threads, for this I hav

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 06:10

    Just look here: x86 spinlock using cmpxchg

    And thanks to Cory Nelson

    __asm{
    spin_lock:
    xorl %ecx, %ecx
    incl %ecx
    spin_lock_retry:
    xorl %eax, %eax
    lock; cmpxchgl %ecx, (lock_addr)
    jnz spin_lock_retry
    ret
    
    spin_unlock:
    movl $0 (lock_addr)
    ret
    }
    

    And another source says: http://www.geoffchappell.com/studies/windows/km/cpu/cx8.htm

           lock    cmpxchg8b qword ptr [esi]
    is replaceable with the following sequence
    
    try:
            lock    bts dword ptr [edi],0
            jnb     acquired
    wait:
            test    dword ptr [edi],1
            je      try
            pause                   ; if available
            jmp     wait
    
    acquired:
            cmp     eax,[esi]
            jne     fail
            cmp     edx,[esi+4]
            je      exchange
    
    fail:
            mov     eax,[esi]
            mov     edx,[esi+4]
            jmp     done
    
    exchange:
            mov     [esi],ebx
            mov     [esi+4],ecx
    
    done:
            mov     byte ptr [edi],0
    

    And here is a discussion about lock-free vs lock implementations: http://newsgroups.derkeiler.com/Archive/Comp/comp.programming.threads/2011-10/msg00009.html

提交回复
热议问题