tthread.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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: cardinal; Delta: longint;
  48. PortID: cardinal): cardinal; cdecl; external 'DOSCALLS' index 236;
  49. procedure DosExit (Action, Result: cardinal); cdecl;
  50. external 'DOSCALLS' index 233;
  51. function DosCreateThread (var TID: cardinal; Address: TThreadEntry;
  52. aParam: pointer; Flags: cardinal; StackSize: cardinal): cardinal; cdecl;
  53. external 'DOSCALLS' index 311;
  54. function DosKillThread (TID: cardinal): cardinal; cdecl;
  55. external 'DOSCALLS' index 111;
  56. function DosResumeThread (TID: cardinal): cardinal; cdecl;
  57. external 'DOSCALLS' index 237;
  58. function DosSuspendThread (TID: cardinal): cardinal; cdecl;
  59. external 'DOSCALLS' index 238;
  60. function DosWaitThread (var TID: cardinal; Option: cardinal): cardinal; 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. function ThreadProc(Args: pointer): Integer; cdecl;
  120. var
  121. FreeThread: Boolean;
  122. Thread: TThread absolute Args;
  123. begin
  124. try
  125. Thread.Execute;
  126. except
  127. Thread.FFatalException := TObject(AcquireExceptionObject);
  128. end;
  129. FreeThread := Thread.FFreeOnTerminate;
  130. Result := Thread.FReturnValue;
  131. Thread.FFinished := True;
  132. Thread.DoTerminate;
  133. if FreeThread then Thread.Free;
  134. DosExit (deThread, Result);
  135. end;
  136. constructor TThread.Create(CreateSuspended: Boolean);
  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. WaitFor := DosWaitThread (FH, dtWait);
  184. end;
  185. {
  186. $Log$
  187. Revision 1.6 2005-02-25 21:41:09 florian
  188. * generic tthread.synchronize
  189. * delphi compatible wakemainthread
  190. Revision 1.5 2005/02/14 17:13:31 peter
  191. * truncate log
  192. }