From 8488c87b20efb6e3a934a9d71475ac3a24d9d6ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Van=20Canneyt?= Date: Wed, 8 Jan 2025 14:56:16 +0100 Subject: [PATCH] * New fix for fpc_atomic_cmp_xchg_alu, as suggested by Sven --- rtl/inc/generic.inc | 20 +++++++------------- rtl/wasm32/cpuh.inc | 2 +- rtl/wasm32/wasm32.inc | 13 +++++++++++++ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/rtl/inc/generic.inc b/rtl/inc/generic.inc index fdd8e4b2e8..8d35b2430f 100644 --- a/rtl/inc/generic.inc +++ b/rtl/inc/generic.inc @@ -3532,24 +3532,18 @@ end; {$endif FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_64} {$if defined(FPC_SYSTEM_NEEDS_ATOMIC_FALLBACK)} -Type -{$IFDEF CPUWASM32} - TAtomicLockType = longint; -{$ELSE} - TAtomicLockType = ALUSInt; -{$ENDIF} var - gAtomicLock: TAtomicLockType = 0; + gAtomicLock: ALUSInt = 0; -function fpc_atomic_cmp_xchg_alu(var Target: TAtomicLockType; NewValue: TAtomicLockType; Comparand: TAtomicLockType): TAtomicLockType; external name -{$if SIZEOF(TAtomicLockType)=1} +function fpc_atomic_cmp_xchg_alu(var Target: ALUSInt; NewValue: ALUSInt; Comparand: ALUSInt): ALUSInt; external name +{$if defined(CPUINT8)} 'FPC_ATOMIC_CMP_XCHG_8' -{$elseif SIZEOF(TAtomicLockType)=2} +{$elseif defined(CPUINT16)} 'FPC_ATOMIC_CMP_XCHG_16' -{$elseif SIZEOF(TAtomicLockType)=4} +{$elseif defined(CPUINT32)} 'FPC_ATOMIC_CMP_XCHG_32' -{$elseif SIZEOF(TAtomicLockType)=8} +{$elseif defined(CPUINT64)} 'FPC_ATOMIC_CMP_XCHG_64' {$else} 'FPC_ATOMIC_CMP_XCHG_UNKNOWN' @@ -3558,7 +3552,7 @@ function fpc_atomic_cmp_xchg_alu(var Target: TAtomicLockType; NewValue: TAtomicL procedure AtomicEnterLock; var - r: TAtomicLockType; + r: ALUSInt; begin { spin until we get the lock } repeat diff --git a/rtl/wasm32/cpuh.inc b/rtl/wasm32/cpuh.inc index b302d9c898..db306eab76 100644 --- a/rtl/wasm32/cpuh.inc +++ b/rtl/wasm32/cpuh.inc @@ -81,7 +81,7 @@ function fpc_wasm32_i32_atomic_rmw_cmpxchg_u(Dest: PLongWord; Expected, Replacem function fpc_wasm32_i64_atomic_rmw8_cmpxchg_u(Dest: PByte; Expected, Replacement: QWord): QWord;[internproc:fpc_in_wasm32_i64_atomic_rmw8_cmpxchg_u]; function fpc_wasm32_i64_atomic_rmw16_cmpxchg_u(Dest: PWord; Expected, Replacement: QWord): QWord;[internproc:fpc_in_wasm32_i64_atomic_rmw16_cmpxchg_u]; function fpc_wasm32_i64_atomic_rmw32_cmpxchg_u(Dest: PWord; Expected, Replacement: QWord): QWord;[internproc:fpc_in_wasm32_i64_atomic_rmw32_cmpxchg_u]; -function fpc_wasm32_i64_atomic_rmw_cmpxchg_u(Dest: PLongWord; Expected, Replacement: QWord): QWord;[internproc:fpc_in_wasm32_i64_atomic_rmw_cmpxchg]; +function fpc_wasm32_i64_atomic_rmw_cmpxchg_u(Dest: PQWord; Expected, Replacement: QWord): QWord;[internproc:fpc_in_wasm32_i64_atomic_rmw_cmpxchg]; function fpc_wasm32_memory_atomic_wait32(Dest: PLongWord; Expected: LongWord; Timeout: Int64): LongInt;[internproc:fpc_in_wasm32_memory_atomic_wait32]; function fpc_wasm32_memory_atomic_wait64(Dest: PQWord; Expected: QWord; Timeout: Int64): LongInt;[internproc:fpc_in_wasm32_memory_atomic_wait64]; diff --git a/rtl/wasm32/wasm32.inc b/rtl/wasm32/wasm32.inc index 43c7392c8b..2917af1469 100644 --- a/rtl/wasm32/wasm32.inc +++ b/rtl/wasm32/wasm32.inc @@ -145,6 +145,19 @@ function fpc_atomic_cmp_xchg_32 (var Target: longint; NewValue: longint; Compara {$endif FPC_WASM_THREADS} end; +{$ifndef VER3_2} +{$define FPC_SYSTEM_HAS_ATOMIC_CMP_XCHG_64} +function fpc_atomic_cmp_xchg_64 (var Target: Int64; NewValue: Int64; Comparand: Int64) : Int64; [public,alias:'FPC_ATOMIC_CMP_XCHG_64']; + begin +{$ifdef FPC_WASM_THREADS} + Result:=Int64(fpc_wasm32_i64_atomic_rmw_cmpxchg_u(@Target,QWord({$ifdef VER3_2}Comperand{$else}Comparand{$endif}),LongWord(NewValue))); +{$else FPC_WASM_THREADS} + Result:=Target; + if Target={$ifdef VER3_2}Comperand{$else}Comparand{$endif} then + Target:=NewValue; +{$endif FPC_WASM_THREADS} + end; +{$endif VER3_2} {$ifdef VER3_2} function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint;