macosassemblykernelcompare-and-swapatomic-swap

Is OSCompareAndSwap (Mac OS X) equivalent to CMPXCHG8B?


Is OSCompareAndSwap (Mac OS X) equivalent to CMPXCHG8B?


Solution

  • 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