proxygen
Wait.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-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/coroutine>
19 #include <future>
20 
21 namespace folly {
22 namespace coro {
23 
24 class Wait {
25  public:
26  class promise_type {
27  public:
29  return Wait(promise_.get_future());
30  }
31 
32  std::experimental::suspend_never initial_suspend() {
33  return {};
34  }
35 
36  std::experimental::suspend_never final_suspend() {
37  return {};
38  }
39 
40  void return_void() {
41  promise_.set_value();
42  }
43 
45  promise_.set_exception(std::current_exception());
46  }
47 
48  private:
49  std::promise<void> promise_;
50  };
51 
52  explicit Wait(std::future<void> future) : future_(std::move(future)) {}
53 
54  Wait(Wait&&) = default;
55 
56  void detach() {
57  future_ = {};
58  }
59 
60  ~Wait() {
61  if (future_.valid()) {
62  future_.get();
63  }
64  }
65 
66  private:
67  std::future<void> future_;
68 };
69 } // namespace coro
70 } // namespace folly
void detach()
Definition: Wait.h:56
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
Wait(std::future< void > future)
Definition: Wait.h:52
std::experimental::suspend_never final_suspend()
Definition: Wait.h:36
std::future< void > future_
Definition: Wait.h:67
std::promise< void > promise_
Definition: Wait.h:49
std::experimental::suspend_never initial_suspend()
Definition: Wait.h:32