proxygen
BatchDispatcher.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-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 <folly/Function.h>
19 #include <folly/futures/Future.h>
20 #include <folly/futures/Promise.h>
21 #include <exception>
22 #include <memory>
23 #include <stdexcept>
24 #include <vector>
25 
26 namespace folly {
27 namespace fibers {
28 
71 template <typename ValueT, typename ResultT, typename ExecutorT>
73  public:
74  using ValueBatchT = std::vector<ValueT>;
75  using ResultBatchT = std::vector<ResultT>;
76  using PromiseBatchT = std::vector<folly::Promise<ResultT>>;
78 
79  BatchDispatcher(ExecutorT& executor, DispatchFunctionT dispatchFunc)
80  : executor_(executor),
81  state_(new DispatchState(std::move(dispatchFunc))) {}
82 
84  if (state_->values.empty()) {
86  }
87 
88  folly::Promise<ResultT> resultPromise;
89  auto resultFuture = resultPromise.getFuture();
90 
91  state_->values.emplace_back(std::move(value));
92  state_->promises.emplace_back(std::move(resultPromise));
93 
94  return resultFuture;
95  }
96 
97  private:
98  struct DispatchState {
99  explicit DispatchState(DispatchFunctionT&& dispatchFunction)
100  : dispatchFunc(std::move(dispatchFunction)) {}
101 
105  };
106 
110  state.values.swap(values);
111  state.promises.swap(promises);
112 
113  try {
114  auto results = state.dispatchFunc(std::move(values));
115  if (results.size() != promises.size()) {
116  throw std::logic_error(
117  "Unexpected number of results returned from dispatch function");
118  }
119 
120  for (size_t i = 0; i < promises.size(); i++) {
121  promises[i].setValue(std::move(results[i]));
122  }
123  } catch (const std::exception& ex) {
124  for (size_t i = 0; i < promises.size(); i++) {
125  promises[i].setException(
126  exception_wrapper(std::current_exception(), ex));
127  }
128  } catch (...) {
129  for (size_t i = 0; i < promises.size(); i++) {
130  promises[i].setException(exception_wrapper(std::current_exception()));
131  }
132  }
133  }
134 
135  ExecutorT& executor_;
136  std::shared_ptr<DispatchState> state_;
137 };
138 } // namespace fibers
139 } // namespace folly
std::vector< folly::Promise< ResultT >> PromiseBatchT
static void dispatchFunctionWrapper(DispatchState &state)
std::vector< ValueT > ValueBatchT
Future< ResultT > add(ValueT value)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
int32_t ValueT
PUSHMI_INLINE_VAR constexpr __adl::get_executor_fn executor
std::vector< ResultT > ResultBatchT
Future< T > getFuture()
Definition: Promise-inl.h:97
std::shared_ptr< DispatchState > state_
DispatchState(DispatchFunctionT &&dispatchFunction)
BatchDispatcher(ExecutorT &executor, DispatchFunctionT dispatchFunc)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
state
Definition: http_parser.c:272