CppCommon  1.0.4.1
C++ Common Library
named_rw_lock.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_NAMED_RW_LOCK_H
10 #define CPPCOMMON_THREADS_NAMED_RW_LOCK_H
11 
12 #include "threads/locker.h"
13 #include "time/timestamp.h"
14 
15 #include <memory>
16 #include <string>
17 
18 namespace CppCommon {
19 
21 
33 {
34 public:
36 
39  explicit NamedRWLock(const std::string& name);
40  NamedRWLock(const NamedRWLock&) = delete;
41  NamedRWLock(NamedRWLock&& lock) = delete;
42  ~NamedRWLock();
43 
44  NamedRWLock& operator=(const NamedRWLock&) = delete;
45  NamedRWLock& operator=(NamedRWLock&& lock) = delete;
46 
48  const std::string& name() const;
49 
51 
56  bool TryLockRead();
58 
63  bool TryLockWrite();
65 
70  bool TryConvertWriteToRead();
71 
73 
79  bool TryLockReadFor(const Timespan& timespan);
81 
87  bool TryLockWriteFor(const Timespan& timespan);
89 
95  bool TryConvertWriteToReadFor(const Timespan& timespan);
97 
103  bool TryLockReadUntil(const UtcTimestamp& timestamp)
104  { return TryLockReadFor(timestamp - UtcTimestamp()); }
106 
112  bool TryLockWriteUntil(const UtcTimestamp& timestamp)
113  { return TryLockWriteFor(timestamp - UtcTimestamp()); }
115 
122  { return TryConvertWriteToReadFor(timestamp - UtcTimestamp()); }
123 
125 
128  void LockRead();
130 
133  void LockWrite();
134 
136 
139  void UnlockRead();
141 
144  void UnlockWrite();
145 
147 
154  void ConvertWriteToRead();
155 
156 private:
157  class Impl;
158 
159  Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
160  const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
161 
162  static const size_t StorageSize = 184;
163  static const size_t StorageAlign = 8;
164  alignas(StorageAlign) std::byte _storage[StorageSize];
165 };
166 
169 } // namespace CppCommon
170 
171 #endif // CPPCOMMON_THREADS_NAMED_RW_LOCK_H
Named read/write lock synchronization primitive.
Definition: named_rw_lock.h:33
void UnlockRead()
Release read lock.
void UnlockWrite()
Release write lock.
bool TryConvertWriteToRead()
Try to convert write lock to read lock without block.
bool TryLockReadFor(const Timespan &timespan)
Try to acquire read lock for the given timespan.
void LockRead()
Acquire read lock with block.
NamedRWLock(NamedRWLock &&lock)=delete
bool TryLockWrite()
Try to acquire write lock without block.
bool TryConvertWriteToReadUntil(const UtcTimestamp &timestamp)
Try to convert write lock to read lock until the given timestamp.
void ConvertWriteToRead()
Convert write lock to read lock without block.
bool TryLockReadUntil(const UtcTimestamp &timestamp)
Try to acquire read lock until the given timestamp.
bool TryLockRead()
Try to acquire read lock without block.
NamedRWLock(const NamedRWLock &)=delete
bool TryLockWriteFor(const Timespan &timespan)
Try to acquire write lock for the given timespan.
bool TryLockWriteUntil(const UtcTimestamp &timestamp)
Try to acquire write lock until the given timestamp.
NamedRWLock & operator=(const NamedRWLock &)=delete
void LockWrite()
Acquire write lock with block.
NamedRWLock(const std::string &name)
Default class constructor.
NamedRWLock & operator=(NamedRWLock &&lock)=delete
const std::string & name() const
Get the read/write lock name.
bool TryConvertWriteToReadFor(const Timespan &timespan)
Try to convert write lock to read lock for the given timespan.
UTC timestamp.
Definition: timestamp.h:248
Locker synchronization primitive definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Timestamp definition.