proxygen
pool.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018-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 <experimental/thread_pool> // @manual
19 
23 
24 #if __cpp_deduction_guides >= 201703
25 #define MAKE(x) x MAKE_
26 #define MAKE_(...) \
27  { __VA_ARGS__ }
28 #else
29 #define MAKE(x) make_##x
30 #endif
31 
32 namespace folly {
33 namespace pushmi {
34 
35 using std::experimental::static_thread_pool;
36 namespace execution = std::experimental::execution;
37 
38 template <class Executor>
39 struct pool_executor {
40  using properties = property_set<
46 
47  using e_t = Executor;
48  e_t e;
49  explicit pool_executor(e_t e) : e(std::move(e)) {}
50  auto executor() {
51  return *this;
52  }
53  PUSHMI_TEMPLATE(class Out)
54  (requires Receiver<Out>)
55  void submit(Out out) const {
56  e.execute(
57  [e = *this, out = std::move(out)]() mutable { set_value(out, e); });
58  }
59 };
60 
61 class pool {
62  static_thread_pool p;
63 
64  public:
65  inline explicit pool(std::size_t threads) : p(threads) {}
66 
67  inline auto executor() {
68  auto exec = execution::require(
69  p.executor(), execution::never_blocking, execution::oneway);
70  return pool_executor<decltype(exec)>{exec};
71  }
72 
73  inline void stop() {
74  p.stop();
75  }
76  inline void wait() {
77  p.wait();
78  }
79 };
80 
81 } // namespace pushmi
82 } // namespace folly
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
auto executor()
Definition: pool.h:67
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::vector< std::thread::id > threads
void stop()
Definition: pool.h:73
requires requires(detail::apply_impl(std::declval< F >(), std::declval< Tuple >(), detail::tupidxs< Tuple >{}))) const expr decltype(auto) apply(F &&f
PUSHMI_TEMPLATE(class E=std::exception_ptr, class Wrapped)(requires Sender< detail
Definition: executor.h:102
requires Receiver< Out > void submit(Out out) const
Definition: pool.h:55
PUSHMI_INLINE_VAR constexpr __adl::set_value_fn set_value
static_thread_pool p
Definition: pool.h:62
pool(std::size_t threads)
Definition: pool.h:65
void wait()
Definition: pool.h:76