tthread.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. procedure TThread.SysCreate(CreateSuspended: Boolean;
  80. const StackSize: SizeUInt);
  81. var
  82. Flags: cardinal;
  83. begin
  84. AddThread;
  85. { Always start in suspended state, will be resumed in AfterConstruction if necessary
  86. See Mantis #16884 }
  87. Flags := dtStack_Commited or dtSuspended;
  88. FSuspended := CreateSuspended;
  89. FInitialSuspended := CreateSuspended;
  90. if FSuspended then Flags := Flags or dtSuspended;
  91. FHandle := BeginThread (nil, StackSize, @ThreadProc, pointer (Self),
  92. Flags, FThreadID);
  93. FFatalException := nil;
  94. end;
  95. procedure TThread.SysDestroy;
  96. begin
  97. if not FFinished and not Suspended then
  98. begin
  99. Terminate;
  100. WaitFor;
  101. end;
  102. { if FHandle <> 0 then DosKillThread (cardinal (FHandle));}
  103. FFatalException.Free;
  104. FFatalException := nil;
  105. RemoveThread;
  106. end;
  107. procedure TThread.CallOnTerminate;
  108. begin
  109. FOnTerminate (Self);
  110. end;
  111. procedure TThread.DoTerminate;
  112. begin
  113. if Assigned (FOnTerminate) then
  114. Synchronize (@CallOnTerminate);
  115. end;
  116. function TThread.GetPriority: TThreadPriority;
  117. var
  118. PTIB: PThreadInfoBlock;
  119. PPIB: PProcessInfoBlock;
  120. I: TThreadPriority;
  121. begin
  122. DosGetInfoBlocks (@PTIB, @PPIB);
  123. with PTIB^.TIB2^ do
  124. if Priority >= $300 then GetPriority := tpTimeCritical else
  125. if Priority < $200 then GetPriority := tpIdle else
  126. begin
  127. I := Succ (Low (TThreadPriority));
  128. while (I < High (TThreadPriority)) and
  129. (Priority - Priorities [I] <= Priorities [Succ (I)] - Priority) do Inc (I);
  130. GetPriority := I;
  131. end;
  132. end;
  133. procedure TThread.SetPriority(Value: TThreadPriority);
  134. var
  135. PTIB: PThreadInfoBlock;
  136. PPIB: PProcessInfoBlock;
  137. RC: cardinal;
  138. begin
  139. DosGetInfoBlocks (@PTIB, @PPIB);
  140. (*
  141. PTIB^.TIB2^.Priority := Priorities [Value];
  142. *)
  143. RC := DosSetPriority (2, High (Priorities [Value]),
  144. Low (Priorities [Value]) - PTIB^.TIB2^.Priority, FHandle);
  145. if RC <> 0 then
  146. OSErrorWatch (RC);
  147. end;
  148. procedure TThread.SetSuspended(Value: Boolean);
  149. begin
  150. if Value <> FSuspended then
  151. begin
  152. if Value then
  153. Suspend
  154. else
  155. Resume;
  156. end;
  157. end;
  158. procedure TThread.Suspend;
  159. begin
  160. FSuspended := true;
  161. SuspendThread (FHandle);
  162. {DosSuspendThread (cardinal (FHandle)) = 0;}
  163. end;
  164. procedure TThread.Resume;
  165. begin
  166. if ResumeThread (FHandle) = 1 then
  167. FSuspended := false;
  168. { FSuspended := not (DosResumeThread (cardinal (FHandle)) = 0);}
  169. end;
  170. procedure TThread.Terminate;
  171. begin
  172. FTerminated := true;
  173. TerminatedSet;
  174. end;
  175. function TThread.WaitFor: Integer;
  176. var
  177. FH: cardinal;
  178. RC: cardinal;
  179. begin
  180. if GetCurrentThreadID = MainThreadID then
  181. while not (FFinished) do
  182. CheckSynchronize (1000);
  183. RC := DosWaitThread (FH, dtWait);
  184. if RC <> 0 then
  185. OSErrorWatch (RC);
  186. WaitFor := RC;
  187. end;