diff --git a/include/Vutils.h b/include/Vutils.h index da6a7f8..f82514c 100644 --- a/include/Vutils.h +++ b/include/Vutils.h @@ -2396,7 +2396,7 @@ class RegistryW : public RegistryX }; /** - * CriticalSection + * ThreadLock */ class ThreadLock @@ -2412,6 +2412,35 @@ class ThreadLock CRITICAL_SECTION m_cs; }; +/** + * ThreadSignal + */ + +class ThreadSignal +{ +public: + ThreadSignal(const bool waiting = false); + ThreadSignal(const ThreadSignal& right); + + virtual ~ThreadSignal(); + + operator bool() const; + bool get() const; + void set(bool waiting); + void notify(); + bool wait(ulong time_out = INFINITE) const; + + bool operator==(ThreadSignal& right); + bool operator!=(ThreadSignal& right); + bool operator==(bool waiting); + bool operator!=(bool waiting); + const ThreadSignal& operator=(bool waiting); + const ThreadSignal& operator=(const ThreadSignal& right); + +private: + HANDLE m_event; +}; + /** * Stop Watch */ diff --git a/src/details/crisec.cpp b/src/details/crisec.cpp index a6d3fa2..3e23fc8 100644 --- a/src/details/crisec.cpp +++ b/src/details/crisec.cpp @@ -30,4 +30,83 @@ void vuapi ThreadLock::unlock() LeaveCriticalSection(&m_cs); } +ThreadSignal::ThreadSignal(bool waiting) +{ + m_event = CreateEventA(nullptr, TRUE, waiting, nullptr); +} + +ThreadSignal::ThreadSignal(const ThreadSignal& right) +{ + m_event = CreateEventA(nullptr, TRUE, right.get(), nullptr); +} + +ThreadSignal::~ThreadSignal() +{ + CloseHandle(m_event); +} + +const ThreadSignal& ThreadSignal::operator=(const ThreadSignal& right) +{ + this->set(right.get()); + return *this; +} + +const ThreadSignal& ThreadSignal::operator=(bool waiting) +{ + this->set(waiting); + return *this; +} + +ThreadSignal::operator bool() const +{ + return this->get(); +} + +bool ThreadSignal::operator==(ThreadSignal& right) +{ + return right.get() == this->get(); +} + +bool ThreadSignal::operator!=(ThreadSignal& right) +{ + return !this->operator==(right); +} + +bool ThreadSignal::operator==(bool waiting) +{ + return this->get() == waiting; +} + +bool ThreadSignal::operator!=(bool waiting) +{ + return !this->operator==(waiting); +} + +void ThreadSignal::set(bool state) +{ + if (state) + { + SetEvent(m_event); + } + else + { + ResetEvent(m_event); + } +} + +void ThreadSignal::notify() +{ + this->set(true); +} + +bool ThreadSignal::get() const +{ + return this->wait(0); +} + +bool ThreadSignal::wait(ulong time_out) const +{ + return WaitForSingleObject(m_event, time_out) == WAIT_OBJECT_0; +} + } // namespace vu \ No newline at end of file