Question about inline assembler and C backend.

Just starting out? Need help? Post your questions and find answers here.
User avatar
idle
Always Here
Always Here
Posts: 5881
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Question about inline assembler and C backend.

Post by idle »

Code: Select all

Procedure lockinc(var) 
    CompilerIf #PB_Compiler_Backend = #PB_Backend_C 
      !asm("lock inc %0" : "+m" (v_var) : "r" (v_var)); 
    CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x86
      !lock inc dword [p.v_var] 
    CompilerElse
      !lock inc qword [p.v_var] 
    CompilerEndIf
    ProcedureReturn var  
EndProcedure 
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Question about inline assembler and C backend.

Post by breeze4me »

Armoured wrote: Sat Feb 08, 2025 9:44 am I need a function like InterlockedIncrement.
There are the following built-in functions.
__sync_add_and_fetch , __atomic_add_fetch

Code: Select all

CompilerIf #PB_Compiler_Backend <> #PB_Backend_C
  CompilerError "Use C backend."
CompilerEndIf


Global a

Structure aa
  i.i
EndStructure

Global b.aa

Procedure t(v)
  Delay(Random(80, 50))
  For i = 1 To 1000000
    !__sync_add_and_fetch(&g_a, 1);
    ;!__sync_add_and_fetch(&g_b.f_i, 1);
  Next
EndProcedure


t1 = CreateThread(@t(), 0)
t2 = CreateThread(@t(), 0)
t3 = CreateThread(@t(), 0)
t4 = CreateThread(@t(), 0)

WaitThread(t1)
WaitThread(t2)
WaitThread(t3)
WaitThread(t4)

MessageRequester("", "" + a)
;MessageRequester("", "" + b\i)
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Question about inline assembler and C backend.

Post by Armoured »

Ok! Thanks Fred, Jack, Idle, Brezee4me and Mk-soft.
This discussion was helpful to me.
Post Reply