thread.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. I: TThreadPriority;
  83. begin
  84. DosGetInfoBlocks (@PTIB, nil);
  85. with PTIB^.TIB2^ do
  86. if Priority >= $300 then GetPriority := tpTimeCritical else
  87. if Priority < $200 then GetPriority := tpIdle else
  88. begin
  89. I := Succ (Low (TThreadPriority));
  90. while (I < High (TThreadPriority)) and
  91. (Priority - Priorities [I] <= Priorities [Succ (I)] - Priority) do Inc (I);
  92. GetPriority := I;
  93. end;
  94. end;
  95. procedure TThread.SetPriority(Value: TThreadPriority);
  96. var
  97. PTIB: PThreadInfoBlock;
  98. begin
  99. DosGetInfoBlocks (@PTIB, nil);
  100. (*
  101. PTIB^.TIB2^.Priority := Priorities [Value];
  102. *)
  103. DosSetPriority (2, High (Priorities [Value]),
  104. Low (Priorities [Value]) - PTIB^.TIB2^.Priority, FHandle);
  105. end;
  106. procedure TThread.SetSuspended(Value: Boolean);
  107. begin
  108. if Value <> FSuspended then
  109. begin
  110. if Value then Suspend else Resume;
  111. end;
  112. end;
  113. procedure TThread.DoTerminate;
  114. begin
  115. if Assigned (FOnTerminate) then Synchronize (@CallOnTerminate);
  116. end;
  117. procedure TThread.Synchronize(Method: TThreadMethod);
  118. begin
  119. end;
  120. function ThreadProc(Args: pointer): Integer; cdecl;
  121. var
  122. FreeThread: Boolean;
  123. Thread: TThread absolute Args;
  124. begin
  125. Thread.Execute;
  126. FreeThread := Thread.FFreeOnTerminate;
  127. Result := Thread.FReturnValue;
  128. Thread.FFinished := True;
  129. Thread.DoTerminate;
  130. if FreeThread then Thread.Free;
  131. DosExit (deThread, Result);
  132. end;
  133. constructor TThread.Create(CreateSuspended: Boolean);
  134. var
  135. Flags: Integer;
  136. begin
  137. inherited Create;
  138. AddThread (Self);
  139. FSuspended := CreateSuspended;
  140. Flags := dtStack_Commited;
  141. if FSuspended then Flags := Flags or dtSuspended;
  142. if DosCreateThread (FThreadID, @ThreadProc, pointer (Self), Flags, 16384)
  143. <> 0 then
  144. begin
  145. FFinished := true;
  146. Destroy;
  147. end else FHandle := FThreadID;
  148. IsMultiThread := TRUE;
  149. end;
  150. destructor TThread.Destroy;
  151. begin
  152. if not FFinished and not Suspended then
  153. begin
  154. Terminate;
  155. WaitFor;
  156. end;
  157. if FHandle <> -1 then DosKillThread (FHandle);
  158. inherited Destroy;
  159. RemoveThread (Self);
  160. end;
  161. procedure TThread.Resume;
  162. begin
  163. FSuspended := not (DosResumeThread (FHandle) = 0);
  164. end;
  165. procedure TThread.Suspend;
  166. begin
  167. FSuspended := DosSuspendThread (FHandle) = 0;
  168. end;
  169. procedure TThread.Terminate;
  170. begin
  171. FTerminated := true;
  172. end;
  173. function TThread.WaitFor: Integer;
  174. begin
  175. WaitFor := DosWaitThread (FHandle, dtWait);
  176. end;
  177. {
  178. $Log$
  179. Revision 1.6 2002-09-07 15:15:27 peter
  180. * old logs removed and tabs fixed
  181. Revision 1.5 2002/02/10 13:38:14 hajny
  182. * DosCalls dependency removed to avoid type redefinitions
  183. }