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.
21 #include <folly/Optional.h>
22 #include <folly/experimental/fibers/Promise.h>
23 #include <folly/futures/Try.h>
25 namespace folly { namespace fibers {
31 * Schedules several tasks and immediately returns an iterator, that
32 * allow to traverse tasks in the order of their completion. All results and
33 * exptions thrown are stored alongside with the task id and are
34 * accessible via iterator.
36 * @param first Range of tasks to be scheduled
39 * @return movable, non-copyable iterator
41 template <class InputIterator>
43 typename std::result_of<
44 typename std::iterator_traits<InputIterator>::value_type()>::type>
45 inline addTasks(InputIterator first, InputIterator last);
53 TaskIterator(const TaskIterator& other) = delete;
54 TaskIterator& operator=(const TaskIterator& other) = delete;
57 TaskIterator(TaskIterator&& other) noexcept;
58 TaskIterator& operator=(TaskIterator&& other) = delete;
61 * @return True if there are tasks immediately available to be consumed (no
62 * need to await on them).
64 bool hasCompleted() const;
67 * @return True if there are tasks pending execution (need to awaited on).
69 bool hasPending() const;
72 * @return True if there are any tasks (hasCompleted() || hasPending()).
77 * Await for another task to complete. Will not await if the result is
80 * @return result of the task completed.
81 * @throw exception thrown by the task.
86 * Await until the specified number of tasks completes or there are no
87 * tasks left to await for.
88 * Note: Will not await if there are already the specified number of tasks
91 * @param n Number of tasks to await for completition.
93 void reserve(size_t n);
96 * @return id of the last task that was processed by awaitNext().
98 size_t getTaskID() const;
101 template <class InputIterator>
103 typename std::result_of<
104 typename std::iterator_traits<InputIterator>::value_type()>::type>
105 addTasks(InputIterator first, InputIterator last);
108 std::vector<std::pair<size_t, folly::Try<T>>> results;
109 folly::Optional<Promise<void>> promise;
110 size_t totalTasks{0};
111 size_t tasksConsumed{0};
112 size_t tasksToFulfillPromise{0};
115 std::shared_ptr<Context> context_;
118 explicit TaskIterator(std::shared_ptr<Context> context);
120 folly::Try<T> awaitNextResult();
125 #include <folly/experimental/fibers/AddTasks-inl.h>