From 5aa790de8dd06fdcb3c4617cb454216d475246fa Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Mon, 29 Nov 2021 08:58:44 +0100 Subject: Implemented NamedMutex for Linux --- zencore/thread.cpp | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'zencore/thread.cpp') diff --git a/zencore/thread.cpp b/zencore/thread.cpp index 3b69b7a1e..8508f35c6 100644 --- a/zencore/thread.cpp +++ b/zencore/thread.cpp @@ -390,7 +390,9 @@ NamedMutex::~NamedMutex() CloseHandle(m_MutexHandle); } #else - /* ZEN_TODO_MR: NamedMutex */ + int Inner = int(intptr_t(m_MutexHandle)); + flock(Inner, LOCK_UN); + close(Inner); #endif } @@ -410,8 +412,23 @@ NamedMutex::Create(std::string_view MutexName) return !!m_MutexHandle; #else - ZEN_UNUSED(MutexName); - /* ZEN_TODO_MR: NamedMutex */ + ExtendableStringBuilder<64> Name; + Name << "/tmp/" << MutexName; + + int Inner = open(Name.c_str(), O_RDWR|O_CREAT|O_CLOEXEC, 0644); + if (Inner < 0) + { + return false; + } + + if (flock(Inner, LOCK_EX) != 0) + { + close(Inner); + Inner = 0; + return false; + } + + m_MutexHandle = (void*)(intptr_t(Inner)); return true; #endif // ZEN_PLATFORM_WINDOWS } @@ -437,9 +454,25 @@ NamedMutex::Exists(std::string_view MutexName) return true; #else - ZEN_UNUSED(MutexName); - /* ZEN_TODO_MR: NamedMutex */ - return false; + ExtendableStringBuilder<64> Name; + Name << "/tmp/" << MutexName; + + bool bExists = false; + int Fd = open(Name.c_str(), O_RDWR, 0644); + if (Fd >= 0) + { + if (flock(Fd, LOCK_EX|LOCK_NB) == 0) + { + flock(Fd, LOCK_UN|LOCK_NB); + } + else + { + bExists = true; + } + close(Fd); + } + + return bExists; #endif // ZEN_PLATFORM_WINDOWS } -- cgit v1.2.3