-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathsemaphore.cpp
295 lines (228 loc) · 9.29 KB
/
semaphore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <userver/engine/semaphore.hpp>
#include <fmt/format.h>
#include <userver/engine/task/cancel.hpp>
#include <userver/utils/assert.hpp>
#include <engine/impl/wait_list.hpp>
#include <engine/task/task_context.hpp>
USERVER_NAMESPACE_BEGIN
namespace engine {
class CancellableSemaphore::SemaphoreWaitStrategy final : public impl::WaitStrategy {
public:
SemaphoreWaitStrategy(
impl::TaskContext& current,
CancellableSemaphore& sem,
CancellableSemaphore::Counter count
) noexcept
: sem_(sem), current_(current), waiter_token_(*sem_.lock_waiters_), count_(count) {}
impl::EarlyWakeup SetupWakeups() override {
impl::WaitList::Lock lock(*sem_.lock_waiters_);
status_ = sem_.DoTryLock(count_);
if (status_ != TryLockStatus::kTransientFailure) {
return impl::EarlyWakeup{status_ == TryLockStatus::kSuccess};
}
if (sem_.UsedApprox() <= sem_.GetCapacity() - count_) {
return impl::EarlyWakeup{true};
}
// A race is not possible here, because check + Append is performed under
// WaitList::Lock, and notification also takes WaitList::Lock.
sem_.lock_waiters_->Append(lock, ¤t_);
return impl::EarlyWakeup{false};
}
void DisableWakeups() noexcept override {
impl::WaitList::Lock lock(*sem_.lock_waiters_);
sem_.lock_waiters_->Remove(lock, current_);
}
TryLockStatus GetTryLockStatus() const noexcept { return status_; }
private:
CancellableSemaphore& sem_;
impl::TaskContext& current_;
const impl::WaitList::WaitersScopeCounter waiter_token_;
const CancellableSemaphore::Counter count_;
TryLockStatus status_{TryLockStatus::kTransientFailure};
};
CancellableSemaphore::CancellableSemaphore(Counter capacity) : acquired_locks_(0), capacity_(capacity) {}
CancellableSemaphore::~CancellableSemaphore() {
UASSERT_MSG(
acquired_locks_.load() == 0,
fmt::format(
"CancellableSemaphore is destroyed while in use "
"(acquired={}, capacity={})",
acquired_locks_.load(),
capacity_.load()
)
);
}
void CancellableSemaphore::SetCapacity(Counter capacity) {
capacity_.store(capacity);
if (lock_waiters_->GetCountOfSleepies()) {
impl::WaitList::Lock lock{*lock_waiters_};
lock_waiters_->WakeupAll(lock);
}
}
CancellableSemaphore::Counter CancellableSemaphore::GetCapacity() //
const noexcept {
return capacity_.load();
}
std::size_t CancellableSemaphore::RemainingApprox() const {
const auto acquired = acquired_locks_.load(std::memory_order_relaxed);
const auto capacity = capacity_.load(std::memory_order_relaxed);
return capacity >= acquired ? capacity - acquired : 0;
}
std::size_t CancellableSemaphore::UsedApprox() const { return acquired_locks_.load(std::memory_order_relaxed); }
void CancellableSemaphore::lock_shared() { lock_shared_count(1); }
void CancellableSemaphore::lock_shared_count(const Counter count) {
const bool success = try_lock_shared_until_count(Deadline{}, count);
if (!success) {
if (engine::current_task::ShouldCancel()) {
throw SemaphoreLockCancelledError("Semaphore lock is stopped by task cancellation");
} else {
throw UnreachableSemaphoreLockError(fmt::format(
"The amount of locks requested is greater than CancellableSemaphore "
"capacity: count={}, capacity={}",
count,
capacity_.load()
));
}
}
}
void CancellableSemaphore::unlock_shared() { unlock_shared_count(1); }
void CancellableSemaphore::unlock_shared_count(const Counter count) {
UASSERT(count > 0);
const auto old_acquired_locks = acquired_locks_.fetch_sub(count, std::memory_order_acq_rel);
UASSERT_MSG(
old_acquired_locks >= old_acquired_locks - count,
fmt::format(
"Trying to release more locks than have been "
"acquired: count={}, acquired={}",
count,
old_acquired_locks
)
);
if (lock_waiters_->GetCountOfSleepies()) {
impl::WaitList::Lock lock{*lock_waiters_};
if (count > 1) {
lock_waiters_->WakeupAll(lock);
} else {
lock_waiters_->WakeupOne(lock);
}
}
}
bool CancellableSemaphore::try_lock_shared() { return try_lock_shared_count(1); }
bool CancellableSemaphore::try_lock_shared_count(const Counter count) {
return LockFastPath(count) == TryLockStatus::kSuccess;
}
bool CancellableSemaphore::try_lock_shared_until(Deadline deadline) { return try_lock_shared_until_count(deadline, 1); }
bool CancellableSemaphore::try_lock_shared_until_count(Deadline deadline, const Counter count) {
const auto status = LockFastPath(count);
if (status == TryLockStatus::kSuccess) return true;
if (status == TryLockStatus::kPermanentFailure) return false;
return LockSlowPath(deadline, count);
}
bool CancellableSemaphore::LockSlowPath(Deadline deadline, const Counter count) {
UASSERT(count > 0);
auto& current = current_task::GetCurrentTaskContext();
SemaphoreWaitStrategy wait_strategy{current, *this, count};
while (true) {
const auto wakeup_source = current.Sleep(wait_strategy, deadline);
if (wait_strategy.GetTryLockStatus() != TryLockStatus::kTransientFailure) {
return wait_strategy.GetTryLockStatus() == TryLockStatus::kSuccess;
}
if (!impl::HasWaitSucceeded(wakeup_source)) {
return false;
}
}
}
CancellableSemaphore::TryLockStatus CancellableSemaphore::DoTryLock(const Counter count) {
auto capacity = capacity_.load(std::memory_order_acquire);
if (count > capacity) return TryLockStatus::kPermanentFailure;
auto expected = acquired_locks_.load(std::memory_order_acquire);
bool success = false;
while (expected <= capacity - count && !success) {
success = acquired_locks_.compare_exchange_weak(expected, expected + count, std::memory_order_relaxed);
}
return success ? TryLockStatus::kSuccess : TryLockStatus::kTransientFailure;
}
CancellableSemaphore::TryLockStatus CancellableSemaphore::LockFastPath(const Counter count) {
UASSERT(count > 0);
const auto status = DoTryLock(count);
return status;
}
Semaphore::Semaphore(Counter capacity) : sem_(capacity) {}
Semaphore::~Semaphore() = default;
void Semaphore::SetCapacity(Counter capacity) { sem_.SetCapacity(capacity); }
Semaphore::Counter Semaphore::GetCapacity() const noexcept { return sem_.GetCapacity(); }
std::size_t Semaphore::RemainingApprox() const { return sem_.RemainingApprox(); }
std::size_t Semaphore::UsedApprox() const { return sem_.UsedApprox(); }
void Semaphore::lock_shared() {
const engine::TaskCancellationBlocker blocker;
sem_.lock_shared();
}
void Semaphore::unlock_shared() { sem_.unlock_shared(); }
bool Semaphore::try_lock_shared() {
const engine::TaskCancellationBlocker blocker;
return sem_.try_lock_shared();
}
bool Semaphore::try_lock_shared_until(Deadline deadline) {
const engine::TaskCancellationBlocker blocker;
return sem_.try_lock_shared_until(deadline);
}
void Semaphore::lock_shared_count(Counter count) {
const engine::TaskCancellationBlocker blocker;
sem_.lock_shared_count(count);
}
void Semaphore::unlock_shared_count(Counter count) { sem_.unlock_shared_count(count); }
bool Semaphore::try_lock_shared_count(Counter count) {
const engine::TaskCancellationBlocker blocker;
return sem_.try_lock_shared_count(count);
}
bool Semaphore::try_lock_shared_until_count(Deadline deadline, Counter count) {
const engine::TaskCancellationBlocker blocker;
return sem_.try_lock_shared_until_count(deadline, count);
}
SemaphoreLock::SemaphoreLock(Semaphore& sem) : sem_(&sem), owns_lock_(true) { sem_->lock_shared(); }
SemaphoreLock::SemaphoreLock(Semaphore& sem, std::defer_lock_t) noexcept : sem_(&sem) {}
SemaphoreLock::SemaphoreLock(Semaphore& sem, std::try_to_lock_t) : sem_(&sem) { TryLock(); }
SemaphoreLock::SemaphoreLock(Semaphore& sem, std::adopt_lock_t) noexcept : sem_(&sem), owns_lock_(true) {}
SemaphoreLock::SemaphoreLock(Semaphore& sem, Deadline deadline) : sem_(&sem) { TryLockUntil(deadline); }
SemaphoreLock& SemaphoreLock::operator=(SemaphoreLock&& other) noexcept {
if (OwnsLock()) Unlock();
sem_ = other.sem_;
owns_lock_ = other.owns_lock_;
other.owns_lock_ = false;
return *this;
}
SemaphoreLock::SemaphoreLock(SemaphoreLock&& other) noexcept
: sem_(other.sem_), owns_lock_(std::exchange(other.owns_lock_, false)) {}
SemaphoreLock::~SemaphoreLock() {
if (OwnsLock()) Unlock();
}
bool SemaphoreLock::OwnsLock() const noexcept { return owns_lock_; }
void SemaphoreLock::Lock() {
UASSERT(sem_);
UASSERT(!owns_lock_);
sem_->lock_shared();
}
bool SemaphoreLock::TryLock() {
UASSERT(sem_);
UASSERT(!owns_lock_);
owns_lock_ = sem_->try_lock_shared();
return owns_lock_;
}
bool SemaphoreLock::TryLockUntil(Deadline deadline) {
UASSERT(sem_);
UASSERT(!owns_lock_);
owns_lock_ = sem_->try_lock_shared_until(deadline);
return owns_lock_;
}
void SemaphoreLock::Unlock() {
UASSERT(sem_);
UASSERT(owns_lock_);
sem_->unlock_shared();
owns_lock_ = false;
}
void SemaphoreLock::Release() {
sem_ = nullptr;
owns_lock_ = false;
}
} // namespace engine
USERVER_NAMESPACE_END