summaryrefslogtreecommitdiff
path: root/lib/DecoderWorkerLock.cpp
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2026-10-09 14:12:49 +0200
committerChristoph Berg <myon@debian.org>2026-10-09 14:12:49 +0200
commit76956486fa7ef52a51d4e56df1e13a9f19a6c93c (patch)
treefe0c394439fdc167d1970372e8998874090854dd /lib/DecoderWorkerLock.cpp
parent0685fcada9f95fe90f70b387cd7d757da8209624 (diff)
parentffc62c792c9a5196cb4ce7a353287dfe20db6524 (diff)
Update upstream source from tag 'upstream/3.3.0_beta1+dfsg'
Update to upstream version '3.3.0~beta1+dfsg' with Debian dir 85037bbb9376721108594b59b54ead82ac6924a3
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_);
+}