proxygen
twoway_execute_2.cpp
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 #include <algorithm>
17 #include <cassert>
18 #include <iostream>
19 #include <vector>
20 
21 #include <futures.h>
22 #include <atomic>
23 #include <functional>
24 #include <memory>
25 #include <thread>
26 #include <utility>
27 
29 
31 
32 using namespace folly::pushmi::aliases;
33 
34 namespace p1054 {
35 // A promise refers to a promise and is associated with a future,
36 // either through type-erasure or through construction of an
37 // underlying promise with an overload of make_promise_contract().
38 
39 // make_promise_contract() cannot be written to produce a lazy future.
40 // the promise has to exist prior to .then() getting a continuation.
41 // there must be a shared allocation to connect the promise and future.
42 template <class T, class Executor>
43 std::pair<
44  std::experimental::standard_promise<T>,
45  std::experimental::standard_future<T, std::decay_t<Executor>>>
46 make_promise_contract(const Executor& e) {
47  std::experimental::standard_promise<T> promise;
48  auto ex = e;
49  return {promise, promise.get_future(std::move(ex))};
50 }
51 
52 template <class Executor, class Function>
53 std::experimental::standard_future<
54  std::result_of_t<std::decay_t<Function>()>,
55  std::decay_t<Executor>>
56 twoway_execute(Executor&& e, Function&& f) {
57  using T = std::result_of_t<std::decay_t<Function>()>;
58  auto pc = make_promise_contract<T>(e);
59  auto p = std::get<0>(pc);
60  auto r = std::get<1>(pc);
61  e.execute([p, f]() mutable { p.set_value(f()); });
62  return r;
63 }
64 } // namespace p1054
65 
66 namespace p1055 {
67 
68 template <class Executor, class Function>
69 auto twoway_execute(Executor&& e, Function&& f) {
70  return e | op::transform([f](auto) { return f(); });
71 }
72 
73 } // namespace p1055
74 
75 int main() {
76  mi::pool p{std::max(1u, std::thread::hardware_concurrency())};
77 
78  std::experimental::static_thread_pool sp{
79  std::max(1u, std::thread::hardware_concurrency())};
80 
81  p1054::twoway_execute(sp.executor(), []() { return 42; }).get();
82 
83  p1055::twoway_execute(p.executor(), []() { return 42; }) | op::get<int>;
84 
85  sp.stop();
86  sp.wait();
87  p.stop();
88  p.wait();
89 
90  std::cout << "OK" << std::endl;
91 }
propagate_const< T > pc
auto twoway_execute(Executor &&e, Function &&f)
auto f
LogLevel max
Definition: LogLevel.cpp:31
std::pair< std::experimental::standard_promise< T >, std::experimental::standard_future< T, std::decay_t< Executor > > > make_promise_contract(const Executor &e)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::std T
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
int main()
std::experimental::standard_future< std::result_of_t< std::decay_t< Function >)>, std::decay_t< Executor > > twoway_execute(Executor &&e, Function &&f)