userver: userver/engine/shared_mutex.hpp Source File
Loading...
Searching...
No Matches
shared_mutex.hpp
Go to the documentation of this file.
1#pragma once
2
5
7#include <userver/engine/impl/actor.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace engine {
14
30class SharedMutex final {
31public:
32 SharedMutex();
33 ~SharedMutex();
34
35 SharedMutex(const SharedMutex&) = delete;
36 SharedMutex(SharedMutex&&) = delete;
37 SharedMutex& operator=(const SharedMutex&) = delete;
38 SharedMutex& operator=(SharedMutex&&) = delete;
39
44 void lock();
45
51 void unlock();
52
54 [[nodiscard]] bool try_lock();
55
60 template <typename Rep, typename Period>
61 [[nodiscard]] bool try_lock_for(const std::chrono::duration<Rep, Period>&);
62
67 template <typename Clock, typename Duration>
68 [[nodiscard]] bool try_lock_until(const std::chrono::time_point<Clock, Duration>&);
69
71 [[nodiscard]] bool try_lock_until(Deadline deadline);
72
78
85
90
93 [[nodiscard]] bool try_lock_shared();
94
99 template <typename Rep, typename Period>
100 [[nodiscard]] bool try_lock_shared_for(const std::chrono::duration<Rep, Period>&);
101
106 template <typename Clock, typename Duration>
107 [[nodiscard]] bool try_lock_shared_until(const std::chrono::time_point<Clock, Duration>&);
108
110 [[nodiscard]] bool try_lock_shared_until(Deadline deadline);
111
112private:
113 bool HasRegisteredWriter() const noexcept;
114
115 bool WaitForNoRegisteredWriters(Deadline deadline);
116
117 enum class DeadlockDetectorTrackingStatus : std::uint8_t { kUntracked, kTracked };
118
119 void UnregisterWriter(DeadlockDetectorTrackingStatus tracking_status);
120
121 /* Semaphore can be get by 1 or by SIZE_MAX.
122 * 1 = reader, SIZE_MAX = writer.
123 *
124 * Three possible cases:
125 * 1) semaphore is free
126 * 2) there are readers in critical section (any count)
127 * 3) there is a single writer in critical section
128 */
129 Semaphore semaphore_;
130
131 /* Readers don't try to hold semaphore_ if there is at least one
132 * registered writer => writers don't starve.
133 */
134 std::atomic<ssize_t> registered_writers_count_;
135 Mutex registered_writers_count_mutex_;
136 ConditionVariable registered_writers_count_cv_;
137
138 struct RegisteredWritersActor final : public impl::deadlock_detector::Actor {
139 utils::StringLiteral GetActorType() const override;
140 };
141
142 struct MutexActor final : public impl::deadlock_detector::Actor {
143 utils::StringLiteral GetActorType() const override;
144 };
145
146 // An identity actor for registered (waiting and active) writers.
147 // Used to track dependencies between shared readers and registered writers in deadlock detector.
148 // While there are registered writers owning this actor all shared readers will wait for it.
149 RegisteredWritersActor registered_writers_actor_;
150 // An identity actor for the mutex itself.
151 // Used to track ownership of the mutex and waits on the mutex in deadlock detector.
152 // While there is an active writer or a set of active shared readers owning this actor
153 // other writers will waiting for it.
154 MutexActor mutex_actor_;
155};
156
157template <typename Rep, typename Period>
158bool SharedMutex::try_lock_for(const std::chrono::duration<Rep, Period>& duration) {
159 return try_lock_until(Deadline::FromDuration(duration));
160}
161
162template <typename Rep, typename Period>
163bool SharedMutex::try_lock_shared_for(const std::chrono::duration<Rep, Period>& duration) {
165}
166
167template <typename Clock, typename Duration>
168bool SharedMutex::try_lock_until(const std::chrono::time_point<Clock, Duration>& until) {
170}
171
172template <typename Clock, typename Duration>
173bool SharedMutex::try_lock_shared_until(const std::chrono::time_point<Clock, Duration>& until) {
175}
176
177} // namespace engine
178
179USERVER_NAMESPACE_END