* WebAssembly threads: bug fixes in LockMutexTimeoutWait

This commit is contained in:
Nikolay Nikolov 2024-08-03 01:29:04 +03:00
parent 8708144c50
commit 692bd62359

View File

@ -104,6 +104,7 @@ Var
Res : TLockMutexResult; Res : TLockMutexResult;
MyThread : TThreadID; MyThread : TThreadID;
EndTime: TOSTime; EndTime: TOSTime;
RemainingTime: Int64;
begin begin
{$IFDEF DEBUGWASMTHREADS}DebugWriteln('LockMutexTimeoutWait('+IntToStr(m.locked)+','+intToStr(aTimeOutNS)+')');{$ENDIF} {$IFDEF DEBUGWASMTHREADS}DebugWriteln('LockMutexTimeoutWait('+IntToStr(m.locked)+','+intToStr(aTimeOutNS)+')');{$ENDIF}
@ -112,30 +113,37 @@ begin
if aTimeOutNS>=0 then if aTimeOutNS>=0 then
EndTime:=GetClockTime+aTimeOutNS EndTime:=GetClockTime+aTimeOutNS
else else
EndTime:=0; begin
EndTime:=0;
RemainingTime:=-1;
end;
InterLockedIncrement(M.Waiters); InterLockedIncrement(M.Waiters);
{$IFDEF DEBUGWASMTHREADS}DebugWriteln('LockMutexTimeoutWait: entering loop');{$ENDIF} {$IFDEF DEBUGWASMTHREADS}DebugWriteln('LockMutexTimeoutWait: entering loop');{$ENDIF}
Repeat Repeat
Case fpc_wasm32_memory_atomic_wait32(@M.Locked,1,1000) of if TryLockMutex(m) then
0 : begin Res:=lmrOk
if M.Destroying then else
Res:=lmrError begin
else if aTimeOutNS>=0 then
Res:=lmrOK; begin
RemainingTime:=EndTime-GetClockTime;
if RemainingTime<0 then
Res:=lmrTimeOut;;
end; end;
1 : Res:=lmrError; if Res<>lmrNone then
2 : begin Case fpc_wasm32_memory_atomic_wait32(@M.Locked,1,RemainingTime) of
if M.Destroying then 0, 1:
Res:=lmrError if M.Destroying then
else if (GetThreadState(MyThread)<>tsRunning) then Res:=lmrError;
Res:=lmrError 2:
else if M.Destroying then
begin Res:=lmrError
If (aTimeOutNS>=0) and (GetClockTime>EndTime) then else if (GetThreadState(MyThread)<>tsRunning) then
Res:=lmrTimeOut Res:=lmrError
end; else
end; Res:=lmrTimeOut;
end; end;
end;
Until Res<>lmrNone; Until Res<>lmrNone;
{$IFDEF DEBUGWASMTHREADS}DebugWriteln('LockMutexTimeoutWait: done loop');{$ENDIF} {$IFDEF DEBUGWASMTHREADS}DebugWriteln('LockMutexTimeoutWait: done loop');{$ENDIF}
InterLockedDecrement(M.Waiters); InterLockedDecrement(M.Waiters);