2

OSCompareAndSwap 是否对 CMPXCHG8B 等 ABA 问题免疫?

4

1 回答 1

2

这一切都取决于实施。OSCompareAndSwap* 只是一个保证原子 CAS 运算符的接口(如果 CPU 支持它)。

对于x86,此 64 位函数实现为

_OSCompareAndSwap64:
    pushl       %edi
    pushl       %ebx

    movl         4+8(%esp), %eax    #; low 32-bits of oldValue
    movl         8+8(%esp), %edx    #; high 32-bits of oldValue
    movl        12+8(%esp), %ebx    #; low 32-bits of newValue
    movl        16+8(%esp), %ecx    #; high 32-bits of newValue
    movl        20+8(%esp), %edi    #; ptr
    lock
    cmpxchg8b   0(%edi)     #; CAS (eax:edx, ebx:ecx implicit)
    sete        %al         #; did CAS succeed? (TZ=1)
    movzbl      %al, %eax       #; clear out the high bytes

    popl        %ebx
    popl        %edi
    ret

所以你的答案可能是“是”。

于 2010-03-19T13:03:22.813 回答