2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
20 * author: Niek J Bouman
21 * reviewer: Avi Kivity
29 #include <type_traits>
31 #include <seastar/core/future.hh>
32 #include <seastar/core/shared_ptr.hh>
36 template <class Sequence>
37 struct when_any_result {
45 promise<std::size_t> _promise;
48 void done(std::size_t index) {
51 _promise.set_value(index);
54 auto get_future() { return _promise.get_future(); }
57 } // namespace internal
59 /// Wait for the first of multiple futures to complete (iterator version).
61 /// Given a range of futures as input, wait for the first of them
62 /// to resolve (either successfully or with an exception), and return
63 /// all of them in a \c when_any_result (following the concurrency TS from
64 /// the standard library), containing a std::vector to all futures
65 /// and the index (into the vector) of the future that resolved.
67 /// \param begin an \c InputIterator designating the beginning of the range of futures
68 /// \param end an \c InputIterator designating the end of the range of futures
69 /// \return a \c when_any_result of all the futures in the input; when
70 /// ready, at least one of the contained futures (the one indicated by index) will be ready.
71 template <class FutureIterator>
72 SEASTAR_CONCEPT( requires requires (FutureIterator i) { { *i++ }; requires is_future<std::remove_reference_t<decltype(*i)>>::value; } )
73 auto when_any(FutureIterator begin, FutureIterator end) noexcept
74 -> future<when_any_result<std::vector<std::decay_t<typename std::iterator_traits<FutureIterator>::value_type>>>>
76 using ReturnType = when_any_result<std::vector<typename std::iterator_traits<FutureIterator>::value_type>>;
78 return make_ready_future<ReturnType>();
81 result.futures.reserve(std::distance(begin, end));
82 auto waiter_obj = make_lw_shared<internal::waiter>();
84 for (auto it = begin; it != end; ++it) {
85 if (it->available()) {
86 result.futures.push_back(std::move(*it));
87 waiter_obj->done(index);
89 result.futures.push_back(it->finally([waiter_obj, index] {
90 waiter_obj->done(index);
95 return waiter_obj->get_future().then(
96 [result = std::move(result)](std::size_t index) mutable {
98 return std::move(result);
102 } // namespace seastar