thread.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. procedure AddThread;
  18. begin
  19. Inc (ThreadCount);
  20. end;
  21. procedure RemoveThread;
  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. I: TThreadPriority;
  33. begin
  34. DosGetInfoBlocks (@PTIB, nil);
  35. with PTIB^.TIB2^ do
  36. if Priority >= $300 then GetPriority := tpTimeCritical else
  37. if Priority < $200 then GetPriority := tpIdle else
  38. begin
  39. I := Succ (Low (TThreadPriority));
  40. while (I < High (TThreadPriority)) and
  41. (Priority - Priorities [I] <= Priorities [Succ (I)] - Priority) do Inc (I);
  42. GetPriority := I;
  43. end;
  44. end;
  45. procedure TThread.SetPriority(Value: TThreadPriority);
  46. var
  47. PTIB: PThreadInfoBlock;
  48. begin
  49. DosGetInfoBlocks (@PTIB, nil);
  50. (*
  51. PTIB^.TIB2^.Priority := Priorities [Value];
  52. *)
  53. DosSetPriority (2, High (Priorities [Value]),
  54. Low (Priorities [Value]) - PTIB^.TIB2^.Priority, FHandle);
  55. end;
  56. procedure TThread.SetSuspended(Value: Boolean);
  57. begin
  58. if Value <> FSuspended then
  59. begin
  60. if Value then Suspend else Resume;
  61. end;
  62. end;
  63. procedure TThread.DoTerminate;
  64. begin
  65. if Assigned (FOnTerminate) then Synchronize (@CallOnTerminate);
  66. end;
  67. procedure TThread.Synchronize(Method: TThreadMethod);
  68. begin
  69. end;
  70. function ThreadProc(Thread: TThread): Integer; cdecl;
  71. var
  72. FreeThread: Boolean;
  73. begin
  74. Thread.Execute;
  75. FreeThread := Thread.FFreeOnTerminate;
  76. Result := Thread.FReturnValue;
  77. Thread.FFinished := True;
  78. Thread.DoTerminate;
  79. if FreeThread then Thread.Free;
  80. DosExit (deThread, Result);
  81. end;
  82. constructor TThread.Create(CreateSuspended: Boolean);
  83. var
  84. Flags: Integer;
  85. begin
  86. inherited Create;
  87. AddThread (Self);
  88. FSuspended := CreateSuspended;
  89. Flags := dtStack_Committed;
  90. if FSuspended then Flags := Flags or dtSuspended;
  91. if DosCreateThread (FThreadID, @ThreadProc, pointer (Self), Flags, 16384)
  92. <> 0 then
  93. begin
  94. FFinished := true;
  95. Destroy;
  96. end else FHandle := FThreadID;
  97. end;
  98. destructor TThread.Destroy;
  99. begin
  100. if not FFinished and not Suspended then
  101. begin
  102. Terminate;
  103. WaitFor;
  104. end;
  105. if FHandle <> -1 then DosKillThread (FHandle);
  106. inherited Destroy;
  107. RemoveThread (Self);
  108. end;
  109. procedure TThread.Resume;
  110. begin
  111. FSuspended := not (DosResumeThread (FHandle) = 0);
  112. end;
  113. procedure TThread.Suspend;
  114. begin
  115. FSuspended := DosSuspendThread (FHandle) = 0;
  116. end;
  117. procedure TThread.Terminate;
  118. begin
  119. FTerminated := true;
  120. end;
  121. function TThread.WaitFor: Integer;
  122. begin
  123. WaitFor := DosWaitThread (FHandle, dtWait);
  124. end;
  125. {
  126. $Log$
  127. Revision 1.1 2000-07-13 06:33:44 michael
  128. + Initial import
  129. Revision 1.5 2000/04/01 10:45:52 hajny
  130. OS/2 implementation started
  131. Revision 1.4 2000/01/07 01:24:34 peter
  132. * updated copyright to 2000
  133. Revision 1.3 2000/01/06 01:20:34 peter
  134. * moved out of packages/ back to topdir
  135. Revision 1.1 2000/01/03 19:33:09 peter
  136. * moved to packages dir
  137. Revision 1.1 1999/05/30 10:46:43 peter
  138. * start of tthread for linux,win32
  139. Revision 1.2 1999/04/08 10:18:57 peter
  140. * makefile updates
  141. }