diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/system/event.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/event.cpp')
-rw-r--r-- | util/system/event.cpp | 178 |
1 files changed, 89 insertions, 89 deletions
diff --git a/util/system/event.cpp b/util/system/event.cpp index 79b3cdb291..3d4780ea10 100644 --- a/util/system/event.cpp +++ b/util/system/event.cpp @@ -1,114 +1,114 @@ #include "datetime.h" #include "defaults.h" - -#include <cstdio> - + +#include <cstdio> + #include "atomic.h" -#include "event.h" -#include "mutex.h" -#include "condvar.h" +#include "event.h" +#include "mutex.h" +#include "condvar.h" #ifdef _win_ - #include "winint.h" -#endif - + #include "winint.h" +#endif + class TSystemEvent::TEvImpl: public TAtomicRefCount<TSystemEvent::TEvImpl> { -public: -#ifdef _win_ - inline TEvImpl(ResetMode rmode) { +public: +#ifdef _win_ + inline TEvImpl(ResetMode rmode) { cond = CreateEvent(nullptr, rmode == rManual ? true : false, false, nullptr); - } - + } + inline ~TEvImpl() { - CloseHandle(cond); - } - + CloseHandle(cond); + } + inline void Reset() noexcept { - ResetEvent(cond); - } - + ResetEvent(cond); + } + inline void Signal() noexcept { - SetEvent(cond); - } - + SetEvent(cond); + } + inline bool WaitD(TInstant deadLine) noexcept { - if (deadLine == TInstant::Max()) { - return WaitForSingleObject(cond, INFINITE) == WAIT_OBJECT_0; - } - - const TInstant now = Now(); - - if (now < deadLine) { - //TODO - return WaitForSingleObject(cond, (deadLine - now).MilliSeconds()) == WAIT_OBJECT_0; - } - + if (deadLine == TInstant::Max()) { + return WaitForSingleObject(cond, INFINITE) == WAIT_OBJECT_0; + } + + const TInstant now = Now(); + + if (now < deadLine) { + //TODO + return WaitForSingleObject(cond, (deadLine - now).MilliSeconds()) == WAIT_OBJECT_0; + } + return (WaitForSingleObject(cond, 0) == WAIT_OBJECT_0); - } -#else - inline TEvImpl(ResetMode rmode) + } +#else + inline TEvImpl(ResetMode rmode) : Manual(rmode == rManual ? true : false) - { - } - + { + } + inline void Signal() noexcept { if (Manual && AtomicGet(Signaled)) { - return; // shortcut - } - - with_lock (Mutex) { + return; // shortcut + } + + with_lock (Mutex) { AtomicSet(Signaled, 1); - } - - if (Manual) { - Cond.BroadCast(); - } else { - Cond.Signal(); - } - } + } + + if (Manual) { + Cond.BroadCast(); + } else { + Cond.Signal(); + } + } inline void Reset() noexcept { AtomicSet(Signaled, 0); - } - + } + inline bool WaitD(TInstant deadLine) noexcept { if (Manual && AtomicGet(Signaled)) { - return true; // shortcut - } - - bool resSignaled = true; - - with_lock (Mutex) { + return true; // shortcut + } + + bool resSignaled = true; + + with_lock (Mutex) { while (!AtomicGet(Signaled)) { - if (!Cond.WaitD(Mutex, deadLine)) { + if (!Cond.WaitD(Mutex, deadLine)) { resSignaled = AtomicGet(Signaled); // timed out, but Signaled could have been set - - break; - } - } - - if (!Manual) { + + break; + } + } + + if (!Manual) { AtomicSet(Signaled, 0); - } - } - - return resSignaled; - } -#endif - -private: -#ifdef _win_ - HANDLE cond; -#else - TCondVar Cond; - TMutex Mutex; + } + } + + return resSignaled; + } +#endif + +private: +#ifdef _win_ + HANDLE cond; +#else + TCondVar Cond; + TMutex Mutex; TAtomic Signaled = 0; - bool Manual; -#endif -}; - + bool Manual; +#endif +}; + TSystemEvent::TSystemEvent(ResetMode rmode) - : EvImpl_(new TEvImpl(rmode)) + : EvImpl_(new TEvImpl(rmode)) { } @@ -125,13 +125,13 @@ TSystemEvent& TSystemEvent::operator=(const TSystemEvent& other) noexcept { TSystemEvent::~TSystemEvent() = default; void TSystemEvent::Reset() noexcept { - EvImpl_->Reset(); + EvImpl_->Reset(); } void TSystemEvent::Signal() noexcept { - EvImpl_->Signal(); + EvImpl_->Signal(); } bool TSystemEvent::WaitD(TInstant deadLine) noexcept { - return EvImpl_->WaitD(deadLine); -} + return EvImpl_->WaitD(deadLine); +} |