Fix #includes
[folly.git] / folly / wangle / GenericThreadGate.h
1 /*
2  * Copyright 2014 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
17 #pragma once
18 #include <folly/wangle/ThreadGate.h>
19 #include <folly/wangle/Executor.h>
20 #include <type_traits>
21
22 namespace folly { namespace wangle {
23
24 /// This generic threadgate takes two executors and an optional waiter (if you
25 /// need to support waiting). Hint: use executors that inherit from Executor
26 /// (in Executor.h), then you just do
27 ///
28 ///   GenericThreadGate tg(westExecutor, eastExecutor, waiter);
29 template <
30   class WestExecutorPtr = Executor*,
31   class EastExecutorPtr = Executor*,
32   class WaiterPtr = void*>
33 class GenericThreadGate : public ThreadGate {
34 public:
35   /**
36     EastExecutor and WestExecutor respond threadsafely to
37     `add(std::function<void()>&&)`
38
39     Waiter responds to `makeProgress()`. It may block, as long as progress
40     will be made on the west front.
41     */
42   GenericThreadGate(WestExecutorPtr west,
43                     EastExecutorPtr east,
44                     WaiterPtr waiter = nullptr) :
45     westExecutor(west),
46     eastExecutor(east),
47     waiter(waiter)
48   {}
49
50   void addWest(std::function<void()>&& fn) { westExecutor->add(std::move(fn)); }
51   void addEast(std::function<void()>&& fn) { eastExecutor->add(std::move(fn)); }
52
53   virtual void makeProgress() {
54     makeProgress_(std::is_same<WaiterPtr, void*>());
55   }
56
57   WestExecutorPtr westExecutor;
58   EastExecutorPtr eastExecutor;
59   WaiterPtr waiter;
60
61 private:
62   void makeProgress_(std::true_type const&) {
63     throw std::logic_error("No waiter.");
64   }
65
66   void makeProgress_(std::false_type const&) {
67     waiter->makeProgress();
68   }
69 };
70
71 }} // executor