views:

58

answers:

2

Is OSCompareAndSwap (Mac OS X) equivalent to CMPXCHG8B?

A: 

lock cmpxchg8b

more exactly

It's for intel processors, but take into account, that osx exists not only for intel architecture, so asm will be different

drlazy
Thanks, I know "lock cmpxchg8b" and aware that OS X runs on PPC too. But for the case when OSX on Intel 32bit, would it be equivalent?
Viet
Moving from function parameters to appropriate registers will be appended before.
drlazy
+1  A: 

You can find the definition of the atomic functions in the OSAtomic.s file in the XNU source. For example, here's OSAtomicCompareAndSwapPtr for x86_64 in version 1486.2.11:

_OSCompareAndSwap64:
_OSCompareAndSwapPtr: #;oldValue, newValue, ptr
    movq         %rdi, %rax
    lock
    cmpxchgq    %rsi, 0(%rdx)   #; CAS (eax is an implicit operand)
    sete        %al             #; did CAS succeed? (TZ=1)
    movzbq      %al, %rax       #; clear out the high bytes
    ret
Ken
+1 thanks. So eventually is OSCompareAndSwap is immune to ABA problem like CMPXCHG8B?
Viet
I'm not really familiar with CMPXCHG8B, but it looks like it has the same semantics as cmpxchg and differs only in what it looks at. In 32 bit, registers are only 32 bits wide, so CMPXCHG8B looks at pairs of registers to allow doing 64 bit CAS. You can see that it's used for the 64 bit CAS operations in the i386 bit OSAtomic.s file.
Ken
It means no. Thanks.
Viet