It is currently Tue Nov 19, 2019 7:20 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Cross-platform atomic exchange/compare exchange x86/x64
PostPosted: Thu Jul 02, 2009 12:59 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
Frustrated with this problem here:
http://purebasic.fr/english/viewtopic.php?t=37782

I've compiled source code generously provided here for native cross-platform equivalents:
http://purebasic.fr/english/viewtopic.php?t=36939

Code:
Procedure InterlockedExchange(*Destination.Integer, Value.i)
  CompilerIf #PB_Compiler_Processor=#PB_Processor_x86
    !mov ecx,[p.p_Destination]
    !mov eax,[p.v_Value]
    !xchg [ecx],eax
  CompilerEndIf
  CompilerIf #PB_Compiler_Processor=#PB_Processor_x64
    !mov rcx,[p.p_Destination]
    !mov rax,[p.v_Value]
    !xchg [rcx],rax
  CompilerEndIf
  ProcedureReturn
EndProcedure

Procedure InterlockedCompareExchange(*Destination.Integer, Exchange.i, Comparand.i)
  CompilerIf #PB_Compiler_Processor=#PB_Processor_x86
    !mov ecx,[p.p_Destination]
    !mov eax,[p.v_Comparand]
    !mov edx,[p.v_Exchange]
    !lock cmpxchg [ecx],edx
  CompilerEndIf
  CompilerIf #PB_Compiler_Processor=#PB_Processor_x64
    !mov rcx,[p.p_Destination]
    !mov rax,[p.v_Comparand]
    !mov rdx,[p.v_Exchange]
    !lock cmpxchg [rcx],rdx
  CompilerEndIf
  ProcedureReturn
EndProcedure


InterlockedExchange(@This,2)
Debug This

InterlockedCompareExchange(@This,4,1)
Debug This
InterlockedCompareExchange(@This,4,2)
Debug This


XCHG is i286 and up and CMPXCHG is i486 and up, I believe.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye