tthread.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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: 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. procedure TThread.SetSuspended(Value: Boolean);
  165. begin
  166. if Value <> FSuspended then
  167. if Value then
  168. Suspend else
  169. Resume;
  170. end;
  171. procedure TThread.Suspend;
  172. begin
  173. FSuspended := True;
  174. SuspendThread(FHandle);
  175. end;
  176. procedure TThread.Resume;
  177. begin
  178. if ResumeThread(FHandle) = 1 then FSuspended := False;
  179. end;
  180. procedure TThread.Terminate;
  181. begin
  182. FTerminated := True;
  183. end;
  184. function TThread.WaitFor: Integer;
  185. var
  186. Msg: TMsg;
  187. begin
  188. if GetCurrentThreadID = MainThreadID then
  189. while MsgWaitForMultipleObjects(1, FHandle, False, INFINITE, QS_SENDMESSAGE) = WAIT_OBJECT_0 + 1 do
  190. PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE)
  191. else
  192. WaitForSingleObject(ulong(FHandle), INFINITE);
  193. GetExitCodeThread(FHandle, DWord(Result));
  194. end;
  195. {
  196. $Log$
  197. Revision 1.7 2005-02-25 21:41:09 florian
  198. * generic tthread.synchronize
  199. * delphi compatible wakemainthread
  200. Revision 1.6 2005/02/14 17:13:32 peter
  201. * truncate log
  202. Revision 1.5 2005/02/06 13:06:20 peter
  203. * moved file and dir functions to sysfile/sysdir
  204. * win32 thread in systemunit
  205. }