tthread.inc 5.8 KB

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