IdSchedulerOfThread.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.32 3/23/2005 8:20:18 AM JPMugaas
  18. Temp fix for a double-free problem causing an AV. I will explain on Core.
  19. Rev 1.31 6/11/2004 8:48:32 AM DSiders
  20. Added "Do not Localize" comments.
  21. Rev 1.30 2004.03.01 5:12:38 PM czhower
  22. -Bug fix for shutdown of servers when connections still existed (AV)
  23. -Implicit HELP support in CMDserver
  24. -Several command handler bugs
  25. -Additional command handler functionality.
  26. Rev 1.29 2004.02.03 4:17:04 PM czhower
  27. For unit name changes.
  28. Rev 1.28 2004.01.22 5:59:14 PM czhower
  29. IdCriticalSection
  30. Rev 1.27 2004.01.20 10:03:32 PM czhower
  31. InitComponent
  32. Rev 1.26 6/11/2003 8:28:42 PM GGrieve
  33. remove wrong call to inherited StartYarn
  34. Rev 1.25 2003.10.24 12:59:18 PM czhower
  35. Name change
  36. Rev 1.24 2003.10.21 12:19:00 AM czhower
  37. TIdTask support and fiber bug fixes.
  38. Rev 1.23 10/15/2003 8:35:30 PM DSiders
  39. Added resource string for exception raised in TIdSchedulerOfThread.NewYarn.
  40. Rev 1.22 2003.10.14 11:18:10 PM czhower
  41. Fix for AV on shutdown and other bugs
  42. Rev 1.21 2003.10.11 5:49:32 PM czhower
  43. -VCL fixes for servers
  44. -Chain suport for servers (Super core)
  45. -Scheduler upgrades
  46. -Full yarn support
  47. Rev 1.20 2003.09.19 10:11:18 PM czhower
  48. Next stage of fiber support in servers.
  49. Rev 1.19 2003.09.19 11:54:30 AM czhower
  50. -Completed more features necessary for servers
  51. -Fixed some bugs
  52. Rev 1.18 2003.09.18 4:43:18 PM czhower
  53. -Removed IdBaseThread
  54. -Threads now have default names
  55. Rev 1.17 2003.09.18 4:10:26 PM czhower
  56. Preliminary changes for Yarn support.
  57. Rev 1.16 2003.07.17 1:08:04 PM czhower
  58. Fixed warning
  59. Rev 1.15 7/6/2003 8:04:06 PM BGooijen
  60. Renamed IdScheduler* to IdSchedulerOf*
  61. Rev 1.14 7/5/2003 11:49:06 PM BGooijen
  62. Cleaned up and fixed av in threadpool
  63. Rev 1.13 2003.06.30 9:39:44 PM czhower
  64. Comments and small change.
  65. Rev 1.12 6/25/2003 3:54:02 PM BGooijen
  66. Destructor waits now until all threads are terminated
  67. Rev 1.11 2003.06.25 4:27:02 PM czhower
  68. Fixed some formatting and fixed one line ifs.
  69. Rev 1.10 4/11/2003 6:35:28 PM BGooijen
  70. Rev 1.9 3/27/2003 5:17:22 PM BGooijen
  71. Moved some code to TIdScheduler, made ThreadPriority published
  72. Rev 1.8 3/22/2003 1:49:38 PM BGooijen
  73. Fixed warnings (.ShouldStop)
  74. Rev 1.7 3/13/2003 10:18:30 AM BGooijen
  75. Server side fibers, bug fixes
  76. Rev 1.6 1/23/2003 11:55:24 PM BGooijen
  77. Rev 1.5 1/23/2003 8:32:40 PM BGooijen
  78. Added termination handler
  79. Rev 1.3 1/23/2003 11:05:58 AM BGooijen
  80. Rev 1.2 1-17-2003 23:22:16 BGooijen
  81. added MaxThreads property
  82. Rev 1.1 1/17/2003 03:43:04 PM JPMugaas
  83. Updated to use new class.
  84. Rev 1.0 1/17/2003 03:29:50 PM JPMugaas
  85. Renamed from ThreadMgr for new design.
  86. Rev 1.0 11/13/2002 09:01:32 AM JPMugaas
  87. 02 Oct 2001 - Allen O'Neill
  88. Added support for thread priority - new property Threadpriority,
  89. new line added to OnCreate
  90. }
  91. unit IdSchedulerOfThread;
  92. interface
  93. {$i IdCompilerDefines.inc}
  94. uses
  95. Classes,
  96. IdException, IdGlobal, IdScheduler,
  97. IdThread, IdTask, IdYarn;
  98. type
  99. TIdYarnOfThread = class(TIdYarn)
  100. protected
  101. // TODO: should these be [Weak] on ARC systems?
  102. FScheduler: TIdScheduler;
  103. FThread: TIdThreadWithTask;
  104. public
  105. constructor Create(AScheduler: TIdScheduler; AThread: TIdThreadWithTask); reintroduce;
  106. destructor Destroy; override;
  107. //
  108. property Thread: TIdThreadWithTask read FThread;
  109. end;
  110. TIdSchedulerOfThread = class(TIdScheduler)
  111. protected
  112. FMaxThreads: Integer;
  113. FThreadPriority: TIdThreadPriority;
  114. FThreadClass: TIdThreadWithTaskClass;
  115. //
  116. procedure InitComponent; override;
  117. public
  118. destructor Destroy; override;
  119. function NewThread: TIdThreadWithTask; virtual;
  120. function NewYarn(AThread: TIdThreadWithTask = nil): TIdYarnOfThread;
  121. procedure StartYarn(AYarn: TIdYarn; ATask: TIdTask); override;
  122. procedure TerminateYarn(AYarn: TIdYarn); override;
  123. property ThreadClass: TIdThreadWithTaskClass read FThreadClass write FThreadClass;
  124. published
  125. property MaxThreads: Integer read FMaxThreads write FMaxThreads;
  126. property ThreadPriority: TIdThreadPriority read FThreadPriority write FThreadPriority default tpNormal;
  127. end;
  128. implementation
  129. uses
  130. {$IFDEF KYLIXCOMPAT}
  131. Libc,
  132. {$ENDIF}
  133. IdResourceStringsCore, IdThreadSafe, IdExceptionCore, SysUtils;
  134. { TIdSchedulerOfThread }
  135. destructor TIdSchedulerOfThread.Destroy;
  136. begin
  137. TerminateAllYarns;
  138. inherited Destroy;
  139. end;
  140. procedure TIdSchedulerOfThread.StartYarn(AYarn: TIdYarn; ATask: TIdTask);
  141. var
  142. LThread: TIdThreadWithTask;
  143. begin
  144. LThread := TIdYarnOfThread(AYarn).Thread;
  145. LThread.Task := ATask;
  146. LThread.Start;
  147. end;
  148. function TIdSchedulerOfThread.NewThread: TIdThreadWithTask;
  149. begin
  150. Assert(FThreadClass<>nil);
  151. if (FMaxThreads <> 0) and (not ActiveYarns.IsCountLessThan(FMaxThreads + 1)) then begin
  152. raise EIdSchedulerMaxThreadsExceeded.Create(RSchedMaxThreadEx);
  153. end;
  154. Result := FThreadClass.Create(nil, IndyFormat('%s User', [Name])); {do not localize}
  155. if ThreadPriority <> tpNormal then begin
  156. IndySetThreadPriority(Result, ThreadPriority);
  157. end;
  158. end;
  159. function TIdSchedulerOfThread.NewYarn(AThread: TIdThreadWithTask): TIdYarnOfThread;
  160. begin
  161. if not Assigned(AThread) then begin
  162. raise EIdException.Create(RSThreadSchedulerThreadRequired); // TODO: create a new Exception class for this
  163. end;
  164. // Create Yarn
  165. Result := TIdYarnOfThread.Create(Self, AThread);
  166. end;
  167. procedure TIdSchedulerOfThread.TerminateYarn(AYarn: TIdYarn);
  168. var
  169. LYarn: TIdYarnOfThread;
  170. LThread: TIdThreadWithTask;
  171. begin
  172. Assert(AYarn<>nil);
  173. LYarn := TIdYarnOfThread(AYarn);
  174. LThread := LYarn.Thread;
  175. if (LThread <> nil) and (not LThread.Suspended) then begin
  176. // Is still running and will free itself
  177. LThread.Stop;
  178. // Dont free the yarn. The thread frees it (IdThread.pas)
  179. end else
  180. begin
  181. // If suspended, was created but never started
  182. // ie waiting on connection accept
  183. // RLebeau: free the yarn here as well. This allows TIdSchedulerOfThreadPool
  184. // to put the suspended thread, if present, back in the pool.
  185. IdDisposeAndNil(LYarn);
  186. end;
  187. end;
  188. procedure TIdSchedulerOfThread.InitComponent;
  189. begin
  190. inherited InitComponent;
  191. FThreadPriority := tpNormal;
  192. FMaxThreads := 0;
  193. FThreadClass := TIdThreadWithTask;
  194. end;
  195. { TIdYarnOfThread }
  196. constructor TIdYarnOfThread.Create(
  197. AScheduler: TIdScheduler;
  198. AThread: TIdThreadWithTask
  199. );
  200. begin
  201. inherited Create;
  202. FScheduler := AScheduler;
  203. FThread := AThread;
  204. AThread.Yarn := Self;
  205. end;
  206. destructor TIdYarnOfThread.Destroy;
  207. begin
  208. FScheduler.ReleaseYarn(Self);
  209. inherited Destroy;
  210. end;
  211. end.