thread.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 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. const
  15. Priorities: array [TThreadPriority] of word = ($100, $200, $207, $20F, $217,
  16. $21F, $300);
  17. ThreadCount: longint = 0;
  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. I: TThreadPriority;
  34. begin
  35. DosGetInfoBlocks (@PTIB, nil);
  36. with PTIB^.TIB2^ do
  37. if Priority >= $300 then GetPriority := tpTimeCritical else
  38. if Priority < $200 then GetPriority := tpIdle else
  39. begin
  40. I := Succ (Low (TThreadPriority));
  41. while (I < High (TThreadPriority)) and
  42. (Priority - Priorities [I] <= Priorities [Succ (I)] - Priority) do Inc (I);
  43. GetPriority := I;
  44. end;
  45. end;
  46. procedure TThread.SetPriority(Value: TThreadPriority);
  47. var
  48. PTIB: PThreadInfoBlock;
  49. begin
  50. DosGetInfoBlocks (@PTIB, nil);
  51. (*
  52. PTIB^.TIB2^.Priority := Priorities [Value];
  53. *)
  54. DosSetPriority (2, High (Priorities [Value]),
  55. Low (Priorities [Value]) - PTIB^.TIB2^.Priority, FHandle);
  56. end;
  57. procedure TThread.SetSuspended(Value: Boolean);
  58. begin
  59. if Value <> FSuspended then
  60. begin
  61. if Value then Suspend else Resume;
  62. end;
  63. end;
  64. procedure TThread.DoTerminate;
  65. begin
  66. if Assigned (FOnTerminate) then Synchronize (@CallOnTerminate);
  67. end;
  68. procedure TThread.Synchronize(Method: TThreadMethod);
  69. begin
  70. end;
  71. function ThreadProc(Args: pointer): Integer; cdecl;
  72. var
  73. FreeThread: Boolean;
  74. Thread: TThread absolute Args;
  75. begin
  76. Thread.Execute;
  77. FreeThread := Thread.FFreeOnTerminate;
  78. Result := Thread.FReturnValue;
  79. Thread.FFinished := True;
  80. Thread.DoTerminate;
  81. if FreeThread then Thread.Free;
  82. DosExit (deThread, Result);
  83. end;
  84. constructor TThread.Create(CreateSuspended: Boolean);
  85. var
  86. Flags: Integer;
  87. begin
  88. inherited Create;
  89. AddThread (Self);
  90. FSuspended := CreateSuspended;
  91. Flags := dtStack_Commited;
  92. if FSuspended then Flags := Flags or dtSuspended;
  93. if DosCreateThread (FThreadID, @ThreadProc, pointer (Self), Flags, 16384)
  94. <> 0 then
  95. begin
  96. FFinished := true;
  97. Destroy;
  98. end else FHandle := FThreadID;
  99. IsMultiThread := TRUE;
  100. end;
  101. destructor TThread.Destroy;
  102. begin
  103. if not FFinished and not Suspended then
  104. begin
  105. Terminate;
  106. WaitFor;
  107. end;
  108. if FHandle <> -1 then DosKillThread (FHandle);
  109. inherited Destroy;
  110. RemoveThread (Self);
  111. end;
  112. procedure TThread.Resume;
  113. begin
  114. FSuspended := not (DosResumeThread (FHandle) = 0);
  115. end;
  116. procedure TThread.Suspend;
  117. begin
  118. FSuspended := DosSuspendThread (FHandle) = 0;
  119. end;
  120. procedure TThread.Terminate;
  121. begin
  122. FTerminated := true;
  123. end;
  124. function TThread.WaitFor: Integer;
  125. begin
  126. WaitFor := DosWaitThread (FHandle, dtWait);
  127. end;
  128. {
  129. $Log$
  130. Revision 1.4 2001-10-09 02:21:00 carl
  131. * bugfix #1639 (IsMultiThread varialbe setting)
  132. Revision 1.3 2000/12/19 00:43:07 hajny
  133. + FCL made compilable under OS/2
  134. Revision 1.2 2000/07/13 11:33:02 michael
  135. + removed logs
  136. }