impl.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #pragma once
  2. #include "callbacks.h"
  3. #include "cont_poller.h"
  4. #include "iostatus.h"
  5. #include "poller.h"
  6. #include "stack/stack_common.h"
  7. #include "trampoline.h"
  8. #include "custom_time.h"
  9. #include <library/cpp/containers/intrusive_rb_tree/rb_tree.h>
  10. #include <util/system/error.h>
  11. #include <util/generic/ptr.h>
  12. #include <util/generic/intrlist.h>
  13. #include <util/datetime/base.h>
  14. #include <util/generic/maybe.h>
  15. #include <util/generic/function.h>
  16. #define EWAKEDUP 34567
  17. class TCont;
  18. struct TContRep;
  19. class TContExecutor;
  20. class TContPollEvent;
  21. namespace NCoro::NStack {
  22. class IAllocator;
  23. }
  24. class TCont : private TIntrusiveListItem<TCont> {
  25. friend class TContExecutor;
  26. friend class TIntrusiveListItem<TCont>;
  27. friend class NCoro::TEventWaitQueue;
  28. friend class NCoro::TTrampoline;
  29. public:
  30. struct TJoinWait: public TIntrusiveListItem<TJoinWait> {
  31. TJoinWait(TCont& c) noexcept;
  32. void Wake() noexcept;
  33. public:
  34. TCont& Cont_;
  35. };
  36. private:
  37. TCont(
  38. NCoro::NStack::IAllocator& allocator,
  39. uint32_t stackSize,
  40. TContExecutor& executor,
  41. NCoro::TTrampoline::TFunc func,
  42. const char* name
  43. ) noexcept;
  44. public:
  45. TContExecutor* Executor() noexcept {
  46. return &Executor_;
  47. }
  48. const TContExecutor* Executor() const noexcept {
  49. return &Executor_;
  50. }
  51. const char* Name() const noexcept {
  52. return Name_;
  53. }
  54. void PrintMe(IOutputStream& out) const noexcept;
  55. void Yield() noexcept;
  56. void ReScheduleAndSwitch() noexcept;
  57. /// @return ETIMEDOUT on success
  58. int SleepD(TInstant deadline) noexcept;
  59. int SleepT(TDuration timeout) noexcept {
  60. return SleepD(timeout.ToDeadLine());
  61. }
  62. int SleepI() noexcept {
  63. return SleepD(TInstant::Max());
  64. }
  65. bool IAmRunning() const noexcept;
  66. void Cancel() noexcept;
  67. void Cancel(THolder<std::exception> exception) noexcept;
  68. bool Cancelled() const noexcept {
  69. return Cancelled_;
  70. }
  71. bool Scheduled() const noexcept {
  72. return Scheduled_;
  73. }
  74. /// \param this корутина, которая будет ждать
  75. /// \param c корутина, которую будем ждать
  76. /// \param deadLine максимальное время ожидания
  77. /// \param forceStop кастомный обработчик ситуации, когда завершается время ожидания или отменяется ожидающая корутина (this)
  78. /// дефолтное поведение - отменить ожидаемую корутину (c->Cancel())
  79. bool Join(TCont* c, TInstant deadLine = TInstant::Max(), std::function<void(TJoinWait&, TCont*)> forceStop = {}) noexcept;
  80. void ReSchedule() noexcept;
  81. void Switch() noexcept;
  82. void SwitchTo(TExceptionSafeContext* ctx) {
  83. Trampoline_.SwitchTo(ctx);
  84. }
  85. THolder<std::exception> TakeException() noexcept {
  86. return std::move(Exception_);
  87. }
  88. void SetException(THolder<std::exception> exception) noexcept {
  89. Exception_ = std::move(exception);
  90. }
  91. private:
  92. void Terminate();
  93. private:
  94. TContExecutor& Executor_;
  95. // TODO(velavokr): allow name storage owning (for generated names backed by TString)
  96. const char* Name_ = nullptr;
  97. NCoro::TTrampoline Trampoline_;
  98. TIntrusiveList<TJoinWait> Waiters_;
  99. bool Cancelled_ = false;
  100. bool Scheduled_ = false;
  101. THolder<std::exception> Exception_;
  102. };
  103. TCont* RunningCont();
  104. template <class Functor>
  105. static void ContHelperFunc(TCont* cont, void* arg) {
  106. (*((Functor*)(arg)))(cont);
  107. }
  108. template <typename T, void (T::*M)(TCont*)>
  109. static void ContHelperMemberFunc(TCont* c, void* arg) {
  110. ((reinterpret_cast<T*>(arg))->*M)(c);
  111. }
  112. class IUserEvent
  113. : public TIntrusiveListItem<IUserEvent>
  114. {
  115. public:
  116. virtual ~IUserEvent() = default;
  117. virtual void Execute() = 0;
  118. };
  119. /// Central coroutine class.
  120. /// Note, coroutines are single-threaded, and all methods must be called from the single thread
  121. class TContExecutor {
  122. friend class TCont;
  123. using TContList = TIntrusiveList<TCont>;
  124. public:
  125. TContExecutor(
  126. uint32_t defaultStackSize,
  127. THolder<IPollerFace> poller = IPollerFace::Default(),
  128. NCoro::IScheduleCallback* = nullptr,
  129. NCoro::IEnterPollerCallback* = nullptr,
  130. NCoro::NStack::EGuard stackGuard = NCoro::NStack::EGuard::Canary,
  131. TMaybe<NCoro::NStack::TPoolAllocatorSettings> poolSettings = Nothing(),
  132. NCoro::ITime* time = nullptr
  133. );
  134. ~TContExecutor();
  135. // if we already have a coroutine to run
  136. void Execute() noexcept;
  137. void Execute(TContFunc func, void* arg = nullptr) noexcept;
  138. template <class Functor>
  139. void Execute(Functor& f) noexcept {
  140. Execute((TContFunc)ContHelperFunc<Functor>, (void*)&f);
  141. }
  142. template <typename T, void (T::*M)(TCont*)>
  143. void Execute(T* obj) noexcept {
  144. Execute(ContHelperMemberFunc<T, M>, obj);
  145. }
  146. template <class Functor>
  147. TCont* Create(
  148. Functor& f,
  149. const char* name,
  150. TMaybe<ui32> customStackSize = Nothing()
  151. ) noexcept {
  152. return Create((TContFunc)ContHelperFunc<Functor>, (void*)&f, name, customStackSize);
  153. }
  154. template <typename T, void (T::*M)(TCont*)>
  155. TCont* Create(
  156. T* obj,
  157. const char* name,
  158. TMaybe<ui32> customStackSize = Nothing()
  159. ) noexcept {
  160. return Create(ContHelperMemberFunc<T, M>, obj, name, customStackSize);
  161. }
  162. TCont* Create(
  163. TContFunc func,
  164. void* arg,
  165. const char* name,
  166. TMaybe<ui32> customStackSize = Nothing()
  167. ) noexcept;
  168. TCont* CreateOwned(
  169. NCoro::TTrampoline::TFunc func,
  170. const char* name,
  171. TMaybe<ui32> customStackSize = Nothing()
  172. ) noexcept;
  173. NCoro::TContPoller* Poller() noexcept {
  174. return &Poller_;
  175. }
  176. TCont* Running() noexcept {
  177. return Current_;
  178. }
  179. const TCont* Running() const noexcept {
  180. return Current_;
  181. }
  182. size_t TotalReadyConts() const noexcept {
  183. return Ready_.Size() + TotalScheduledConts();
  184. }
  185. size_t TotalScheduledConts() const noexcept {
  186. return ReadyNext_.Size();
  187. }
  188. size_t TotalConts() const noexcept {
  189. return Allocated_;
  190. }
  191. size_t TotalWaitingConts() const noexcept {
  192. return TotalConts() - TotalReadyConts();
  193. }
  194. NCoro::NStack::TAllocatorStats GetAllocatorStats() const noexcept;
  195. // TODO(velavokr): rename, it is just CancelAll actually
  196. void Abort() noexcept;
  197. void SetFailOnError(bool fail) noexcept {
  198. FailOnError_ = fail;
  199. }
  200. bool FailOnError() const noexcept {
  201. return FailOnError_;
  202. }
  203. void RegisterInWaitQueue(NCoro::TContPollEvent* event) {
  204. WaitQueue_.Register(event);
  205. }
  206. void ScheduleIoWait(TFdEvent* event) {
  207. RegisterInWaitQueue(event);
  208. Poller_.Schedule(event);
  209. }
  210. void ScheduleIoWait(TTimerEvent* event) noexcept {
  211. RegisterInWaitQueue(event);
  212. }
  213. void ScheduleUserEvent(IUserEvent* event) {
  214. UserEvents_.PushBack(event);
  215. }
  216. void Pause();
  217. TInstant Now();
  218. private:
  219. void Release(TCont* cont) noexcept;
  220. void Exit(TCont* cont) noexcept;
  221. void RunScheduler() noexcept;
  222. void ScheduleToDelete(TCont* cont) noexcept;
  223. void ScheduleExecution(TCont* cont) noexcept;
  224. void ScheduleExecutionNow(TCont* cont) noexcept;
  225. void DeleteScheduled() noexcept;
  226. void WaitForIO();
  227. void Poll(TInstant deadline);
  228. private:
  229. NCoro::IScheduleCallback* const ScheduleCallback_ = nullptr;
  230. NCoro::IEnterPollerCallback* const EnterPollerCallback_ = nullptr;
  231. const uint32_t DefaultStackSize_;
  232. THolder<NCoro::NStack::IAllocator> StackAllocator_;
  233. TExceptionSafeContext SchedContext_;
  234. TContList ToDelete_;
  235. TContList Ready_;
  236. TContList ReadyNext_;
  237. NCoro::TEventWaitQueue WaitQueue_;
  238. NCoro::TContPoller Poller_;
  239. NCoro::TContPoller::TEvents PollerEvents_;
  240. TInstant LastPoll_;
  241. TIntrusiveList<IUserEvent> UserEvents_;
  242. size_t Allocated_ = 0;
  243. TCont* Current_ = nullptr;
  244. bool FailOnError_ = false;
  245. bool Paused_ = false;
  246. NCoro::ITime* Time_ = nullptr;
  247. };