IdSchedulerOfThreadDefault.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.12 2004.02.03 4:17:06 PM czhower
  18. For unit name changes.
  19. Rev 1.11 2003.10.24 12:59:20 PM czhower
  20. Name change
  21. Rev 1.10 2003.10.21 12:19:00 AM czhower
  22. TIdTask support and fiber bug fixes.
  23. Rev 1.9 2003.10.11 5:49:40 PM czhower
  24. -VCL fixes for servers
  25. -Chain suport for servers (Super core)
  26. -Scheduler upgrades
  27. -Full yarn support
  28. Rev 1.8 2003.09.19 10:11:18 PM czhower
  29. Next stage of fiber support in servers.
  30. Rev 1.7 2003.09.19 11:54:30 AM czhower
  31. -Completed more features necessary for servers
  32. -Fixed some bugs
  33. Rev 1.6 2003.09.18 4:10:26 PM czhower
  34. Preliminary changes for Yarn support.
  35. Rev 1.5 7/6/2003 8:04:06 PM BGooijen
  36. Renamed IdScheduler* to IdSchedulerOf*
  37. Rev 1.4 7/5/2003 11:49:06 PM BGooijen
  38. Cleaned up and fixed av in threadpool
  39. Rev 1.3 2003.06.25 4:26:40 PM czhower
  40. Removed unecessary code in RemoveThread
  41. Rev 1.2 3/13/2003 10:18:32 AM BGooijen
  42. Server side fibers, bug fixes
  43. Rev 1.1 1/23/2003 11:06:02 AM BGooijen
  44. Rev 1.0 1/17/2003 03:29:54 PM JPMugaas
  45. Renamed from ThreadMgr for new design.
  46. Rev 1.0 11/13/2002 09:01:40 AM JPMugaas
  47. }
  48. unit IdSchedulerOfThreadDefault;
  49. interface
  50. {$i IdCompilerDefines.inc}
  51. uses
  52. IdThread, IdSchedulerOfThread, IdScheduler, IdYarn;
  53. type
  54. TIdSchedulerOfThreadDefault = class(TIdSchedulerOfThread)
  55. public
  56. function AcquireYarn: TIdYarn; override;
  57. procedure ReleaseYarn(AYarn: TIdYarn); override;
  58. function NewThread: TIdThreadWithTask; override;
  59. end;
  60. implementation
  61. uses
  62. IdGlobal;
  63. { TIdSchedulerOfThreadDefault }
  64. function TIdSchedulerOfThreadDefault.AcquireYarn: TIdYarn;
  65. begin
  66. Result := NewYarn(NewThread);
  67. ActiveYarns.Add(Result);
  68. end;
  69. type
  70. TIdYarnOfThreadAccess = class(TIdYarnOfThread)
  71. end;
  72. procedure TIdSchedulerOfThreadDefault.ReleaseYarn(AYarn: TIdYarn);
  73. //only gets called from YarnOf(Fiber/Thread).Destroy
  74. var
  75. LThread: TIdThreadWithTask;
  76. begin
  77. //take posession of the thread
  78. LThread := TIdYarnOfThread(AYarn).Thread;
  79. {$I IdObjectChecksOff.inc}
  80. TIdYarnOfThreadAccess(AYarn).FThread := nil;
  81. {$I IdObjectChecksOn.inc}
  82. //Currently LThread can =nil. Is that a valid condition?
  83. //Assert(LThread<>nil);
  84. // inherited removes from ActiveYarns list
  85. inherited ReleaseYarn(AYarn);
  86. if LThread <> nil then begin
  87. // need to destroy the thread
  88. LThread.Yarn := nil; // Yarn is being destroyed, de-couple it from the thread
  89. LThread.Terminate;
  90. // RLebeau - ReleaseYarn() can be called in the context of
  91. // the yarn's thread (when TIdThread.Cleanup() destroys the
  92. // yarn between connnections), so have to check which context
  93. // we're in here so as not to deadlock the thread!
  94. if IsCurrentThread(LThread) then begin
  95. LThread.FreeOnTerminate := True;
  96. end else begin
  97. {$IFDEF DEPRECATED_TThread_SuspendResume}
  98. LThread.Suspended := False;
  99. {$ELSE}
  100. LThread.Resume;
  101. {$ENDIF}
  102. LThread.WaitFor;
  103. LThread.Free;
  104. end;
  105. end;
  106. end;
  107. function TIdSchedulerOfThreadDefault.NewThread: TIdThreadWithTask;
  108. begin
  109. Result := inherited NewThread;
  110. // RLebeau 2/25/2010: do not let the thread free itself on termination yet.
  111. // It can cause crashes during Yarn shutdown, so let the Scheduler decide
  112. // what to do with the thread later...
  113. //Result.FreeOnTerminate := True;
  114. end;
  115. end.