userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
shared_mutex.hpp
Go to the documentation of this file.
1
#pragma once
2
5
6
#include <
userver/engine/condition_variable.hpp
>
7
#include <userver/engine/impl/actor.hpp>
8
#include <
userver/engine/mutex.hpp
>
9
#include <
userver/engine/semaphore.hpp
>
10
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
engine
{
14
30
class
SharedMutex final {
31
public
:
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
77
void
lock_shared
();
78
84
void
unlock_shared
();
85
89
void
unlock_and_lock_shared
();
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
112
private
:
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
157
template
<
typename
Rep,
typename
Period>
158
bool
SharedMutex::try_lock_for
(
const
std::chrono::duration<Rep, Period>& duration) {
159
return
try_lock_until
(
Deadline::FromDuration
(duration));
160
}
161
162
template
<
typename
Rep,
typename
Period>
163
bool
SharedMutex::try_lock_shared_for
(
const
std::chrono::duration<Rep, Period>& duration) {
164
return
try_lock_shared_until
(
Deadline::FromDuration
(duration));
165
}
166
167
template
<
typename
Clock,
typename
Duration>
168
bool
SharedMutex::try_lock_until
(
const
std::chrono::time_point<Clock, Duration>& until) {
169
return
try_lock_until
(
Deadline::FromTimePoint
(until));
170
}
171
172
template
<
typename
Clock,
typename
Duration>
173
bool
SharedMutex::try_lock_shared_until
(
const
std::chrono::time_point<Clock, Duration>& until) {
174
return
try_lock_shared_until
(
Deadline::FromTimePoint
(until));
175
}
176
177
}
// namespace engine
178
179
USERVER_NAMESPACE_END
userver
engine
shared_mutex.hpp
Generated on
for userver by
Doxygen
1.17.0