Unpoison stack memory before deallocation
[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 {
26 namespace fibers {
27
28 template <typename T>
29 class TaskIterator;
30
31 /**
32  * Schedules several tasks and immediately returns an iterator, that
33  * allow to traverse tasks in the order of their completion. All results and
34  * exptions thrown are stored alongside with the task id and are
35  * accessible via iterator.
36  *
37  * @param first Range of tasks to be scheduled
38  * @param last
39  *
40  * @return movable, non-copyable iterator
41  */
42 template <class InputIterator>
43 TaskIterator<typename std::result_of<
44     typename std::iterator_traits<InputIterator>::value_type()>::
45                  type> 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<typename std::result_of<
103       typename std::iterator_traits<InputIterator>::value_type()>::type>
104   addTasks(InputIterator first, InputIterator last);
105
106   struct Context {
107     std::vector<std::pair<size_t, folly::Try<T>>> results;
108     folly::Optional<Promise<void>> promise;
109     size_t totalTasks{0};
110     size_t tasksConsumed{0};
111     size_t tasksToFulfillPromise{0};
112   };
113
114   std::shared_ptr<Context> context_;
115   size_t id_;
116
117   explicit TaskIterator(std::shared_ptr<Context> context);
118
119   folly::Try<T> awaitNextResult();
120 };
121 }
122 }
123
124 #include <folly/experimental/fibers/AddTasks-inl.h>