2 * Copyright 2014-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <type_traits>
28 * Schedules several tasks and blocks until n of these tasks are completed.
29 * If any of these n tasks throws an exception, this exception will be
30 * re-thrown, but only when n tasks are complete. If several tasks throw
31 * exceptions one of them will be re-thrown.
33 * @param first Range of tasks to be scheduled
35 * @param n Number of tasks to wait for
37 * @return vector of pairs (task index, return value of task)
39 template <class InputIterator>
41 typename std::enable_if<
43 typename std::result_of<typename std::iterator_traits<
44 InputIterator>::value_type()>::type,
48 typename std::result_of<typename std::iterator_traits<
49 InputIterator>::value_type()>::type>>::
50 type> inline collectN(InputIterator first, InputIterator last, size_t n);
53 * collectN specialization for functions returning void
55 * @param first Range of tasks to be scheduled
57 * @param n Number of tasks to wait for
59 * @return vector of completed task indices
61 template <class InputIterator>
62 typename std::enable_if<
64 typename std::result_of<
65 typename std::iterator_traits<InputIterator>::value_type()>::type,
67 std::vector<size_t>>::
68 type inline collectN(InputIterator first, InputIterator last, size_t n);
71 * Schedules several tasks and blocks until all of these tasks are completed.
72 * If any of the tasks throws an exception, this exception will be re-thrown,
73 * but only when all the tasks are complete. If several tasks throw exceptions
74 * one of them will be re-thrown.
76 * @param first Range of tasks to be scheduled
79 * @return vector of values returned by tasks
81 template <class InputIterator>
82 typename std::vector<typename std::enable_if<
84 typename std::result_of<
85 typename std::iterator_traits<InputIterator>::value_type()>::type,
87 typename std::result_of<
88 typename std::iterator_traits<InputIterator>::value_type()>::
89 type>::type> inline collectAll(InputIterator first, InputIterator last);
92 * collectAll specialization for functions returning void
94 * @param first Range of tasks to be scheduled
97 template <class InputIterator>
98 typename std::enable_if<
100 typename std::result_of<
101 typename std::iterator_traits<InputIterator>::value_type()>::type,
103 void>::type inline collectAll(InputIterator first, InputIterator last);
106 * Schedules several tasks and blocks until one of them is completed.
107 * If this task throws an exception, this exception will be re-thrown.
108 * Exceptions thrown by all other tasks will be ignored.
110 * @param first Range of tasks to be scheduled
113 * @return pair of index of the first completed task and its return value
115 template <class InputIterator>
116 typename std::enable_if<
118 typename std::result_of<
119 typename std::iterator_traits<InputIterator>::value_type()>::type,
123 typename std::result_of<typename std::iterator_traits<
124 InputIterator>::value_type()>::type>>::
125 type inline collectAny(InputIterator first, InputIterator last);
128 * WhenAny specialization for functions returning void.
130 * @param first Range of tasks to be scheduled
133 * @return index of the first completed task
135 template <class InputIterator>
136 typename std::enable_if<
138 typename std::result_of<
139 typename std::iterator_traits<InputIterator>::value_type()>::type,
141 size_t>::type inline collectAny(InputIterator first, InputIterator last);
142 } // namespace fibers
145 #include <folly/fibers/WhenN-inl.h>