delphi – Is AtomicCmpExchange reliable on all platforms? – Stack Overflow
Posted by jpluimers on 2017/09/19
TL;DR: yes it is.
Answer by Allen Bauer at delphi – Is AtomicCmpExchange reliable on all platforms? – Stack Overflow [WayBack]
On Windows, it directly translates into lock cmpxchg
which is way faster than the Windows API call [WayBack] InterlockedCompareExchange
, as that is a jump to the actual code:
InterlockedCompareExchange: 00417CA8 FF2528E12901 jmp dword ptr [$0129e128]
KERNEL32.InterlockedCompareExchange: 75855E40 8BFF mov edi,edi 75855E42 55 push ebp 75855E43 8BEC mov ebp,esp 75855E45 8B550C mov edx,[ebp+$0c] 75855E48 8B4D08 mov ecx,[ebp+$08] 75855E4B 8B4510 mov eax,[ebp+$10] 75855E4E F00FB111 lock cmpxchg [ecx],edx 75855E52 5D pop ebp 75855E53 C20C00 ret $000c
whereas AtomicCmdExchange
looks like this:
Test.pas.20: RestoreValue := AtomicCmpExchange(FieldToBeModfied, 1 {new value}, 0 {expected value}, Success {true if the expected value was found, and new value set}); 0111A838 8B45FC mov eax,[ebp-$04] 0111A83B 8D500C lea edx,[eax+$0c] 0111A83E 33C0 xor eax,eax 0111A840 B901000000 mov ecx,$00000001 0111A845 F00FB10A lock cmpxchg [edx],ecx 0111A849 0F9445F2 setz byte ptr [ebp-$0e] 0111A84D 8945D8 mov [ebp-$28],eax
–jeroen
Leave a Reply