From 389305ded99e9c6dc0827584d6e997151fb25cb6 Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Thu, 25 Nov 2021 14:31:44 +0100 Subject: Use file descriptor locks to claim global ownership of message queues --- zencore/thread.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'zencore/thread.cpp') diff --git a/zencore/thread.cpp b/zencore/thread.cpp index e009ccc41..3eeecbf4c 100644 --- a/zencore/thread.cpp +++ b/zencore/thread.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,7 @@ # include # include # include +# include # include # include #endif @@ -267,13 +269,18 @@ NamedEvent::NamedEvent(std::string_view EventName) 0, // current messages }; - int Inner = mq_open(Name.c_str(), O_RDWR|O_CREAT|O_CLOEXEC, 0666, &Attributes); + int Inner = mq_open(Name.c_str(), O_RDWR|O_CREAT|O_CLOEXEC, 0644, &Attributes); if (Inner < 0) { ThrowLastError("Failed to get message queue from mq_open()"); } - //mq_unlink(Name.c_str()); + int LockResult = flock(Inner, LOCK_EX|LOCK_NB); + if (LockResult == 0) + { + ZEN_ASSERT( IsThereAMessageInQueue(Inner) == false, + "Ownership of a non-empty '{}' message queue occurred", Name.c_str()); + } m_EventHandle = (void*)intptr_t(Inner); #else @@ -297,6 +304,14 @@ void NamedEvent::Close() CloseHandle(m_EventHandle); #elif ZEN_PLATFORM_LINUX int Inner = int(intptr_t(m_EventHandle)); + + if (flock(Inner, LOCK_EX|LOCK_NB) == 0) + { + flock(Inner, LOCK_UN|LOCK_NB); + std::filesystem::path Name = PathFromHandle((void*)(intptr_t(Inner))); + mq_unlink(Name.c_str()); + } + close(Inner); #endif -- cgit v1.2.3