-
Notifications
You must be signed in to change notification settings - Fork 186
/
when_all_range.hpp
333 lines (288 loc) · 10.2 KB
/
when_all_range.hpp
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
/*
* Copyright (c) Meta Platforms, Inc. and its affiliates.
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <algorithm>
#include <atomic>
#include <cstddef>
#include <functional>
#include <optional>
#include <type_traits>
#include <variant>
#include <vector>
#include <unifex/blocking.hpp>
#include <unifex/continuations.hpp>
#include <unifex/get_stop_token.hpp>
#include <unifex/inplace_stop_token.hpp>
#include <unifex/manual_lifetime.hpp>
#include <unifex/receiver_concepts.hpp>
#include <unifex/sender_concepts.hpp>
#include <unifex/std_concepts.hpp>
#include <unifex/type_list.hpp>
#include <unifex/detail/prologue.hpp>
namespace unifex {
namespace _when_all_range {
template <typename Receiver, typename Sender>
struct _operation final {
struct type;
};
template <typename Receiver, typename Sender>
struct _element_receiver final {
struct type;
};
template <typename Receiver, typename Sender>
using element_receiver = typename _element_receiver<Receiver, Sender>::type;
template <typename Receiver, typename Sender>
using operation =
typename _operation<unifex::remove_cvref_t<Receiver>, Sender>::type;
template <typename Receiver, typename Sender>
struct _operation<Receiver, Sender>::type final {
private:
using sender_nonvoid_value_type =
unifex::sender_single_value_result_t<unifex::remove_cvref_t<Sender>>;
using element_receiver_t = element_receiver<Receiver, Sender>;
friend struct _element_receiver<Receiver, Sender>::type;
struct cancel_operation final {
type& op_;
void operator()() noexcept { op_.request_stop(); }
};
struct _operation_holder final {
std::optional<sender_nonvoid_value_type> value;
unifex::connect_result_t<Sender, element_receiver_t> connection;
_operation_holder(Sender&& sender, type& op, std::size_t index) noexcept(
unifex::is_nothrow_connectable_v<Sender, element_receiver_t>&& std::
is_nothrow_constructible_v<element_receiver_t, type&, std::size_t>)
: connection(unifex::connect(
static_cast<Sender&&>(sender), element_receiver_t(op, index))) {}
};
public:
type(Receiver&& receiver, std::vector<Sender>&& senders)
: receiver_(std::move(receiver))
, refCount_(senders.size()) {
std::allocator<_operation_holder> allocator;
holders_ = allocator.allocate(senders.size());
try {
for (auto&& sender : senders) {
new (holders_ + numHolders_)
_operation_holder{std::move(sender), *this, numHolders_};
++numHolders_;
}
} catch (...) {
std::destroy(
std::make_reverse_iterator(holders_ + numHolders_),
std::make_reverse_iterator(holders_));
allocator.deallocate(holders_, senders.size());
holders_ = nullptr;
throw;
}
}
type(type&&) = delete;
// does not run when constructor throws, numHolders_ is the correct size
~type() {
std::allocator<_operation_holder> allocator;
std::destroy(
std::make_reverse_iterator(holders_ + numHolders_),
std::make_reverse_iterator(holders_));
allocator.deallocate(holders_, numHolders_);
}
void start() noexcept {
if (numHolders_ == 0) {
// In case there is 0 sender, immediately complete
unifex::set_value(
std::move(receiver_), std::vector<sender_nonvoid_value_type>{});
} else {
stopCallback_.construct(
unifex::get_stop_token(receiver_), cancel_operation{*this});
// last start() might destroy this
std::for_each(
holders_, holders_ + numHolders_, [](auto& holder) noexcept {
unifex::start(holder.connection);
});
}
}
void request_stop() noexcept {
if (refCount_.fetch_add(1, std::memory_order_relaxed) == 0) {
// deliver_result already called
return;
}
stopSource_.request_stop();
element_complete();
}
void element_complete() noexcept {
if (refCount_.fetch_sub(1, std::memory_order_acq_rel) == 1) {
stopCallback_.destruct();
if (doneOrError_.load(std::memory_order_relaxed)) {
if (error_.has_value()) {
std::visit(
[this](auto&& error) {
unifex::set_error(
std::move(receiver_), std::forward<decltype(error)>(error));
},
std::move(error_.value()));
} else {
unifex::set_done(std::move(receiver_));
}
} else {
UNIFEX_TRY {
std::vector<sender_nonvoid_value_type> values;
values.reserve(numHolders_);
std::transform(
holders_,
holders_ + numHolders_,
std::back_inserter(values),
[](auto&& h) -> decltype(auto) {
return std::move(h.value.value());
});
unifex::set_value(std::move(receiver_), std::move(values));
}
UNIFEX_CATCH(...) {
unifex::set_error(std::move(receiver_), std::current_exception());
}
}
}
}
_operation_holder* holders_;
std::size_t numHolders_{0};
UNIFEX_NO_UNIQUE_ADDRESS
std::optional<unifex::sender_error_types_t<Sender, std::variant>>
error_;
UNIFEX_NO_UNIQUE_ADDRESS
unifex::manual_lifetime<typename unifex::stop_token_type_t<
Receiver&>::template callback_type<cancel_operation>>
stopCallback_;
UNIFEX_NO_UNIQUE_ADDRESS Receiver receiver_;
std::atomic<std::size_t> refCount_;
std::atomic<bool> doneOrError_{false};
unifex::inplace_stop_source stopSource_;
};
template <typename Receiver, typename Sender>
struct _element_receiver<Receiver, Sender>::type final {
operation<Receiver, Sender>& op_;
size_t index_;
type(operation<Receiver, Sender>& op, size_t index) noexcept
: op_(op)
, index_(index){};
template <typename... Value>
void set_value(Value&&... value) noexcept {
UNIFEX_TRY {
op_.holders_[index_].value.emplace(std::forward<Value>(value)...);
op_.element_complete();
}
UNIFEX_CATCH(...) { this->set_error(std::current_exception()); }
}
template <typename Error>
void set_error(Error&& error) noexcept {
if (!op_.doneOrError_.exchange(true, std::memory_order_relaxed)) {
op_.error_.emplace(
std::in_place_type_t<std::decay_t<Error>>{}, (Error &&) error);
op_.stopSource_.request_stop();
}
op_.element_complete();
}
void set_done() noexcept {
if (!op_.doneOrError_.exchange(true, std::memory_order_relaxed)) {
op_.stopSource_.request_stop();
}
op_.element_complete();
}
[[nodiscard]] Receiver& get_receiver() const noexcept {
return op_.receiver_;
}
template(typename CPO, typename R) //
(requires unifex::is_receiver_query_cpo_v<CPO> AND //
unifex::same_as<R, type> AND //
unifex::is_callable_v<CPO, const Receiver&>) //
friend auto tag_invoke(CPO cpo, const R& r) //
noexcept(unifex::is_nothrow_callable_v<CPO, const Receiver&>)
-> unifex::callable_result_t<CPO, const Receiver&> {
return std::move(cpo)(std::as_const(r.get_receiver()));
}
[[nodiscard]] unifex::inplace_stop_source& get_stop_source() const noexcept {
return op_.stopSource_;
}
friend unifex::inplace_stop_token tag_invoke(
unifex::tag_t<unifex::get_stop_token> /* unused */,
const type& r) noexcept {
return r.get_stop_source().get_token();
}
template <typename Func>
friend void tag_invoke(
unifex::tag_t<unifex::visit_continuations> /* unused */,
const type& r,
Func&& func) noexcept {
std::invoke(func, r.get_receiver());
}
};
// Sender adapter for vector<Sender>
template <typename Sender>
struct _sender final {
struct type;
};
template <typename Sender>
using sender = typename _sender<Sender>::type;
template <typename Sender>
struct _sender<Sender>::type final {
public:
using sender_value_type =
unifex::sender_single_value_result_t<unifex::remove_cvref_t<Sender>>;
explicit type(std::vector<Sender>&& senders) noexcept(
std::is_nothrow_move_constructible_v<std::vector<Sender>>)
: senders_(std::move(senders)) {}
template <
template <typename...>
class Variant,
template <typename...>
class Tuple>
using value_types = Variant<Tuple<std::vector<sender_value_type>>>;
template <template <typename...> class Variant>
using error_types = Variant<std::exception_ptr>;
static constexpr bool sends_done = true;
private:
template(typename Sender2, typename Receiver) //
(requires unifex::same_as<unifex::remove_cvref_t<Sender2>, type> AND
unifex::receiver<Receiver>) //
friend auto tag_invoke(
unifex::tag_t<unifex::connect> /* unused */,
Sender2&& sender,
Receiver&& receiver) {
// return an operation that wraps all connections
return operation<unifex::remove_cvref_t<Receiver>, Sender>{
static_cast<Receiver&&>(receiver),
std::move((static_cast<Sender2&&>(sender)).senders_)};
}
std::vector<Sender> senders_;
};
namespace _cpo {
struct _fn final {
template(typename Sender) //
(requires(unifex::sender<Sender>)) //
auto
operator()(std::vector<Sender> senders) const {
return _when_all_range::sender<Sender>(std::move(senders));
}
template <typename Iterator>
auto operator()(Iterator first, Iterator last) const -> decltype(operator()(
std::vector<typename std::iterator_traits<Iterator>::value_type>{
first, last})) {
return operator()(
std::vector<typename std::iterator_traits<Iterator>::value_type>{
first, last});
}
};
} // namespace _cpo
} // namespace _when_all_range
inline constexpr _when_all_range::_cpo::_fn when_all_range{};
} // namespace unifex
#include <unifex/detail/epilogue.hpp>