aboutsummaryrefslogtreecommitdiff
path: root/zencore/thread.cpp
diff options
context:
space:
mode:
authorMartin Ridgers <[email protected]>2021-11-25 14:31:44 +0100
committerMartin Ridgers <[email protected]>2021-11-25 14:37:40 +0100
commit389305ded99e9c6dc0827584d6e997151fb25cb6 (patch)
tree61d088dfbc8b3bddd38f696a326dde9c251f7cbf /zencore/thread.cpp
parentA whitespace garden (diff)
downloadzen-389305ded99e9c6dc0827584d6e997151fb25cb6.tar.xz
zen-389305ded99e9c6dc0827584d6e997151fb25cb6.zip
Use file descriptor locks to claim global ownership of message queues
Diffstat (limited to 'zencore/thread.cpp')
-rw-r--r--zencore/thread.cpp19
1 files changed, 17 insertions, 2 deletions
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 <zencore/thread.h>
#include <zencore/except.h>
+#include <zencore/filesystem.h>
#include <zencore/scopeguard.h>
#include <zencore/string.h>
#include <zencore/testing.h>
@@ -20,6 +21,7 @@
# include <mqueue.h>
# include <pthread.h>
# include <signal.h>
+# include <sys/file.h>
# include <time.h>
# include <unistd.h>
#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