6#include <userver/engine/condition_variable.hpp>
7#include <userver/engine/mutex.hpp>
8#include <userver/engine/semaphore.hpp>
10USERVER_NAMESPACE_BEGIN
29class SharedMutex
final {
32 ~SharedMutex() =
default;
34 SharedMutex(
const SharedMutex&) =
delete;
35 SharedMutex(SharedMutex&&) =
delete;
36 SharedMutex& operator=(
const SharedMutex&) =
delete;
37 SharedMutex& operator=(SharedMutex&&) =
delete;
62 template <
typename Rep,
typename Period>
63 [[nodiscard]]
bool try_lock_for(
const std::chrono::duration<Rep, Period>&);
70 template <
typename Clock,
typename Duration>
71 [[nodiscard]]
bool try_lock_until(
const std::chrono::time_point<Clock, Duration>&);
99 template <
typename Rep,
typename Period>
107 template <
typename Clock,
typename Duration>
114 bool HasWaitingWriter()
const noexcept;
116 bool WaitForNoWaitingWriters(Deadline deadline);
118 void DecWaitingWriters();
121
122
123
124
125
126
127
128 Semaphore semaphore_;
131
132
133 std::atomic_size_t waiting_writers_count_;
134 Mutex waiting_writers_count_mutex_;
135 ConditionVariable waiting_writers_count_cv_;
138template <
typename Rep,
typename Period>
139bool SharedMutex::
try_lock_for(
const std::chrono::duration<Rep, Period>& duration) {
140 return try_lock_until(Deadline::FromDuration(duration));
143template <
typename Rep,
typename Period>
145 return try_lock_shared_until(Deadline::FromDuration(duration));
148template <
typename Clock,
typename Duration>
149bool SharedMutex::
try_lock_until(
const std::chrono::time_point<Clock, Duration>& until) {
150 return try_lock_until(Deadline::FromTimePoint(until));
153template <
typename Clock,
typename Duration>
155 return try_lock_shared_until(Deadline::FromTimePoint(until));