Repository navigation
Expand file tree
/
Copy pathSharedLocker.mpp
More file actions
240 lines (211 loc) · 6.68 KB
/
Copy pathSharedLocker.mpp
File metadata and controls
240 lines (211 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
export module CppUtils.Thread.SharedLocker;
import std;
export import CppUtils.Thread.UniqueLocker;
export namespace CppUtils::Thread
{
class SharedMutex final
{
public:
inline constexpr SharedMutex() noexcept
{
if not consteval
{
m_mutex.emplace();
}
}
SharedMutex(const SharedMutex&) = delete;
auto operator=(const SharedMutex&) -> SharedMutex& = delete;
SharedMutex(SharedMutex&&) = delete;
auto operator=(SharedMutex&&) -> SharedMutex& = delete;
inline constexpr auto lock() -> void
{
if not consteval
{
m_mutex->lock();
}
}
inline constexpr auto try_lock() -> bool
{
if not consteval
{
return m_mutex->try_lock();
}
return true;
}
inline constexpr auto unlock() -> void
{
if not consteval
{
m_mutex->unlock();
}
}
inline constexpr auto lock_shared() -> void
{
if not consteval
{
m_mutex->lock_shared();
}
}
inline constexpr auto try_lock_shared() -> bool
{
if not consteval
{
return m_mutex->try_lock_shared();
}
return true;
}
inline constexpr auto unlock_shared() -> void
{
if not consteval
{
m_mutex->unlock_shared();
}
}
private:
std::optional<std::shared_mutex> m_mutex;
};
/**
* @brief RAII accessor providing concurrent read-only access to a protected value.
*
* Uses std::shared_lock internally to allow multiple readers simultaneously.
*
* @see https://morgancaron.github.io/CppUtils/fr/guides/lockers/ (Guide de synchronisation RAII)
* @see https://morgancaron.github.io/CppUtils/en/guides/lockers/ (RAII synchronization guide)
*
* @tparam T Type of the value being accessed.
* @tparam Mutex Mutex type used for locking (defaults to SharedMutex).
*/
template<class T, class Mutex = SharedMutex>
class ReadOnlyAccessor final
{
public:
/**
* @brief Constructs a read-only accessor by acquiring a shared lock on the mutex.
* @param mutex Shared mutex to lock.
* @param value Reference to the value to read.
*/
inline explicit constexpr ReadOnlyAccessor(Mutex& mutex, const T& value):
m_value{std::cref(value)}
{
if not consteval
{
m_lockGuard.emplace(mutex);
}
}
/// @brief Returns a const reference to the protected value.
[[nodiscard]] inline constexpr auto value(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> decltype(auto)
{
return self.m_value.get();
}
/// @brief Accesses const member functions or fields of the protected value.
[[nodiscard]] inline constexpr auto operator->(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> const T*
{
return std::addressof(self.value());
}
/// @brief Dereferences to obtain the protected const value (equivalent to calling value()).
[[nodiscard]] inline constexpr auto operator*(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> decltype(auto)
{
return self.value();
}
/**
* @brief Returns a reference to the underlying @c std::shared_lock guard.
*
* Provides direct access to the lock guard, allowing it to be used with
* synchronization primitives requiring a @c std::shared_lock (such as @c std::condition_variable_any::wait).
*
* When used with @c std::condition_variable_any::wait, the wait function atomically releases the shared mutex while
* placing the thread to sleep, and re-acquires the shared lock upon waking before returning once the predicate evaluates to true:
* @code
* auto reader = cache.sharedAccess();
* conditionAny.wait(reader.getLockGuard(), [&] {
* return reader->contains("ready");
* });
* @endcode
*
* @return Reference to the internal @c std::shared_lock<Mutex>.
*/
[[nodiscard]] inline constexpr auto getLockGuard(this auto&& self [[clang::lifetimebound, msvc::lifetimebound]]) noexcept -> auto&
{
return self.m_lockGuard.value();
}
private:
mutable std::optional<std::shared_lock<Mutex>> m_lockGuard;
std::reference_wrapper<const T> m_value;
};
template<class Mutex, class T>
ReadOnlyAccessor(Mutex&, const T&) -> ReadOnlyAccessor<T, Mutex>;
/**
* @brief Monitor Object wrapper providing shared read / exclusive write synchronization.
*
* Encapsulates a resource with a std::shared_mutex, allowing either multiple concurrent
* read-only accessors (via sharedAccess()) or a single exclusive read-write accessor
* (via uniqueAccess()).
*
* @see https://morgancaron.github.io/CppUtils/fr/guides/lockers/ (Guide de synchronisation RAII)
* @see https://morgancaron.github.io/CppUtils/en/guides/lockers/ (RAII synchronization guide)
*
* @tparam T Type of the protected resource.
*/
template<class T>
class SharedLocker final
{
public:
using UniqueAccessor = Accessor<T, SharedMutex>;
using SharedAccessor = ReadOnlyAccessor<T, SharedMutex>;
inline constexpr SharedLocker() requires std::default_initializable<T> = default;
/**
* @brief Constructs the protected resource in place.
* @param first First argument forwarded to the resource constructor.
* @param rest Rest of arguments forwarded to the resource constructor.
*/
inline explicit constexpr SharedLocker(auto&& first, auto&&... rest):
m_value{std::forward<decltype(first)>(first), std::forward<decltype(rest)>(rest)...}
{}
SharedLocker(const SharedLocker&) = delete;
auto operator=(const SharedLocker&) -> SharedLocker& = delete;
/// @brief Move constructor. Acquires lock on the source locker before moving its value.
inline constexpr SharedLocker(SharedLocker&& other) noexcept(std::is_nothrow_move_constructible_v<T>):
m_value{std::move(other.uniqueAccess().value())}
{}
/// @brief Move assignment. Acquires scoped lock on both mutexes before moving the value.
inline constexpr auto operator=(SharedLocker&& other) noexcept(std::is_nothrow_move_assignable_v<T>) -> SharedLocker&
{
if (this != std::addressof(other))
{
if not consteval
{
auto guard = std::scoped_lock{m_mutex, other.m_mutex};
m_value = std::move(other.m_value);
}
else
{
m_value = std::move(other.m_value);
}
}
return *this;
}
/**
* @brief Acquires exclusive read/write access.
* @return UniqueAccessor holding a unique lock.
*/
[[nodiscard]] inline constexpr auto uniqueAccess() -> UniqueAccessor
{
return Accessor{m_mutex, m_value};
}
/**
* @brief Acquires shared read-only access.
* @return SharedAccessor holding a shared lock.
*/
[[nodiscard]] inline constexpr auto sharedAccess() const -> SharedAccessor
{
return ReadOnlyAccessor{m_mutex, m_value};
}
private:
mutable SharedMutex m_mutex;
T m_value{};
template<class... Lockers>
friend struct MultipleAccessor;
};
template<class T>
SharedLocker(T) -> SharedLocker<T>;
}