tthread.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2002 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************}
  12. {* TThread *}
  13. {****************************************************************************}
  14. {$WARNING This file is only a stub, and will not work!}
  15. const
  16. ThreadCount: longint = 0;
  17. (* Implementation of exported functions *)
  18. procedure AddThread (T: TThread);
  19. begin
  20. Inc (ThreadCount);
  21. end;
  22. procedure RemoveThread (T: TThread);
  23. begin
  24. Dec (ThreadCount);
  25. end;
  26. procedure TThread.CallOnTerminate;
  27. begin
  28. FOnTerminate (Self);
  29. end;
  30. function TThread.GetPriority: TThreadPriority;
  31. var
  32. { PTIB: PThreadInfoBlock;
  33. PPIB: PProcessInfoBlock;}
  34. I: TThreadPriority;
  35. begin
  36. {
  37. DosGetInfoBlocks (@PTIB, @PPIB);
  38. with PTIB^.TIB2^ do
  39. if Priority >= $300 then GetPriority := tpTimeCritical else
  40. if Priority < $200 then GetPriority := tpIdle else
  41. begin
  42. I := Succ (Low (TThreadPriority));
  43. while (I < High (TThreadPriority)) and
  44. (Priority - Priorities [I] <= Priorities [Succ (I)] - Priority) do Inc (I);
  45. GetPriority := I;
  46. end;
  47. }
  48. end;
  49. procedure TThread.SetPriority(Value: TThreadPriority);
  50. {var
  51. PTIB: PThreadInfoBlock;
  52. PPIB: PProcessInfoBlock;}
  53. begin
  54. { DosGetInfoBlocks (@PTIB, @PPIB);}
  55. (*
  56. PTIB^.TIB2^.Priority := Priorities [Value];
  57. *)
  58. {
  59. DosSetPriority (2, High (Priorities [Value]),
  60. Low (Priorities [Value]) - PTIB^.TIB2^.Priority, FHandle);}
  61. end;
  62. procedure TThread.SetSuspended(Value: Boolean);
  63. begin
  64. if Value <> FSuspended then
  65. begin
  66. if Value then Suspend else Resume;
  67. end;
  68. end;
  69. procedure TThread.DoTerminate;
  70. begin
  71. if Assigned (FOnTerminate) then Synchronize (@CallOnTerminate);
  72. end;
  73. function ThreadProc(Args: pointer): Integer; cdecl;
  74. var
  75. FreeThread: Boolean;
  76. Thread: TThread absolute Args;
  77. begin
  78. try
  79. Thread.Execute;
  80. except
  81. Thread.FFatalException := TObject(AcquireExceptionObject);
  82. end;
  83. FreeThread := Thread.FFreeOnTerminate;
  84. Result := Thread.FReturnValue;
  85. Thread.FFinished := True;
  86. Thread.DoTerminate;
  87. if FreeThread then Thread.Free;
  88. {
  89. DosExit (deThread, Result);
  90. }
  91. end;
  92. constructor TThread.Create(CreateSuspended: Boolean);
  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;
  145. {
  146. $Log$
  147. Revision 1.3 2005-02-25 21:41:09 florian
  148. * generic tthread.synchronize
  149. * delphi compatible wakemainthread
  150. Revision 1.2 2005/02/14 17:13:30 peter
  151. * truncate log
  152. }