tthread.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. (* OS/2 specific declarations - see unit DosCalls for descriptions *)
  14. type
  15. TByteArray = array [0..$fff0] of byte;
  16. PByteArray = ^TByteArray;
  17. TThreadEntry = function (Param: pointer): longint; cdecl;
  18. TSysThreadIB = record
  19. TID, Priority, Version: longint;
  20. MCCount, MCForceFlag: word;
  21. end;
  22. PSysThreadIB = ^TSysThreadIB;
  23. TThreadInfoBlock = record
  24. Exh_Chain, Stack, StackLimit: pointer;
  25. TIB2: PSysThreadIB;
  26. Version, Ordinal: longint;
  27. end;
  28. PThreadInfoBlock = ^TThreadInfoBlock;
  29. PPThreadInfoBlock = ^PThreadInfoBlock;
  30. TProcessInfoBlock = record
  31. PID, ParentPID, HMTE: longint;
  32. Cmd, Env: PByteArray;
  33. flStatus, tType: longint;
  34. end;
  35. PProcessInfoBlock = ^TProcessInfoBlock;
  36. PPProcessInfoBlock = ^PProcessInfoBlock;
  37. const
  38. deThread = 0;
  39. deProcess = 1;
  40. dtSuspended = 1;
  41. dtStack_Commited = 2;
  42. dtWait = 0;
  43. dtNoWait = 1;
  44. procedure DosGetInfoBlocks (PATIB: PPThreadInfoBlock;
  45. PAPIB: PPProcessInfoBlock); cdecl; external 'DOSCALLS' index 312;
  46. function DosSetPriority (Scope, TrClass: cardinal; Delta: longint;
  47. PortID: cardinal): cardinal; cdecl; external 'DOSCALLS' index 236;
  48. procedure DosExit (Action, Result: cardinal); cdecl;
  49. external 'DOSCALLS' index 233;
  50. function DosCreateThread (var TID: cardinal; Address: TThreadEntry;
  51. aParam: pointer; Flags: cardinal; StackSize: cardinal): cardinal; cdecl;
  52. external 'DOSCALLS' index 311;
  53. function DosKillThread (TID: cardinal): cardinal; cdecl;
  54. external 'DOSCALLS' index 111;
  55. function DosResumeThread (TID: cardinal): cardinal; cdecl;
  56. external 'DOSCALLS' index 237;
  57. function DosSuspendThread (TID: cardinal): cardinal; cdecl;
  58. external 'DOSCALLS' index 238;
  59. function DosWaitThread (var TID: cardinal; Option: cardinal): cardinal; cdecl;
  60. external 'DOSCALLS' index 349;
  61. const
  62. Priorities: array [TThreadPriority] of word = ($100, $200, $207, $20F, $217,
  63. $21F, $300);
  64. ThreadCount: longint = 0;
  65. (* Implementation of exported functions *)
  66. procedure AddThread (T: TThread);
  67. begin
  68. Inc (ThreadCount);
  69. end;
  70. procedure RemoveThread (T: TThread);
  71. begin
  72. Dec (ThreadCount);
  73. end;
  74. procedure TThread.CallOnTerminate;
  75. begin
  76. FOnTerminate (Self);
  77. end;
  78. function TThread.GetPriority: TThreadPriority;
  79. var
  80. PTIB: PThreadInfoBlock;
  81. PPIB: PProcessInfoBlock;
  82. I: TThreadPriority;
  83. begin
  84. DosGetInfoBlocks (@PTIB, @PPIB);
  85. with PTIB^.TIB2^ do
  86. if Priority >= $300 then GetPriority := tpTimeCritical else
  87. if Priority < $200 then GetPriority := tpIdle else
  88. begin
  89. I := Succ (Low (TThreadPriority));
  90. while (I < High (TThreadPriority)) and
  91. (Priority - Priorities [I] <= Priorities [Succ (I)] - Priority) do Inc (I);
  92. GetPriority := I;
  93. end;
  94. end;
  95. procedure TThread.SetPriority(Value: TThreadPriority);
  96. var
  97. PTIB: PThreadInfoBlock;
  98. PPIB: PProcessInfoBlock;
  99. begin
  100. DosGetInfoBlocks (@PTIB, @PPIB);
  101. (*
  102. PTIB^.TIB2^.Priority := Priorities [Value];
  103. *)
  104. DosSetPriority (2, High (Priorities [Value]),
  105. Low (Priorities [Value]) - PTIB^.TIB2^.Priority, FHandle);
  106. end;
  107. procedure TThread.SetSuspended(Value: Boolean);
  108. begin
  109. if Value <> FSuspended then
  110. begin
  111. if Value then Suspend else Resume;
  112. end;
  113. end;
  114. procedure TThread.DoTerminate;
  115. begin
  116. if Assigned (FOnTerminate) then Synchronize (@CallOnTerminate);
  117. end;
  118. constructor TThread.Create(CreateSuspended: Boolean;
  119. const StackSize: SizeUInt = DefaultStackSize);
  120. var
  121. Flags: cardinal;
  122. begin
  123. inherited Create;
  124. AddThread (Self);
  125. FSuspended := CreateSuspended;
  126. Flags := dtStack_Commited;
  127. if FSuspended then Flags := Flags or dtSuspended;
  128. if DosCreateThread (cardinal (FThreadID), @ThreadProc, pointer (Self),
  129. Flags, 16384) <> 0 then
  130. begin
  131. FFinished := true;
  132. Destroy;
  133. end else FHandle := FThreadID;
  134. IsMultiThread := true;
  135. FFatalException := nil;
  136. end;
  137. destructor TThread.Destroy;
  138. begin
  139. if not FFinished and not Suspended then
  140. begin
  141. Terminate;
  142. WaitFor;
  143. end;
  144. if FHandle <> -1 then DosKillThread (cardinal (FHandle));
  145. FFatalException.Free;
  146. FFatalException := nil;
  147. inherited Destroy;
  148. RemoveThread (Self);
  149. end;
  150. procedure TThread.Resume;
  151. begin
  152. FSuspended := not (DosResumeThread (cardinal (FHandle)) = 0);
  153. end;
  154. procedure TThread.Suspend;
  155. begin
  156. FSuspended := DosSuspendThread (cardinal (FHandle)) = 0;
  157. end;
  158. procedure TThread.Terminate;
  159. begin
  160. FTerminated := true;
  161. end;
  162. function TThread.WaitFor: Integer;
  163. var
  164. FH: cardinal;
  165. begin
  166. if GetCurrentThreadID = MainThreadID then
  167. while not (FFinished) do
  168. CheckSynchronize (1000);
  169. WaitFor := DosWaitThread (FH, dtWait);
  170. end;