From 5f96a135c503c02e5ef200be1a4f68be4ada60c2 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Thu, 5 Aug 2021 21:37:35 +0200 Subject: Added basic NamedMutex implementation --- zencore/thread.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'zencore/thread.cpp') diff --git a/zencore/thread.cpp b/zencore/thread.cpp index 4451fd302..5e558069b 100644 --- a/zencore/thread.cpp +++ b/zencore/thread.cpp @@ -94,6 +94,51 @@ NamedEvent::NamedEvent(std::string_view EventName) : Event(nullptr) m_EventHandle = CreateEventA(nullptr, true, false, Name.c_str()); } +NamedMutex::~NamedMutex() +{ + if (m_MutexHandle) + { + CloseHandle(m_MutexHandle); + } +} + +bool +NamedMutex::Create(std::string_view MutexName) +{ + ZEN_ASSERT(m_MutexHandle == nullptr); + + using namespace std::literals; + + ExtendableStringBuilder<64> Name; + Name << "Global\\"sv; + Name << MutexName; + + m_MutexHandle = CreateMutexA(nullptr, /* InitialOwner */ TRUE, Name.c_str()); + + return !!m_MutexHandle; +} + +bool +NamedMutex::Exists(std::string_view MutexName) +{ + using namespace std::literals; + + ExtendableStringBuilder<64> Name; + Name << "Global\\"sv; + Name << MutexName; + + void* MutexHandle = OpenMutexA(SYNCHRONIZE, /* InheritHandle */ FALSE, Name.c_str()); + + if (MutexHandle == nullptr) + { + return false; + } + + CloseHandle(MutexHandle); + + return true; +} + Process::Process() = default; void -- cgit v1.2.3