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.
18 namespace folly { namespace fibers {
21 * Schedules several tasks and blocks until n of these tasks are completed.
22 * If any of these n tasks throws an exception, this exception will be
23 * re-thrown, but only when n tasks are complete. If several tasks throw
24 * exceptions one of them will be re-thrown.
26 * @param first Range of tasks to be scheduled
28 * @param n Number of tasks to wait for
30 * @return vector of pairs (task index, return value of task)
32 template <class InputIterator>
34 typename std::enable_if<
36 typename std::result_of<
37 typename std::iterator_traits<InputIterator>::value_type()>::type,
41 typename std::result_of<
42 typename std::iterator_traits<InputIterator>::value_type()>::type>
45 inline collectN(InputIterator first, InputIterator last, size_t n);
48 * collectN specialization for functions returning void
50 * @param first Range of tasks to be scheduled
52 * @param n Number of tasks to wait for
54 * @return vector of completed task indices
56 template <class InputIterator>
57 typename std::enable_if<
59 typename std::result_of<
60 typename std::iterator_traits<InputIterator>::value_type()>::type, void
61 >::value, std::vector<size_t>>::type
62 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>
77 typename std::enable_if<
79 typename std::result_of<
80 typename std::iterator_traits<InputIterator>::value_type()>::type,
82 typename std::result_of<
83 typename std::iterator_traits<InputIterator>::value_type()>::type>::type
85 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, void
99 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, void
119 typename std::result_of<
120 typename std::iterator_traits<InputIterator>::value_type()>::type>
122 inline collectAny(InputIterator first, InputIterator last);
125 * WhenAny specialization for functions returning void.
127 * @param first Range of tasks to be scheduled
130 * @return index of the first completed task
132 template <class InputIterator>
133 typename std::enable_if<
135 typename std::result_of<
136 typename std::iterator_traits<InputIterator>::value_type()>::type, void
137 >::value, size_t>::type
138 inline collectAny(InputIterator first, InputIterator last);
142 #include <folly/experimental/fibers/WhenN-inl.h>