BuildCache
file_lock.hpp
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (c) 2019 Marcus Geelnard
3 //
4 // This software is provided 'as-is', without any express or implied warranty. In no event will the
5 // authors be held liable for any damages arising from the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose, including commercial
8 // applications, and to alter it and redistribute it freely, subject to the following restrictions:
9 //
10 // 1. The origin of this software must not be misrepresented; you must not claim that you wrote
11 // the original software. If you use this software in a product, an acknowledgment in the
12 // product documentation would be appreciated but is not required.
13 //
14 // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
15 // being the original software.
16 //
17 // 3. This notice may not be removed or altered from any source distribution.
18 //--------------------------------------------------------------------------------------------------
19 
20 #ifndef BUILDCACHE_FILE_LOCK_HPP_
21 #define BUILDCACHE_FILE_LOCK_HPP_
22 
23 #include <string>
24 
25 namespace bcache {
26 namespace file {
61 class file_lock_t {
62 public:
65  }
66 
72  explicit file_lock_t(const std::string& path, const bool remote_lock);
73 
74  // Support move semantics.
75  file_lock_t(file_lock_t&& other) noexcept;
76  file_lock_t& operator=(file_lock_t&& other) noexcept;
77  file_lock_t(const file_lock_t& other) = delete;
78  file_lock_t& operator=(const file_lock_t& other) = delete;
79 
83  ~file_lock_t();
84 
86  bool has_lock() const {
87  return m_has_lock;
88  }
89 
90 private:
91 #if defined(_WIN32)
92  // WIN32 API HANDLE type is void*.
93  using file_handle_t = void*;
94  using mutex_handle_t = void*;
95 #else
96  // POSIX file descriptor type is int.
97  using file_handle_t = int;
98  using mutex_handle_t = void*; // dummy
99 #endif
100 
101  static file_handle_t invalid_file_handle() {
102 #if defined(_WIN32)
103  return reinterpret_cast<void*>(-1); // INVALID_HANDLE_VALUE
104 #else
105  return -1;
106 #endif
107  }
108 
109  static mutex_handle_t invalid_mutex_handle() {
110 #if defined(_WIN32)
111  return nullptr;
112 #else
113  return nullptr;
114 #endif
115  }
116 
117  std::string m_path;
118  file_handle_t m_file_handle = invalid_file_handle();
119  mutex_handle_t m_mutex_handle = invalid_mutex_handle();
120  bool m_has_lock = false;
121 };
122 
123 } // namespace file
124 } // namespace bcache
125 
126 #endif // BUILDCACHE_FILE_LOCK_HPP_
bcache::file::file_lock_t::~file_lock_t
~file_lock_t()
Release the lock.
Definition: file_lock.cpp:190
bcache
The top level namespace.
Definition: compressor.cpp:34
bcache::file::file_lock_t::has_lock
bool has_lock() const
Definition: file_lock.hpp:86
bcache::file::file_lock_t
A scoped exclusive global lock class.
Definition: file_lock.hpp:61
bcache::file::file_lock_t::file_lock_t
file_lock_t()
Create an empty (unlocked) lock object.
Definition: file_lock.hpp:64