userver: userver/engine/semaphore.hpp Source File
Loading...
Searching...
No Matches
semaphore.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <atomic>
7#include <chrono>
8#include <shared_mutex> // for std locks
9#include <stdexcept>
10
12#include <userver/engine/impl/wait_list_fwd.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace engine {
17
20class UnreachableSemaphoreLockError final : public std::runtime_error {
21public:
22 using std::runtime_error::runtime_error;
23};
24
26class SemaphoreLockCancelledError final : public std::runtime_error {
27public:
28 using std::runtime_error::runtime_error;
29};
30
42public:
43 using Counter = std::size_t;
44
47 explicit CancellableSemaphore(Counter capacity);
48
50
53 CancellableSemaphore& operator=(CancellableSemaphore&&) = delete;
54 CancellableSemaphore& operator=(const CancellableSemaphore&) = delete;
55
58 void SetCapacity(Counter capacity);
59
61 [[nodiscard]] Counter GetCapacity() const noexcept;
62
64 [[nodiscard]] std::size_t RemainingApprox() const;
65
67 [[nodiscard]] std::size_t UsedApprox() const;
68
82
93
94 [[nodiscard]] bool try_lock_shared();
95
96 template <typename Rep, typename Period>
97 [[nodiscard]] bool try_lock_shared_for(std::chrono::duration<Rep, Period>);
98
99 template <typename Clock, typename Duration>
100 [[nodiscard]] bool try_lock_shared_until(std::chrono::time_point<Clock, Duration>);
101
102 [[nodiscard]] bool try_lock_shared_until(Deadline deadline);
103
104 void lock_shared_count(Counter count);
105
106 void unlock_shared_count(Counter count);
107
108 [[nodiscard]] bool try_lock_shared_count(Counter count);
109
110 [[nodiscard]] bool try_lock_shared_until_count(Deadline deadline, Counter count);
111
112private:
113 enum class TryLockStatus { kSuccess, kTransientFailure, kPermanentFailure };
114 class SemaphoreAwaitable;
115
116 TryLockStatus DoTryLock(Counter count);
117 TryLockStatus LockFastPath(Counter count);
118 bool LockSlowPath(Deadline, Counter count);
119
120 impl::FastPimplWaitList lock_awaiters_;
121 std::atomic<Counter> acquired_locks_;
122 std::atomic<Counter> capacity_;
123};
124
136class Semaphore final {
137public:
138 using Counter = std::size_t;
139
142 explicit Semaphore(Counter capacity);
143
144 ~Semaphore();
145
146 Semaphore(Semaphore&&) = delete;
147 Semaphore(const Semaphore&) = delete;
148 Semaphore& operator=(Semaphore&&) = delete;
149 Semaphore& operator=(const Semaphore&) = delete;
150
153 void SetCapacity(Counter capacity);
154
156 [[nodiscard]] Counter GetCapacity() const noexcept;
157
159 [[nodiscard]] std::size_t RemainingApprox() const;
160
162 [[nodiscard]] std::size_t UsedApprox() const;
163
172
181
186 [[nodiscard]] bool try_lock_shared();
187
188 template <typename Rep, typename Period>
189 [[nodiscard]] bool try_lock_shared_for(std::chrono::duration<Rep, Period>);
190
191 template <typename Clock, typename Duration>
192 [[nodiscard]] bool try_lock_shared_until(std::chrono::time_point<Clock, Duration>);
193
194 [[nodiscard]] bool try_lock_shared_until(Deadline deadline);
195
196 void lock_shared_count(Counter count);
197
198 void unlock_shared_count(Counter count);
199
200 [[nodiscard]] bool try_lock_shared_count(Counter count);
201
202 [[nodiscard]] bool try_lock_shared_until_count(Deadline deadline, Counter count);
203
204private:
206};
207
209class SemaphoreLock final {
210public:
211 SemaphoreLock() noexcept = default;
212 explicit SemaphoreLock(Semaphore&);
213 SemaphoreLock(Semaphore&, std::defer_lock_t) noexcept;
214 SemaphoreLock(Semaphore&, std::try_to_lock_t);
215 SemaphoreLock(Semaphore&, std::adopt_lock_t) noexcept;
216
217 template <typename Rep, typename Period>
218 SemaphoreLock(Semaphore&, std::chrono::duration<Rep, Period>);
219
220 template <typename Clock, typename Duration>
221 SemaphoreLock(Semaphore&, std::chrono::time_point<Clock, Duration>);
222
223 SemaphoreLock(Semaphore&, Deadline);
224
225 ~SemaphoreLock();
226
227 SemaphoreLock(const SemaphoreLock&) = delete;
228 SemaphoreLock(SemaphoreLock&&) noexcept;
229 SemaphoreLock& operator=(const SemaphoreLock&) = delete;
230 SemaphoreLock& operator=(SemaphoreLock&&) noexcept;
231
232 [[nodiscard]] bool OwnsLock() const noexcept;
233 explicit operator bool() const noexcept { return OwnsLock(); }
234
235 void Lock();
236 bool TryLock();
237
238 template <typename Rep, typename Period>
239 bool TryLockFor(std::chrono::duration<Rep, Period>);
240
241 template <typename Clock, typename Duration>
242 bool TryLockUntil(std::chrono::time_point<Clock, Duration>);
243
244 bool TryLockUntil(Deadline);
245
246 void Unlock();
247 void Release();
248
249private:
250 Semaphore* sem_{nullptr};
251 bool owns_lock_{false};
252};
253
254template <typename Rep, typename Period>
255bool Semaphore::try_lock_shared_for(std::chrono::duration<Rep, Period> duration) {
256 return try_lock_shared_until(Deadline::FromDuration(duration));
257}
258
259template <typename Clock, typename Duration>
260bool Semaphore::try_lock_shared_until(std::chrono::time_point<Clock, Duration> until) {
261 return try_lock_shared_until(Deadline::FromTimePoint(until));
262}
263
264template <typename Rep, typename Period>
265bool CancellableSemaphore::try_lock_shared_for(std::chrono::duration<Rep, Period> duration) {
266 return try_lock_shared_until(Deadline::FromDuration(duration));
267}
268
269template <typename Clock, typename Duration>
270bool CancellableSemaphore::try_lock_shared_until(std::chrono::time_point<Clock, Duration> until) {
271 return try_lock_shared_until(Deadline::FromTimePoint(until));
272}
273
274template <typename Rep, typename Period>
275SemaphoreLock::SemaphoreLock(Semaphore& sem, std::chrono::duration<Rep, Period> duration)
276 : sem_(&sem)
277{
278 TryLockFor(duration);
279}
280
281template <typename Clock, typename Duration>
282SemaphoreLock::SemaphoreLock(Semaphore& sem, std::chrono::time_point<Clock, Duration> until)
283 : sem_(&sem)
284{
285 TryLockUntil(until);
286}
287
288template <typename Rep, typename Period>
289bool SemaphoreLock::TryLockFor(std::chrono::duration<Rep, Period> duration) {
290 return TryLockUntil(Deadline::FromDuration(duration));
291}
292
293template <typename Clock, typename Duration>
294bool SemaphoreLock::TryLockUntil(std::chrono::time_point<Clock, Duration> until) {
295 return TryLockUntil(Deadline::FromTimePoint(until));
296}
297
298} // namespace engine
299
300USERVER_NAMESPACE_END