From d312856e0cb89be73f16e328858eb5ab04aeec02 Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Thu, 11 Nov 2021 12:53:05 +0100 Subject: NamedEvent is no longer an Event-type object. Platforms other than Windows do not really have a named event-like primitive or ones that are close are fallible if a process hard-terminates. Separating from Event more clearly conveys the use of NamedEvent objects; to synchronise two processes. --- zencore/thread.cpp | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'zencore/thread.cpp') diff --git a/zencore/thread.cpp b/zencore/thread.cpp index 3e40b6336..6c5419494 100644 --- a/zencore/thread.cpp +++ b/zencore/thread.cpp @@ -228,7 +228,7 @@ Event::Wait(int TimeoutMs) #if ZEN_PLATFORM_WINDOWS -NamedEvent::NamedEvent(std::u8string_view EventName) : Event(nullptr) +NamedEvent::NamedEvent(std::u8string_view EventName) { using namespace std::literals; @@ -239,7 +239,7 @@ NamedEvent::NamedEvent(std::u8string_view EventName) : Event(nullptr) m_EventHandle = CreateEventA(nullptr, true, false, Name.c_str()); } -NamedEvent::NamedEvent(std::string_view EventName) : Event(nullptr) +NamedEvent::NamedEvent(std::string_view EventName) { using namespace std::literals; @@ -250,6 +250,43 @@ NamedEvent::NamedEvent(std::string_view EventName) : Event(nullptr) m_EventHandle = CreateEventA(nullptr, true, false, Name.c_str()); } +NamedEvent::~NamedEvent() +{ + Close(); +} + +void NamedEvent::Close() +{ + if (m_EventHandle == nullptr) + { + return; + } + + CloseHandle(m_EventHandle); + + m_EventHandle = nullptr; +} + +void NamedEvent::Set() +{ + SetEvent(m_EventHandle); +} + +bool NamedEvent::Wait(int TimeoutMs) +{ + const DWORD Timeout = (TimeoutMs < 0) ? INFINITE : TimeoutMs; + + DWORD Result = WaitForSingleObject(m_EventHandle, Timeout); + + if (Result == WAIT_FAILED) + { + using namespace std::literals; + zen::ThrowLastError("Event wait failed"sv); + } + + return (Result == WAIT_OBJECT_0); +} + #endif // ZEN_PLATFORM_WINDOWS ////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3