/
channel.h
344 lines (296 loc) · 8.92 KB
/
channel.h
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2023-2023 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef BAREOS_LIB_CHANNEL_H_
#define BAREOS_LIB_CHANNEL_H_
#include <condition_variable>
#include <optional>
#include <deque>
#include <utility>
#include <variant>
#include "thread_util.h"
namespace channel {
// a simple single consumer/ single producer queue
// Its composed of three parts: the input, the output and the
// actual queue data.
// Instead of directly interacting with the queue itself you instead
// interact with either the input or the output.
// This ensures that there is only one producer (who writes to the input)
// and one consumer (who reads from the output).
struct failed_to_acquire_lock {};
struct channel_closed {};
template <typename T> class queue {
struct internal {
std::deque<T> data;
bool in_dead;
bool out_dead;
};
synchronized<internal> shared{};
std::condition_variable in_update{};
std::condition_variable out_update{};
std::size_t max_size;
public:
explicit queue(std::size_t max_size) : max_size(max_size) {}
queue(const queue&) = delete;
queue& operator=(const queue&) = delete;
queue(queue&&) = delete;
queue& operator=(queue&&) = delete;
using locked_type = decltype(shared.lock());
class handle {
locked_type locked;
std::condition_variable* update;
public:
handle(locked_type locked, std::condition_variable* update)
: locked{std::move(locked)}, update(update)
{
}
std::deque<T>& data() { return locked->data; }
~handle()
{
if (update) { update->notify_one(); }
}
};
/* the *_lock functions return std::nullopt only if the channel is closed
* otherwise they wait until they get the lock
* The try_*_lock functions instead return a tristate indicating whether
* they succeeded, failed to acquire the lock or if the channel was closed
* from the other side. */
std::optional<handle> output_lock()
{
auto locked = shared.lock();
if (locked->out_dead) {
// note(ssura): This happening is programmer error.
// Maybe we should assert this instead ?
Dmsg0(50,
"Tried to read from channel that was closed from the read side.\n");
return std::nullopt;
}
locked.wait(in_update, [](const auto& intern) {
return intern.data.size() > 0 || intern.in_dead;
});
if (locked->data.size() == 0) {
return std::nullopt;
} else {
return std::make_optional<handle>(std::move(locked), &out_update);
}
}
std::optional<handle> input_lock()
{
auto locked = shared.lock();
locked.wait(out_update, [max_size = max_size](const auto& intern) {
return intern.data.size() < max_size || intern.out_dead;
});
if (locked->in_dead) {
// note(ssura): This happening is programmer error.
// Maybe we should assert this instead ?
Dmsg0(50,
"Tried to write to channel that was closed from the write side.\n");
return std::nullopt;
}
if (locked->out_dead) {
return std::nullopt;
} else {
return std::make_optional<handle>(std::move(locked), &in_update);
}
}
using try_result
= std::variant<handle, failed_to_acquire_lock, channel_closed>;
try_result try_output_lock()
{
auto locked = shared.try_lock();
if (!locked) { return failed_to_acquire_lock{}; }
if (locked.value()->out_dead) {
// note(ssura): This happening is programmer error.
// Maybe we should assert this instead ?
Dmsg0(50,
"Tried to read from channel that was closed from the read side.\n");
return channel_closed{};
}
if (locked.value()->data.size() == 0) {
if (locked.value()->in_dead) {
return channel_closed{};
} else {
return failed_to_acquire_lock{};
}
}
return try_result(std::in_place_type<handle>, std::move(locked).value(),
&out_update);
}
try_result try_input_lock()
{
auto locked = shared.try_lock();
if (!locked) { return failed_to_acquire_lock{}; }
if (locked.value()->in_dead) {
// note(ssura): This happening is programmer error.
// Maybe we should assert this instead ?
Dmsg0(50,
"Tried to write to channel that was closed from the write side.\n");
return channel_closed{};
}
if (locked.value()->out_dead) { return channel_closed{}; }
if (locked.value()->data.size() >= max_size) {
return failed_to_acquire_lock{};
}
return try_result(std::in_place_type<handle>, std::move(locked).value(),
&in_update);
}
void close_in()
{
shared.lock()->in_dead = true;
in_update.notify_one();
}
void close_out()
{
shared.lock()->out_dead = true;
out_update.notify_one();
}
};
template <typename T> class input {
std::shared_ptr<queue<T>> shared;
bool did_close{false};
public:
explicit input(std::shared_ptr<queue<T>> shared) : shared{std::move(shared)}
{
}
// after move only operator= and ~in are allowed to be called
input(input&&) = default;
input& operator=(input&&) = default;
input(const input&) = delete;
input& operator=(const input&) = delete;
template <typename... Args> bool emplace(Args... args)
{
if (did_close) { return false; }
if (auto handle = shared->input_lock()) {
handle->data().emplace_back(std::forward<Args>(args)...);
return true;
} else {
close();
return false;
}
}
template <typename... Args> bool try_emplace(Args... args)
{
if (did_close) { return false; }
auto result = shared->try_input_lock();
if (std::holds_alternative<failed_to_acquire_lock>(result)) {
return false;
} else if (std::holds_alternative<channel_closed>(result)) {
close();
return false;
} else {
std::get<typename queue<T>::handle>(result).data().emplace_back(
std::forward<Args>(args)...);
return true;
}
}
void close()
{
if (!did_close) {
shared->close_in();
did_close = true;
}
}
bool closed() const { return did_close; }
~input()
{
if (shared) { close(); }
}
};
template <typename T> class output {
std::shared_ptr<queue<T>> shared;
std::deque<T> cache;
bool did_close{false};
public:
explicit output(std::shared_ptr<queue<T>> shared) : shared{std::move(shared)}
{
}
// after move only operator= and ~out are allowed to be called
output(output&&) = default;
output& operator=(output&&) = default;
output(const output&) = delete;
output& operator=(const output&) = delete;
std::optional<T> get()
{
if (did_close) { return std::nullopt; }
update_cache();
if (cache.size() > 0) {
std::optional result = std::make_optional<T>(std::move(cache.front()));
cache.pop_front();
return result;
} else {
return std::nullopt;
}
}
std::optional<T> try_get()
{
if (did_close) { return std::nullopt; }
try_update_cache();
if (cache.size() > 0) {
std::optional result = std::make_optional<T>(std::move(cache.front()));
cache.pop_front();
return result;
} else {
return std::nullopt;
}
}
void close()
{
if (!did_close) {
shared->close_out();
did_close = true;
}
}
bool closed() const { return did_close; }
~output()
{
if (shared) { close(); }
}
private:
void update_cache()
{
if (cache.empty()) {
if (auto handle = shared->output_lock()) {
std::swap(handle->data(), cache);
} else {
// this can only happen if the channel was closed.
close();
}
}
}
void try_update_cache()
{
if (cache.empty()) {
auto result = shared->try_output_lock();
if (std::holds_alternative<failed_to_acquire_lock>(result)) {
// intentionally left empty
} else if (std::holds_alternative<channel_closed>(result)) {
close();
} else {
std::swap(std::get<typename queue<T>::handle>(result).data(), cache);
}
}
}
};
template <typename T>
std::pair<input<T>, output<T>> CreateBufferedChannel(std::size_t capacity)
{
auto shared = std::make_shared<queue<T>>(capacity);
auto in = input(shared);
auto out = output(shared);
return {std::move(in), std::move(out)};
}
} // namespace channel
#endif // BAREOS_LIB_CHANNEL_H_