diff options
| author | Martin Ridgers <[email protected]> | 2021-11-29 08:58:44 +0100 |
|---|---|---|
| committer | Martin Ridgers <[email protected]> | 2021-11-29 08:59:50 +0100 |
| commit | 5aa790de8dd06fdcb3c4617cb454216d475246fa (patch) | |
| tree | 76f19c02df371248adc03898367a984d87d03bb3 /zencore/thread.cpp | |
| parent | Use the /proc/self mount to get information about ourselves (diff) | |
| download | zen-5aa790de8dd06fdcb3c4617cb454216d475246fa.tar.xz zen-5aa790de8dd06fdcb3c4617cb454216d475246fa.zip | |
Implemented NamedMutex for Linux
Diffstat (limited to 'zencore/thread.cpp')
| -rw-r--r-- | zencore/thread.cpp | 45 |
1 files changed, 39 insertions, 6 deletions
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 } |