]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Make util::LockFile movable
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 30 Dec 2022 21:00:17 +0000 (22:00 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 11 Jan 2023 18:42:32 +0000 (19:42 +0100)
src/util/LockFile.cpp
src/util/LockFile.hpp

index 26b38e6b978eb5f8ca367fae72eb89038a3b373b..2e5bef320e7da6483636f19ca80961c3a0cb2a65 100644 (file)
@@ -82,6 +82,43 @@ LockFile::LockFile(const std::string& path)
 {
 }
 
+LockFile::LockFile(LockFile&& other) noexcept
+  : m_lock_file(std::move(other.m_lock_file)),
+#ifndef _WIN32
+    m_lock_manager(other.m_lock_manager),
+    m_alive_file(std::move(other.m_alive_file)),
+    m_acquired(other.m_acquired)
+#else
+    m_handle(other.m_handle)
+#endif
+{
+#ifndef _WIN32
+  other.m_lock_manager = nullptr;
+  other.m_acquired = false;
+#else
+  other.m_handle = INVALID_HANDLE_VALUE;
+#endif
+}
+
+LockFile&
+LockFile::operator=(LockFile&& other) noexcept
+{
+  if (&other != this) {
+    m_lock_file = std::move(other.m_lock_file);
+#ifndef _WIN32
+    m_lock_manager = other.m_lock_manager;
+    other.m_lock_manager = nullptr;
+    m_alive_file = std::move(other.m_alive_file);
+    m_acquired = other.m_acquired;
+    other.m_acquired = false;
+#else
+    m_handle = other.m_handle;
+    other.m_handle = INVALID_HANDLE_VALUE;
+#endif
+  }
+  return *this;
+}
+
 void
 LockFile::make_long_lived(
   [[maybe_unused]] LongLivedLockFileManager& lock_manager)
index bdb3f8cb9b1f39b6c6a70709f78fd2c334989f46..67d1cb043fe884457aeb4d2bf19f266466688c65 100644 (file)
@@ -34,6 +34,9 @@ class LockFile : NonCopyable
 {
 public:
   explicit LockFile(const std::string& path);
+  LockFile(LockFile&& other) noexcept;
+
+  LockFile& operator=(LockFile&& other) noexcept;
 
   // Release the lock if previously acquired.
   ~LockFile();