std::chrono'ize EventBase::SmoothLoopTime
[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_.loopKeepAliveAtomic();
22   loopKeepAlive_ = loopKeepAliveAtomic();
23 }
24
25 VirtualEventBase::~VirtualEventBase() {
26   CHECK(!evb_.inRunningEventBaseThread());
27
28   CHECK(evb_.runInEventBaseThread([&] { loopKeepAlive_.reset(); }));
29   loopKeepAliveBaton_.wait();
30
31   CHECK(evb_.runInEventBaseThreadAndWait([&] {
32     clearCobTimeouts();
33
34     onDestructionCallbacks_.withWLock([&](LoopCallbackList& callbacks) {
35       while (!callbacks.empty()) {
36         auto& callback = callbacks.front();
37         callbacks.pop_front();
38         callback.runLoopCallback();
39       }
40     });
41
42     evbLoopKeepAlive_.reset();
43   }));
44 }
45
46 void VirtualEventBase::runOnDestruction(EventBase::LoopCallback* callback) {
47   onDestructionCallbacks_.withWLock([&](LoopCallbackList& callbacks) {
48     callback->cancelLoopCallback();
49     callbacks.push_back(*callback);
50   });
51 }
52 }