thread.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. Thread.Execute;
  128. FreeThread := Thread.FFreeOnTerminate;
  129. Result := Thread.FReturnValue;
  130. Thread.FFinished := True;
  131. Thread.DoTerminate;
  132. if FreeThread then Thread.Free;
  133. DosExit (deThread, Result);
  134. end;
  135. constructor TThread.Create(CreateSuspended: Boolean);
  136. var
  137. Flags: Integer;
  138. begin
  139. inherited Create;
  140. AddThread (Self);
  141. FSuspended := CreateSuspended;
  142. Flags := dtStack_Commited;
  143. if FSuspended then Flags := Flags or dtSuspended;
  144. if DosCreateThread (FThreadID, @ThreadProc, pointer (Self), Flags, 16384)
  145. <> 0 then
  146. begin
  147. FFinished := true;
  148. Destroy;
  149. end else FHandle := FThreadID;
  150. IsMultiThread := TRUE;
  151. end;
  152. destructor TThread.Destroy;
  153. begin
  154. if not FFinished and not Suspended then
  155. begin
  156. Terminate;
  157. WaitFor;
  158. end;
  159. if FHandle <> -1 then DosKillThread (FHandle);
  160. inherited Destroy;
  161. RemoveThread (Self);
  162. end;
  163. procedure TThread.Resume;
  164. begin
  165. FSuspended := not (DosResumeThread (FHandle) = 0);
  166. end;
  167. procedure TThread.Suspend;
  168. begin
  169. FSuspended := DosSuspendThread (FHandle) = 0;
  170. end;
  171. procedure TThread.Terminate;
  172. begin
  173. FTerminated := true;
  174. end;
  175. function TThread.WaitFor: Integer;
  176. begin
  177. WaitFor := DosWaitThread (FHandle, dtWait);
  178. end;
  179. {
  180. $Log$
  181. Revision 1.7 2003-02-20 17:12:39 hajny
  182. * fixes for OS/2 v2.1 incompatibility
  183. Revision 1.6 2002/09/07 15:15:27 peter
  184. * old logs removed and tabs fixed
  185. Revision 1.5 2002/02/10 13:38:14 hajny
  186. * DosCalls dependency removed to avoid type redefinitions
  187. }