tthread.inc 5.7 KB

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