folly copyright 2015 -> copyright 2016
[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/Promise.h>
23 #include <folly/futures/Try.h>
24
25 namespace folly { namespace fibers {
26
27 template <typename T>
28 class TaskIterator;
29
30 /**
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.
35  *
36  * @param first Range of tasks to be scheduled
37  * @param last
38  *
39  * @return movable, non-copyable iterator
40  */
41 template <class InputIterator>
42 TaskIterator<
43   typename std::result_of<
44     typename std::iterator_traits<InputIterator>::value_type()>::type>
45 inline addTasks(InputIterator first, InputIterator last);
46
47 template <typename T>
48 class TaskIterator {
49  public:
50   typedef T value_type;
51
52   // not copyable
53   TaskIterator(const TaskIterator& other) = delete;
54   TaskIterator& operator=(const TaskIterator& other) = delete;
55
56   // movable
57   TaskIterator(TaskIterator&& other) noexcept;
58   TaskIterator& operator=(TaskIterator&& other) = delete;
59
60   /**
61    * @return True if there are tasks immediately available to be consumed (no
62    *         need to await on them).
63    */
64   bool hasCompleted() const;
65
66   /**
67    * @return True if there are tasks pending execution (need to awaited on).
68    */
69   bool hasPending() const;
70
71   /**
72    * @return True if there are any tasks (hasCompleted() || hasPending()).
73    */
74   bool hasNext() const;
75
76   /**
77    * Await for another task to complete. Will not await if the result is
78    * already available.
79    *
80    * @return result of the task completed.
81    * @throw exception thrown by the task.
82    */
83   T awaitNext();
84
85   /**
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
89    * available.
90    *
91    * @param n   Number of tasks to await for completition.
92    */
93   void reserve(size_t n);
94
95   /**
96    * @return id of the last task that was processed by awaitNext().
97    */
98   size_t getTaskID() const;
99
100  private:
101   template <class InputIterator>
102   friend TaskIterator<
103    typename std::result_of<
104      typename std::iterator_traits<InputIterator>::value_type()>::type>
105   addTasks(InputIterator first, InputIterator last);
106
107   struct Context {
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};
113   };
114
115   std::shared_ptr<Context> context_;
116   size_t id_;
117
118   explicit TaskIterator(std::shared_ptr<Context> context);
119
120   folly::Try<T> awaitNextResult();
121 };
122
123 }}
124
125 #include <folly/experimental/fibers/AddTasks-inl.h>