6#include <userver/engine/condition_variable.hpp>
7#include <userver/engine/mutex.hpp>
8#include <userver/engine/semaphore.hpp>
10USERVER_NAMESPACE_BEGIN
23class SharedMutex
final {
26 ~SharedMutex() =
default;
28 SharedMutex(
const SharedMutex&) =
delete;
29 SharedMutex(SharedMutex&&) =
delete;
30 SharedMutex& operator=(
const SharedMutex&) =
delete;
31 SharedMutex& operator=(SharedMutex&&) =
delete;
38 template <
typename Rep,
typename Period>
39 bool try_lock_for(
const std::chrono::duration<Rep, Period>&);
41 template <
typename Clock,
typename Duration>
42 bool try_lock_until(
const std::chrono::time_point<Clock, Duration>&);
44 bool try_lock_until(Deadline deadline);
48 bool try_lock_shared();
50 template <
typename Rep,
typename Period>
51 bool try_lock_shared_for(
const std::chrono::duration<Rep, Period>&);
53 template <
typename Clock,
typename Duration>
54 bool try_lock_shared_until(
const std::chrono::time_point<Clock, Duration>&);
56 bool try_lock_shared_until(Deadline deadline);
59 bool HasWaitingWriter()
const noexcept;
61 bool WaitForNoWaitingWriters(Deadline deadline);
63 void DecWaitingWriters();
66
67
68
69
70
71
72
76
77
78 std::atomic_size_t waiting_writers_count_;
79 Mutex waiting_writers_count_mutex_;
80 ConditionVariable waiting_writers_count_cv_;
83template <
typename Rep,
typename Period>
84bool SharedMutex::try_lock_for(
85 const std::chrono::duration<Rep, Period>& duration) {
86 return try_lock_until(Deadline::FromDuration(duration));
89template <
typename Rep,
typename Period>
90bool SharedMutex::try_lock_shared_for(
91 const std::chrono::duration<Rep, Period>& duration) {
92 return try_lock_shared_until(Deadline::FromDuration(duration));
95template <
typename Clock,
typename Duration>
96bool SharedMutex::try_lock_until(
97 const std::chrono::time_point<Clock, Duration>& until) {
98 return try_lock_until(Deadline::FromTimePoint(until));
101template <
typename Clock,
typename Duration>
102bool SharedMutex::try_lock_shared_until(
103 const std::chrono::time_point<Clock, Duration>& until) {
104 return try_lock_shared_until(Deadline::FromTimePoint(until));