tthread.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. { Thread management routines }
  2. type
  3. PRaiseFrame = ^TRaiseFrame;
  4. TRaiseFrame = record
  5. NextRaise: PRaiseFrame;
  6. ExceptAddr: Pointer;
  7. ExceptObject: TObject;
  8. ExceptionRecord: pointer; {PExceptionRecord}
  9. end;
  10. constructor TThread.Create(CreateSuspended: Boolean;
  11. const StackSize: SizeUInt = DefaultStackSize);
  12. var
  13. Flags: Integer;
  14. begin
  15. inherited Create;
  16. FSuspended := CreateSuspended;
  17. Flags := 0;
  18. if CreateSuspended then Flags := CREATE_SUSPENDED;
  19. FHandle := BeginThread(nil, StackSize, @ThreadProc, pointer(self), Flags,
  20. FThreadID);
  21. if FHandle = TThreadID(0) then
  22. raise EThread.create('Failed to create new thread, code:'+inttostr(getlasterror));
  23. FFatalException := nil;
  24. end;
  25. destructor TThread.Destroy;
  26. begin
  27. if FHandle<>0 then
  28. begin
  29. if not FFinished and not Suspended then
  30. begin
  31. Terminate;
  32. WaitFor;
  33. end;
  34. CloseHandle(FHandle);
  35. end;
  36. FFatalException.Free;
  37. FFatalException := nil;
  38. inherited Destroy;
  39. end;
  40. procedure TThread.CallOnTerminate;
  41. begin
  42. FOnTerminate(Self);
  43. end;
  44. procedure TThread.DoTerminate;
  45. begin
  46. if Assigned(FOnTerminate) then
  47. Synchronize(@CallOnTerminate);
  48. end;
  49. const
  50. Priorities: array [TThreadPriority] of Integer =
  51. (THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
  52. THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL,
  53. THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL);
  54. function TThread.GetPriority: TThreadPriority;
  55. var
  56. P: Integer;
  57. I: TThreadPriority;
  58. begin
  59. P := GetThreadPriority(FHandle);
  60. Result := tpNormal;
  61. for I := Low(TThreadPriority) to High(TThreadPriority) do
  62. if Priorities[I] = P then Result := I;
  63. end;
  64. procedure TThread.SetPriority(Value: TThreadPriority);
  65. begin
  66. SetThreadPriority(FHandle, Priorities[Value]);
  67. end;
  68. procedure TThread.SetSuspended(Value: Boolean);
  69. begin
  70. if Value <> FSuspended then
  71. if Value then
  72. Suspend
  73. else
  74. Resume;
  75. end;
  76. procedure TThread.Suspend;
  77. begin
  78. FSuspended := True;
  79. SuspendThread(FHandle);
  80. end;
  81. procedure TThread.Resume;
  82. begin
  83. if ResumeThread(FHandle) = 1 then FSuspended := False;
  84. end;
  85. procedure TThread.Terminate;
  86. begin
  87. FTerminated := True;
  88. end;
  89. function TThread.WaitFor: Integer;
  90. var
  91. Msg: TMsg;
  92. WaitHandles : array[0..1] of THandle;
  93. begin
  94. if GetCurrentThreadID = MainThreadID then
  95. begin
  96. WaitHandles[0]:=FHandle;
  97. WaitHandles[1]:=THandle(SynchronizeTimeoutEvent);
  98. while true do
  99. begin
  100. case MsgWaitForMultipleObjects(2, WaitHandles, False, INFINITE, QS_SENDMESSAGE) of
  101. WAIT_OBJECT_0:
  102. break;
  103. WAIT_OBJECT_0+1:
  104. CheckSynchronize;
  105. WAIT_OBJECT_0+2:
  106. PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE)
  107. end;
  108. end;
  109. end
  110. else
  111. WaitForSingleObject(ulong(FHandle), INFINITE);
  112. GetExitCodeThread(FHandle, DWord(Result));
  113. end;