From 25af4be58089eb7451b1691783043f8f3286fa0c Mon Sep 17 00:00:00 2001 From: Olly Date: Sun, 15 Oct 2023 17:03:05 +0100 Subject: [PATCH] Fix RunInMainThread/RunInThread. We need everything as cdecl. --- .../imports/simba/simba.import_threading.pas | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/Source/script/imports/simba/simba.import_threading.pas b/Source/script/imports/simba/simba.import_threading.pas index 4479db0b6..eb1fd1ba3 100644 --- a/Source/script/imports/simba/simba.import_threading.pas +++ b/Source/script/imports/simba/simba.import_threading.pas @@ -18,8 +18,57 @@ procedure ImportThreading(Compiler: TSimbaScript_Compiler); implementation uses - lptypes, lpparser, ffi, - simba.threading; + lptypes, lpparser, ffi; + +// All this is because we use cdecl on win32... +type + TScriptThreadMethod = procedure() of object; cdecl; + + TSyncObject = object + FMethod: TScriptThreadMethod; + + procedure Execute; + end; + + TThreadObject = class(TThread) + protected + FMethod: TScriptThreadMethod; + + procedure Execute; override; + public + constructor Create(Method: TScriptThreadMethod); reintroduce; + end; + +procedure TThreadObject.Execute; +begin + try + if Assigned(FMethod) then + FMethod(); + except + on E: Exception do + DebugLn('RunInThread exception: ' + E.Message); + end; +end; + +constructor TThreadObject.Create(Method: TScriptThreadMethod); +begin + inherited Create(False, DefaultStackSize div 2); + + FMethod := Method; + + FreeOnTerminate := True; +end; + +procedure TSyncObject.Execute; +begin + try + if Assigned(FMethod) then + FMethod(); + except + on E: Exception do + DebugLn('RunOnMainThread exception: ' + E.Message); + end; +end; procedure _LapeCurrentThreadID(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV begin @@ -32,13 +81,18 @@ procedure _LapeMainThreadID(const Params: PParamArray; const Result: Pointer); L end; procedure _LapeRunInMainThread(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV +var + {%H-}SyncObject: TSyncObject; begin - RunInMainThread(TThreadMethod(Params^[0]^)); + SyncObject := Default(TSyncObject); + SyncObject.FMethod := TScriptThreadMethod(Params^[0]^); + + TThread.Synchronize(nil, @SyncObject.Execute); end; procedure _LapeRunInThread(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV begin - RunInThread(TThreadMethod(Params^[0]^), True); + TThreadObject.Create(TScriptThreadMethod(Params^[0]^)); end; procedure ImportThreading(Compiler: TSimbaScript_Compiler);