From 84679edbf04cb38d1f0c8f6cbf69e721cc5bfdd5 Mon Sep 17 00:00:00 2001 From: Michael Van Canneyt Date: Wed, 2 Apr 2025 16:45:42 +0200 Subject: [PATCH] * Cleanup worker after thread has been run --- packages/wasi/src/rtl.webthreads.pas | 6 ++++-- packages/wasi/src/wasiworkerthreadhost.pas | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/wasi/src/rtl.webthreads.pas b/packages/wasi/src/rtl.webthreads.pas index 5614d00..8f0425f 100644 --- a/packages/wasi/src/rtl.webthreads.pas +++ b/packages/wasi/src/rtl.webthreads.pas @@ -127,13 +127,14 @@ Type // Cleanup thread info: put this worker into unusued workers TWorkerCleanupCommand = class external name 'Object' (TWorkerCommand) + exitstatus : integer; end; { TWorkerCleanupCommandHelper } TWorkerCleanupCommandHelper = class helper for TWorkerCleanupCommand Class function CommandName : string; static; - Class function Create(aThreadID : Integer): TWorkerCleanupCommand; static; reintroduce; + class function Create(aThreadID, aExitStatus: Integer): TWorkerCleanupCommand; static; reintroduce; end; @@ -471,9 +472,10 @@ begin Result:=cmdCleanup end; -class function TWorkerCleanupCommandHelper.Create(aThreadID: Integer): TWorkerCleanupCommand; +class function TWorkerCleanupCommandHelper.Create(aThreadID, aExitStatus: Integer): TWorkerCleanupCommand; begin Result:=TWorkerCleanupCommand(TWorkerCommand.NewWorker(CommandName,aThreadID)); + Result.ExitStatus:=aExitStatus; end; { TWorkerConsoleCommandHelper } diff --git a/packages/wasi/src/wasiworkerthreadhost.pas b/packages/wasi/src/wasiworkerthreadhost.pas index 772cf98..8b961fd 100644 --- a/packages/wasi/src/wasiworkerthreadhost.pas +++ b/packages/wasi/src/wasiworkerthreadhost.pas @@ -56,7 +56,6 @@ Type Private Type TWorkerState = (wsNeutral, wsLoading, wsLoaded, wsRunWaiting, wsRunning); - procedure DoRunThread(aExports: TWASIExports); Private FState: TWorkerState; FCurrentThreadInfo : TThreadinfo; @@ -65,6 +64,7 @@ Type FWasiHost: TWASIThreadHost; Protected // Incoming messages + procedure DoRunThread(aExports: TWASIExports); virtual; procedure LoadWasmModule(aCommand: TWorkerLoadCommand); virtual; procedure RunWasmModule(aCommand: TWorkerRunCommand); virtual; procedure CancelWasmModule(aCommand: TWorkerCancelCommand); virtual; @@ -408,6 +408,7 @@ Procedure TWorkerThreadSupport.DoRunThread(aExports : TWASIExports); Var aResult : Integer; + begin try FState:=wsRunning; @@ -416,6 +417,7 @@ begin FState:=wsLoaded; if aResult>0 then SendConsoleMessage('Thread run function result= %d ',[aResult]); + SendCommand(TWorkerCleanupCommand.Create(Self.FCurrentThreadInfo.ThreadID,aResult)); except on E : Exception do SendException(E); @@ -896,7 +898,7 @@ end; function TWorkerThreadControllerApplication.CreateThreadSupport(aEnv : TPas2JSWASIEnvironment) : TThreadController; begin - Result:=TThreadController.Create(aEnv,ThreadRunnerScript,20); + Result:=TThreadController.Create(aEnv,ThreadRunnerScript,ThreadCount); end; function TWorkerThreadControllerApplication.CreateHost: TWASIHost;