aboutsummaryrefslogtreecommitdiff
path: root/zencore/thread.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'zencore/thread.cpp')
-rw-r--r--zencore/thread.cpp41
1 files changed, 39 insertions, 2 deletions
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
//////////////////////////////////////////////////////////////////////////