adding Promise::await
[folly.git] / folly / experimental / fibers / AddTasks.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #pragma once
17
18 #include <functional>
19 #include <vector>
20
21 #include <folly/Optional.h>
22 #include <folly/experimental/fibers/FiberManager.h>
23 #include <folly/experimental/fibers/Promise.h>
24 #include <folly/futures/Try.h>
25
26 namespace folly {
27 namespace fibers {
28
29 template <typename T>
30 class TaskIterator;
31
32 /**
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.
37  *
38  * @param first Range of tasks to be scheduled
39  * @param last
40  *
41  * @return movable, non-copyable iterator
42  */
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);
47
48 template <typename T>
49 class TaskIterator {
50  public:
51   typedef T value_type;
52
53   TaskIterator() : fm_(FiberManager::getFiberManager()) {}
54
55   // not copyable
56   TaskIterator(const TaskIterator& other) = delete;
57   TaskIterator& operator=(const TaskIterator& other) = delete;
58
59   // movable
60   TaskIterator(TaskIterator&& other) noexcept;
61   TaskIterator& operator=(TaskIterator&& other) = delete;
62
63   /**
64    * Add one more task to the TaskIterator.
65    *
66    * @param func task to be added, will be scheduled on current FiberManager
67    */
68   template <typename F>
69   void addTask(F&& func);
70
71   /**
72    * @return True if there are tasks immediately available to be consumed (no
73    *         need to await on them).
74    */
75   bool hasCompleted() const;
76
77   /**
78    * @return True if there are tasks pending execution (need to awaited on).
79    */
80   bool hasPending() const;
81
82   /**
83    * @return True if there are any tasks (hasCompleted() || hasPending()).
84    */
85   bool hasNext() const;
86
87   /**
88    * Await for another task to complete. Will not await if the result is
89    * already available.
90    *
91    * @return result of the task completed.
92    * @throw exception thrown by the task.
93    */
94   T awaitNext();
95
96   /**
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
100    * available.
101    *
102    * @param n   Number of tasks to await for completition.
103    */
104   void reserve(size_t n);
105
106   /**
107    * @return id of the last task that was processed by awaitNext().
108    */
109   size_t getTaskID() const;
110
111  private:
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);
116
117   struct Context {
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};
123   };
124
125   std::shared_ptr<Context> context_{std::make_shared<Context>()};
126   size_t id_{std::numeric_limits<size_t>::max()};
127   FiberManager& fm_;
128
129   folly::Try<T> awaitNextResult();
130 };
131 }
132 }
133
134 #include <folly/experimental/fibers/AddTasks-inl.h>