event.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <util/datetime/base.h>
  4. struct TEventResetType {
  5. enum ResetMode {
  6. rAuto, // the state will be nonsignaled after Wait() returns
  7. rManual, // we need call Reset() to set the state to nonsignaled.
  8. };
  9. };
  10. /**
  11. * DEPRECATED!
  12. *
  13. * Use TAutoEvent, TManualEvent for the direct replacement.
  14. * Use TManualEvent to prevent SEGFAULT (http://nga.at.yandex-team.ru/5772).
  15. */
  16. class TSystemEvent: public TEventResetType {
  17. public:
  18. TSystemEvent(ResetMode rmode = rManual);
  19. TSystemEvent(const TSystemEvent& other) noexcept;
  20. TSystemEvent& operator=(const TSystemEvent& other) noexcept;
  21. ~TSystemEvent();
  22. void Reset() noexcept;
  23. void Signal() noexcept;
  24. /*
  25. * return true if signaled, false if timed out.
  26. */
  27. bool WaitD(TInstant deadLine) noexcept;
  28. /*
  29. * return true if signaled, false if timed out.
  30. */
  31. inline bool WaitT(TDuration timeOut) noexcept {
  32. return WaitD(timeOut.ToDeadLine());
  33. }
  34. /*
  35. * wait infinite time
  36. */
  37. inline void WaitI() noexcept {
  38. WaitD(TInstant::Max());
  39. }
  40. // return true if signaled, false if timed out.
  41. inline bool Wait(ui32 timer) noexcept {
  42. return WaitT(TDuration::MilliSeconds(timer));
  43. }
  44. inline bool Wait() noexcept {
  45. WaitI();
  46. return true;
  47. }
  48. private:
  49. class TEvImpl;
  50. TIntrusivePtr<TEvImpl> EvImpl_;
  51. };
  52. class TAutoEvent: public TSystemEvent {
  53. public:
  54. TAutoEvent()
  55. : TSystemEvent(TSystemEvent::rAuto)
  56. {
  57. }
  58. private:
  59. void Reset() noexcept;
  60. };
  61. /**
  62. * Prevents from a "shortcut problem" (see http://nga.at.yandex-team.ru/5772): if Wait will be called after Signaled
  63. * flag set to true in Signal method but before CondVar.BroadCast - Wait will shortcut (without actual wait on condvar).
  64. * If Wait thread will destruct event - Signal thread will do broadcast on a destructed CondVar.
  65. */
  66. class TManualEvent {
  67. public:
  68. TManualEvent()
  69. : Ev(TEventResetType::rManual)
  70. {
  71. }
  72. void Reset() noexcept {
  73. TSystemEvent{Ev}.Reset();
  74. }
  75. void Signal() noexcept {
  76. TSystemEvent{Ev}.Signal();
  77. }
  78. /** return true if signaled, false if timed out. */
  79. bool WaitD(TInstant deadLine) noexcept {
  80. return TSystemEvent{Ev}.WaitD(deadLine);
  81. }
  82. /** return true if signaled, false if timed out. */
  83. inline bool WaitT(TDuration timeOut) noexcept {
  84. return TSystemEvent{Ev}.WaitT(timeOut);
  85. }
  86. /** Wait infinite time */
  87. inline void WaitI() noexcept {
  88. TSystemEvent{Ev}.WaitI();
  89. }
  90. /** return true if signaled, false if timed out. */
  91. inline bool Wait(ui32 timer) noexcept {
  92. return TSystemEvent{Ev}.Wait(timer);
  93. }
  94. inline bool Wait() noexcept {
  95. return TSystemEvent{Ev}.Wait();
  96. }
  97. private:
  98. TSystemEvent Ev;
  99. };