2 * Copyright 2016 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.
24 * Schedules several tasks and blocks until n of these tasks are completed.
25 * If any of these n tasks throws an exception, this exception will be
26 * re-thrown, but only when n tasks are complete. If several tasks throw
27 * exceptions one of them will be re-thrown.
29 * @param first Range of tasks to be scheduled
31 * @param n Number of tasks to wait for
33 * @return vector of pairs (task index, return value of task)
35 template <class InputIterator>
37 typename std::enable_if<
39 typename std::result_of<typename std::iterator_traits<
40 InputIterator>::value_type()>::type,
44 typename std::result_of<typename std::iterator_traits<
45 InputIterator>::value_type()>::type>>::
46 type> inline collectN(InputIterator first, InputIterator last, size_t n);
49 * collectN specialization for functions returning void
51 * @param first Range of tasks to be scheduled
53 * @param n Number of tasks to wait for
55 * @return vector of completed task indices
57 template <class InputIterator>
58 typename std::enable_if<
60 typename std::result_of<
61 typename std::iterator_traits<InputIterator>::value_type()>::type,
63 std::vector<size_t>>::
64 type inline collectN(InputIterator first, InputIterator last, size_t n);
67 * Schedules several tasks and blocks until all of these tasks are completed.
68 * If any of the tasks throws an exception, this exception will be re-thrown,
69 * but only when all the tasks are complete. If several tasks throw exceptions
70 * one of them will be re-thrown.
72 * @param first Range of tasks to be scheduled
75 * @return vector of values returned by tasks
77 template <class InputIterator>
78 typename std::vector<typename std::enable_if<
80 typename std::result_of<
81 typename std::iterator_traits<InputIterator>::value_type()>::type,
83 typename std::result_of<
84 typename std::iterator_traits<InputIterator>::value_type()>::
85 type>::type> inline collectAll(InputIterator first, InputIterator last);
88 * collectAll specialization for functions returning void
90 * @param first Range of tasks to be scheduled
93 template <class InputIterator>
94 typename std::enable_if<
96 typename std::result_of<
97 typename std::iterator_traits<InputIterator>::value_type()>::type,
99 void>::type inline collectAll(InputIterator first, InputIterator last);
102 * Schedules several tasks and blocks until one of them is completed.
103 * If this task throws an exception, this exception will be re-thrown.
104 * Exceptions thrown by all other tasks will be ignored.
106 * @param first Range of tasks to be scheduled
109 * @return pair of index of the first completed task and its return value
111 template <class InputIterator>
112 typename std::enable_if<
114 typename std::result_of<
115 typename std::iterator_traits<InputIterator>::value_type()>::type,
119 typename std::result_of<typename std::iterator_traits<
120 InputIterator>::value_type()>::type>>::
121 type inline collectAny(InputIterator first, InputIterator last);
124 * WhenAny specialization for functions returning void.
126 * @param first Range of tasks to be scheduled
129 * @return index of the first completed task
131 template <class InputIterator>
132 typename std::enable_if<
134 typename std::result_of<
135 typename std::iterator_traits<InputIterator>::value_type()>::type,
137 size_t>::type inline collectAny(InputIterator first, InputIterator last);
141 #include <folly/fibers/WhenN-inl.h>