tthread.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. function ThreadProc(Args: pointer): Integer; cdecl;
  119. var
  120. FreeThread: Boolean;
  121. Thread: TThread absolute Args;
  122. begin
  123. try
  124. Thread.Execute;
  125. except
  126. Thread.FFatalException := TObject(AcquireExceptionObject);
  127. end;
  128. FreeThread := Thread.FFreeOnTerminate;
  129. Result := Thread.FReturnValue;
  130. Thread.FFinished := True;
  131. Thread.DoTerminate;
  132. if FreeThread then Thread.Free;
  133. DosExit (deThread, Result);
  134. end;
  135. constructor TThread.Create(CreateSuspended: Boolean;
  136. const StackSize: SizeUInt = DefaultStackSize);
  137. var
  138. Flags: cardinal;
  139. begin
  140. inherited Create;
  141. AddThread (Self);
  142. FSuspended := CreateSuspended;
  143. Flags := dtStack_Commited;
  144. if FSuspended then Flags := Flags or dtSuspended;
  145. if DosCreateThread (cardinal (FThreadID), @ThreadProc, pointer (Self),
  146. Flags, 16384) <> 0 then
  147. begin
  148. FFinished := true;
  149. Destroy;
  150. end else FHandle := FThreadID;
  151. IsMultiThread := true;
  152. FFatalException := nil;
  153. end;
  154. destructor TThread.Destroy;
  155. begin
  156. if not FFinished and not Suspended then
  157. begin
  158. Terminate;
  159. WaitFor;
  160. end;
  161. if FHandle <> -1 then DosKillThread (cardinal (FHandle));
  162. FFatalException.Free;
  163. FFatalException := nil;
  164. inherited Destroy;
  165. RemoveThread (Self);
  166. end;
  167. procedure TThread.Resume;
  168. begin
  169. FSuspended := not (DosResumeThread (cardinal (FHandle)) = 0);
  170. end;
  171. procedure TThread.Suspend;
  172. begin
  173. FSuspended := DosSuspendThread (cardinal (FHandle)) = 0;
  174. end;
  175. procedure TThread.Terminate;
  176. begin
  177. FTerminated := true;
  178. end;
  179. function TThread.WaitFor: Integer;
  180. var
  181. FH: cardinal;
  182. begin
  183. if GetCurrentThreadID = MainThreadID then
  184. while not (FFinished) do
  185. CheckSynchronize (1000);
  186. WaitFor := DosWaitThread (FH, dtWait);
  187. end;