Is OSCompareAndSwap (Mac OS X) equivalent to CMPXCHG8B?
Asked
Active
Viewed 657 times
3
2 Answers
2
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
- 12,933
- 4
- 29
- 32
-
+1 thanks. So eventually is OSCompareAndSwap is immune to ABA problem like CMPXCHG8B? – Viet Mar 19 '10 at 11:55
-
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 Mar 19 '10 at 21:51
0
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
- 71
- 3
-
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 Mar 19 '10 at 11:32
-
Moving from function parameters to appropriate registers will be appended before. – drlazy Mar 19 '10 at 13:02