CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
rw_lock.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_THREADS_RW_LOCK_H
10#define CPPCOMMON_THREADS_RW_LOCK_H
11
12#include "threads/locker.h"
13#include "time/timestamp.h"
14
15#include <memory>
16
17namespace CppCommon {
18
20
30class RWLock
31{
32public:
33 RWLock();
34 RWLock(const RWLock&) = delete;
35 RWLock(RWLock&& lock) = delete;
36 ~RWLock();
37
38 RWLock& operator=(const RWLock&) = delete;
39 RWLock& operator=(RWLock&& lock) = delete;
40
42
47 bool TryLockRead();
49
54 bool TryLockWrite();
55
57
63 bool TryLockReadFor(const Timespan& timespan);
65
71 bool TryLockWriteFor(const Timespan& timespan);
73
79 bool TryLockReadUntil(const UtcTimestamp& timestamp)
80 { return TryLockReadFor(timestamp - UtcTimestamp()); }
82
88 bool TryLockWriteUntil(const UtcTimestamp& timestamp)
89 { return TryLockWriteFor(timestamp - UtcTimestamp()); }
90
92
95 void LockRead();
97
100 void LockWrite();
101
103
106 void UnlockRead();
108
111 void UnlockWrite();
112
113private:
114 class Impl;
115
116 Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
117 const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
118
119#if defined(__APPLE__)
120 static const size_t StorageSize = 256;
121#else
122 static const size_t StorageSize = 56;
123#endif
124 static const size_t StorageAlign = 8;
125 alignas(StorageAlign) std::byte _storage[StorageSize];
126};
127
130} // namespace CppCommon
131
132#endif // CPPCOMMON_THREADS_RW_LOCK_H
Read/Write lock synchronization primitive.
Definition rw_lock.h:31
RWLock & operator=(const RWLock &)=delete
bool TryLockWriteUntil(const UtcTimestamp &timestamp)
Try to acquire write lock until the given timestamp.
Definition rw_lock.h:88
void UnlockRead()
Release read lock.
Definition rw_lock.cpp:199
void LockWrite()
Acquire write lock with block.
Definition rw_lock.cpp:198
void UnlockWrite()
Release write lock.
Definition rw_lock.cpp:200
bool TryLockRead()
Try to acquire read lock without block.
Definition rw_lock.cpp:146
RWLock(RWLock &&lock)=delete
void LockRead()
Acquire read lock with block.
Definition rw_lock.cpp:197
RWLock & operator=(RWLock &&lock)=delete
bool TryLockReadUntil(const UtcTimestamp &timestamp)
Try to acquire read lock until the given timestamp.
Definition rw_lock.h:79
RWLock(const RWLock &)=delete
bool TryLockReadFor(const Timespan &timespan)
Try to acquire read lock for the given timespan.
Definition rw_lock.cpp:149
bool TryLockWrite()
Try to acquire write lock without block.
Definition rw_lock.cpp:147
bool TryLockWriteFor(const Timespan &timespan)
Try to acquire write lock for the given timespan.
Definition rw_lock.cpp:173
Locker synchronization primitive definition.
C++ Common project definitions.
Timestamp definition.