cont_poller.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "cont_poller.h"
  2. #include "impl.h"
  3. namespace NCoro {
  4. namespace {
  5. template <class T>
  6. int DoExecuteEvent(T* event) noexcept {
  7. auto* cont = event->Cont();
  8. if (cont->Cancelled()) {
  9. return ECANCELED;
  10. }
  11. cont->Executor()->ScheduleIoWait(event);
  12. cont->Switch();
  13. if (cont->Cancelled()) {
  14. return ECANCELED;
  15. }
  16. return event->Status();
  17. }
  18. }
  19. void TContPollEvent::Wake() noexcept {
  20. UnLink();
  21. Cont()->ReSchedule();
  22. }
  23. TInstant TEventWaitQueue::WakeTimedout(TInstant now) noexcept {
  24. TIoWait::TIterator it = IoWait_.Begin();
  25. if (it != IoWait_.End()) {
  26. if (it->DeadLine() > now) {
  27. return it->DeadLine();
  28. }
  29. do {
  30. (it++)->Wake(ETIMEDOUT);
  31. } while (it != IoWait_.End() && it->DeadLine() <= now);
  32. }
  33. return now;
  34. }
  35. void TEventWaitQueue::Register(NCoro::TContPollEvent* event) {
  36. IoWait_.Insert(event);
  37. event->Cont()->Unlink();
  38. }
  39. void TEventWaitQueue::Abort() noexcept {
  40. auto visitor = [](TContPollEvent& e) {
  41. e.Cont()->Cancel();
  42. };
  43. IoWait_.ForEach(visitor);
  44. }
  45. }
  46. void TFdEvent::RemoveFromIOWait() noexcept {
  47. this->Cont()->Executor()->Poller()->Remove(this);
  48. }
  49. int ExecuteEvent(TFdEvent* event) noexcept {
  50. return NCoro::DoExecuteEvent(event);
  51. }
  52. int ExecuteEvent(TTimerEvent* event) noexcept {
  53. return NCoro::DoExecuteEvent(event);
  54. }