summaryrefslogtreecommitdiff
path: root/lib/DecoderWorkerLock.cpp
diff options
context:
space:
mode:
authorDave Hibberd <hibby@debian.org>2026-10-20 22:35:06 +0100
committerDave Hibberd <hibby@debian.org>2026-10-20 22:35:06 +0100
commit82ebb1ed1af30cee4dfbb1c628431d32885a00e0 (patch)
tree1a1f782d3e576b758be0bc61171574a7cd8f2376 /lib/DecoderWorkerLock.cpp
parent061658210acb6425517733ea012dda803a7f72a1 (diff)
parent61d684922b0f1c8455cc60eb3d123df058c9e1f5 (diff)
Merge tag 'debian/3.3.0_beta2+dfsg-1' into hibbian/trixie-backports
tagging wsjtx 3.3.0~beta2+dfsg-1
Diffstat (limited to 'lib/DecoderWorkerLock.cpp')
-rw-r--r--lib/DecoderWorkerLock.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/DecoderWorkerLock.cpp b/lib/DecoderWorkerLock.cpp
new file mode 100644
index 00000000..617894af
--- /dev/null
+++ b/lib/DecoderWorkerLock.cpp
@@ -0,0 +1,55 @@
+#include "DecoderWorkerLock.hpp"
+
+#include <cerrno>
+#include <system_error>
+
+#ifndef _WIN32
+#include <fcntl.h>
+#include <sys/file.h>
+#include <unistd.h>
+#endif
+
+DecoderWorkerLock::DecoderWorkerLock (char const * path) noexcept
+{
+#ifndef _WIN32
+ if (!path || !*path) { error_ = EINVAL; return; }
+ do
+ {
+ descriptor_ = open (path, O_RDWR | O_CREAT | O_CLOEXEC | O_NOFOLLOW, 0600);
+ }
+ while (descriptor_ == -1 && errno == EINTR);
+ if (descriptor_ == -1) error_ = errno;
+#else
+ (void) path;
+#endif
+}
+
+DecoderWorkerLock::~DecoderWorkerLock ()
+{
+#ifndef _WIN32
+ // Keep the inode: unlinking it would let another worker lock a different file.
+ if (descriptor_ != -1) close (descriptor_);
+#endif
+}
+
+bool DecoderWorkerLock::tryLock () noexcept
+{
+#ifndef _WIN32
+ if (descriptor_ == -1) return false;
+ int result;
+ do { result = flock (descriptor_, LOCK_EX | LOCK_NB); }
+ while (result == -1 && errno == EINTR);
+ error_ = result == -1 ? errno : 0;
+#endif
+ return error_ == 0;
+}
+
+bool DecoderWorkerLock::busy () const noexcept
+{
+ return error_ == EWOULDBLOCK || error_ == EAGAIN;
+}
+
+std::string DecoderWorkerLock::errorString () const
+{
+ return std::system_category ().message (error_);
+}