The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,575 other followers

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 [WayBackInterlockedCompareExchange, as that is a jump to the actual code:

00417CA8 FF2528E12901     jmp dword ptr [$0129e128]
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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: