proxygen
AddTasks.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present 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/Try.h>
24 #include <folly/fibers/Promise.h>
25 
26 namespace folly {
27 namespace fibers {
28 
29 template <typename T>
31 
43 template <class InputIterator>
45  typename std::iterator_traits<InputIterator>::
46  value_type>> inline addTasks(InputIterator first, InputIterator last);
47 
48 template <typename T>
49 class TaskIterator {
50  public:
51  typedef T value_type;
52 
54 
55  // not copyable
56  TaskIterator(const TaskIterator& other) = delete;
57  TaskIterator& operator=(const TaskIterator& other) = delete;
58 
59  // movable
61  TaskIterator& operator=(TaskIterator&& other) = delete;
62 
68  template <typename F>
69  void addTask(F&& func);
70 
75  bool hasCompleted() const;
76 
80  bool hasPending() const;
81 
85  bool hasNext() const;
86 
94  T awaitNext();
95 
104  void reserve(size_t n);
105 
109  size_t getTaskID() const;
110 
111  private:
112  template <class InputIterator>
113  friend TaskIterator<
115  addTasks(InputIterator first, InputIterator last);
116 
117  struct Context {
118  std::vector<std::pair<size_t, folly::Try<T>>> results;
120  size_t totalTasks{0};
121  size_t tasksConsumed{0};
123  };
124 
125  std::shared_ptr<Context> context_{std::make_shared<Context>()};
128 
130 };
131 } // namespace fibers
132 } // namespace folly
133 
friend TaskIterator< invoke_result_t< typename std::iterator_traits< InputIterator >::value_type > > addTasks(InputIterator first, InputIterator last)
Definition: AddTasks-inl.h:114
folly::Try< T > awaitNextResult()
Definition: AddTasks-inl.h:42
typename invoke_result< F, Args... >::type invoke_result_t
Definition: Invoke.h:142
LogLevel max
Definition: LogLevel.cpp:31
folly::Optional< Promise< void > > promise
Definition: AddTasks.h:119
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
std::vector< std::pair< size_t, folly::Try< T > > > results
Definition: AddTasks.h:118
Single-threaded task execution engine.
TaskIterator & operator=(const TaskIterator &other)=delete
size_t getTaskID() const
Definition: AddTasks-inl.h:82
TaskIterator< invoke_result_t< typename std::iterator_traits< InputIterator >::value_type > > addTasks(InputIterator first, InputIterator last)
Definition: AddTasks-inl.h:114
Definition: Try.h:51
std::shared_ptr< Context > context_
Definition: AddTasks.h:125
constexpr detail::First first
Definition: Base-inl.h:2553
FiberManager & getFiberManager(EventBase &evb, const FiberManager::Options &opts)