proxygen
VirtualEventBase.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-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 
17 #pragma once
18 
19 #include <future>
20 
21 #include <folly/Executor.h>
22 #include <folly/Synchronized.h>
25 
26 namespace folly {
27 
41  public:
42  explicit VirtualEventBase(EventBase& evb);
43 
44  VirtualEventBase(const VirtualEventBase&) = delete;
46 
47  ~VirtualEventBase() override;
48 
50  return *evb_;
51  }
52 
64 
71  template <typename F>
72  void runInEventBaseThread(F&& f) {
73  // KeepAlive token has to be released in the EventBase thread. If
74  // runInEventBaseThread() fails, we can't extract the KeepAlive token
75  // from the callback to properly release it.
76  CHECK(evb_->runInEventBaseThread(
77  [keepAliveToken = getKeepAliveToken(this),
78  f = std::forward<F>(f)]() mutable { f(); }));
79  }
80 
82  return evb_->timer();
83  }
84 
86  AsyncTimeout* obj,
87  TimeoutManager::InternalEnum internal) override {
88  evb_->attachTimeoutManager(obj, internal);
89  }
90 
91  void detachTimeoutManager(AsyncTimeout* obj) override {
92  evb_->detachTimeoutManager(obj);
93  }
94 
96  override {
97  return evb_->scheduleTimeout(obj, timeout);
98  }
99 
100  void cancelTimeout(AsyncTimeout* obj) override {
101  evb_->cancelTimeout(obj);
102  }
103 
104  void bumpHandlingTime() override {
105  evb_->bumpHandlingTime();
106  }
107 
108  bool isInTimeoutManagerThread() override {
109  return evb_->isInTimeoutManagerThread();
110  }
111 
115  void add(folly::Func f) override {
117  }
118 
120  return evb_->inRunningEventBaseThread();
121  }
122 
123  protected:
124  bool keepAliveAcquire() override {
125  if (evb_->inRunningEventBaseThread()) {
126  DCHECK(loopKeepAliveCount_ + loopKeepAliveCountAtomic_.load() > 0);
127 
129  } else {
131  }
132  return true;
133  }
134 
135  void keepAliveRelease() override {
136  if (!evb_->inRunningEventBaseThread()) {
137  return evb_->add([=] { keepAliveRelease(); });
138  }
139  if (loopKeepAliveCountAtomic_.load()) {
141  }
142  DCHECK(loopKeepAliveCount_ > 0);
143  if (--loopKeepAliveCount_ == 0) {
144  destroyImpl();
145  }
146  }
147 
148  private:
149  friend class EventBase;
150 
151  ssize_t keepAliveCount() {
152  if (loopKeepAliveCountAtomic_.load()) {
154  }
155  return loopKeepAliveCount_;
156  }
157 
158  std::future<void> destroy();
159  void destroyImpl();
160 
162 
164 
166  std::atomic<ssize_t> loopKeepAliveCountAtomic_{0};
167  std::promise<void> destroyPromise_;
168  std::future<void> destroyFuture_{destroyPromise_.get_future()};
170  makeKeepAlive<VirtualEventBase>(this)};
171 
173 };
174 } // namespace folly
void attachTimeoutManager(AsyncTimeout *obj, TimeoutManager::InternalEnum internal) override
auto f
std::chrono::milliseconds timeout_type
void runInEventBaseThread(F &&f)
std::promise< void > destroyPromise_
bool inRunningEventBaseThread() const
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void runOnDestruction(EventBase::LoopCallback *callback)
void keepAliveRelease() override
folly::Synchronized< LoopCallbackList > onDestructionCallbacks_
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
bool isInTimeoutManagerThread() override
KeepAlive< EventBase > evb_
void detachTimeoutManager(AsyncTimeout *obj) override
EventBase::LoopCallback::List LoopCallbackList
bool keepAliveAcquire() override
void cancelTimeout(AsyncTimeout *obj) override
std::atomic< ssize_t > loopKeepAliveCountAtomic_
VirtualEventBase & operator=(const VirtualEventBase &)=delete
static KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:138
void add(folly::Func f) override
bool scheduleTimeout(AsyncTimeout *obj, TimeoutManager::timeout_type timeout) override
boost::intrusive::list< LoopCallback, boost::intrusive::constant_time_size< false > > List
Definition: EventBase.h:167
VirtualEventBase(EventBase &evb)
void bumpHandlingTime() override
KeepAlive< VirtualEventBase > loopKeepAlive_
std::future< void > destroy()
std::future< void > destroyFuture_
HHWheelTimer & timer()