mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-28 11:50:28 +02:00
+ implemented InterlockedIncrement, InterlockedDecrement, InterlockedExchange,
InterlockedCompareExchange and InterlockedExchangeAdd for WebAssembly in a thread safe way, using the thread and atomics extension, when the RTL is compiled with -dFPC_WASM_THREADS
This commit is contained in:
parent
ccc843f983
commit
47c271dcd0
@ -73,71 +73,119 @@ function Sptr : pointer;
|
|||||||
|
|
||||||
function InterLockedDecrement (var Target: longint) : longint;
|
function InterLockedDecrement (var Target: longint) : longint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
{$push}{$R-,Q-}
|
||||||
|
Result:=fpc_wasm32_i32_atomic_rmw_sub(@Target,1)-1;
|
||||||
|
{$pop}
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
dec(Target);
|
dec(Target);
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function InterLockedIncrement (var Target: longint) : longint;
|
function InterLockedIncrement (var Target: longint) : longint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
{$push}{$R-,Q-}
|
||||||
|
Result:=fpc_wasm32_i32_atomic_rmw_add(@Target,1)+1;
|
||||||
|
{$pop}
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
inc(Target);
|
inc(Target);
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function InterLockedExchange (var Target: longint;Source : longint) : longint;
|
function InterLockedExchange (var Target: longint;Source : longint) : longint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
Result:=LongInt(fpc_wasm32_i32_atomic_rmw_xchg(@Target,LongWord(Source)));
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
Target:=Source;
|
Target:=Source;
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
|
function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
Result:=LongInt(fpc_wasm32_i32_atomic_rmw_cmpxchg_u(@Target,LongWord(Comperand),LongWord(NewValue)));
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
if Target=Comperand then
|
if Target=Comperand then
|
||||||
Target:=NewValue;
|
Target:=NewValue;
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
|
function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
Result:=LongInt(fpc_wasm32_i32_atomic_rmw_add(@Target,LongWord(Source)));
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
inc(Target,Source);
|
inc(Target,Source);
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function InterLockedDecrement (var Target: smallint) : smallint;
|
function InterLockedDecrement (var Target: smallint) : smallint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
{$push}{$R-,Q-}
|
||||||
|
Result:=smallint(fpc_wasm32_i32_atomic_rmw16_sub_u(@Target,1)-1);
|
||||||
|
{$pop}
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
dec(Target);
|
dec(Target);
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function InterLockedIncrement (var Target: smallint) : smallint;
|
function InterLockedIncrement (var Target: smallint) : smallint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
{$push}{$R-,Q-}
|
||||||
|
Result:=smallint(fpc_wasm32_i32_atomic_rmw16_add_u(@Target,1)+1);
|
||||||
|
{$pop}
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
inc(Target);
|
inc(Target);
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function InterLockedExchange (var Target: smallint;Source : smallint) : smallint;
|
function InterLockedExchange (var Target: smallint;Source : smallint) : smallint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
Result:=SmallInt(fpc_wasm32_i32_atomic_rmw16_xchg_u(@Target,Word(Source)));
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
Target:=Source;
|
Target:=Source;
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function InterlockedCompareExchange(var Target: smallint; NewValue: smallint; Comperand: smallint): smallint;
|
function InterlockedCompareExchange(var Target: smallint; NewValue: smallint; Comperand: smallint): smallint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
Result:=SmallInt(fpc_wasm32_i32_atomic_rmw16_cmpxchg_u(@Target,Word(Comperand),Word(NewValue)));
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
if Target=Comperand then
|
if Target=Comperand then
|
||||||
Target:=NewValue;
|
Target:=NewValue;
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function InterLockedExchangeAdd (var Target: smallint;Source : smallint) : smallint;
|
function InterLockedExchangeAdd (var Target: smallint;Source : smallint) : smallint;
|
||||||
begin
|
begin
|
||||||
|
{$ifdef FPC_WASM_THREADS}
|
||||||
|
Result:=SmallInt(fpc_wasm32_i32_atomic_rmw16_add_u(@Target,Word(Source)));
|
||||||
|
{$else FPC_WASM_THREADS}
|
||||||
Result:=Target;
|
Result:=Target;
|
||||||
inc(Target,Source);
|
inc(Target,Source);
|
||||||
|
{$endif FPC_WASM_THREADS}
|
||||||
end;
|
end;
|
||||||
|
Loading…
Reference in New Issue
Block a user