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.
22 * Schedules several tasks and blocks until n of these tasks are completed.
23 * If any of these n tasks throws an exception, this exception will be
24 * re-thrown, but only when n tasks are complete. If several tasks throw
25 * exceptions one of them will be re-thrown.
27 * @param first Range of tasks to be scheduled
29 * @param n Number of tasks to wait for
31 * @return vector of pairs (task index, return value of task)
33 template <class InputIterator>
35 typename std::enable_if<
37 typename std::result_of<typename std::iterator_traits<
38 InputIterator>::value_type()>::type,
42 typename std::result_of<typename std::iterator_traits<
43 InputIterator>::value_type()>::type>>::
44 type> inline collectN(InputIterator first, InputIterator last, size_t n);
47 * collectN specialization for functions returning void
49 * @param first Range of tasks to be scheduled
51 * @param n Number of tasks to wait for
53 * @return vector of completed task indices
55 template <class InputIterator>
56 typename std::enable_if<
58 typename std::result_of<
59 typename std::iterator_traits<InputIterator>::value_type()>::type,
61 std::vector<size_t>>::
62 type inline collectN(InputIterator first, InputIterator last, size_t n);
65 * Schedules several tasks and blocks until all of these tasks are completed.
66 * If any of the tasks throws an exception, this exception will be re-thrown,
67 * but only when all the tasks are complete. If several tasks throw exceptions
68 * one of them will be re-thrown.
70 * @param first Range of tasks to be scheduled
73 * @return vector of values returned by tasks
75 template <class InputIterator>
76 typename std::vector<typename std::enable_if<
78 typename std::result_of<
79 typename std::iterator_traits<InputIterator>::value_type()>::type,
81 typename std::result_of<
82 typename std::iterator_traits<InputIterator>::value_type()>::
83 type>::type> inline collectAll(InputIterator first, InputIterator last);
86 * collectAll specialization for functions returning void
88 * @param first Range of tasks to be scheduled
91 template <class InputIterator>
92 typename std::enable_if<
94 typename std::result_of<
95 typename std::iterator_traits<InputIterator>::value_type()>::type,
97 void>::type inline collectAll(InputIterator first, InputIterator last);
100 * Schedules several tasks and blocks until one of them is completed.
101 * If this task throws an exception, this exception will be re-thrown.
102 * Exceptions thrown by all other tasks will be ignored.
104 * @param first Range of tasks to be scheduled
107 * @return pair of index of the first completed task and its return value
109 template <class InputIterator>
110 typename std::enable_if<
112 typename std::result_of<
113 typename std::iterator_traits<InputIterator>::value_type()>::type,
117 typename std::result_of<typename std::iterator_traits<
118 InputIterator>::value_type()>::type>>::
119 type inline collectAny(InputIterator first, InputIterator last);
122 * WhenAny specialization for functions returning void.
124 * @param first Range of tasks to be scheduled
127 * @return index of the first completed task
129 template <class InputIterator>
130 typename std::enable_if<
132 typename std::result_of<
133 typename std::iterator_traits<InputIterator>::value_type()>::type,
135 size_t>::type inline collectAny(InputIterator first, InputIterator last);
139 #include <folly/fibers/WhenN-inl.h>