PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/concurrent/dotnet/ActorPool.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 143 lines | 96 code | 22 blank | 25 comment | 11 complexity | c53da42cea9c22b91d8330f359fa3475 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2009, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 30 Mar 09 Andy Frank Creation
  7. //
  8. using System.Collections;
  9. using Fanx.Util;
  10. namespace Fan.Sys
  11. {
  12. /// <summary>
  13. /// Controller for a group of actors which manages their execution.
  14. /// </summary>
  15. public class ActorPool : FanObj
  16. {
  17. //////////////////////////////////////////////////////////////////////////
  18. // Construction
  19. //////////////////////////////////////////////////////////////////////////
  20. public static ActorPool make() { return make(null); }
  21. public static ActorPool make(Func itBlock)
  22. {
  23. ActorPool self = new ActorPool();
  24. make_(self, itBlock);
  25. return self;
  26. }
  27. public static void make_(ActorPool self) { make_(self, null); }
  28. public static void make_(ActorPool self, Func itBlock)
  29. {
  30. if (itBlock != null)
  31. {
  32. itBlock.enterCtor(self);
  33. itBlock.call(self);
  34. itBlock.exitCtor();
  35. }
  36. if (self.m_maxThreads < 1) throw ArgErr.make("ActorPool.maxThreads must be >= 1, not " + self.m_maxThreads).val;
  37. self.m_threadPool = new ThreadPool((int)self.m_maxThreads);
  38. self.m_scheduler = new Scheduler();
  39. }
  40. //////////////////////////////////////////////////////////////////////////
  41. // Obj
  42. //////////////////////////////////////////////////////////////////////////
  43. public override Type @typeof()
  44. {
  45. if (m_type == null) m_type = Type.find("concurrent::ActorPool");
  46. return m_type;
  47. }
  48. private static Type m_type;
  49. //////////////////////////////////////////////////////////////////////////
  50. // ActorPool
  51. //////////////////////////////////////////////////////////////////////////
  52. public bool isStopped()
  53. {
  54. return m_threadPool.isStopped();
  55. }
  56. public bool isDone()
  57. {
  58. return m_threadPool.isDone();
  59. }
  60. public ActorPool stop()
  61. {
  62. m_scheduler.stop();
  63. m_threadPool.stop();
  64. return this;
  65. }
  66. public ActorPool kill()
  67. {
  68. m_killed = true;
  69. m_scheduler.stop();
  70. m_threadPool.kill();
  71. return this;
  72. }
  73. public ActorPool join() { return join(null); }
  74. public ActorPool join(Duration timeout)
  75. {
  76. if (!isStopped()) throw Err.make("ActorPool is not stopped").val;
  77. long ms = timeout == null ? System.Int32.MaxValue : timeout.millis();
  78. try
  79. {
  80. if (m_threadPool.join(ms)) return this;
  81. }
  82. catch (System.Threading.ThreadInterruptedException e)
  83. {
  84. throw InterruptedErr.make(e).val;
  85. }
  86. throw TimeoutErr.make("ActorPool.join timed out").val;
  87. }
  88. public override object trap(string name, List args)
  89. {
  90. if (name == "dump") { m_threadPool.dump(args); return null; }
  91. return base.trap(name, args);
  92. }
  93. internal void submit(Actor actor)
  94. {
  95. m_threadPool.submit(actor);
  96. }
  97. internal void schedule(Actor a, Duration d, Future f)
  98. {
  99. m_scheduler.schedule(d.ticks(), new ScheduledWork(a, f));
  100. }
  101. //////////////////////////////////////////////////////////////////////////
  102. // ScheduledWork
  103. //////////////////////////////////////////////////////////////////////////
  104. internal class ScheduledWork : Scheduler.Work
  105. {
  106. public ScheduledWork(Actor a, Future f) { actor = a; future = f; }
  107. public string toString() { return "ScheduledWork msg=" + future.m_msg; }
  108. public void work() { if (!future.isCancelled()) actor._enqueue(future, false); }
  109. public void cancel() { future.cancel(); }
  110. internal readonly Actor actor;
  111. internal readonly Future future;
  112. }
  113. //////////////////////////////////////////////////////////////////////////
  114. // Fields
  115. //////////////////////////////////////////////////////////////////////////
  116. private ThreadPool m_threadPool;
  117. private Scheduler m_scheduler;
  118. internal volatile bool m_killed;
  119. public long m_maxThreads = 100;
  120. }
  121. }