thread.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. end;
  100. destructor TThread.Destroy;
  101. begin
  102. if not FFinished and not Suspended then
  103. begin
  104. Terminate;
  105. WaitFor;
  106. end;
  107. if FHandle <> -1 then DosKillThread (FHandle);
  108. inherited Destroy;
  109. RemoveThread (Self);
  110. end;
  111. procedure TThread.Resume;
  112. begin
  113. FSuspended := not (DosResumeThread (FHandle) = 0);
  114. end;
  115. procedure TThread.Suspend;
  116. begin
  117. FSuspended := DosSuspendThread (FHandle) = 0;
  118. end;
  119. procedure TThread.Terminate;
  120. begin
  121. FTerminated := true;
  122. end;
  123. function TThread.WaitFor: Integer;
  124. begin
  125. WaitFor := DosWaitThread (FHandle, dtWait);
  126. end;
  127. {
  128. $Log$
  129. Revision 1.3 2000-12-19 00:43:07 hajny
  130. + FCL made compilable under OS/2
  131. Revision 1.2 2000/07/13 11:33:02 michael
  132. + removed logs
  133. }