actor.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #include "executor.h"
  3. #include "tasks.h"
  4. #include "what_thread_does.h"
  5. #include <util/system/yassert.h>
  6. namespace NActor {
  7. class IActor: protected IWorkItem {
  8. public:
  9. // TODO: make private
  10. TTasks Tasks;
  11. public:
  12. virtual void ScheduleHereV() = 0;
  13. virtual void ScheduleV() = 0;
  14. virtual void ScheduleHereAtMostOnceV() = 0;
  15. // TODO: make private
  16. virtual void RefV() = 0;
  17. virtual void UnRefV() = 0;
  18. // mute warnings
  19. ~IActor() override {
  20. }
  21. };
  22. struct TDefaultTag {};
  23. template <typename TThis, typename TTag = TDefaultTag>
  24. class TActor: public IActor {
  25. private:
  26. TExecutor* const Executor;
  27. public:
  28. TActor(TExecutor* executor)
  29. : Executor(executor)
  30. {
  31. }
  32. void AddTaskFromActorLoop() {
  33. bool schedule = Tasks.AddTask();
  34. // TODO: check thread id
  35. Y_ASSERT(!schedule);
  36. }
  37. /**
  38. * Schedule actor.
  39. *
  40. * If actor is sleeping, then actor will be executed right now.
  41. * If actor is executing right now, it will be executed one more time.
  42. * If this method is called multiple time, actor will be re-executed no more than one more time.
  43. */
  44. void Schedule() {
  45. if (Tasks.AddTask()) {
  46. EnqueueWork();
  47. }
  48. }
  49. /**
  50. * Schedule actor, execute it in current thread.
  51. *
  52. * If actor is running, continue executing where it is executing.
  53. * If actor is sleeping, execute it in current thread.
  54. *
  55. * Operation is useful for tasks that are likely to complete quickly.
  56. */
  57. void ScheduleHere() {
  58. if (Tasks.AddTask()) {
  59. Loop();
  60. }
  61. }
  62. /**
  63. * Schedule actor, execute in current thread no more than once.
  64. *
  65. * If actor is running, continue executing where it is executing.
  66. * If actor is sleeping, execute one iteration here, and if actor got new tasks,
  67. * reschedule it in worker pool.
  68. */
  69. void ScheduleHereAtMostOnce() {
  70. if (Tasks.AddTask()) {
  71. bool fetched = Tasks.FetchTask();
  72. Y_ABORT_UNLESS(fetched, "happens");
  73. DoAct();
  74. // if someone added more tasks, schedule them
  75. if (Tasks.FetchTask()) {
  76. bool added = Tasks.AddTask();
  77. Y_ABORT_UNLESS(!added, "happens");
  78. EnqueueWork();
  79. }
  80. }
  81. }
  82. void ScheduleHereV() override {
  83. ScheduleHere();
  84. }
  85. void ScheduleV() override {
  86. Schedule();
  87. }
  88. void ScheduleHereAtMostOnceV() override {
  89. ScheduleHereAtMostOnce();
  90. }
  91. void RefV() override {
  92. GetThis()->Ref();
  93. }
  94. void UnRefV() override {
  95. GetThis()->UnRef();
  96. }
  97. private:
  98. TThis* GetThis() {
  99. return static_cast<TThis*>(this);
  100. }
  101. void EnqueueWork() {
  102. GetThis()->Ref();
  103. Executor->EnqueueWork({this});
  104. }
  105. void DoAct() {
  106. WHAT_THREAD_DOES_PUSH_POP_CURRENT_FUNC();
  107. GetThis()->Act(TTag());
  108. }
  109. void Loop() {
  110. // TODO: limit number of iterations
  111. while (Tasks.FetchTask()) {
  112. DoAct();
  113. }
  114. }
  115. void DoWork() override {
  116. Y_ASSERT(GetThis()->RefCount() >= 1);
  117. Loop();
  118. GetThis()->UnRef();
  119. }
  120. };
  121. }