CppCommon  1.0.4.1
C++ Common Library
file_lock.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_FILE_LOCK_H
10 #define CPPCOMMON_THREADS_FILE_LOCK_H
11 
12 #include "filesystem/file.h"
13 #include "threads/locker.h"
14 #include "time/timestamp.h"
15 
16 #include <memory>
17 
18 namespace CppCommon {
19 
21 
33 class FileLock
34 {
35 public:
36  FileLock();
37  explicit FileLock(const Path& path);
38  FileLock(const FileLock&) = delete;
39  FileLock(FileLock&& lock) = delete;
40  ~FileLock();
41 
42  FileLock& operator=(const Path& path);
43  FileLock& operator=(const FileLock&) = delete;
44  FileLock& operator=(FileLock&& lock) = delete;
45 
47  const Path& path() const noexcept;
48 
50 
53  void Assign(const Path& path);
54 
56  void Reset();
57 
59 
64  bool TryLockRead();
66 
71  bool TryLockWrite();
72 
74 
80  bool TryLockReadFor(const Timespan& timespan);
82 
88  bool TryLockWriteFor(const Timespan& timespan);
90 
96  bool TryLockReadUntil(const UtcTimestamp& timestamp)
97  { return TryLockReadFor(timestamp - UtcTimestamp()); }
99 
105  bool TryLockWriteUntil(const UtcTimestamp& timestamp)
106  { return TryLockWriteFor(timestamp - UtcTimestamp()); }
107 
109 
112  void LockRead();
114 
117  void LockWrite();
118 
120 
123  void UnlockRead();
125 
128  void UnlockWrite();
129 
130 private:
131  class Impl;
132 
133  Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
134  const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
135 
136  static const size_t StorageSize = 48;
137  static const size_t StorageAlign = 8;
138  alignas(StorageAlign) std::byte _storage[StorageSize];
139 };
140 
143 } // namespace CppCommon
144 
145 #endif // CPPCOMMON_THREADS_FILE_LOCK_H
File-lock synchronization primitive.
Definition: file_lock.h:34
FileLock(const FileLock &)=delete
void UnlockRead()
Release read lock.
Definition: file_lock.cpp:382
FileLock & operator=(const Path &path)
Definition: file_lock.cpp:318
FileLock & operator=(FileLock &&lock)=delete
bool TryLockWrite()
Try to acquire write lock without block.
Definition: file_lock.cpp:330
void LockRead()
Acquire read lock with block.
Definition: file_lock.cpp:380
bool TryLockReadFor(const Timespan &timespan)
Try to acquire read lock for the given timespan.
Definition: file_lock.cpp:332
const Path & path() const noexcept
Get the file-lock path.
Definition: file_lock.cpp:324
FileLock & operator=(const FileLock &)=delete
void UnlockWrite()
Release write lock.
Definition: file_lock.cpp:383
bool TryLockWriteUntil(const UtcTimestamp &timestamp)
Try to acquire write lock until the given timestamp.
Definition: file_lock.h:105
bool TryLockReadUntil(const UtcTimestamp &timestamp)
Try to acquire read lock until the given timestamp.
Definition: file_lock.h:96
bool TryLockRead()
Try to acquire read lock without block.
Definition: file_lock.cpp:329
void Reset()
Reset file-lock.
Definition: file_lock.cpp:327
bool TryLockWriteFor(const Timespan &timespan)
Try to acquire write lock for the given timespan.
Definition: file_lock.cpp:356
void Assign(const Path &path)
Assign a new file-lock path.
Definition: file_lock.cpp:326
void LockWrite()
Acquire write lock with block.
Definition: file_lock.cpp:381
FileLock(FileLock &&lock)=delete
Filesystem path.
Definition: path.h:90
UTC timestamp.
Definition: timestamp.h:248
Filesystem file definition.
Locker synchronization primitive definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Timestamp definition.