Consistently have the namespace closing comment
[folly.git] / folly / io / async / VirtualEventBase.cpp
1 /*
2  * Copyright 2017 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 #include <folly/io/async/VirtualEventBase.h>
17
18 namespace folly {
19
20 VirtualEventBase::VirtualEventBase(EventBase& evb) : evb_(evb) {
21   evbLoopKeepAlive_ = evb_.getKeepAliveToken();
22 }
23
24 std::future<void> VirtualEventBase::destroy() {
25   CHECK(evb_.runInEventBaseThread([this] { loopKeepAlive_.reset(); }));
26
27   return std::move(destroyFuture_);
28 }
29
30 void VirtualEventBase::destroyImpl() {
31   // Make sure we release EventBase KeepAlive token even if exception occurs
32   auto evbLoopKeepAlive = std::move(evbLoopKeepAlive_);
33   try {
34     clearCobTimeouts();
35
36     onDestructionCallbacks_.withWLock([&](LoopCallbackList& callbacks) {
37       while (!callbacks.empty()) {
38         auto& callback = callbacks.front();
39         callbacks.pop_front();
40         callback.runLoopCallback();
41       }
42     });
43
44     destroyPromise_.set_value();
45   } catch (...) {
46     destroyPromise_.set_exception(std::current_exception());
47   }
48 }
49
50 VirtualEventBase::~VirtualEventBase() {
51   if (!destroyFuture_.valid()) {
52     return;
53   }
54   CHECK(!evb_.inRunningEventBaseThread());
55   destroy().get();
56 }
57
58 void VirtualEventBase::runOnDestruction(EventBase::LoopCallback* callback) {
59   onDestructionCallbacks_.withWLock([&](LoopCallbackList& callbacks) {
60     callback->cancelLoopCallback();
61     callbacks.push_back(*callback);
62   });
63 }
64 } // namespace folly