tthread.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. const StackSize: SizeUInt = DefaultStackSize);
  93. var
  94. Flags: cardinal;
  95. begin
  96. inherited Create;
  97. AddThread (Self);
  98. {
  99. FSuspended := CreateSuspended;
  100. Flags := dtStack_Commited;
  101. if FSuspended then Flags := Flags or dtSuspended;
  102. if DosCreateThread (cardinal (FThreadID), @ThreadProc, pointer (Self),
  103. Flags, 16384) <> 0 then
  104. begin
  105. FFinished := true;
  106. Destroy;
  107. end else FHandle := FThreadID;
  108. IsMultiThread := true;
  109. FFatalException := nil;
  110. }
  111. end;
  112. destructor TThread.Destroy;
  113. begin
  114. if not FFinished and not Suspended then
  115. begin
  116. Terminate;
  117. WaitFor;
  118. end;
  119. {
  120. if FHandle <> -1 then DosKillThread (cardinal (FHandle));
  121. FFatalException.Free;
  122. FFatalException := nil;
  123. inherited Destroy;
  124. RemoveThread (Self);
  125. }
  126. end;
  127. procedure TThread.Resume;
  128. begin
  129. { FSuspended := not (DosResumeThread (cardinal (FHandle)) = 0);}
  130. end;
  131. procedure TThread.Suspend;
  132. begin
  133. { FSuspended := DosSuspendThread (cardinal (FHandle)) = 0;}
  134. end;
  135. procedure TThread.Terminate;
  136. begin
  137. FTerminated := true;
  138. end;
  139. function TThread.WaitFor: Integer;
  140. var
  141. FH: cardinal;
  142. begin
  143. { WaitFor := DosWaitThread (FH, dtWait);}
  144. end;