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/fibers/FiberManager.h>
23 #include <folly/fibers/Promise.h>
24 #include <folly/Try.h>
33 * Schedules several tasks and immediately returns an iterator, that
34 * allow one to traverse tasks in the order of their completion. All results
35 * and exceptions thrown are stored alongside with the task id and are
36 * accessible via iterator.
38 * @param first Range of tasks to be scheduled
41 * @return movable, non-copyable iterator
43 template <class InputIterator>
44 TaskIterator<typename std::result_of<
45 typename std::iterator_traits<InputIterator>::value_type()>::
46 type> inline addTasks(InputIterator first, InputIterator last);
53 TaskIterator() : fm_(FiberManager::getFiberManager()) {}
56 TaskIterator(const TaskIterator& other) = delete;
57 TaskIterator& operator=(const TaskIterator& other) = delete;
60 TaskIterator(TaskIterator&& other) noexcept;
61 TaskIterator& operator=(TaskIterator&& other) = delete;
64 * Add one more task to the TaskIterator.
66 * @param func task to be added, will be scheduled on current FiberManager
69 void addTask(F&& func);
72 * @return True if there are tasks immediately available to be consumed (no
73 * need to await on them).
75 bool hasCompleted() const;
78 * @return True if there are tasks pending execution (need to awaited on).
80 bool hasPending() const;
83 * @return True if there are any tasks (hasCompleted() || hasPending()).
88 * Await for another task to complete. Will not await if the result is
91 * @return result of the task completed.
92 * @throw exception thrown by the task.
97 * Await until the specified number of tasks completes or there are no
98 * tasks left to await for.
99 * Note: Will not await if there are already the specified number of tasks
102 * @param n Number of tasks to await for completition.
104 void reserve(size_t n);
107 * @return id of the last task that was processed by awaitNext().
109 size_t getTaskID() const;
112 template <class InputIterator>
113 friend TaskIterator<typename std::result_of<
114 typename std::iterator_traits<InputIterator>::value_type()>::type>
115 addTasks(InputIterator first, InputIterator last);
118 std::vector<std::pair<size_t, folly::Try<T>>> results;
119 folly::Optional<Promise<void>> promise;
120 size_t totalTasks{0};
121 size_t tasksConsumed{0};
122 size_t tasksToFulfillPromise{0};
125 std::shared_ptr<Context> context_{std::make_shared<Context>()};
126 size_t id_{std::numeric_limits<size_t>::max()};
129 folly::Try<T> awaitNextResult();
134 #include <folly/fibers/AddTasks-inl.h>