Browse Source

* WebAssembly threads: also access TRTLCriticalSection.Owner using atomic loads and stores

Nikolay Nikolov 1 year ago
parent
commit
e612401562
1 changed files with 5 additions and 4 deletions
  1. 5 4
      rtl/wasi/wasmmutex.inc

+ 5 - 4
rtl/wasi/wasmmutex.inc

@@ -22,6 +22,7 @@ begin
     aOwner:=GetSelfThread;
     aOwner:=GetSelfThread;
   M.Creator:=aOwner;
   M.Creator:=aOwner;
   M.Kind:=Ord(aKind);
   M.Kind:=Ord(aKind);
+  fpc_wasm32_i32_atomic_store(@M.Owner,0);
   fpc_wasm32_i32_atomic_store(@M.Locked,0);
   fpc_wasm32_i32_atomic_store(@M.Locked,0);
 end;
 end;
 
 
@@ -45,7 +46,7 @@ Var
 
 
 begin
 begin
   // We already have the lock ?
   // We already have the lock ?
-  Res:=(fpc_wasm32_i32_atomic_load(@M.Locked)=1) and (M.Owner=GetSelfThread);
+  Res:=(fpc_wasm32_i32_atomic_load(@M.Locked)=1) and (TThreadID(fpc_wasm32_i32_atomic_load(@M.Owner))=GetSelfThread);
   if Not Res then
   if Not Res then
     Res:=fpc_wasm32_i32_atomic_rmw_cmpxchg_u(@M.Locked,0,1)=0
     Res:=fpc_wasm32_i32_atomic_rmw_cmpxchg_u(@M.Locked,0,1)=0
   else
   else
@@ -58,7 +59,7 @@ begin
     if (MutexKind(M)=mkRecursive) or (M.Count=0) then
     if (MutexKind(M)=mkRecursive) or (M.Count=0) then
       InterLockedIncrement(M.Count);
       InterLockedIncrement(M.Count);
 //    {$IFDEF DEBUGWASMTHREADS}DebugWriteln('TryLockMutex : setting owner to '+IntToStr(PtrUint(GetSelfThread))+'.');{$ENDIF}
 //    {$IFDEF DEBUGWASMTHREADS}DebugWriteln('TryLockMutex : setting owner to '+IntToStr(PtrUint(GetSelfThread))+'.');{$ENDIF}
-    M.Owner:=GetSelfThread;
+    fpc_wasm32_i32_atomic_store(@M.Owner,LongWord(GetSelfThread));
     end;
     end;
   TryLockMutex:=Res;
   TryLockMutex:=Res;
 end;
 end;
@@ -180,7 +181,7 @@ var
 begin
 begin
   Res:=lmrNone;
   Res:=lmrNone;
   MyThread:=GetSelfThread;
   MyThread:=GetSelfThread;
-  if MyThread<>M.owner then
+  if MyThread<>TThreadID(fpc_wasm32_i32_atomic_load(@M.Owner)) then
     Res:=lmrNotOwner
     Res:=lmrNotOwner
   else if M.Count=0 then
   else if M.Count=0 then
     Res:=lmrError
     Res:=lmrError
@@ -191,7 +192,7 @@ begin
       InterLockedDecrement(M.Count);
       InterLockedDecrement(M.Count);
     if (M.Count=0) then
     if (M.Count=0) then
       begin
       begin
-        M.Owner:=nil;
+        fpc_wasm32_i32_atomic_store(@M.Owner,0);
         fpc_wasm32_i32_atomic_store(@M.Locked,0);
         fpc_wasm32_i32_atomic_store(@M.Locked,0);
         a:=fpc_wasm32_memory_atomic_notify(@M.Locked,1);
         a:=fpc_wasm32_memory_atomic_notify(@M.Locked,1);
       end;
       end;