From 19cce3e42e46f010065146dec761dc6b00a6efc1 Mon Sep 17 00:00:00 2001
From: Nikolay Nikolov <nickysn@gmail.com>
Date: Wed, 25 May 2022 20:11:54 +0300
Subject: [PATCH] + added AtomicWait() to the WebAssembly unit

---
 rtl/wasm32/webassembly.pp | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/rtl/wasm32/webassembly.pp b/rtl/wasm32/webassembly.pp
index 23d0f28b22..109dbb7763 100644
--- a/rtl/wasm32/webassembly.pp
+++ b/rtl/wasm32/webassembly.pp
@@ -19,6 +19,14 @@ unit WebAssembly;
 
 interface
 
+const
+  { Special values for the TimeoutNanoseconds parameter of AtomicWait }
+  awtInfiniteTimeout = -1;
+  { AtomicWait result values }
+  awrOk = 0;       { woken by another agent in the cluster }
+  awrNotEqual = 1; { the loaded value did not match the expected value }
+  awrTimedOut = 2; { not woken before timeout expired }
+
 procedure AtomicFence; inline;
 
 function AtomicLoad(constref Mem: Int8): Int8; inline;
@@ -102,6 +110,11 @@ function AtomicCompareExchange(var Mem: UInt32; Compare, Data: UInt32): UInt32;
 function AtomicCompareExchange(var Mem: Int64; Compare, Data: Int64): Int64; inline;
 function AtomicCompareExchange(var Mem: UInt64; Compare, Data: UInt64): UInt64; inline;
 
+function AtomicWait(constref Mem: Int32; Compare: Int32; TimeoutNanoseconds: Int64): Int32;
+function AtomicWait(constref Mem: UInt32; Compare: UInt32; TimeoutNanoseconds: Int64): Int32;
+function AtomicWait(constref Mem: Int64; Compare: Int64; TimeoutNanoseconds: Int64): Int32;
+function AtomicWait(constref Mem: UInt64; Compare: UInt64; TimeoutNanoseconds: Int64): Int32;
+
 implementation
 
 {$I cpuh.inc}
@@ -471,4 +484,24 @@ begin
   AtomicCompareExchange:=fpc_wasm32_i64_atomic_rmw_cmpxchg_u(@Mem,Compare,Data);
 end;
 
+function AtomicWait(constref Mem: Int32; Compare: Int32; TimeoutNanoseconds: Int64): Int32;
+begin
+  AtomicWait:=fpc_wasm32_memory_atomic_wait32(@Mem,LongWord(Compare),TimeoutNanoseconds);
+end;
+
+function AtomicWait(constref Mem: UInt32; Compare: UInt32; TimeoutNanoseconds: Int64): Int32;
+begin
+  AtomicWait:=fpc_wasm32_memory_atomic_wait32(@Mem,Compare,TimeoutNanoseconds);
+end;
+
+function AtomicWait(constref Mem: Int64; Compare: Int64; TimeoutNanoseconds: Int64): Int32;
+begin
+  AtomicWait:=fpc_wasm32_memory_atomic_wait64(@Mem,QWord(Compare),TimeoutNanoseconds);
+end;
+
+function AtomicWait(constref Mem: UInt64; Compare: UInt64; TimeoutNanoseconds: Int64): Int32;
+begin
+  AtomicWait:=fpc_wasm32_memory_atomic_wait64(@Mem,Compare,TimeoutNanoseconds);
+end;
+
 end.