event.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "defaults.h"
  2. #include "event.h"
  3. #include "mutex.h"
  4. #include "condvar.h"
  5. #ifdef _win_
  6. #include "winint.h"
  7. #endif
  8. #include <atomic>
  9. class TSystemEvent::TEvImpl: public TAtomicRefCount<TSystemEvent::TEvImpl> {
  10. public:
  11. #ifdef _win_
  12. inline TEvImpl(ResetMode rmode) {
  13. cond = CreateEvent(nullptr, rmode == rManual ? true : false, false, nullptr);
  14. }
  15. inline ~TEvImpl() {
  16. CloseHandle(cond);
  17. }
  18. inline void Reset() noexcept {
  19. ResetEvent(cond);
  20. }
  21. inline void Signal() noexcept {
  22. SetEvent(cond);
  23. }
  24. inline bool WaitD(TInstant deadLine) noexcept {
  25. if (deadLine == TInstant::Max()) {
  26. return WaitForSingleObject(cond, INFINITE) == WAIT_OBJECT_0;
  27. }
  28. const TInstant now = Now();
  29. if (now < deadLine) {
  30. // TODO
  31. return WaitForSingleObject(cond, (deadLine - now).MilliSeconds()) == WAIT_OBJECT_0;
  32. }
  33. return (WaitForSingleObject(cond, 0) == WAIT_OBJECT_0);
  34. }
  35. #else
  36. inline TEvImpl(ResetMode rmode)
  37. : Manual(rmode == rManual ? true : false)
  38. {
  39. }
  40. inline void Signal() noexcept {
  41. if (Manual && Signaled.load(std::memory_order_acquire)) {
  42. return; // shortcut
  43. }
  44. with_lock (Mutex) {
  45. Signaled.store(true, std::memory_order_release);
  46. }
  47. if (Manual) {
  48. Cond.BroadCast();
  49. } else {
  50. Cond.Signal();
  51. }
  52. }
  53. inline void Reset() noexcept {
  54. Signaled.store(false, std::memory_order_release);
  55. }
  56. inline bool WaitD(TInstant deadLine) noexcept {
  57. if (Manual && Signaled.load(std::memory_order_acquire)) {
  58. return true; // shortcut
  59. }
  60. bool resSignaled = true;
  61. with_lock (Mutex) {
  62. while (!Signaled.load(std::memory_order_acquire)) {
  63. if (!Cond.WaitD(Mutex, deadLine)) {
  64. resSignaled = Signaled.load(std::memory_order_acquire); // timed out, but Signaled could have been set
  65. break;
  66. }
  67. }
  68. if (!Manual) {
  69. Signaled.store(false, std::memory_order_release);
  70. }
  71. }
  72. return resSignaled;
  73. }
  74. #endif
  75. private:
  76. #ifdef _win_
  77. HANDLE cond;
  78. #else
  79. TCondVar Cond;
  80. TMutex Mutex;
  81. std::atomic<bool> Signaled = false;
  82. bool Manual;
  83. #endif
  84. };
  85. TSystemEvent::TSystemEvent(ResetMode rmode)
  86. : EvImpl_(new TEvImpl(rmode))
  87. {
  88. }
  89. TSystemEvent::TSystemEvent(const TSystemEvent& other) noexcept
  90. : EvImpl_(other.EvImpl_)
  91. {
  92. }
  93. TSystemEvent& TSystemEvent::operator=(const TSystemEvent& other) noexcept {
  94. EvImpl_ = other.EvImpl_;
  95. return *this;
  96. }
  97. TSystemEvent::~TSystemEvent() = default;
  98. void TSystemEvent::Reset() noexcept {
  99. EvImpl_->Reset();
  100. }
  101. void TSystemEvent::Signal() noexcept {
  102. EvImpl_->Signal();
  103. }
  104. bool TSystemEvent::WaitD(TInstant deadLine) noexcept {
  105. return EvImpl_->WaitD(deadLine);
  106. }