How do I atomically swap 2 ints in C#?

后端 未结 8 778
执念已碎
执念已碎 2020-11-29 06:50

What (if any) is the C# equivalent of the x86 asm xchg instruction?

With that command, which imo is a genuine exchange (unlike Interlocked.Exchange), I

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 07:14

    This is the likely implementation for Interlocked.Exchange() in the CLR, copied from the SSCLI20 source:

    Note that UP in the function name means UniProcessor. This is not atomic on SMP / multi-core systems. This implementation will only be used by CLR on single-core systems.

    FASTCALL_FUNC ExchangeUP,8
            _ASSERT_ALIGNED_4_X86 ecx
            mov     eax, [ecx]      ; attempted comparand
    retry:
            cmpxchg [ecx], edx
            jne     retry1          ; predicted NOT taken
            retn
    retry1:
            jmp     retry
    FASTCALL_ENDFUNC ExchangeUP
    

    It is superior to using XCHG because this code works without taking a bus lock. xchg has an implicit lock prefix, so unlike xadd or cmpxchg it simply can't be omitted for single-core systems to still do the operation in one instruction to make it atomic with respect to interrupts (and thus other threads on uniprocessor).

    The odd looking jumping code is an optimization in case branch prediction data is not available. Needless to say perhaps, trying to do a better job than what has been mulled over for many years by very good software engineers with generous helpings from the chip manufacturers is a tall task.

提交回复
热议问题