proxygen
VirtualEventBase.cpp
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  */
17 
18 namespace folly {
19 
21  : evb_(getKeepAliveToken(evb)) {}
22 
23 std::future<void> VirtualEventBase::destroy() {
24  CHECK(evb_->runInEventBaseThread([this] { loopKeepAlive_.reset(); }));
25 
26  return std::move(destroyFuture_);
27 }
28 
30  try {
31  {
32  // After destroyPromise_ is posted this object may be destroyed, so make
33  // sure we release EventBase's keep-alive token before that.
34  SCOPE_EXIT {
35  evb_.reset();
36  };
37 
39 
40  // To avoid potential deadlock, do not hold the mutex while invoking
41  // user-supplied callbacks.
42  LoopCallbackList callbacks;
43  onDestructionCallbacks_.swap(callbacks);
44  while (!callbacks.empty()) {
45  auto& callback = callbacks.front();
46  callbacks.pop_front();
47  callback.runLoopCallback();
48  }
49  }
50 
51  destroyPromise_.set_value();
52  } catch (...) {
53  destroyPromise_.set_exception(std::current_exception());
54  }
55 }
56 
58  if (!destroyFuture_.valid()) {
59  return;
60  }
61  CHECK(!evb_->inRunningEventBaseThread());
62  destroy().get();
63 }
64 
66  onDestructionCallbacks_.withWLock([&](LoopCallbackList& callbacks) {
67  callback->cancelLoopCallback();
68  callbacks.push_back(*callback);
69  });
70 }
71 } // namespace folly
std::promise< void > destroyPromise_
void swap(Synchronized &rhs)
Definition: Synchronized.h:684
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void runOnDestruction(EventBase::LoopCallback *callback)
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
folly::Synchronized< LoopCallbackList > onDestructionCallbacks_
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
KeepAlive< EventBase > evb_
EventBase::LoopCallback::List LoopCallbackList
EventBase * evb_
VirtualEventBase(EventBase &evb)
Executor::KeepAlive< ExecutorT > getKeepAliveToken(ExecutorT *executor)
Definition: Executor.h:200
std::future< void > destroy()
std::future< void > destroyFuture_