tthread.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. { Thread management routines }
  2. const
  3. CM_EXECPROC = $8FFF;
  4. CM_DESTROYWINDOW = $8FFE;
  5. type
  6. PRaiseFrame = ^TRaiseFrame;
  7. TRaiseFrame = record
  8. NextRaise: PRaiseFrame;
  9. ExceptAddr: Pointer;
  10. ExceptObject: TObject;
  11. ExceptionRecord: pointer; {PExceptionRecord}
  12. end;
  13. var
  14. ThreadWindow: HWND;
  15. ThreadCount: Integer;
  16. { event that happens when gui thread is done executing the method
  17. }
  18. ExecuteEvent: PRtlEvent;
  19. { guard for synchronization variables }
  20. SynchronizeCritSect: systhrds.TRtlCriticalSection;
  21. { method to execute }
  22. SynchronizeMethod: TThreadMethod;
  23. { caught exception in gui thread, to be raised in calling thread }
  24. SynchronizeException: Exception;
  25. function ThreadWndProc(Window: HWnd; AMessage:UInt; WParam : WParam; LParam: LParam): Longint; stdcall;
  26. begin
  27. case AMessage of
  28. CM_EXECPROC:
  29. with TThread(lParam) do
  30. begin
  31. Result := 0;
  32. try
  33. FSynchronizeException := nil;
  34. FMethod;
  35. except
  36. { if RaiseList <> nil then
  37. begin
  38. FSynchronizeException := PRaiseFrame(RaiseList)^.ExceptObject;
  39. PRaiseFrame(RaiseList)^.ExceptObject := nil;
  40. end; }
  41. end;
  42. end;
  43. CM_DESTROYWINDOW:
  44. begin
  45. DestroyWindow(Window);
  46. Result := 0;
  47. end;
  48. else
  49. Result := DefWindowProc(Window, AMessage, wParam, lParam);
  50. end;
  51. end;
  52. const
  53. ThreadWindowClass: TWndClass = (
  54. style: 0;
  55. lpfnWndProc: nil;
  56. cbClsExtra: 0;
  57. cbWndExtra: 0;
  58. hInstance: 0;
  59. hIcon: 0;
  60. hCursor: 0;
  61. hbrBackground: 0;
  62. lpszMenuName: nil;
  63. lpszClassName: 'TThreadWindow');
  64. procedure AddThread;
  65. function AllocateWindow: HWND;
  66. var
  67. TempClass: TWndClass;
  68. ClassRegistered: Boolean;
  69. begin
  70. ThreadWindowClass.hInstance := HInstance;
  71. ThreadWindowClass.lpfnWndProc:=WndProc(@ThreadWndProc);
  72. ClassRegistered := GetClassInfo(HInstance, ThreadWindowClass.lpszClassName,
  73. @TempClass);
  74. if not ClassRegistered or (TempClass.lpfnWndProc <> WndProc(@ThreadWndProc)) then
  75. begin
  76. if ClassRegistered then
  77. Windows.UnregisterClass(ThreadWindowClass.lpszClassName, HInstance);
  78. Windows.RegisterClass(ThreadWindowClass);
  79. end;
  80. Result := CreateWindow(ThreadWindowClass.lpszClassName, '', 0,
  81. 0, 0, 0, 0, 0, 0, HInstance, nil);
  82. end;
  83. begin
  84. if ThreadCount = 0 then
  85. ThreadWindow := AllocateWindow;
  86. Inc(ThreadCount);
  87. end;
  88. procedure RemoveThread;
  89. begin
  90. Dec(ThreadCount);
  91. if ThreadCount = 0 then
  92. PostMessage(ThreadWindow, CM_DESTROYWINDOW, 0, 0);
  93. end;
  94. { TThread }
  95. function ThreadProc(ThreadObjPtr: Pointer): Integer;
  96. var
  97. FreeThread: Boolean;
  98. Thread: TThread absolute ThreadObjPtr;
  99. begin
  100. try
  101. Thread.Execute;
  102. except
  103. Thread.FFatalException := TObject(AcquireExceptionObject);
  104. end;
  105. FreeThread := Thread.FFreeOnTerminate;
  106. Result := Thread.FReturnValue;
  107. Thread.FFinished := True;
  108. Thread.DoTerminate;
  109. if FreeThread then Thread.Free;
  110. end;
  111. constructor TThread.Create(CreateSuspended: Boolean);
  112. var
  113. Flags: Integer;
  114. begin
  115. inherited Create;
  116. AddThread;
  117. FSuspended := CreateSuspended;
  118. Flags := 0;
  119. if CreateSuspended then Flags := CREATE_SUSPENDED;
  120. FHandle := BeginThread(nil, 0, @ThreadProc, pointer(self), Flags, FThreadID);
  121. FFatalException := nil;
  122. end;
  123. destructor TThread.Destroy;
  124. begin
  125. if not FFinished and not Suspended then
  126. begin
  127. Terminate;
  128. WaitFor;
  129. end;
  130. if FHandle <> 0 then CloseHandle(FHandle);
  131. FFatalException.Free;
  132. FFatalException := nil;
  133. inherited Destroy;
  134. RemoveThread;
  135. end;
  136. procedure TThread.CallOnTerminate;
  137. begin
  138. FOnTerminate(Self);
  139. end;
  140. procedure TThread.DoTerminate;
  141. begin
  142. if Assigned(FOnTerminate) then
  143. Synchronize(@CallOnTerminate);
  144. end;
  145. const
  146. Priorities: array [TThreadPriority] of Integer =
  147. (THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
  148. THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL,
  149. THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL);
  150. function TThread.GetPriority: TThreadPriority;
  151. var
  152. P: Integer;
  153. I: TThreadPriority;
  154. begin
  155. P := GetThreadPriority(FHandle);
  156. Result := tpNormal;
  157. for I := Low(TThreadPriority) to High(TThreadPriority) do
  158. if Priorities[I] = P then Result := I;
  159. end;
  160. procedure TThread.SetPriority(Value: TThreadPriority);
  161. begin
  162. SetThreadPriority(FHandle, Priorities[Value]);
  163. end;
  164. { old implementation? :
  165. procedure TThread.Synchronize(Method: TThreadMethod);
  166. begin
  167. FSynchronizeException := nil;
  168. FMethod := Method;
  169. SendMessage(ThreadWindow, CM_EXECPROC, 0, Longint(Self));
  170. if Assigned(FSynchronizeException) then raise FSynchronizeException;
  171. end;
  172. }
  173. procedure TThread.Synchronize(Method: TThreadMethod);
  174. var
  175. LocalSyncException: Exception;
  176. begin
  177. if SynchronizeMethodProc = nil then
  178. { raise some error? }
  179. exit;
  180. systhrds.EnterCriticalSection(SynchronizeCritSect);
  181. SynchronizeMethod := Method;
  182. SynchronizeException := nil;
  183. SynchronizeMethodProc;
  184. // wait infinitely
  185. RtlEventWaitFor(ExecuteEvent);
  186. SynchronizeMethod := nil;
  187. LocalSyncException := SynchronizeException;
  188. systhrds.LeaveCriticalSection(SynchronizeCritSect);
  189. if LocalSyncException <> nil then
  190. raise LocalSyncException;
  191. end;
  192. procedure CheckSynchronize;
  193. { assumes being called from GUI thread }
  194. begin
  195. if SynchronizeMethod = nil then
  196. exit;
  197. try
  198. SynchronizeMethod;
  199. except
  200. SynchronizeException := Exception(AcquireExceptionObject);
  201. end;
  202. RtlEventSetEvent(ExecuteEvent);
  203. end;
  204. procedure TThread.SetSuspended(Value: Boolean);
  205. begin
  206. if Value <> FSuspended then
  207. if Value then
  208. Suspend else
  209. Resume;
  210. end;
  211. procedure TThread.Suspend;
  212. begin
  213. FSuspended := True;
  214. SuspendThread(FHandle);
  215. end;
  216. procedure TThread.Resume;
  217. begin
  218. if ResumeThread(FHandle) = 1 then FSuspended := False;
  219. end;
  220. procedure TThread.Terminate;
  221. begin
  222. FTerminated := True;
  223. end;
  224. function TThread.WaitFor: Integer;
  225. var
  226. Msg: TMsg;
  227. begin
  228. if GetCurrentThreadID = MainThreadID then
  229. while MsgWaitForMultipleObjects(1, FHandle, False, INFINITE, QS_SENDMESSAGE) = WAIT_OBJECT_0 + 1 do
  230. PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE)
  231. else
  232. WaitForSingleObject(ulong(FHandle), INFINITE);
  233. GetExitCodeThread(FHandle, DWord(Result));
  234. end;
  235. {
  236. $Log$
  237. Revision 1.4 2004-12-26 13:46:45 peter
  238. * tthread uses systhrds
  239. Revision 1.3 2004/12/23 09:42:42 marco
  240. * first tthread.synchronize support (merged neli's patches)
  241. Revision 1.2 2004/01/29 16:58:28 marco
  242. * threadproc is passed to OS and must be stdcall;
  243. Revision 1.1 2003/10/06 21:01:07 peter
  244. * moved classes unit to rtl
  245. Revision 1.8 2003/10/06 17:06:55 florian
  246. * applied Johannes Berg's patch for exception handling in threads
  247. Revision 1.7 2003/04/23 11:35:30 peter
  248. * wndproc definition fix
  249. Revision 1.6 2002/09/07 15:15:29 peter
  250. * old logs removed and tabs fixed
  251. }