tthread.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2002 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************}
  12. {* TThread *}
  13. {****************************************************************************}
  14. (* OS/2 specific declarations - see unit DosCalls for descriptions *)
  15. type
  16. TByteArray = array [0..$fff0] of byte;
  17. PByteArray = ^TByteArray;
  18. TThreadEntry = function (Param: pointer): longint; cdecl;
  19. TSysThreadIB = record
  20. TID, Priority, Version: longint;
  21. MCCount, MCForceFlag: word;
  22. end;
  23. PSysThreadIB = ^TSysThreadIB;
  24. TThreadInfoBlock = record
  25. Exh_Chain, Stack, StackLimit: pointer;
  26. TIB2: PSysThreadIB;
  27. Version, Ordinal: longint;
  28. end;
  29. PThreadInfoBlock = ^TThreadInfoBlock;
  30. PPThreadInfoBlock = ^PThreadInfoBlock;
  31. TProcessInfoBlock = record
  32. PID, ParentPID, HMTE: longint;
  33. Cmd, Env: PByteArray;
  34. flStatus, tType: longint;
  35. end;
  36. PProcessInfoBlock = ^TProcessInfoBlock;
  37. PPProcessInfoBlock = ^PProcessInfoBlock;
  38. const
  39. deThread = 0;
  40. deProcess = 1;
  41. dtSuspended = 1;
  42. dtStack_Commited = 2;
  43. dtWait = 0;
  44. dtNoWait = 1;
  45. procedure DosGetInfoBlocks (PATIB: PPThreadInfoBlock;
  46. PAPIB: PPProcessInfoBlock); cdecl; external 'DOSCALLS' index 312;
  47. function DosSetPriority (Scope, TrClass, Delta, PortID: longint): longint;
  48. cdecl; external 'DOSCALLS' index 236;
  49. procedure DosExit (Action, Result: longint); cdecl;
  50. external 'DOSCALLS' index 233;
  51. function DosCreateThread (var TID: longint; Address: TThreadEntry;
  52. aParam: pointer; Flags: longint; StackSize: longint): longint; cdecl;
  53. external 'DOSCALLS' index 311;
  54. function DosKillThread (TID: longint): longint; cdecl;
  55. external 'DOSCALLS' index 111;
  56. function DosResumeThread (TID: longint): longint; cdecl;
  57. external 'DOSCALLS' index 237;
  58. function DosSuspendThread (TID: longint): longint; cdecl;
  59. external 'DOSCALLS' index 238;
  60. function DosWaitThread (var TID: longint; Option: longint): longint; cdecl;
  61. external 'DOSCALLS' index 349;
  62. const
  63. Priorities: array [TThreadPriority] of word = ($100, $200, $207, $20F, $217,
  64. $21F, $300);
  65. ThreadCount: longint = 0;
  66. (* Implementation of exported functions *)
  67. procedure AddThread (T: TThread);
  68. begin
  69. Inc (ThreadCount);
  70. end;
  71. procedure RemoveThread (T: TThread);
  72. begin
  73. Dec (ThreadCount);
  74. end;
  75. procedure TThread.CallOnTerminate;
  76. begin
  77. FOnTerminate (Self);
  78. end;
  79. function TThread.GetPriority: TThreadPriority;
  80. var
  81. PTIB: PThreadInfoBlock;
  82. PPIB: PProcessInfoBlock;
  83. I: TThreadPriority;
  84. begin
  85. DosGetInfoBlocks (@PTIB, @PPIB);
  86. with PTIB^.TIB2^ do
  87. if Priority >= $300 then GetPriority := tpTimeCritical else
  88. if Priority < $200 then GetPriority := tpIdle else
  89. begin
  90. I := Succ (Low (TThreadPriority));
  91. while (I < High (TThreadPriority)) and
  92. (Priority - Priorities [I] <= Priorities [Succ (I)] - Priority) do Inc (I);
  93. GetPriority := I;
  94. end;
  95. end;
  96. procedure TThread.SetPriority(Value: TThreadPriority);
  97. var
  98. PTIB: PThreadInfoBlock;
  99. PPIB: PProcessInfoBlock;
  100. begin
  101. DosGetInfoBlocks (@PTIB, @PPIB);
  102. (*
  103. PTIB^.TIB2^.Priority := Priorities [Value];
  104. *)
  105. DosSetPriority (2, High (Priorities [Value]),
  106. Low (Priorities [Value]) - PTIB^.TIB2^.Priority, FHandle);
  107. end;
  108. procedure TThread.SetSuspended(Value: Boolean);
  109. begin
  110. if Value <> FSuspended then
  111. begin
  112. if Value then Suspend else Resume;
  113. end;
  114. end;
  115. procedure TThread.DoTerminate;
  116. begin
  117. if Assigned (FOnTerminate) then Synchronize (@CallOnTerminate);
  118. end;
  119. procedure TThread.Synchronize(Method: TThreadMethod);
  120. begin
  121. end;
  122. function ThreadProc(Args: pointer): Integer; cdecl;
  123. var
  124. FreeThread: Boolean;
  125. Thread: TThread absolute Args;
  126. begin
  127. try
  128. Thread.Execute;
  129. except
  130. Thread.FFatalException := TObject(AcquireExceptionObject);
  131. end;
  132. FreeThread := Thread.FFreeOnTerminate;
  133. Result := Thread.FReturnValue;
  134. Thread.FFinished := True;
  135. Thread.DoTerminate;
  136. if FreeThread then Thread.Free;
  137. DosExit (deThread, Result);
  138. end;
  139. constructor TThread.Create(CreateSuspended: Boolean);
  140. var
  141. Flags: Integer;
  142. begin
  143. inherited Create;
  144. AddThread (Self);
  145. FSuspended := CreateSuspended;
  146. Flags := dtStack_Commited;
  147. if FSuspended then Flags := Flags or dtSuspended;
  148. if DosCreateThread (FThreadID, @ThreadProc, pointer (Self), Flags, 16384)
  149. <> 0 then
  150. begin
  151. FFinished := true;
  152. Destroy;
  153. end else FHandle := FThreadID;
  154. IsMultiThread := TRUE;
  155. FFatalException := nil;
  156. end;
  157. destructor TThread.Destroy;
  158. begin
  159. if not FFinished and not Suspended then
  160. begin
  161. Terminate;
  162. WaitFor;
  163. end;
  164. if FHandle <> -1 then DosKillThread (FHandle);
  165. FFatalException.Free;
  166. FFatalException := nil;
  167. inherited Destroy;
  168. RemoveThread (Self);
  169. end;
  170. procedure TThread.Resume;
  171. begin
  172. FSuspended := not (DosResumeThread (FHandle) = 0);
  173. end;
  174. procedure TThread.Suspend;
  175. begin
  176. FSuspended := DosSuspendThread (FHandle) = 0;
  177. end;
  178. procedure TThread.Terminate;
  179. begin
  180. FTerminated := true;
  181. end;
  182. function TThread.WaitFor: Integer;
  183. begin
  184. WaitFor := DosWaitThread (FHandle, dtWait);
  185. end;
  186. {
  187. $Log$
  188. Revision 1.1 2003-10-06 21:01:07 peter
  189. * moved classes unit to rtl
  190. Revision 1.8 2003/10/06 17:06:55 florian
  191. * applied Johannes Berg's patch for exception handling in threads
  192. Revision 1.7 2003/02/20 17:12:39 hajny
  193. * fixes for OS/2 v2.1 incompatibility
  194. Revision 1.6 2002/09/07 15:15:27 peter
  195. * old logs removed and tabs fixed
  196. Revision 1.5 2002/02/10 13:38:14 hajny
  197. * DosCalls dependency removed to avoid type redefinitions
  198. }