tthread.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2002 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {****************************************************************************}
  11. {* TThread *}
  12. {****************************************************************************}
  13. {$WARNING This file is only a stub, and will not work!}
  14. const
  15. ThreadCount: longint = 0;
  16. (* Implementation of exported functions *)
  17. procedure AddThread (T: TThread);
  18. begin
  19. Inc (ThreadCount);
  20. end;
  21. procedure RemoveThread (T: TThread);
  22. begin
  23. Dec (ThreadCount);
  24. end;
  25. procedure TThread.CallOnTerminate;
  26. begin
  27. FOnTerminate (Self);
  28. end;
  29. function TThread.GetPriority: TThreadPriority;
  30. var
  31. { PTIB: PThreadInfoBlock;
  32. PPIB: PProcessInfoBlock;}
  33. I: TThreadPriority;
  34. begin
  35. {
  36. DosGetInfoBlocks (@PTIB, @PPIB);
  37. with PTIB^.TIB2^ do
  38. if Priority >= $300 then GetPriority := tpTimeCritical else
  39. if Priority < $200 then GetPriority := tpIdle else
  40. begin
  41. I := Succ (Low (TThreadPriority));
  42. while (I < High (TThreadPriority)) and
  43. (Priority - Priorities [I] <= Priorities [Succ (I)] - Priority) do Inc (I);
  44. GetPriority := I;
  45. end;
  46. }
  47. end;
  48. procedure TThread.SetPriority(Value: TThreadPriority);
  49. {var
  50. PTIB: PThreadInfoBlock;
  51. PPIB: PProcessInfoBlock;}
  52. begin
  53. { DosGetInfoBlocks (@PTIB, @PPIB);}
  54. (*
  55. PTIB^.TIB2^.Priority := Priorities [Value];
  56. *)
  57. {
  58. DosSetPriority (2, High (Priorities [Value]),
  59. Low (Priorities [Value]) - PTIB^.TIB2^.Priority, FHandle);}
  60. end;
  61. procedure TThread.SetSuspended(Value: Boolean);
  62. begin
  63. if Value <> FSuspended then
  64. begin
  65. if Value then Suspend else Resume;
  66. end;
  67. end;
  68. procedure TThread.DoTerminate;
  69. begin
  70. if Assigned (FOnTerminate) then Synchronize (@CallOnTerminate);
  71. end;
  72. function ThreadProc(Args: pointer): Integer; cdecl;
  73. var
  74. FreeThread: Boolean;
  75. Thread: TThread absolute Args;
  76. begin
  77. try
  78. Thread.Execute;
  79. except
  80. Thread.FFatalException := TObject(AcquireExceptionObject);
  81. end;
  82. FreeThread := Thread.FFreeOnTerminate;
  83. Result := Thread.FReturnValue;
  84. Thread.FFinished := True;
  85. Thread.DoTerminate;
  86. if FreeThread then Thread.Free;
  87. {
  88. DosExit (deThread, Result);
  89. }
  90. end;
  91. constructor TThread.Create(CreateSuspended: Boolean);
  92. var
  93. Flags: cardinal;
  94. begin
  95. inherited Create;
  96. AddThread (Self);
  97. {
  98. FSuspended := CreateSuspended;
  99. Flags := dtStack_Commited;
  100. if FSuspended then Flags := Flags or dtSuspended;
  101. if DosCreateThread (cardinal (FThreadID), @ThreadProc, pointer (Self),
  102. Flags, 16384) <> 0 then
  103. begin
  104. FFinished := true;
  105. Destroy;
  106. end else FHandle := FThreadID;
  107. IsMultiThread := true;
  108. FFatalException := nil;
  109. }
  110. end;
  111. destructor TThread.Destroy;
  112. begin
  113. if not FFinished and not Suspended then
  114. begin
  115. Terminate;
  116. WaitFor;
  117. end;
  118. {
  119. if FHandle <> -1 then DosKillThread (cardinal (FHandle));
  120. FFatalException.Free;
  121. FFatalException := nil;
  122. inherited Destroy;
  123. RemoveThread (Self);
  124. }
  125. end;
  126. procedure TThread.Resume;
  127. begin
  128. { FSuspended := not (DosResumeThread (cardinal (FHandle)) = 0);}
  129. end;
  130. procedure TThread.Suspend;
  131. begin
  132. { FSuspended := DosSuspendThread (cardinal (FHandle)) = 0;}
  133. end;
  134. procedure TThread.Terminate;
  135. begin
  136. FTerminated := true;
  137. end;
  138. function TThread.WaitFor: Integer;
  139. var
  140. FH: cardinal;
  141. begin
  142. { WaitFor := DosWaitThread (FH, dtWait);}
  143. end;