s/valueTry/getTry/
[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 "ThreadGate.h"
19 #include "Executor.h"
20 #include <type_traits>
21
22 namespace folly { namespace wangle {
23
24 template <
25   class WestExecutorPtr = Executor*,
26   class EastExecutorPtr = Executor*,
27   class WaiterPtr = void*>
28 class GenericThreadGate : public ThreadGate {
29 public:
30   /**
31     EastExecutor and WestExecutor respond threadsafely to
32     `add(std::function<void()>&&)`
33
34     Waiter responds to `makeProgress()`. It may block, as long as progress
35     will be made on the west front.
36     */
37   GenericThreadGate(WestExecutorPtr west,
38                     EastExecutorPtr east,
39                     WaiterPtr waiter = nullptr) :
40     westExecutor(west),
41     eastExecutor(east),
42     waiter(waiter)
43   {}
44
45   void addWest(std::function<void()>&& fn) { westExecutor->add(std::move(fn)); }
46   void addEast(std::function<void()>&& fn) { eastExecutor->add(std::move(fn)); }
47
48   virtual void makeProgress() {
49     makeProgress_(std::is_same<WaiterPtr, void*>());
50   }
51
52   WestExecutorPtr westExecutor;
53   EastExecutorPtr eastExecutor;
54   WaiterPtr waiter;
55 private:
56   void makeProgress_(std::true_type const&) {
57     throw std::logic_error("No waiter.");
58   }
59
60   void makeProgress_(std::false_type const&) {
61     waiter->makeProgress();
62   }
63 };
64
65 }} // executor