diff options
| author | Stefan Boberg <[email protected]> | 2021-08-05 21:37:35 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-08-05 21:37:35 +0200 |
| commit | 5f96a135c503c02e5ef200be1a4f68be4ada60c2 (patch) | |
| tree | c1ac1cfdeddd297ba80b0284aa1f3bbf3348d053 /zencore/thread.cpp | |
| parent | Added Oodle to CompressedBuffer (#5) (diff) | |
| download | zen-5f96a135c503c02e5ef200be1a4f68be4ada60c2.tar.xz zen-5f96a135c503c02e5ef200be1a4f68be4ada60c2.zip | |
Added basic NamedMutex implementation
Diffstat (limited to 'zencore/thread.cpp')
| -rw-r--r-- | zencore/thread.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
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 |