CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
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
18namespace CppCommon {
19
21
34{
35public:
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
130private:
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
FileLock & operator=(FileLock &&lock)=delete
void UnlockRead()
Release read lock.
FileLock & operator=(const Path &path)
bool TryLockWrite()
Try to acquire write lock without block.
void LockRead()
Acquire read lock with block.
FileLock & operator=(const FileLock &)=delete
bool TryLockReadFor(const Timespan &timespan)
Try to acquire read lock for the given timespan.
const Path & path() const noexcept
Get the file-lock path.
void UnlockWrite()
Release write lock.
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.
void Reset()
Reset file-lock.
bool TryLockWriteFor(const Timespan &timespan)
Try to acquire write lock for the given timespan.
void Assign(const Path &path)
Assign a new file-lock path.
void LockWrite()
Acquire write lock with block.
FileLock(FileLock &&lock)=delete
Filesystem path.
Definition path.h:90
Filesystem file definition.
Locker synchronization primitive definition.
C++ Common project definitions.
Timestamp definition.