@@ -18,57 +18,8 @@ procedure ImportThreading(Compiler: TSimbaScript_Compiler);
18
18
implementation
19
19
20
20
uses
21
- lptypes, lpparser, ffi;
22
-
23
- // All this is because we use cdecl on win32...
24
- type
25
- TScriptThreadMethod = procedure() of object ; cdecl;
26
-
27
- TSyncObject = object
28
- FMethod: TScriptThreadMethod;
29
-
30
- procedure Execute ;
31
- end ;
32
-
33
- TThreadObject = class (TThread)
34
- protected
35
- FMethod: TScriptThreadMethod;
36
-
37
- procedure Execute ; override;
38
- public
39
- constructor Create(Method: TScriptThreadMethod); reintroduce;
40
- end ;
41
-
42
- procedure TThreadObject.Execute ;
43
- begin
44
- try
45
- if Assigned(FMethod) then
46
- FMethod();
47
- except
48
- on E: Exception do
49
- DebugLn(' RunInThread exception: ' + E.Message);
50
- end ;
51
- end ;
52
-
53
- constructor TThreadObject.Create(Method: TScriptThreadMethod);
54
- begin
55
- inherited Create(False, DefaultStackSize div 2 );
56
-
57
- FMethod := Method;
58
-
59
- FreeOnTerminate := True;
60
- end ;
61
-
62
- procedure TSyncObject.Execute ;
63
- begin
64
- try
65
- if Assigned(FMethod) then
66
- FMethod();
67
- except
68
- on E: Exception do
69
- DebugLn(' RunOnMainThread exception: ' + E.Message);
70
- end ;
71
- end ;
21
+ lptypes, lpparser, ffi,
22
+ simba.threading;
72
23
73
24
procedure _LapeCurrentThreadID (const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
74
25
begin
@@ -80,19 +31,19 @@ procedure _LapeMainThreadID(const Params: PParamArray; const Result: Pointer); L
80
31
TThreadID(Result^) := MainThreadID;
81
32
end ;
82
33
83
- procedure _LapeRunInMainThread (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
84
- var
85
- { %H-} SyncObject: TSyncObject;
34
+ procedure _LapeQueueInMainThread (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
86
35
begin
87
- SyncObject := Default(TSyncObject );
88
- SyncObject.FMethod := TScriptThreadMethod(Params^[ 0 ]^) ;
36
+ QueueOnMainThread(TThreadMethod(Params^[ 0 ]^) );
37
+ end ;
89
38
90
- TThread.Synchronize(nil , @SyncObject.Execute);
39
+ procedure _LapeRunInMainThread (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
40
+ begin
41
+ RunInMainThread(TThreadMethod(Params^[0 ]^));
91
42
end ;
92
43
93
44
procedure _LapeRunInThread (const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
94
45
begin
95
- TThreadObject.Create(TScriptThreadMethod (Params^[0 ]^));
46
+ RunInThread(TThreadMethod (Params^[0 ]^), True );
96
47
end ;
97
48
98
49
procedure ImportThreading (Compiler: TSimbaScript_Compiler);
@@ -104,11 +55,12 @@ procedure ImportThreading(Compiler: TSimbaScript_Compiler);
104
55
addGlobalVar(CPUCount, ' CPU_COUNT' ).isConstant := True;
105
56
106
57
addGlobalType(getBaseType(DetermineIntType(SizeOf(TThreadID), False)).createCopy(), ' TThreadID' );
107
- addGlobalType(' procedure() of object' , ' TThreadMethod' , { $IF DEFINED(CPU32) and DEFINED(LAPE_CDECL) } FFI_CDECL { $ELSE } FFI_DEFAULT_ABI{ $ENDIF } );
58
+ addGlobalType(' procedure() of object' , ' TThreadMethod' , FFI_DEFAULT_ABI);
108
59
109
60
addGlobalFunc(' function CurrentThreadID: TThreadID' , @_LapeCurrentThreadID);
110
61
addGlobalFunc(' function MainThreadID: TThreadID' , @_LapeMainThreadID);
111
62
63
+ addGlobalFunc(' procedure QueueInMainThread(Method: TThreadMethod)' , @_LapeQueueInMainThread);
112
64
addGlobalFunc(' procedure RunInMainThread(Method: TThreadMethod)' , @_LapeRunInMainThread);
113
65
addGlobalFunc(' procedure RunInThread(Method: TThreadMethod)' , @_LapeRunInThread);
114
66
0 commit comments