2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ThreadGate.h"
20 #include <type_traits>
22 namespace folly { namespace wangle {
25 class WestExecutorPtr = Executor*,
26 class EastExecutorPtr = Executor*,
27 class WaiterPtr = void*>
28 class GenericThreadGate : public ThreadGate {
31 EastExecutor and WestExecutor respond threadsafely to
32 `add(std::function<void()>&&)`
34 Waiter responds to `makeProgress()`. It may block, as long as progress
35 will be made on the west front.
37 GenericThreadGate(WestExecutorPtr west,
39 WaiterPtr waiter = nullptr) :
45 void addWest(std::function<void()>&& fn) { westExecutor->add(std::move(fn)); }
46 void addEast(std::function<void()>&& fn) { eastExecutor->add(std::move(fn)); }
48 virtual void makeProgress() {
49 makeProgress_(std::is_same<WaiterPtr, void*>());
52 WestExecutorPtr westExecutor;
53 EastExecutorPtr eastExecutor;
56 void makeProgress_(std::true_type const&) {
57 throw std::logic_error("No waiter.");
60 void makeProgress_(std::false_type const&) {
61 waiter->makeProgress();